60089cda3a070862a3dc98b6220aa7ee75f31c7f
[pelican-mode.git] / pelican-mode.el
1 ;;; pelican-mode.el --- Minor mode for editing Pelican sites -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright 2013-2017 Joe Wreschnig
4 ;;
5 ;; Author: Joe Wreschnig <joe.wreschnig@gmail.com>
6 ;; Package-Version: 20170730
7 ;; Package-Requires: ((emacs "25"))
8 ;; Keywords: convenience, editing
9 ;;
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.
14 ;;
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.
19 ;;
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/>.
22
23 \f
24
25 ;;; Commentary:
26 ;;
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/.
31 ;;
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:
37 ;;
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.
44 ;;
45 ;; To enable by default on all text files in a Pelican site:
46 ;;
47 ;; (require 'pelican-mode)
48 ;; (pelican-global-mode)
49 ;;
50 ;; Or, register `pelican-mode' or `pelican-mode-enable-if-site'
51 ;; as hook functions for more direct control.
52
53 \f
54
55 ;;; Code:
56
57 (require 'seq)
58 (require 'subr-x)
59
60 ;; Mode Definition
61
62 ;;;###autoload
63 (define-minor-mode pelican-mode
64 "Toggle 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.
68
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/.
72
73 Rather than manually enabling this mode, you may wish to use
74 `pelican-global-mode' or `pelican-mode-enable-if-site'.
75
76 When Pelican mode is enabled, additional commands are available
77 for editing articles or pages:
78
79 \\{pelican-mode-map}"
80 :lighter " Pelican"
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)))
87
88 ;;;###autoload
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.
94
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/.
98
99 When Pelican global mode is enabled, text files which seem to
100 be part of a Pelican site will have `pelican-mode' automatically
101 enabled.
102
103 If you disable this, you may still enable `pelican-mode' manually
104 or add `pelican-mode-enable-if-site' to more specific mode
105 hooks."
106 :global t
107 :group 'pelican-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)))
111
112 ;;;###autoload
113 (defun pelican-mode-enable-if-site ()
114 "Enable `pelican-mode' if this buffer is part of a Pelican site.
115
116 Pelican sites are detected by looking for a file named `pelicanconf.py'
117 in an ancestor directory."
118 (when (pelican-mode-find-root)
119 (pelican-mode)))
120
121 \f
122
123 ;; Customizations
124
125 (defgroup pelican-mode nil
126 "Support for Pelican articles and pages.
127
128 For more information about Pelican see URL https://blog.getpelican.com/."
129 :group 'convenience)
130
131 (defcustom pelican-mode-default-page-fields
132 '(:slug slug)
133 "Fields to include when creating a new page.
134
135 See the documentation for `pelican-mode-set-field' for more information
136 about metadata fields and special values."
137 :group 'pelican-mode
138 :type '(plist))
139
140 (defcustom pelican-mode-default-article-fields
141 '(:date now :status "draft" :slug slug)
142 "Fields to include when creating a new article.
143
144 See the documentation for `pelican-mode-set-field' for more information
145 about metadata fields and special values."
146 :group 'pelican-mode
147 :type '(plist))
148
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.
155
156 This association list maps modes to functions that take two
157 arguments, field and value strings."
158 :group 'pelican-mode
159 :type '(alist :key-type function :value-type function))
160
161 \f
162
163 ;; User Commands
164
165 (defun pelican-mode-set-field (field value)
166 "Set FIELD to VALUE.
167
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).
170
171 When called from Lisp, VALUE may be any value; except for the
172 following special values, the unquoted printed representation of
173 it is used:
174
175 - `now' means the current time.
176
177 - `slug' means the file's path relative to the document root sans
178 extension; see `pelican-mode-default-slug'.
179
180 - nil or an empty string removes the field.
181
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))
188 ('"" nil)
189 (_ value)))
190 (when (symbolp field)
191 (setq field (string-remove-prefix ":" (symbol-name field))))
192 (let ((set-field
193 (assoc-default nil pelican-mode-formats #'derived-mode-p)))
194 (unless set-field
195 (error "Unsupported major mode %S" major-mode))
196 (save-excursion
197 (goto-char 0)
198 (funcall set-field field value))))
199
200 (defun pelican-mode-remove-field (field)
201 "Remove FIELD."
202 (interactive "sField: ")
203 (pelican-mode-set-field field nil))
204
205 (defun pelican-mode-set-title (title)
206 "Set the title to TITLE."
207 (interactive "sTitle: ")
208 (pelican-mode-set-field :title title))
209
210 (defun pelican-mode-update-date (&optional original)
211 "Update the document's modification date.
212
213 If ORIGINAL is non-nil, the publication date is updated rather
214 than the modification date."
215 (interactive "P")
216 (pelican-mode-set-field (if original :date :modified) 'now))
217
218 (defun pelican-mode-publish-draft ()
219 "Remove draft status from a Pelican article."
220 (interactive)
221 (pelican-mode-remove-field :status)
222 (pelican-mode-update-date :date))
223
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
228 `(:title ,title
229 ,@pelican-mode-default-article-fields
230 :tags ,tags)))
231
232 (defun pelican-mode-insert-page-header (title &optional hidden)
233 "Insert a Pelican header for a page with a TITLE.
234
235 If HIDDEN is non-nil, the page is marked hidden; otherwise it
236 has no status."
237 (interactive "sPage title: \nP")
238 (apply #'pelican-mode-set-fields
239 (append
240 (list :title title :status (when hidden "hidden"))
241 pelican-mode-default-page-fields)))
242
243 (defun pelican-mode-insert-header ()
244 "Insert a Pelican header for a page or article."
245 (interactive)
246 (call-interactively
247 (if (pelican-mode-page-p)
248 #'pelican-mode-insert-page-header
249 #'pelican-mode-insert-draft-article-header)))
250
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")))
258
259 (defun pelican-make-html ()
260 "Generate HTML via a Makefile at the root of the site."
261 (interactive)
262 (pelican-make "html"))
263
264 (defun pelican-make-rsync-upload ()
265 "Upload with rsync via a Makefile at the root of the site."
266 (interactive)
267 (pelican-make "rsync_upload"))
268
269 \f
270
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)))
275
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)
284 (insert header)))
285 (let ((text (when value (format ":%s: %s\n" field value))))
286 (when (looking-at "^.*\n#")
287 (forward-line 3))
288 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
289 (replace-match (or text ""))
290 (when text
291 (if (re-search-forward "^$" nil t)
292 (replace-match text)
293 (insert text)))))))
294
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)
300 (replace-match text)
301 (when value
302 (if (re-search-forward "^$" nil t)
303 (replace-match text)
304 (insert text))))))
305
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)
313 (insert header)))
314 (let ((text (when value (format ":%s: %s\n" field value))))
315 (when (looking-at "^=")
316 (forward-line 2))
317 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
318 (replace-match (or text ""))
319 (when text
320 (if (re-search-forward "^$" nil t)
321 (replace-match text)
322 (insert text)))))))
323
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))
329 (setq field
330 (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
331 "#+%s:"
332 "#+PROPERTY: %s")
333 field))
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 ""))
337 (when text
338 (if (re-search-forward "^$" nil t)
339 (replace-match text)
340 (insert text))))))
341
342 (defun pelican-mode-page-p ()
343 "Return non-nil the current buffer is a Pelican page."
344 (string-match-p
345 "^[^/]+/pages/"
346 (file-relative-name
347 (abbreviate-file-name (or (buffer-file-name) (buffer-name)))
348 (pelican-mode-find-root))))
349
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/\\)?" ""
355 (file-relative-name
356 (abbreviate-file-name (or (buffer-file-name) (buffer-name)))
357 (pelican-mode-find-root)))))
358
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"))
362
363 (provide 'pelican-mode)
364 ;;; pelican-mode.el ends here
365
366 \f
367
368 ;; Local Variables:
369 ;; sentence-end-double-space: t
370 ;; End: