-(defun pelican-timestamp-now ()
- "Generate a Pelican-compatible timestamp."
- (format-time-string "%Y-%m-%d %H:%M"))
-
-(defun pelican-is-markdown ()
- "Check if the buffer is likely using Markdown."
- (eq major-mode 'markdown-mode))
-
-(defun pelican-field (name value)
- "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")))
-
-(defun pelican-insert-draft-post-header (title tags)
- "Insert a Pelican header for a draft post."
- (interactive "sPost title: \nsTags: ")
- (let ((slug (pelican-default-slug))
- (header (if (pelican-is-markdown)
- 'pelican-markdown-header 'pelican-rst-header)))
- (save-excursion
- (goto-char 0)
- (insert (funcall header title 't "draft" nil tags slug)))))
+(require 'seq)
+(require 'subr-x)
+
+(defgroup pelican-mode nil
+ "Support for Pelican articles and pages."
+ :group 'convenience)
+
+(defcustom pelican-mode-default-page-fields
+ '(:slug slug)
+ "Fields to include when creating a new page.
+
+See the documentation for `pelican-mode-field' for more information
+about metadata fields and special values."
+ :group 'pelican
+ :type '(plist))
+
+(defcustom pelican-mode-default-article-fields
+ '(:date now :status "draft" :slug slug)
+ "Fields to include when creating a new article.
+
+See the documentation for `pelican-mode-field' for more information
+about metadata fields and special values."
+ :group 'pelican
+ :type '(plist))
+
+(defun pelican-mode-timestamp (&optional time)
+ "Generate a pelican-mode-compatible timestamp for TIME."
+ (format-time-string "%Y-%m-%d %H:%M" time))
+
+(defun pelican-mode-field (name value)
+ "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-mode-timestamp'.
+
+- `slug' means the file's path relative to the document root sans
+ extension; see `pelican-mode-default-slug'.
+
+- nil or an empty strings means return an empty string, without
+ any name or value."
+ (setq value (pcase value
+ ('now (pelican-mode-timestamp))
+ ('slug (pelican-mode-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-mode-rst-title (title)
+ "Format a reStructureText version of TITLE."
+ (concat title "\n" (make-string (string-width title) ?#) "\n\n"))
+
+(defun pelican-mode-title (title)
+ "Format a TITLE for the current document, according to major mode."
+ (cond ((derived-mode-p 'markdown-mode)
+ (pelican-mode-field "title" title))
+ ((derived-mode-p 'rst-mode)
+ (pelican-mode-rst-title title))
+ (t (error "Unsupported major mode %S" major-mode))))
+
+(defun pelican-mode-header (title &rest fields)
+ "Generate a Pelican header for an article with a TITLE and metadata FIELDS."
+ (concat (pelican-mode-title title)
+ (mapconcat (apply-partially #'apply #'pelican-mode-field)
+ (seq-partition fields 2) "")
+ "\n"))
+
+(defun pelican-mode-insert-header (title &rest fields)
+ "Insert a Pelican header for an article with a TITLE and metadata FIELDS."
+ (save-excursion
+ (goto-char 0)
+ (insert (apply #'pelican-mode-header (cons title fields)))))