`org-mode’ support.
[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
24 ;;; Commentary:
25 ;;
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/.
30 ;;
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:
34 ;;
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.
41
42
43 ;;; Code:
44
45 (require 'seq)
46 (require 'subr-x)
47
48 (defgroup pelican-mode nil
49 "Support for Pelican articles and pages.
50
51 For more information about Pelican see URL https://blog.getpelican.com/."
52 :group 'convenience)
53
54 (defcustom pelican-mode-default-page-fields
55 '(:slug slug)
56 "Fields to include when creating a new page.
57
58 See the documentation for `pelican-mode-set-field' for more information
59 about metadata fields and special values."
60 :group 'pelican-mode
61 :type '(plist))
62
63 (defcustom pelican-mode-default-article-fields
64 '(:date now :status "draft" :slug slug)
65 "Fields to include when creating a new article.
66
67 See the documentation for `pelican-mode-set-field' for more information
68 about metadata fields and special values."
69 :group 'pelican-mode
70 :type '(plist))
71
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.
78
79 This association list maps modes to functions that take two
80 arguments, field and value strings."
81 :group 'pelican-mode
82 :type '(alist :key-type function :value-type function))
83
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))
87
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)))
92
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)))
98
99 (defun pelican-mode-insert-page-header (title &optional hidden)
100 "Insert a Pelican header for a page with a TITLE, potentially HIDDEN."
101 (interactive
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"))))
107
108 (defun pelican-mode-insert-auto-header ()
109 "Insert a Pelican header for a page or article."
110 (interactive)
111 (call-interactively
112 (if (pelican-mode-page-p)
113 #'pelican-mode-insert-page-header
114 #'pelican-mode-insert-draft-article-header)))
115
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)
124 (insert header)))
125 (let ((text (when value (format ":%s: %s\n" field value))))
126 (when (looking-at "^.*\n#")
127 (forward-line 3))
128 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
129 (replace-match (or text ""))
130 (when text
131 (if (re-search-forward "^$" nil t)
132 (replace-match text)
133 (insert text)))))))
134
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)
140 (replace-match text)
141 (when value
142 (if (re-search-forward "^$" nil t)
143 (replace-match text)
144 (insert text))))))
145
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)
153 (insert header)))
154 (let ((text (when value (format ":%s: %s\n" field value))))
155 (when (looking-at "^=")
156 (forward-line 2))
157 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
158 (replace-match (or text ""))
159 (when text
160 (if (re-search-forward "^$" nil t)
161 (replace-match text)
162 (insert text)))))))
163
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))
169 (setq field
170 (format (if (member field '("TITLE" "DATE" "CATEGORY" "AUTHOR"))
171 "#+%s:"
172 "#+PROPERTY: %s")
173 field))
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 ""))
177 (when text
178 (if (re-search-forward "^$" nil t)
179 (replace-match text)
180 (insert text))))))
181
182 (defun pelican-mode-set-field (field value)
183 "Set FIELD to VALUE.
184
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).
187
188 When called from Lisp, VALUE may be any value; except for the
189 following special values, the unquoted printed representation of
190 it is used:
191
192 - `now' means the current time; see `pelican-mode-timestamp'.
193
194 - `slug' means the file's path relative to the document root sans
195 extension; see `pelican-mode-default-slug'.
196
197 - nil or an empty string removes the field.
198
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))
205 ('"" nil)
206 (_ value)))
207 (when (symbolp field)
208 (setq field (string-remove-prefix ":" (symbol-name field))))
209 (let ((set-field
210 (assoc-default nil pelican-mode-formats #'derived-mode-p)))
211 (unless set-field
212 (error "Unsupported major mode %S" major-mode))
213 (save-excursion
214 (goto-char 0)
215 (funcall set-field field value))))
216
217 (defun pelican-mode-remove-field (field)
218 "Remove FIELD."
219 (pelican-mode-set-field field nil))
220
221 (defun pelican-mode-set-title (title)
222 "Set the title to TITLE."
223 (interactive "sTitle: ")
224 (pelican-mode-set-field :title title))
225
226 (defun pelican-mode-update-date ()
227 "Update a Pelican date header."
228 (interactive)
229 (pelican-mode-set-field :date 'now))
230
231 (defun pelican-mode-publish-draft ()
232 "Remove draft status from a Pelican article."
233 (interactive)
234 (pelican-mode-remove-field :status)
235 (pelican-mode-update-date))
236
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)))))
243
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))))
255
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"))
259
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")))
267
268 (defun pelican-make-html ()
269 "Generate HTML via a Makefile at the root of the site."
270 (interactive)
271 (pelican-make "html"))
272
273 (defun pelican-make-rsync-upload ()
274 "Upload with rsync via a Makefile at the root of the site."
275 (interactive)
276 (pelican-make "rsync_upload"))
277
278 ;;;###autoload
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.
284
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/.
288
289 When Pelican mode is enabled, additional commands are available
290 for editing articles or pages:
291
292 \\{pelican-mode-map}"
293 :lighter " Pelican"
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)))
300
301 ;;;###autoload
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.
307
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/.
311
312 When Pelican global mode is enabled, text files which seem to
313 be part of a Pelican site will have `pelican-mode' automatically
314 enabled.
315
316 If you disable this, you may still enable `pelican-mode' manually
317 or add `pelican-mode-enable-if-site' to more specific mode
318 hooks."
319 :global t
320 :group 'pelican-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)))
324
325 ;;;###autoload
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)
329 (pelican-mode 1)))
330
331 (provide 'pelican-mode)
332 ;;; pelican-mode.el ends here
333
334 ;; Local Variables:
335 ;; sentence-end-double-space: t
336 ;; End: