Skip to content

Commit

Permalink
fix(citar-format): correctly handle star field widths in templates (#812
Browse files Browse the repository at this point in the history
)

Also add some tests for template parsing.

Fixes #809
  • Loading branch information
roshanshariff authored Nov 12, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ac91ec3 commit 1cf9184
Showing 2 changed files with 28 additions and 17 deletions.
34 changes: 18 additions & 16 deletions citar-format.el
Original file line number Diff line number Diff line change
@@ -133,30 +133,32 @@ for the meaning of HIDE-ELIDED and ELLIPSIS."

(defun citar-format--parse (format-string)
"Parse FORMAT-STRING."
(let ((regex (concat "\\${" ; ${
"\\(.*?\\)" ; field names
"\\(?::[[:blank:]]*" ; : + space
"\\(.*?\\)" ; format spec
"[[:blank:]]*\\)?}")) ; space + }
(let ((regex (concat "\\${" ; ${
"\\(.*?\\)" ; field names
"\\(?::[[:blank:]]*" ; : + space
"\\(.*?\\)\\)?" ; field width
"[[:blank:]]*" ; space
"\\(?:%[[:blank:]]*" ; % + space
"\\(.*?\\)\\)?" ; display transform
"[[:blank:]]*" ; space
"}")) ; }
(position 0)
(fieldspecs nil))
(while (string-match regex format-string position)
(let* ((begin (match-beginning 0))
(end (match-end 0))
(textprops (text-properties-at begin format-string))
(fieldnames (match-string-no-properties 1 format-string))
(spec (match-string-no-properties 2 format-string))
(transform
(let ((tsym
(when spec
(cadr (split-string spec "%")))))
(when tsym
(citar-format--get-transform (intern tsym)))))
(fieldwidth (match-string-no-properties 2 format-string))
(transformkey (match-string-no-properties 3 format-string))
(width (cond
((or (null spec) (string-empty-p spec)
(= 0 (string-to-number spec))) nil)
((string-equal spec "*") '*)
(t (string-to-number spec)))))
((string-equal fieldwidth "*") '*)
((or (null fieldwidth) (string-empty-p fieldwidth)
(= 0 (string-to-number fieldwidth))) nil)
(t (string-to-number fieldwidth))))
(transform
(when (and transformkey (not (string-empty-p transformkey)))
(citar-format--get-transform (intern transformkey)))))
(when (< position begin)
(push (substring format-string position begin) fieldspecs))
(push (cons (nconc (when width `(:width ,width))
11 changes: 10 additions & 1 deletion test/citar-format-test.el
Original file line number Diff line number Diff line change
@@ -50,7 +50,16 @@
(should (equal "foob…baz…" (citar-format--star-widths 3 strings nil "")))
(should (ert-equal-including-properties
#("foobarbazqux" 4 6 (display "") 9 12 (display ""))
(citar-format--star-widths 3 strings t "")))))
(citar-format--star-widths 3 strings t ""))))

(should (equal '((nil "author" "editor") " " (nil "year"))
(citar-format--parse "${author editor} ${year}")))
(should (equal '((nil "author" "editor") " " ((:width 4) "year"))
(citar-format--parse "${author editor} ${year:4}")))
(should (equal '(((:width *) "author" "editor") " " ((:width 4) "year"))
(citar-format--parse "${author editor:*} ${year:4}")))
(should (equal '(((:width * :transform (citar--shorten-names)) "author" "editor") " " ((:width 4) "year"))
(citar-format--parse "${author editor: * % sn} ${year:4}"))))

(provide 'citar-format-test)
;;; citar-format-test.el ends here

0 comments on commit 1cf9184

Please sign in to comment.