~jakob/ox-haunt

ox-haunt/ox-haunt.el -rw-r--r-- 10.9 KiB
d32c4b1aJakob L. Kreuze Lint package a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
;;; ox-haunt.el --- Haunt-flavored HTML backend for the Org export engine -*- lexical-binding: t; -*-

;; Copyright (C) 2019 Jakob L. Kreuze

;; Author: Jakob L. Kreuze <zerodaysfordays@sdf.lonestar.org>
;; Version: 0.1
;; Package-Requires: ((emacs "26.1"))
;; Keywords: convenience hypermedia wp
;; URL: https://git.sr.ht/~jakob/ox-haunt

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This library implements an HTML back-end for the Org generic
;; exporter, producing output appropriate for Haunt's `html-reader'.

;;; Code:

(require 'ox)
(require 'ox-html)

(org-export-define-derived-backend 'haunt 'html
  :menu-entry
  '(?s "Export to Haunt"
       ((?H "As Haunt buffer" ox-haunt-export-as-html)
        (?h "As Haunt file" ox-haunt-export-to-html)
        (?o "As Haunt file and open"
            (lambda (a s v b)
              (if a (ox-haunt-export-to-html t s v b)
                (org-open-file (ox-haunt-export-to-html nil s v b)))))
        (?A "All subtrees as Haunt file" ox-haunt-export-all-subtrees-to-html)))
  :translate-alist
  '((link . ox-haunt-link)
    (template . ox-haunt-template))
  :options-alist
  '((:tags "TAGS" nil nil)
    (:haunt-metadata "HAUNT_METADATA" nil nil)
    (:haunt-base-dir "HAUNT_BASE_DIR" nil ox-haunt-base-dir)
    (:haunt-images-dir "HAUNT_IMAGES_DIR" nil ox-haunt-images-dir)))

