-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathox-blog.el
343 lines (277 loc) · 10.5 KB
/
ox-blog.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
336
337
338
339
340
341
342
343
;;; ox-blog -- is a minor mode, which help to export org-mode file
;;; as an Jekyll- or Hexo-formatted html.
;; Author: Zech XU
;; Created: 2014-05-27
;; Version: 1.0
;; Package-Requires: ((org "8.0"))
;; Keywords: org, blog, Jekyll, Hexo
;; This file is not part of GNU Emacs.
(require 'ox-html)
(defgroup org-export-blog nil
"Options for exporting Org mode files to blog HTML using Jekyll or Hexo."
:tag "Org Blog"
:group 'org-export
:version "24.3")
(defcustom org-blog-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_HTML
---
layout: post
title: Org-mode to GitHub pages with Jekyll/Hexo
excerpt: Introduce how to use Emacs's Org-mode to generate GitHub Pages
categories: [lessons, beginner]
tags: [Emacs, org-mode, GitHub, Jekyll]
tagline: \"Supporting tagline\"
date: 2013-09-15 22:08
comments: true
keywords: blog
description: Instructions on export org file with Jekyll or Hexo
---
#+END_HTML"
:group 'org-export-blog
:type 'boolean)
(defcustom org-blog-engine "hexo"
"Default static webpage generator to use.
This variable will be used to guide the accommodation
for different generators."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-path
(cond ((string= org-blog-engine "jekyll")
"~/Desktop/blog/rnaer.github.io/_posts")
(t
"~/Desktop/blog/hexo-blog/source/_posts"))
"Default publish dir for Blog article."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-yaml-layout "post"
"Default layout used in Blog article."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-yaml-categories "other"
"Default space-separated categories in Blog article."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-yaml-published "true"
"Default publish status in Blog article."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-yaml-comments "true"
"Default comments (disqus) flag in Blog article."
:group 'org-export-blog
:type 'string)
(defcustom org-blog-use-src-plugin nil
"If t, org-blog 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-blog-use-src-plugin
:type 'boolean)
;;; Define Back-End
(org-export-define-derived-backend 'blog 'html
:export-block '("HTML" "BLOG")
:menu-entry
'(?b "Blog: export to HTML with YAML front matter."
((?H "As HTML buffer" org-blog-export-as-html)
(?h "As HTML file" org-blog-export-to-html)))
:translate-alist
'((template . org-blog-template) ;; add YAML front matter.
(src-block . org-blog-src-block)
(inner-template . org-blog-inner-template)) ;; force body-only
:options-alist
'((:blog-engine "BLOG_ENGINE" nil org-blog-engine)
(:blog-path "BLOG_PATH" nil org-blog-path)
(:blog-layout "BLOG_LAYOUT" nil org-blog-yaml-layout)
(:blog-categories "BLOG_CATEGORIES" nil org-blog-yaml-categories)
(:blog-published "BLOG_PUBLISHED" nil org-blog-yaml-published)
(:blog-comments "BLOG_COMMENTS" nil org-blog-yaml-comments)))
;;; Internal Filters
(defun org-blog-src-block (src-block contents info)
"Transcode SRC-BLOCK element into blog code template format
if `org-blog-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-blog-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-blog-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-blog-include-yaml-front-matter
(concat
(org-blog--yaml-front-matter info)
contents)
contents))
(defun org-blog-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 :blog-published))
"<span style=\"background: red;\">PREVIEW</span>")
;; Document contents.
contents
;; Footnotes section.
(org-html-footnote-section info)))
;;; YAML Front Matter
(defun org-blog--yaml-front-matter (info)
(let ((title (plist-get info :title))
(date (or (plist-get info :date)
(format-time-string "%Y-%m-%d %a %H:%M:%S"
(current-time))))
(layout (plist-get info :blog-layout))
(categories (plist-get info :blog-categories))
(published (plist-get info :blog-published))
(comments (plist-get info :blog-comments))
tags)
(if (plist-get info :export-options)
(setq tags (plist-get info :headtags))
(setq tags (append (plist-get info :filetags))))
(concat
"---"
"\ntitle: " (org-element-interpret-data title)
"\ndate: " (org-element-interpret-data date)
"\nlayout: " layout
"\ncategories: [" categories
"]\ntags: [" (mapconcat 'identity tags ", ")
"]\npublished: " published
"\ncomments: " comments
"\n---\n")))
;;; Filename and Date Helper
(defun org-blog-date-from-filename (&optional filename)
"Get the date string from filename 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-blog-property-list (&optional filename)
"Get property list of the current buffer or in the file named FILENAME."
(let ((backend 'blog) 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-get-environment backend))
plist)))
(defun org-blog-property (keys &optional filename)
"Get the property by the KEYS in the file named FILENAME.
Example: (org-blog-property '(:blog-layout) \"index.org\")"
(let ((plist (org-blog-property-list filename)))
(mapcar (lambda (key)
(org-export-data-with-backend (plist-get plist key) 'blog plist))
keys)))
(defun org-blog-date-from-property (&optional filename)
(let ((plist (org-blog-property-list filename)))
(org-read-date
nil nil
(org-export-data-with-backend (plist-get plist :date) 'blog plist))))
(defun org-blog-create-filename ()
"Return the string of current buffer file name by adding or
replacing the heading date."
(let ((date (org-blog-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-blog-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)
(org-export-to-buffer 'blog "*Org Blog HTML Export*"
async subtreep visible-only body-only ext-plist
(lambda () (set-auto-mode t))))
;;;###autoload
(defun org-blog-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)
file props heading time headtags)
(if subtreep
(progn (setq heading (org-get-heading 't 't))
(setq time (and (string-match org-ts-regexp-both heading)
(match-string 0 heading)))
(setq heading (replace-regexp-in-string
"[:=\(\)\? \t]" "_"
(replace-regexp-in-string
org-ts-regexp-both "" heading)))
(setq file (expand-file-name
(concat heading extension)
org-blog-path))
(setq headtags (org-get-tags-at))
(setq props (org-entry-properties))
(if ext-plist
(progn (plist-put ext-plist :headtags headtags)
(plist-put ext-plist :date time))
(setq ext-plist (plist-put ext-plist :headtags headtags))))
(setq file (org-export-output-file-name extension subtreep org-blog-path)))
;; add the time stamp prefix required by Jekyll to the filename
(if (string= org-blog-engine "jekyll")
(setq file (concat
(file-name-directory file)
(format-time-string "%Y-%m-%d-" (current-time))
(file-name-nondirectory file))))
(org-export-to-file 'blog file
async subtreep visible-only body-only ext-plist)))
;;;###autoload
(defun org-blog-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 'blog filename ".html" plist pub-dir))
;;;###autoload
(defun org-blog-insert-export-options-template
(&optional title date setupfile categories tags published layout)
"Insert a settings template for Blog exporter."
(interactive)
(let ((layout (or layout org-blog-yaml-layout))
(published (or published org-blog-yaml-published))
(categories (or categories org-blog-yaml-categories)))
(save-excursion
(insert (concat
"#+TITLE: " title
"\n#+DATE: " date
"\n#+SETUPFILE: " setupfile
"\n#+BLOG_LAYOUT: " layout
"\n#+BLOG_CATEGORIES: " categories
"\n#+BLOG_PUBLISHED: " published
"\n\n* \n")))))
;;;###autoload
(defun org-blog-export-to-blog ()
(interactive)
(let ((properties (org-blog-property-list))
position)
(save-excursion
(goto-char (point-min))
(while (not (eq position (point)))
(setq position (point))
(org-forward-heading-same-level 1)
;; (message "%d" (point))
(org-blog-export-headline properties)))))
(provide 'ox-blog)
;;; ox-blog end here