Skip to content

Commit

Permalink
Skip leading punctuation when sentence casing titles
Browse files Browse the repository at this point in the history
  • Loading branch information
andras-simonyi committed May 9, 2024
1 parent ce5d929 commit 96419b5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions citeproc-s.el
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ 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>\\|: +[[:punct:]]*[[:alpha:]]\\)"))
(protect-level 0)
(first t)
result)
Expand All @@ -165,13 +165,18 @@ If optional OMIT-NOCASE is non-nil then omit the nocase tags from the output."
(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 "^: +[[:punct:]]*[[: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 96419b5

Please sign in to comment.