(defgroup org-export-haunt nil
  "Options for exporting Org mode files to Haunt HTML."
  :tag "Org Export Haunt"
  :group 'org-export)

(defcustom ox-haunt-base-dir nil
  "The default path to write output files to.
This can be specified on a per-file basis with the 'HAUNT_BASE_DIR' keyword."
  :type 'string)

;; Notably, this excludes :haunt-metadata, which we handle separately.
(defcustom ox-haunt-recognized-metadata '(:title :date :tags)
  "A list of keywords to include in the Haunt metadata section."
  :type '(list symbol))

(defcustom ox-haunt-images-dir "/images/"
  "The default path to copy images to.
This can be specified on a per-file basis with the 'HAUNT_IMAGES_DIR' keyword."
  :type 'string)

(defun ox-haunt--check-base-dir (dest-path)
  "Raise an error if DEST-PATH does not name a valid directory."
  (unless dest-path
    (user-error "It is mandatory to set the HAUNT_BASE_DIR property"))
  (unless (file-directory-p dest-path)
    (user-error "The HAUNT_BASE_DIR property must name a directory")))

(defun ox-haunt-link (link desc info)
  "Transcode a LINK object from Org to HTML.
DESC is the description part of the link, or the empty string.
INFO is the current state of the export process, as a plist."
  (let* ((orig-path (org-element-property :path link))
         (filename (file-name-nondirectory orig-path))
         (dest-path (plist-get info :haunt-base-dir))
         (images-path (plist-get info :haunt-images-dir)))
    (when (string= "file" (org-element-property :type link))
      (ox-haunt--check-base-dir dest-path)
      (copy-file orig-path (concat dest-path images-path filename) t)
      (org-element-put-property link :path (concat ".." images-path filename)))
    (org-html-link link desc info)))

(defun ox-haunt--keyword-as-string (info keyword)
  "Obtain the value of KEYWORD as a plaintext string.
INFO is the current state of the export process, as a plist."
  (org-export-data-with-backend (plist-get info keyword) 'ascii info))

(defun ox-haunt--get-valid-subtree ()
  "Return the org element for a valid Haunt post subtree.
The condition to check validity is that the EXPORT_FILE_NAME
property is defined for the subtree element."
  (catch 'break
    (let ((level))
      (while :infinite
	(let ((entry (org-element-at-point))
	      (file-name (org-entry-get (point) "EXPORT_FILE_NAME")))
	  (when file-name
	    (throw 'break entry)))
	(setq level (org-up-heading-safe))
	(unless level
	  (throw 'break nil))))))

(defun ox-haunt--format-subtree-tags (tags)
  "Given a string of tags in ':tag:...:' form return them
formatted as 'tag, ..., tag'."
  (when tags
    (string-join (split-string tags ":" t) ", ")))

(defun ox-haunt--format-subtree-date (date)
  "Given a string with an inactive org-mode timestamp return it
formatted as expected by haunt."
  (when date
    (let ((date-list (split-string (string-trim date "\\[" "\\]"))))
      (concat (elt date-list 0) " "
	      (elt date-list 2)))))

(defun ox-haunt--get-valid-subtree-metadata ()
  "With point on a subtree return the metadata of it or it's
first valid parent as a plist."
  (when (ox-haunt--get-valid-subtree)
   (let ((title (org-entry-get (point) "ITEM"))
	 (tags (ox-haunt--format-subtree-tags
		(org-entry-get (point) "TAGS")))
	 (date (ox-haunt--format-subtree-date
		(or (org-entry-get (point) "CLOSED")
		    (org-entry-get (point) "SCHEDULED"))))
	 (todo-state (org-entry-get (point) "TODO")))
     `(:title ,title :tags ,tags :date ,date :todo-state ,todo-state))))

(defun ox-haunt-template (contents info)
  "Return complete document string after HTML conversion.
CONTENTS is the Org file's contents rendered as HTML.
INFO is the current state of the export process, as a plist."
  (concat
   ;; Output the Haunt metadata section.
   (with-temp-buffer
     (dolist (keyword ox-haunt-recognized-metadata)
       (when (plist-get info keyword)
         (insert (format "%s: %s\n"
                         (substring (symbol-name keyword) 1)
                         (ox-haunt--keyword-as-string info keyword)))))
     ;; Handle :haunt-metadata, which we deliberately exclude from
     ;; `ox-haunt-recognized-metadata'.
     (when (plist-get info :haunt-metadata)
       (dolist (p (read (ox-haunt--keyword-as-string info :haunt-metadata)))
         (insert (format "%s: %s\n" (car p) (cdr p)))))
     (buffer-string))
   "---\n"
   ;; Output the article contents.
   contents))

;;;###autoload
(defun ox-haunt-export-as-html
    (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to a Haunt post buffer.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting buffer should be accessible
through the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write code
between \"<body>\" and \"</body>\" tags.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Export is done in a buffer named \"*Org Haunt Export*\", which
will be displayed when `org-export-show-temporary-export-buffer'
is non-nil."
  (interactive)
  (let* ((info (org-combine-plists
                (org-export--get-export-attributes
                 'haunt subtreep visible-only)
                (org-export--get-buffer-attributes)
                (org-export-get-environment 'haunt subtreep)))
         (dest-path (plist-get info :haunt-base-dir))
	     (subtree-metadata (when subtreep
			                 (ox-haunt--get-valid-subtree-metadata))))
    (cond
     ((and subtreep (not subtree-metadata))
      (message "This is not a valid subtree to export."))
     ((and subtreep (not (string-equal
			              (plist-get subtree-metadata :todo-state)
			              "DONE")))
      (message "The post is not marked as DONE, so it won't export"))
     (t (org-export-to-buffer 'haunt "*Org Haunt Export*"
	      async subtreep visible-only body-only
          ;; Necessary to propagate a buffer-local value for `ox-haunt-base-dir'.
	      (append `(:haunt-base-dir ,dest-path) subtree-metadata ext-plist)
	      (lambda () (set-auto-mode t)))))))

;;;###autoload
(defun ox-haunt-export-to-html
    (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to a Haunt post file.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write code
between \"<body>\" and \"</body>\" tags.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Return output file's name."
  (interactive)
  (let* ((info (org-combine-plists
                (org-export--get-export-attributes
                 'haunt subtreep visible-only)
                (org-export--get-buffer-attributes)
                (org-export-get-environment 'haunt subtreep)))
         (dest-path (plist-get info :haunt-base-dir))
	     (images-path (plist-get info :haunt-images-dir))
	     (subtree-metadata (when subtreep
			                 (ox-haunt--get-valid-subtree-metadata))))
    (ox-haunt--check-base-dir dest-path)
    (cond
     ((and subtreep (not subtree-metadata))
      (message "This is not a valid subtree to export."))
     ((and subtreep (not (string-equal
			              (plist-get subtree-metadata :todo-state)
			              "DONE")))
      (message "The post is not marked as DONE, so it won't export"))
     (t (let* ((extension (concat "." (or (plist-get ext-plist :html-extension)
					                      org-html-extension
					                      "html")))
               (file (org-export-output-file-name extension subtreep))
               (file (concat dest-path "/posts/" file))
               (org-export-coding-system org-html-coding-system))
	      (org-export-to-file 'haunt file
            async subtreep visible-only body-only
            ;; Necessary to propagate a buffer-local value for `ox-haunt-base-dir'.
            (append `(:haunt-base-dir ,dest-path)
		            `(:haunt-images-dir ,images-path)
		            subtree-metadata ext-plist)))))))

;;;###autoload
(defun ox-haunt-export-all-subtrees-to-html (&optional async visible-only body-only ext-plist)
  "Export all valid subtrees in current buffer to Haunt post
  files."
  (org-map-entries
   (lambda () (ox-haunt-export-to-html async t visible-only body-only ext-plist))
   "EXPORT_FILE_NAME<>\"\""))

(provide 'ox-haunt)
;;; ox-haunt.el ends here