-(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)))))
-
-(defun pelican-insert-page-header (title hidden)
- "Insert a Pelican header for a page."
- (interactive
- (list (read-string "Page title: ")
- (y-or-n-p "Hidden? ")))
- (let ((slug (pelican-default-slug))
- (hidden (if hidden "hidden" nil))
- (header (if (pelican-is-markdown)
- 'pelican-markdown-header 'pelican-rst-header)))
+(require 'seq)
+(require 'subr-x)
+
+(defgroup pelican-mode nil
+ "Support for Pelican articles and pages.
+
+For more information about Pelican see URL https://blog.getpelican.com/."
+ :group 'convenience)
+
+(defcustom pelican-mode-default-page-fields
+ '(:slug slug)
+ "Fields to include when creating a new page.
+
+See the documentation for `pelican-mode-set-field' for more information
+about metadata fields and special values."
+ :group 'pelican-mode
+ :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-set-field' for more information
+about metadata fields and special values."
+ :group 'pelican-mode
+ :type '(plist))
+
+(defcustom pelican-mode-formats
+ '((markdown-mode . pelican-mode-set-field-markdown-mode)
+ (adoc-mode . pelican-mode-set-field-adoc-mode)
+ (org-mode . pelican-mode-set-field-org-mode)
+ (rst-mode . pelican-mode-set-field-rst-mode))
+ "Functions to handle setting metadata, based on major mode.
+
+This association list maps modes to functions that take two
+arguments, field and value strings."
+ :group 'pelican-mode
+ :type '(alist :key-type function :value-type function))
+
+(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-set-fields (&rest fields)
+ "Insert a Pelican header for an article with metadata FIELDS."
+ (mapc (apply-partially #'apply #'pelican-mode-set-field)
+ (seq-partition fields 2)))
+
+(defun pelican-mode-insert-draft-article-header (title tags)
+ "Insert a Pelican header for a draft with a TITLE and TAGS."
+ (interactive "sArticle title: \nsTags: ")
+ (apply #'pelican-mode-set-fields
+ `(:title ,title
+ ,@pelican-mode-default-article-fields
+ :tags ,tags)))
+
+(defun pelican-mode-insert-page-header (title &optional hidden)
+ "Insert a Pelican header for a page with a TITLE.
+
+If HIDDEN is non-nil, the page is marked hidden; otherwise it
+has no status."
+ (interactive "sPage title: \nP")
+ (apply #'pelican-mode-set-fields
+ (append
+ (list :title title :status (when hidden "hidden"))
+ pelican-mode-default-page-fields)))
+
+(defun pelican-mode-insert-header ()
+ "Insert a Pelican header for a page or article."
+ (interactive)
+ (call-interactively
+ (if (pelican-mode-page-p)
+ #'pelican-mode-insert-page-header
+ #'pelican-mode-insert-draft-article-header)))
+
+(defun pelican-mode-set-field-rst-mode (field value)
+ "Set reStructuredText metadata FIELD to VALUE."
+ (setq field (downcase field))
+ (if (equal field "title")
+ (let ((header (format "%s\n%s\n\n"
+ value (make-string (string-width value) ?#))))
+ (if (looking-at ".*\n#+\n+")
+ (replace-match header)
+ (insert header)))
+ (let ((text (when value (format ":%s: %s\n" field value))))
+ (when (looking-at "^.*\n#")
+ (forward-line 3))
+ (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
+ (replace-match (or text ""))
+ (when text
+ (if (re-search-forward "^$" nil t)
+ (replace-match text)
+ (insert text)))))))
+
+(defun pelican-mode-set-field-markdown-mode (field value)
+ "Set Markdown metadata FIELD to VALUE."
+ (setq field (capitalize field))
+ (let ((text (when value (format "%s: %s\n" field value))))
+ (if (re-search-forward (format "^%s:.*\n" (regexp-quote field)) nil t)
+ (replace-match text)
+ (when value
+ (if (re-search-forward "^$" nil t)
+ (replace-match text)
+ (insert text))))))
+
+(defun pelican-mode-set-field-adoc-mode (field value)
+ "Set AsciiDoc metadata FIELD to VALUE."
+ (setq field (downcase field))
+ (if (equal field "title")
+ (let ((header (format "= %s\n\n" value)))
+ (if (looking-at "= .*\n\n+")
+ (replace-match header)
+ (insert header)))
+ (let ((text (when value (format ":%s: %s\n" field value))))
+ (when (looking-at "^=")
+ (forward-line 2))
+ (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
+ (replace-match (or text ""))
+ (when text
+ (if (re-search-forward "^$" nil t)
+ (replace-match text)
+ (insert text)))))))
+
+(defun pelican-mode-set-field-org-mode (field value)
+ "Set Org global metadata FIELD to VALUE."
+ ;; None of org-mode's functions I can find for setting properties
+ ;; operate on the global list, only a single property drawer.
+ (setq field (upcase field))
+ (setq field
+ (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
+ "#+%s:"
+ "#+PROPERTY: %s")
+ field))
+ (let ((text (when value (format "%s %s\n" field value))))
+ (if (re-search-forward (format "^%s .*\n" (regexp-quote field)) nil t)
+ (replace-match (or text ""))
+ (when text
+ (if (re-search-forward "^$" nil t)
+ (replace-match text)
+ (insert text))))))
+
+(defun pelican-mode-set-field (field value)
+ "Set FIELD to VALUE.
+
+FIELD may be a string or a symbol; if it is a symbol, the
+symbol name is used (removing a leading ':' if present).
+
+When called from Lisp, 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 string removes the field.
+
+The buffer must be in a format listed in `pelican-mode-formats'
+for this function to work correctly."
+ (interactive "sField: \nsValue: ")
+ (setq value (pcase value
+ ('now (pelican-mode-timestamp))
+ ('slug (pelican-mode-default-slug))
+ ('"" nil)
+ (_ value)))
+ (when (symbolp field)
+ (setq field (string-remove-prefix ":" (symbol-name field))))
+ (let ((set-field
+ (assoc-default nil pelican-mode-formats #'derived-mode-p)))
+ (unless set-field
+ (error "Unsupported major mode %S" major-mode))