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/>.
26 ;; pelican-mode is an Emacs minor mode for editing pages and posts in
27 ;; Pelican sites. Pelican is a static site generator which can
28 ;; process a variety of text file formats. For more information, see
29 ;; URL https://blog.getpelican.com/.
31 ;; It's intended to be used alongside `markdown-mode' or `rst-mode'.
32 ;; It also assumes you've set up Pelican with ``pelican-quickstart''
33 ;; or something like it. In particular it assumes:
35 ;; * The existence of ``pelicanconf.py'' and ``Makefile'' in some
36 ;; ancestor directory.
37 ;; * The first component of the path (e.g. ``content'') after that
38 ;; ancestor is irrelevant.
39 ;; * If the next component is ``pages'', that indicates a page
40 ;; rather than an article.
48 (defgroup pelican-mode nil
49 "Support for Pelican articles and pages.
51 For more information about Pelican see URL https://blog.getpelican.com/."
54 (defcustom pelican-mode-default-page-fields
56 "Fields to include when creating a new page.
58 See the documentation for `pelican-mode-set-field' for more information
59 about metadata fields and special values."
63 (defcustom pelican-mode-default-article-fields
64 '(:date now :status "draft" :slug slug)
65 "Fields to include when creating a new article.
67 See the documentation for `pelican-mode-set-field' for more information
68 about metadata fields and special values."
72 (defcustom pelican-mode-formats
73 '((markdown-mode . pelican-mode-set-field-markdown-mode)
74 (adoc-mode . pelican-mode-set-field-adoc-mode)
75 (org-mode . pelican-mode-set-field-org-mode)
76 (rst-mode . pelican-mode-set-field-rst-mode))
77 "Functions to handle setting metadata, based on major mode.
79 This association list maps modes to functions that take two
80 arguments, field and value strings."
82 :type '(alist :key-type function :value-type function))
84 (defun pelican-mode-timestamp (&optional time)
85 "Generate a pelican-mode-compatible timestamp for TIME."
86 (format-time-string "%Y-%m-%d %H:%M" time))
88 (defun pelican-mode-insert-header (&rest fields)
89 "Insert a Pelican header for an article with metadata FIELDS."
90 (mapc (apply-partially #'apply #'pelican-mode-set-field)
91 (seq-partition fields 2)))
93 (defun pelican-mode-insert-draft-article-header (title tags)
94 "Insert a Pelican header for a draft with a TITLE and TAGS."
95 (interactive "sArticle title: \nsTags: ")
96 (apply #'pelican-mode-insert-header
97 `(:title ,title ,@pelican-mode-default-article-fields :tags ,tags)))
99 (defun pelican-mode-insert-page-header (title &optional hidden)
100 "Insert a Pelican header for a page with a TITLE, potentially HIDDEN."
102 (list (read-string "Page title: ")
103 (y-or-n-p "Hidden? ")))
104 (apply #'pelican-mode-insert-header
105 `(:title ,title ,@pelican-mode-default-page-fields
106 :hidden ,(when hidden "hidden"))))
108 (defun pelican-mode-insert-auto-header ()
109 "Insert a Pelican header for a page or article."
112 (if (pelican-mode-page-p)
113 #'pelican-mode-insert-page-header
114 #'pelican-mode-insert-draft-article-header)))
116 (defun pelican-mode-set-field-rst-mode (field value)
117 "Set reStructuredText metadata FIELD to VALUE."
118 (setq field (downcase field))
119 (if (equal field "title")
120 (let ((header (format "%s\n%s\n\n"
121 value (make-string (string-width value) ?#))))
122 (if (looking-at ".*\n#+\n+")
123 (replace-match header)
125 (let ((text (when value (format ":%s: %s\n" field value))))
126 (when (looking-at "^.*\n#")
128 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
129 (replace-match (or text ""))
131 (if (re-search-forward "^$" nil t)
135 (defun pelican-mode-set-field-markdown-mode (field value)
136 "Set Markdown metadata FIELD to VALUE."
137 (setq field (capitalize field))
138 (let ((text (when value (format "%s: %s\n" field value))))
139 (if (re-search-forward (format "^%s:.*\n" (regexp-quote field)) nil t)
142 (if (re-search-forward "^$" nil t)
146 (defun pelican-mode-set-field-adoc-mode (field value)
147 "Set AsciiDoc metadata FIELD to VALUE."
148 (setq field (downcase field))
149 (if (equal field "title")
150 (let ((header (format "= %s\n\n" value)))
151 (if (looking-at "= .*\n\n+")
152 (replace-match header)
154 (let ((text (when value (format ":%s: %s\n" field value))))
155 (when (looking-at "^=")
157 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
158 (replace-match (or text ""))
160 (if (re-search-forward "^$" nil t)
164 (defun pelican-mode-set-field-org-mode (field value)
165 "Set Org global metadata FIELD to VALUE."
166 ;; None of org-mode's functions I can find for setting properties
167 ;; operate on the global list, only a single property drawer.
168 (setq field (upcase field))
170 (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
174 (let ((text (when value (format "%s %s\n" field value))))
175 (if (re-search-forward (format "^%s .*\n" (regexp-quote field)) nil t)
176 (replace-match (or text ""))
178 (if (re-search-forward "^$" nil t)
182 (defun pelican-mode-set-field (field value)
185 FIELD may be a string or a symbol; if it is a symbol, the
186 symbol name is used (removing a leading ':' if present).
188 When called from Lisp, VALUE may be any value; except for the
189 following special values, the unquoted printed representation of
192 - `now' means the current time; see `pelican-mode-timestamp'.
194 - `slug' means the file's path relative to the document root sans
195 extension; see `pelican-mode-default-slug'.
197 - nil or an empty string removes the field.
199 The buffer must be in a format listed in `pelican-mode-formats'
200 for this function to work correctly."
201 (interactive "sField: \nsValue: ")
202 (setq value (pcase value
203 ('now (pelican-mode-timestamp))
204 ('slug (pelican-mode-default-slug))
207 (when (symbolp field)
208 (setq field (string-remove-prefix ":" (symbol-name field))))
210 (assoc-default nil pelican-mode-formats #'derived-mode-p)))
212 (error "Unsupported major mode %S" major-mode))
215 (funcall set-field field value))))
217 (defun pelican-mode-remove-field (field)
219 (pelican-mode-set-field field nil))
221 (defun pelican-mode-set-title (title)
222 "Set the title to TITLE."
223 (interactive "sTitle: ")
224 (pelican-mode-set-field :title title))
226 (defun pelican-mode-update-date ()
227 "Update a Pelican date header."
229 (pelican-mode-set-field :date 'now))
231 (defun pelican-mode-publish-draft ()
232 "Remove draft status from a Pelican article."
234 (pelican-mode-remove-field :status)
235 (pelican-mode-update-date))
237 (defun pelican-mode-page-p ()
238 "Return non-nil the current buffer is a Pelican page."
239 (when-let (pelican-mode-base (pelican-mode-find-root))
240 (let* ((relative (file-relative-name buffer-file-name pelican-mode-base))
241 (components (split-string relative "/")))
242 (equal "pages" (cadr components)))))
244 (defun pelican-mode-default-slug ()
245 "Generate a Pelican article/page slug for the current buffer."
246 (if-let ((pelican-mode-base (pelican-mode-find-root))
247 (file-name (file-name-sans-extension buffer-file-name)))
248 (let* ((relative (file-relative-name file-name pelican-mode-base))
249 (components (cdr (split-string relative "/")))
250 (components (if (string= "pages" (car components))
251 (cdr components) components)))
252 (mapconcat 'identity components "/"))
253 (when-let (file-name (file-name-sans-extension buffer-file-name))
254 (file-name-base file-name))))
256 (defun pelican-mode-find-root ()
257 "Return the root of the buffer's Pelican site, or nil."
258 (locate-dominating-file default-directory "pelicanconf.py"))
260 (defun pelican-make (target)
261 "Execute TARGET in a Makefile at the root of the site."
262 (interactive "sMake Pelican target: ")
263 (if-let (default-directory (pelican-mode-find-root))
264 (compilation-start (format "make %s" target)
265 nil (lambda (_) "*pelican*"))
266 (user-error "No Pelican site root could be found")))
268 (defun pelican-make-html ()
269 "Generate HTML via a Makefile at the root of the site."
271 (pelican-make "html"))
273 (defun pelican-make-rsync-upload ()
274 "Upload with rsync via a Makefile at the root of the site."
276 (pelican-make "rsync_upload"))
279 (define-minor-mode pelican-mode
280 "Toggle Pelican mode.
281 With a prefix argument ARG, enable Pelican mode if ARG is
282 positive, and disable it otherwise. If called from Lisp, enable
283 the mode if ARG is omitted or nil.
285 Pelican is a static site generator which can process a variety of
286 text file formats. For more information, see URL
287 https://blog.getpelican.com/.
289 When Pelican mode is enabled, additional commands are available
290 for editing articles or pages:
292 \\{pelican-mode-map}"
294 :keymap `((,(kbd "C-c P n") . pelican-mode-insert-auto-header)
295 (,(kbd "C-c P p") . pelican-mode-publish-draft)
296 (,(kbd "C-c P t") . pelican-mode-update-date)
297 (,(kbd "C-c P h") . pelican-make-html)
298 (,(kbd "C-c P f") . pelican-set-field)
299 (,(kbd "C-c P u") . pelican-make-rsync-upload)))
302 (define-minor-mode pelican-global-mode
303 "Toggle Pelican global mode.
304 With a prefix argument ARG, enable Pelican global mode if ARG is
305 positive, and disable it otherwise. If called from Lisp, enable
306 the mode if ARG is omitted or nil.
308 Pelican is a static site generator which can process a variety of
309 text file formats. For more information, see URL
310 https://blog.getpelican.com/.
312 When Pelican global mode is enabled, text files which seem to
313 be part of a Pelican site will have `pelican-mode' automatically
316 If you disable this, you may still enable `pelican-mode' manually
317 or add `pelican-mode-enable-if-site' to more specific mode
321 (if pelican-global-mode
322 (add-hook 'text-mode-hook #'pelican-mode-enable-if-site)
323 (remove-hook 'text-mode-hook #'pelican-mode-enable-if-site)))
326 (defun pelican-mode-enable-if-site ()
327 "Enable `pelican-mode' if this buffer is part of a Pelican site."
328 (when (pelican-mode-find-root)
331 (provide 'pelican-mode)
332 ;;; pelican-mode.el ends here
335 ;; sentence-end-double-space: t