- "Helper to format a field NAME and VALUE."
- (if value (format "%s: %s\n" name value) ""))
-
-(defun pelican-markdown-header (title date status category tags slug)
- "Generate a Pelican Markdown header.
-
-All parameters but TITLE may be nil to omit them. DATE may be a
-string or 't to use the current date and time."
- (let ((title (format "Title: %s\n" title))
- (status (pelican-field "Status" status))
- (category (pelican-field "Category" category))
- (tags (pelican-field "Tags" tags))
- (slug (pelican-field "Slug" slug))
- (date (if date (format "Date: %s\n"
- (if (stringp date) date
- (pelican-timestamp-now)))
- "")))
- (concat title date status tags category slug "\n")))
-
-(defun pelican-rst-header (title date status category tags slug)
- "Generate a Pelican reStructuredText header.
-
-All parameters but TITLE may be nil to omit them. DATE may be a
-string or 't to use the current date and time."
- (let ((title (format "%s\n%s\n\n" title
- (make-string (string-width title) ?#)))
- (status (pelican-field ":status" status))
- (category (pelican-field ":category" category))
- (tags (pelican-field ":tags" tags))
- (slug (pelican-field ":slug" slug))
- (date (if date (format ":date: %s\n"
- (if (stringp date) date
- (pelican-timestamp-now)))
- "")))
- (concat title date status tags category slug "\n")))
+ "Format a line for a field NAME with a VALUE.
+
+NAME may be a string or a symbol; if it is a symbol, the
+symbol name is used (removing a leading ':' if present).
+
+VALUE may be any value; except for the following special values,
+the unquoted printed representation of it is used:
+
+- `now' means the current time; see `pelican-timestamp'.
+
+- `slug' means the file's path relative to the document root sans
+ extension; see `pelican-default-slug'.
+
+- nil or an empty strings means return an empty string, without
+ any name or value."
+ (setq value (pcase value
+ ('now (pelican-timestamp))
+ ('slug (pelican-default-slug))
+ ('"" nil)
+ (_ value)))
+ (when (symbolp name)
+ (setq name (string-remove-prefix ":" (symbol-name name))))
+ (if value
+ (cond ((derived-mode-p 'markdown-mode)
+ (format "%s: %s\n" (capitalize name) value))
+ ((derived-mode-p 'rst-mode)
+ (format ":%s: %s\n" (downcase name) value))
+ (t (error "Unsupported major mode %S" major-mode)))
+ ""))
+
+(defun pelican-rst-title (title)
+ "Format a reStructureText version of TITLE."
+ (concat title "\n" (make-string (string-width title) ?#) "\n\n"))
+
+(defun pelican-title (title)
+ "Format a TITLE for the current document, according to major mode."
+ (cond ((derived-mode-p 'markdown-mode)
+ (pelican-field "title" title))
+ ((derived-mode-p 'rst-mode)
+ (pelican-rst-title title))
+ (t (error "Unsupported major mode %S" major-mode))))
+
+(defun pelican-header (title &rest fields)
+ "Generate a Pelican header for a post with a TITLE and metadata FIELDS."
+ (concat (pelican-title title)
+ (mapconcat (apply-partially #'apply #'pelican-field)
+ (seq-partition fields 2) "")
+ "\n"))
+
+(defun pelican-insert-header (title &rest fields)
+ "Insert a Pelican header for a post with a TITLE and metadata FIELDS."
+ (save-excursion
+ (goto-char 0)
+ (insert (apply #'pelican-header (cons title fields)))))