-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathox-jekyll.el
335 lines (280 loc) · 11.3 KB
/
ox-jekyll.el
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
;;; ox-jekyll.el --- Export Jekyll articles using org-mode.
;; Copyright (C) 2013-2017 Yoshinari Nomura
;; Author: Yoshinari Nomura <nom@quickhack.net>
;; Author: Justin Gordon <justin.gordon@gmail.com>
;; Keywords: org, jekyll
;; Version: 0.1
;; This 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, or (at your option)
;; any later version.
;; This file 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 a Jekyll-style html backend for
;; Org exporter, based on `html' back-end.
;;
;; It provides two commands for export, depending on the desired
;; output: `org-jkl-export-as-html' (temporary buffer) and
;; `org-jkl-export-to-html' ("html" file with YAML front matter).
;;
;; For publishing, `org-jekyll-publish-to-html' is available.
;; For composing, `org-jekyll-insert-export-options-template' is available.
;;; Code:
;;; Dependencies
(require 'ox-html)
;;; User Configurable Variables
(defgroup org-export-jekyll nil
"Options for exporting Org mode files to jekyll HTML."
:tag "Org Jekyll"
:group 'org-export
:version "24.2")
(defcustom org-jekyll-include-yaml-front-matter t
"If true, then include yaml-front-matter when exporting to html.
If false, then you should include the yaml front matter like this at the top of the file:
#+BEGIN_EXPORT HTML
---
layout: post
title: \"Upgrading Octopress\"
date: 2013-09-15 22:08
comments: true
categories: [octopress, rubymine]
tags: tech news
keywords: Octopress
description: Instructions on Upgrading Octopress
---
#+END_EXPORT HTML"
:group 'org-export-jekyll
:type 'boolean)
(defcustom org-jekyll-layout "post"
"Default layout used in Jekyll article."
:group 'org-export-jekyll
:type 'string)
(defcustom org-jekyll-categories ""
"Default space-separated categories in Jekyll article."
:group 'org-export-jekyll
:type 'string)
(defcustom org-jekyll-tags ""
"Default space-separated tags in Jekyll article."
:group 'org-export-jekyll
:type 'string)
(defcustom org-jekyll-published "true"
"Default publish status in Jekyll article."
:group 'org-export-jekyll
:type 'string)
(defcustom org-jekyll-comments ""
"Default comments (disqus) flag in Jekyll article."
:group 'org-export-jekyll
:type 'string)
(defcustom org-jekyll-use-src-plugin nil
"If t, org-jekyll exporter eagerly uses plugins instead of
org-mode's original HTML stuff. For example:
#+BEGIN_SRC ruby
puts \"Hello world\"
#+END_SRC
makes:
{% codeblock ruby %}
puts \"Hello world\"
{% endcodeblock %}"
:group 'org-export-jekyll-use-src-plugin
:type 'boolean)
;;; Define Back-End
(org-export-define-derived-backend 'jekyll 'html
:menu-entry
'(?j "Jekyll: export to HTML with YAML front matter."
((?H "As HTML buffer" org-jekyll-export-as-html)
(?h "As HTML file" org-jekyll-export-to-html)))
:translate-alist
'((template . org-jekyll-template) ;; add YAML front matter.
(src-block . org-jekyll-src-block)
(inner-template . org-jekyll-inner-template)) ;; force body-only
:options-alist
'((:jekyll-layout "JEKYLL_LAYOUT" nil org-jekyll-layout)
(:jekyll-categories "JEKYLL_CATEGORIES" nil org-jekyll-categories)
(:jekyll-tags "JEKYLL_TAGS" nil org-jekyll-tags)
(:jekyll-published "JEKYLL_PUBLISHED" nil org-jekyll-published)
(:jekyll-comments "JEKYLL_COMMENTS" nil org-jekyll-comments)))
;;; Internal Filters
(defun org-jekyll-src-block (src-block contents info)
"Transcode SRC-BLOCK element into jekyll code template format
if `org-jekyll-use-src-plugin` is t. Otherwise, perform as
`org-html-src-block`. CONTENTS holds the contents of the item.
INFO is a plist used as a communication channel."
(if org-jekyll-use-src-plugin
(let ((language (org-element-property :language src-block))
(value (org-remove-indentation
(org-element-property :value src-block))))
(format "{%% codeblock lang:%s %%}\n%s{%% endcodeblock %%}"
language value))
(org-export-with-backend 'html src-block contents info)))
;;; Template
(defun org-jekyll-template (contents info)
"Return complete document string after HTML conversion.
CONTENTS is the transcoded contents string. INFO is a plist
holding export options."
(if org-jekyll-include-yaml-front-matter
(concat
(org-jekyll--yaml-front-matter info)
contents)
contents
))
(defun org-jekyll-inner-template (contents info)
"Return body of document string after HTML conversion.
CONTENTS is the transcoded contents string. INFO is a plist
holding export options."
(concat
;; Table of contents.
(let ((depth (plist-get info :with-toc)))
(when depth (org-html-toc depth info)))
;; PREVIEW mark on the top of article.
(unless (equal "true" (plist-get info :jekyll-published))
"<span style=\"background: red;\">PREVIEW</span>")
;; Document contents.
contents
;; Footnotes section.
(org-html-footnote-section info)))
;;; YAML Front Matter
(defun org-jekyll--get-option (info property-name &optional default)
(let ((property (org-export-data (plist-get info property-name) info)))
(format "%s" (or property default ""))))
(defun org-jekyll--yaml-front-matter (info)
(let ((title
(org-jekyll--get-option info :title))
(date
(org-jekyll--get-option info :date))
(layout
(org-jekyll--get-option info :jekyll-layout org-jekyll-layout))
(categories
(org-jekyll--get-option info :jekyll-categories org-jekyll-categories))
(tags
(org-jekyll--get-option info :jekyll-tags org-jekyll-tags))
(published
(org-jekyll--get-option info :jekyll-published org-jekyll-published))
(comments
(org-jekyll--get-option info :jekyll-comments))
(convert-to-yaml-list
(lambda (arg)
(mapconcat #'(lambda (text)(concat "\n- " text)) (split-string arg) " "))))
(unless (equal published "true")
(setq title (concat "[PREVIEW] " title)))
(concat
"---"
"\ntitle: \"" title
"\"\ndate: " date
"\nlayout: " layout
"\ncategories: " (funcall convert-to-yaml-list categories)
"\ntags: " (funcall convert-to-yaml-list tags)
"\npublished: " published
"\ncomments: " comments
"\n---\n")))
;;; Filename and Date Helper
(defun org-jekyll-date-from-filename (&optional filename)
(let ((fn (file-name-nondirectory (or filename (buffer-file-name)))))
(if (string-match "^[0-9]+-[0-9]+-[0-9]+" fn)
(match-string 0 fn)
nil)))
(defun org-jekyll-property-list (&optional filename)
(let ((backend 'jekyll) plist)
(if filename
(with-temp-buffer
(insert-file-contents filename)
(org-mode)
(setq plist (org-export-get-environment backend))
(setq plist (plist-put plist :input-file filename)))
(setq plist (org-export-backend-options backend))
plist)))
(defun org-jekyll-property (keys &optional filename)
(let ((plist (org-jekyll-property-list filename)))
(mapcar (lambda (key)
(let ((value (plist-get plist key)))
(setq value (if (listp value) (car value) value))
(if (stringp value)
(substring-no-properties value))))
keys)))
(defun org-jekyll-date-from-property (&optional filename)
(let ((plist (org-jekyll-property filename)))
(org-read-date
nil nil
(org-export-data-with-backend (plist-get plist :date) 'jekyll plist))))
(defun org-jekyll-create-filename ()
(let ((date (org-jekyll-date-from-property))
(file (file-name-nondirectory (buffer-file-name)))
(dir (file-name-directory (buffer-file-name))))
(expand-file-name
(replace-regexp-in-string "^[0-9]+-[0-9]+-[0-9]+" date file)
dir)))
;;; End-User functions
;;;###autoload
(defun org-jekyll-export-as-html
(&optional async subtreep visible-only body-only ext-plist)
"Export current buffer to a HTML buffer adding some YAML front matter."
(interactive)
(if async
(org-export-async-start
(lambda (output)
(with-current-buffer (get-buffer-create "*Org Jekyll HTML Export*")
(erase-buffer)
(insert output)
(goto-char (point-min))
(funcall org-html-display-buffer-mode)
(org-export-add-to-stack (current-buffer) 'jekyll)))
`(org-export-as 'jekyll ,subtreep ,visible-only ,body-only ',ext-plist))
(let ((outbuf (org-export-to-buffer
'jekyll "*Org Jekyll HTML Export*"
nil subtreep visible-only body-only ext-plist)))
;; Set major mode.
(with-current-buffer outbuf (set-auto-mode t))
(when org-export-show-temporary-export-buffer
(switch-to-buffer-other-window outbuf)))))
;;;###autoload
(defun org-jekyll-export-to-html
(&optional async subtreep visible-only body-only ext-plist)
"Export current buffer to a HTML file adding some YAML front matter."
(interactive)
(let* ((extension (concat "." org-html-extension))
(file (org-export-output-file-name extension subtreep))
(org-export-coding-system org-html-coding-system))
(if async
(org-export-async-start
(lambda (f) (org-export-add-to-stack f 'jekyll))
(let ((org-export-coding-system org-html-coding-system))
`(expand-file-name
(org-export-to-file
'jekyll ,file nil ,subtreep ,visible-only ,body-only ',ext-plist))))
(let ((org-export-coding-system org-html-coding-system))
(org-export-to-file
'jekyll file nil subtreep visible-only body-only ext-plist)))))
;;;###autoload
(defun org-jekyll-publish-to-html (plist filename pub-dir)
"Publish an org file to HTML with YAML front matter.
FILENAME is the filename of the Org file to be published. PLIST
is the property list for the given project. PUB-DIR is the
publishing directory.
Return output file name."
(org-publish-org-to 'jekyll filename ".html" plist pub-dir))
;;;###autoload
(defun org-jekyll-insert-export-options-template
(&optional title date setupfile categories tags published layout)
"Insert a settings template for Jekyll exporter."
(interactive)
(let ((layout (or layout org-jekyll-layout))
(published (or published org-jekyll-published))
(tags (or tags org-jekyll-tags))
(categories (or categories org-jekyll-categories)))
(save-excursion
(insert (format (concat
"#+TITLE: " title
"\n#+DATE: " date
"\n#+SETUPFILE: " setupfile
"\n#+JEKYLL_LAYOUT: " layout
"\n#+JEKYLL_CATEGORIES: " categories
"\n#+JEKYLL_TAGS: " tags
"\n#+JEKYLL_PUBLISHED: " published
"\n\n* \n\n{{{more}}}"))))))
;;; provide
(provide 'ox-jekyll)
;;; ox-jekyll.el ends here