diff options
-rw-r--r-- | CHANGELOG.org | 5 | ||||
-rw-r--r-- | lisp/seam-export.el | 72 | ||||
-rw-r--r-- | lisp/seam-test.el | 19 |
3 files changed, 71 insertions, 25 deletions
diff --git a/CHANGELOG.org b/CHANGELOG.org index 109467d..223b295 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -34,6 +34,11 @@ =:include-drafts=, controls whether drafts are included in a given export profile. +- Custom template variables can now be defined, and built-in ones + overridden. This is done globally with + =seam-export-template-values=, or per-export with + =:template-values=. + - An option has been added to export internal links with a custom CSS class. The default is set by =seam-export-internal-link-class=, and can overridden per-export using =:internal-link-class=. diff --git a/lisp/seam-export.el b/lisp/seam-export.el index 8cab0ab..9f99a09 100644 --- a/lisp/seam-export.el +++ b/lisp/seam-export.el @@ -33,6 +33,7 @@ (defvar seam-export--types nil) (defvar seam-export--template nil) +(defvar seam-export--template-values nil) (defvar seam-export--root-path nil) (defvar seam-export--include-drafts nil) (defvar seam-export--no-extension nil) @@ -67,6 +68,13 @@ properties: missing, falls back to :template-file, `seam-export-template-file', or `seam-export-template-string' in that order. + `:template-values' + + An alist of (VAR . VALUE) pairs, where VAR is a string naming + a template variable, and VALUE is the value to be used when + interpolating that variable. See the mustache.el docs for + more information. Defaults to nil. + `:root-path' The root path used for rendering internal links. Defaults to \"\", @@ -166,6 +174,14 @@ See `seam-export-alist' for more information about specifying templates." :group 'seam-export :type '(choice string (const nil))) +(defcustom seam-export-template-values nil + "An alist of (VAR . VALUE) pairs, where VAR is a string naming a +template variable, and VALUE is the value to be used when +interpolating that variable. See the mustache.el docs for more +information." + :group 'seam-export + :type '(alist :key-type string :value-type sexp)) + (defcustom seam-export-time-format "%e %B %Y" "Human-readable format for template time strings. Passed to `format-time-string'." @@ -275,30 +291,34 @@ notes)." (insert (mustache-render seam-export--template - `(("title" . - ,(seam-export--org-to-text - (seam-get-title-from-file note-file))) - ("raw-title" . - ,(seam-export--org-to-html - (seam-get-title-from-file note-file))) - ("modified" . - ,(format-time-string - seam-export-time-format - modified - seam-export-time-zone)) - ("modified-dt" . - ,(format-time-string - seam-export-time-format-datetime - modified - seam-export-time-zone)) - ("contents" . - ,(seam-export--export-to-html-string - (insert-file-contents note-file) - (re-search-forward "^\\* ") - (org-mode) ;Needed for `org-set-property'. - (org-set-property "seam-title-p" "t"))) - ("backlinks" . - ,(seam-export--generate-backlinks note-file))))) + (append + seam-export--template-values + seam-export-template-values + `(("title" . + ,(seam-export--org-to-text + (seam-get-title-from-file note-file))) + ("raw-title" . + ,(seam-export--org-to-html + (seam-get-title-from-file note-file))) + ("modified" . + ,(format-time-string + seam-export-time-format + modified + seam-export-time-zone)) + ("modified-dt" . + ,(format-time-string + seam-export-time-format-datetime + modified + seam-export-time-zone)) + ("contents" . + ,(seam-export--export-to-html-string + (insert-file-contents note-file) + (re-search-forward "^\\* ") + (org-mode) ;Needed for `org-set-property'. + (org-set-property "seam-title-p" "t"))) + ("backlinks" . + ,(seam-export--generate-backlinks note-file))) + nil))) (write-file html-file)))) (defun seam-export--file-string (file) @@ -313,7 +333,8 @@ notes)." do (let ((types (plist-get plist :types)) (template-file (plist-get plist :template-file)) - (template-string (plist-get plist :template-string))) + (template-string (plist-get plist :template-string)) + (template-values (plist-get plist :template-values))) (unless types (error "You must specify :types for export")) (let ((template @@ -332,6 +353,7 @@ notes)." (seam-export--include-drafts (plist-get plist :include-drafts)) (seam-export--no-extension (plist-get plist :no-extension)) (seam-export--template template) + (seam-export--template-values template-values) (seam-export--internal-link-class (or (plist-get plist :internal-link-class) seam-export-internal-link-class)) diff --git a/lisp/seam-test.el b/lisp/seam-test.el index f361658..5bae015 100644 --- a/lisp/seam-test.el +++ b/lisp/seam-test.el @@ -48,6 +48,7 @@ (seam-title-formatter (lambda (title _type _draft-p) title)) (seam-export-template-file nil) (seam-export-template-string seam-export-default-template-string) + (seam-export-template-values nil) (seam-export-internal-link-class nil) (seam-export-alist `((,(file-name-concat seam-test-directory "html") @@ -561,6 +562,24 @@ accordingly." (seam-export-all-notes) (seam-test-list-files))))) +(ert-deftest seam-test-template-values () + "Test that custom variables can be used in templates, and that +existing ones can be overridden." + (should + (equal + "Qux\nhello, world\n" + (seam-test-with-notes + ((seam-export-template-values '(("title" . "Bar") + ("greeting" . "hello, world"))) + (seam-export-template-string "{{title}}\n{{greeting}}") + (seam-export-alist + `((,(file-name-concat seam-test-directory "html") + :types ("public") + :root-path "/" + :template-values (("title" . "Qux")))))) + ((foo "Foo" "public")) + (seam-export--file-string "html/foo.html"))))) + (provide 'seam-test) ;;; seam-test.el ends here |