1 ;;; pelican-mode.el --- Minor mode for editing Pelican sites -*- lexical-binding: t -*-
3 ;; Copyright 2013-2017 Joe Wreschnig
5 ;; Author: Joe Wreschnig <joe.wreschnig@gmail.com>
6 ;; Package-Version: 20170730
7 ;; Package-Requires: ((emacs "25"))
8 ;; Keywords: convenience, editing
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ;; pelican-mode is an Emacs minor mode for editing pages and posts in
28 ;; Pelican sites. Pelican is a static site generator which can
29 ;; process a variety of text file formats. For more information, see
30 ;; URL https://blog.getpelican.com/.
32 ;; It's intended to be used alongside a major mode for the Pelican
33 ;; document. Currently supported formats are Markdown,
34 ;; reStructuredText, AsciiDoc, and Org. It also assumes you've set up
35 ;; Pelican with ``pelican-quickstart'' or something like it. In
36 ;; particular it expects:
38 ;; * The existence of ``pelicanconf.py'' and ``Makefile'' in some
39 ;; ancestor directory.
40 ;; * The first component of the path (e.g. ``content'') after that
41 ;; ancestor is irrelevant.
42 ;; * If the next component is ``pages'', that indicates a page
43 ;; rather than an article.
45 ;; To enable by default on all text files in a Pelican site:
47 ;; (require 'pelican-mode)
48 ;; (pelican-global-mode)
50 ;; Or, register `pelican-mode' or `pelican-mode-enable-if-site'
51 ;; as hook functions for more direct control.
63 (define-minor-mode pelican-mode
65 With a prefix argument ARG, enable Pelican mode if ARG is
66 positive, and disable it otherwise. If called from Lisp, enable
67 the mode if ARG is omitted or nil.
69 Pelican is a static site generator which can process a variety of
70 text file formats. For more information, see URL
71 https://blog.getpelican.com/.
73 Rather than manually enabling this mode, you may wish to use
74 `pelican-global-mode' or `pelican-mode-enable-if-site'.
76 When Pelican mode is enabled, additional commands are available
77 for editing articles or pages:
81 :keymap `((,(kbd "C-c P d") . pelican-mode-update-date)
82 (,(kbd "C-c P f") . pelican-set-field)
83 (,(kbd "C-c P h") . pelican-make-html)
84 (,(kbd "C-c P n") . pelican-mode-insert-header)
85 (,(kbd "C-c P p") . pelican-mode-publish-draft)
86 (,(kbd "C-c P u") . pelican-make-rsync-upload)))
89 (define-minor-mode pelican-global-mode
90 "Toggle Pelican global mode.
91 With a prefix argument ARG, enable Pelican global mode if ARG is
92 positive, and disable it otherwise. If called from Lisp, enable
93 the mode if ARG is omitted or nil.
95 Pelican is a static site generator which can process a variety of
96 text file formats. For more information, see URL
97 https://blog.getpelican.com/.
99 When Pelican global mode is enabled, text files which seem to
100 be part of a Pelican site will have `pelican-mode' automatically
103 If you disable this, you may still enable `pelican-mode' manually
104 or add `pelican-mode-enable-if-site' to more specific mode
108 (if pelican-global-mode
109 (add-hook 'text-mode-hook #'pelican-mode-enable-if-site)
110 (remove-hook 'text-mode-hook #'pelican-mode-enable-if-site)))
113 (defun pelican-mode-enable-if-site ()
114 "Enable `pelican-mode' if this buffer is part of a Pelican site.
116 Pelican sites are detected by looking for a file named `pelicanconf.py'
117 in an ancestor directory."
118 (when (pelican-mode-find-root)
125 (defgroup pelican-mode nil
126 "Support for Pelican articles and pages.
128 For more information about Pelican see URL https://blog.getpelican.com/."
131 (defcustom pelican-mode-default-page-fields
133 "Fields to include when creating a new page.
135 See the documentation for `pelican-mode-set-field' for more information
136 about metadata fields and special values."
140 (defcustom pelican-mode-default-article-fields
141 '(:date now :status "draft" :slug slug)
142 "Fields to include when creating a new article.
144 See the documentation for `pelican-mode-set-field' for more information
145 about metadata fields and special values."
149 (defcustom pelican-mode-formats
150 '((adoc-mode . pelican-mode-set-field-adoc-mode)
151 (markdown-mode . pelican-mode-set-field-markdown-mode)
152 (org-mode . pelican-mode-set-field-org-mode)
153 (rst-mode . pelican-mode-set-field-rst-mode))
154 "Functions to handle setting metadata, based on major mode.
156 This association list maps modes to functions that take two
157 arguments, field and value strings."
159 :type '(alist :key-type function :value-type function))
165 (defun pelican-mode-set-field (field value)
168 FIELD may be a string or a symbol; if it is a symbol, the
169 symbol name is used (removing a leading ':' if present).
171 When called from Lisp, VALUE may be any value; except for the
172 following special values, the unquoted printed representation of
175 - `now' means the current time.
177 - `slug' means the file's path relative to the document root sans
178 extension; see `pelican-mode-default-slug'.
180 - nil or an empty string removes the field.
182 The buffer must be in a format listed in `pelican-mode-formats'
183 for this function to work correctly."
184 (interactive "sField: \nsValue: ")
185 (setq value (pcase value
186 ('now (format-time-string "%Y-%m-%d %H:%M"))
187 ('slug (pelican-mode-default-slug))
190 (when (symbolp field)
191 (setq field (string-remove-prefix ":" (symbol-name field))))
193 (assoc-default nil pelican-mode-formats #'derived-mode-p)))
195 (error "Unsupported major mode %S" major-mode))
198 (funcall set-field field value))))
200 (defun pelican-mode-remove-field (field)
202 (interactive "sField: ")
203 (pelican-mode-set-field field nil))
205 (defun pelican-mode-set-title (title)
206 "Set the title to TITLE."
207 (interactive "sTitle: ")
208 (pelican-mode-set-field :title title))
210 (defun pelican-mode-update-date (&optional original)
211 "Update the document's modification date.
213 If ORIGINAL is non-nil, the publication date is updated rather
214 than the modification date."
216 (pelican-mode-set-field (if original :date :modified) 'now))
218 (defun pelican-mode-publish-draft ()
219 "Remove draft status from a Pelican article."
221 (pelican-mode-remove-field :status)
222 (pelican-mode-update-date :date))
224 (defun pelican-mode-insert-draft-article-header (title tags)
225 "Insert a Pelican header for a draft with a TITLE and TAGS."
226 (interactive "sArticle title: \nsTags: ")
227 (apply #'pelican-mode-set-fields
229 ,@pelican-mode-default-article-fields
232 (defun pelican-mode-insert-page-header (title &optional hidden)
233 "Insert a Pelican header for a page with a TITLE.
235 If HIDDEN is non-nil, the page is marked hidden; otherwise it
237 (interactive "sPage title: \nP")
238 (apply #'pelican-mode-set-fields
240 (list :title title :status (when hidden "hidden"))
241 pelican-mode-default-page-fields)))
243 (defun pelican-mode-insert-header ()
244 "Insert a Pelican header for a page or article."
247 (if (pelican-mode-page-p)
248 #'pelican-mode-insert-page-header
249 #'pelican-mode-insert-draft-article-header)))
251 (defun pelican-make (target)
252 "Execute TARGET in a Makefile at the root of the site."
253 (interactive "sMake Pelican target: ")
254 (if-let (default-directory (pelican-mode-find-root))
255 (compilation-start (format "make %s" target)
256 nil (lambda (_) "*pelican*"))
257 (user-error "No Pelican site root could be found")))
259 (defun pelican-make-html ()
260 "Generate HTML via a Makefile at the root of the site."
262 (pelican-make "html"))
264 (defun pelican-make-rsync-upload ()
265 "Upload with rsync via a Makefile at the root of the site."
267 (pelican-make "rsync_upload"))
271 (defun pelican-mode-set-fields (&rest fields)
272 "Insert a Pelican header for an article with metadata FIELDS."
273 (mapc (apply-partially #'apply #'pelican-mode-set-field)
274 (seq-partition fields 2)))
276 (defun pelican-mode-set-field-rst-mode (field value)
277 "Set reStructuredText metadata FIELD to VALUE."
278 (setq field (downcase field))
279 (if (equal field "title")
280 (let ((header (format "%s\n%s\n\n"
281 value (make-string (string-width value) ?#))))
282 (if (looking-at ".*\n#+\n+")
283 (replace-match header)
285 (let ((text (when value (format ":%s: %s\n" field value))))
286 (when (looking-at "^.*\n#")
288 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
289 (replace-match (or text ""))
291 (if (re-search-forward "^$" nil t)
295 (defun pelican-mode-set-field-markdown-mode (field value)
296 "Set Markdown metadata FIELD to VALUE."
297 (setq field (capitalize field))
298 (let ((text (when value (format "%s: %s\n" field value))))
299 (if (re-search-forward (format "^%s:.*\n" (regexp-quote field)) nil t)
302 (if (re-search-forward "^$" nil t)
306 (defun pelican-mode-set-field-adoc-mode (field value)
307 "Set AsciiDoc metadata FIELD to VALUE."
308 (setq field (downcase field))
309 (if (equal field "title")
310 (let ((header (format "= %s\n\n" value)))
311 (if (looking-at "= .*\n\n+")
312 (replace-match header)
314 (let ((text (when value (format ":%s: %s\n" field value))))
315 (when (looking-at "^=")
317 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
318 (replace-match (or text ""))
320 (if (re-search-forward "^$" nil t)
324 (defun pelican-mode-set-field-org-mode (field value)
325 "Set Org global metadata FIELD to VALUE."
326 ;; None of org-mode's functions I can find for setting properties
327 ;; operate on the global list, only a single property drawer.
328 (setq field (upcase field))
330 (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
334 (let ((text (when value (format "%s %s\n" field value))))
335 (if (re-search-forward (format "^%s .*\n" (regexp-quote field)) nil t)
336 (replace-match (or text ""))
338 (if (re-search-forward "^$" nil t)
342 (defun pelican-mode-page-p ()
343 "Return non-nil the current buffer is a Pelican page."
347 (abbreviate-file-name (or (buffer-file-name) (buffer-name)))
348 (pelican-mode-find-root))))
350 (defun pelican-mode-default-slug ()
351 "Generate a Pelican slug for the current buffer."
352 (file-name-sans-extension
353 (replace-regexp-in-string
354 "^[^/]+/\\(?:pages/\\)?" ""
356 (abbreviate-file-name (or (buffer-file-name) (buffer-name)))
357 (pelican-mode-find-root)))))
359 (defun pelican-mode-find-root ()
360 "Return the root of the buffer's Pelican site, or nil."
361 (locate-dominating-file default-directory "pelicanconf.py"))
363 (provide 'pelican-mode)
364 ;;; pelican-mode.el ends here
369 ;; sentence-end-double-space: t