Customizable list of set-field implementations for modes.
[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 ;;; Commentary:
24 ;;
25 ;; Probably, this doesn't handle a lot of error cases. I also never
26 ;; tested it on networked drives and the lookup for pelicanconf.py
27 ;; might slow it down considerably.
28
29
30 ;;; Code:
31
32 (require 'seq)
33 (require 'subr-x)
34
35 (defgroup pelican-mode nil
36 "Support for Pelican articles and pages."
37 :group 'convenience)
38
39 (defcustom pelican-mode-default-page-fields
40 '(:slug slug)
41 "Fields to include when creating a new page.
42
43 See the documentation for `pelican-mode-set-field' for more information
44 about metadata fields and special values."
45 :group 'pelican
46 :type '(plist))
47
48 (defcustom pelican-mode-default-article-fields
49 '(:date now :status "draft" :slug slug)
50 "Fields to include when creating a new article.
51
52 See the documentation for `pelican-mode-set-field' for more information
53 about metadata fields and special values."
54 :group 'pelican
55 :type '(plist))
56
57 (defcustom pelican-mode-set-field-alist
58 '((markdown-mode . pelican-mode-set-field-markdown-mode)
59 (rst-mode . pelican-mode-set-field-rst-mode))
60 "Functions to handle setting metadata, based on major mode.
61
62 This association list maps modes to functions that take two
63 arguments, field and value strings."
64 :group 'pelican
65 :type '(alist :key-type function :value-type function))
66
67 (defun pelican-mode-timestamp (&optional time)
68 "Generate a pelican-mode-compatible timestamp for TIME."
69 (format-time-string "%Y-%m-%d %H:%M" time))
70
71 (defun pelican-mode-insert-header (&rest fields)
72 "Insert a Pelican header for an article with metadata FIELDS."
73 (mapc (apply-partially #'apply #'pelican-mode-set-field)
74 (seq-partition fields 2)))
75
76 (defun pelican-mode-insert-draft-article-header (title tags)
77 "Insert a Pelican header for a draft with a TITLE and TAGS."
78 (interactive "sArticle title: \nsTags: ")
79 (apply #'pelican-mode-insert-header
80 `(:title ,title ,@pelican-mode-default-article-fields :tags ,tags)))
81
82 (defun pelican-mode-insert-page-header (title &optional hidden)
83 "Insert a Pelican header for a page with a TITLE, potentially HIDDEN."
84 (interactive
85 (list (read-string "Page title: ")
86 (y-or-n-p "Hidden? ")))
87 (apply #'pelican-mode-insert-header
88 `(:title ,title ,@pelican-mode-default-page-fields
89 :hidden ,(when hidden "hidden"))))
90
91 (defun pelican-mode-insert-auto-header ()
92 "Insert a Pelican header for a page or article."
93 (interactive)
94 (call-interactively
95 (if (pelican-mode-page-p)
96 #'pelican-mode-insert-page-header
97 #'pelican-mode-insert-draft-article-header)))
98
99 (defun pelican-mode-set-field-rst-mode (field value)
100 "Set reStructuredText metadata FIELD to VALUE."
101 (setq field (downcase field))
102 (if (equal field "title")
103 (let ((header (format "%s\n%s\n\n"
104 value (make-string (string-width value) ?#))))
105 (if (looking-at ".*\n#+\n+")
106 (replace-match header)
107 (insert header)))
108 (let ((text (when value (format ":%s: %s\n" field value))))
109 (when (re-search-forward "^#" nil t)
110 (forward-line 2))
111 (if (re-search-forward (format "^:%s:.*\n" (regexp-quote field)) nil t)
112 (replace-match (or text ""))
113 (when text
114 (re-search-forward "^$")
115 (replace-match text))))))
116
117 (defun pelican-mode-set-field-markdown-mode (field value)
118 "Set Markdown metadata FIELD to VALUE."
119 (setq field (capitalize field))
120 (let ((text (when value (format "%s: %s\n" field value))))
121 (if (re-search-forward (format "^%s:.*\n" (regexp-quote field)) nil t)
122 (replace-match text)
123 (when value
124 (re-search-forward "^$")
125 (replace-match text)))))
126
127 (defun pelican-mode-set-field (field value)
128 "Set FIELD to VALUE.
129
130 FIELD may be a string or a symbol; if it is a symbol, the
131 symbol name is used (removing a leading ':' if present).
132
133 VALUE may be any value; except for the following special values,
134 the unquoted printed representation of it is used:
135
136 - `now' means the current time; see `pelican-mode-timestamp'.
137
138 - `slug' means the file's path relative to the document root sans
139 extension; see `pelican-mode-default-slug'.
140
141 - nil or an empty string removes the field."
142 (interactive "sField: \nsValue: ")
143 (setq value (pcase value
144 ('now (pelican-mode-timestamp))
145 ('slug (pelican-mode-default-slug))
146 ('"" nil)
147 (_ value)))
148 (when (symbolp field)
149 (setq field (string-remove-prefix ":" (symbol-name field))))
150 (let ((set-field
151 (assoc-default nil pelican-mode-set-field-alist #'derived-mode-p)))
152 (unless set-field
153 (error "Unsupported major mode %S" major-mode))
154 (save-excursion
155 (goto-char 0)
156 (funcall set-field field value))))
157
158 (defun pelican-mode-remove-field (field)
159 "Remove FIELD."
160 (pelican-mode-set-field field nil))
161
162 (defun pelican-mode-set-title (title)
163 "Set the title to TITLE."
164 (interactive "sTitle: ")
165 (pelican-mode-set-field :title title))
166
167 (defun pelican-mode-update-date ()
168 "Update a Pelican date header."
169 (interactive)
170 (pelican-mode-set-field :date 'now))
171
172 (defun pelican-mode-publish-draft ()
173 "Remove draft status from a Pelican article."
174 (interactive)
175 (pelican-mode-remove-field :status)
176 (pelican-mode-update-date))
177
178 (defun pelican-mode-page-p ()
179 "Return non-nil the current buffer is a Pelican page."
180 (when-let (pelican-mode-base (pelican-mode-find-root))
181 (let* ((relative (file-relative-name buffer-file-name pelican-mode-base))
182 (components (split-string relative "/")))
183 (equal "pages" (cadr components)))))
184
185 (defun pelican-mode-default-slug ()
186 "Generate a Pelican article/page slug for the current buffer."
187 (if-let ((pelican-mode-base (pelican-mode-find-root))
188 (file-name (file-name-sans-extension buffer-file-name)))
189 (let* ((relative (file-relative-name file-name pelican-mode-base))
190 (components (cdr (split-string relative "/")))
191 (components (if (string= "pages" (car components))
192 (cdr components) components)))
193 (mapconcat 'identity components "/"))
194 (format "%s/%s"
195 (file-name-nondirectory
196 (directory-file-name
197 (file-name-directory file-name)))
198 (file-name-base file-name))))
199
200 (defun pelican-mode-find-in-parents (file-name)
201 "Find FILE-NAME in the default directory or one of its parents, or nil."
202 (let* ((parent (expand-file-name default-directory)))
203 (while (and (not (file-readable-p (concat parent file-name)))
204 (not (string= parent (directory-file-name parent))))
205 (setq parent (file-name-directory (directory-file-name parent))))
206 (let ((found (concat parent file-name)))
207 (if (file-readable-p found) found nil))))
208
209 (defun pelican-mode-find-root ()
210 "Return the root of the buffer's Pelican site, or nil."
211 (when-let (conf (pelican-mode-find-in-parents "pelicanconf.py"))
212 (file-name-directory conf)))
213
214 (defun pelican-make (target)
215 "Execute TARGET in a Makefile at the root of the site."
216 (interactive "sMake Pelican target: ")
217 (if-let (default-directory (pelican-mode-find-root))
218 (compilation-start (format "make %s" target)
219 nil (lambda (_) "*pelican*"))
220 (user-error "This doesn't look like a Pelican site")))
221
222 (defun pelican-make-html ()
223 "Generate HTML via a Makefile at the root of the site."
224 (interactive)
225 (pelican-make "html"))
226
227 (defun pelican-make-rsync-upload ()
228 "Upload with rsync via a Makefile at the root of the site."
229 (interactive)
230 (pelican-make "rsync_upload"))
231
232 ;;;###autoload
233 (define-minor-mode pelican-mode
234 "Toggle Pelican mode.
235 With a prefix argument ARG, enable Pelican mode if ARG is
236 positive, and disable it otherwise. If called from Lisp, enable
237 the mode if ARG is omitted or nil.
238
239 When Pelican mode is enabled, additional commands are available
240 for editing articles or pages:
241
242 \\{pelican-mode-map}"
243 :lighter " Pelican"
244 :keymap `((,(kbd "C-c P n") . pelican-mode-insert-auto-header)
245 (,(kbd "C-c P p") . pelican-mode-publish-draft)
246 (,(kbd "C-c P t") . pelican-mode-update-date)
247 (,(kbd "C-c P h") . pelican-make-html)
248 (,(kbd "C-c P u") . pelican-make-rsync-upload)))
249
250 ;;;###autoload
251 (define-minor-mode pelican-global-mode
252 "Toggle Pelican global mode.
253 With a prefix argument ARG, enable Pelican global mode if ARG is
254 positive, and disable it otherwise. If called from Lisp, enable
255 the mode if ARG is omitted or nil.
256
257 When Pelican global mode is enabled, text files which seem to
258 be part of a Pelican site will have `pelican-mode' automatically
259 enabled.
260
261 If you disable this, you may still enable `pelican-mode' manually
262 or add `pelican-mode-enable-if-site' to more specific mode
263 hooks."
264 :global t
265 :group 'pelican
266 (if pelican-global-mode
267 (add-hook 'text-mode-hook #'pelican-mode-enable-if-site)
268 (remove-hook 'text-mode-hook #'pelican-mode-enable-if-site)))
269
270 ;;;###autoload
271 (defun pelican-mode-enable-if-site ()
272 "Enable `pelican-mode' if this buffer is part of a Pelican site."
273 (when (pelican-mode-find-root)
274 (pelican-mode 1)))
275
276 (provide 'pelican-mode)
277 ;;; pelican-mode.el ends here
278
279 ;; Local Variables:
280 ;; sentence-end-double-space: t
281 ;; End: