Skip to content

Commit

Permalink
Merge pull request #163 from andras-simonyi/143-Capitalize_subtitles_…
Browse files Browse the repository at this point in the history
…that_start_with_interpunction

Skip leading punctuation when sentence casing titles
  • Loading branch information
andras-simonyi authored May 9, 2024
2 parents ce5d929 + 8cce42e commit 0b56312
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions citeproc-s.el
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,33 @@ first word is not in lowercase then return S."
(buffer-string))
s))

(defun citeproc-s-sentence-case-title (s omit-nocase)
(defun citeproc-s-sentence-case-title (s &optional omit-nocase)
"Return a sentence-cased version of title string S.
If optional OMIT-NOCASE is non-nil then omit the nocase tags from the output."
(if (s-blank-p s) s
(let ((sliced (citeproc-s-slice-by-matches
s "\\(<span class=\"nocase\">\\|</span>\\|: +\\w\\)"))
s "\\(<span class=\"nocase\">\\|</span>\\|: +[\"'“‘]*[[:alpha:]]\\)"))
(protect-level 0)
(first t)
result)
(message "SLICE: %s" sliced)
(dolist (slice sliced)
(push
(pcase slice
("<span class=\"nocase\">" (cl-incf protect-level) (if omit-nocase nil slice))
("</span>" (cl-decf protect-level) (if omit-nocase nil slice))
;; Don't touch the first letter after a colon since it is probably a subtitle.
((pred (string-match-p "^:")) slice)
;; Don't touch the first letter after a colon since it probably
;; starts a subtitle.
((pred (string-match-p "^: +[\"'“‘]*[[:alpha:]]")) (setq first nil) slice)
(_ (cond ((< 0 protect-level) (setq first nil) slice)
((not first) (downcase slice))
(t (setq first nil)
(concat (upcase (substring slice 0 1))
(downcase (substring slice 1)))))))
;; We upcase the first letter and downcase the rest.
(let ((pos (string-match "[[:alpha:]]" slice)))
(if pos (concat (substring slice 0 pos)
(upcase (substring slice pos (1+ pos)))
(downcase (substring slice (1+ pos))))
slice))))))
result))
(apply #'concat (nreverse result)))))

Expand Down

0 comments on commit 0b56312

Please sign in to comment.