-
Notifications
You must be signed in to change notification settings - Fork 1
/
org-hyde.el
288 lines (269 loc) · 12.2 KB
/
org-hyde.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
;;; org-hyde.el --- Export hyde-ready posts from org-mode entries
;;;
;;; Author: Puneeth Chaganti <punchagan+org-hyde@gmail.com>
;;;
;; This file 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/>.
;;;
;;; org-hyde is a port of org-jekyll --
;;;
;;; Author: Juan Reyero
;;; Version: 0.3
;;; Home page: http://juanreyero.com/open/org-jekyll/
;;; Repository: http://github.com/juanre/org-jekyll
;;; Public clone: git://github.com/juanre/org-jekyll.git
;;;
;;;
;;; Summary
;;; -------
;;;
;;; Extract subtrees from your org-publish project files that have
;;; a :blog: keyword and an :on: property with a timestamp, and
;;; export them to a subdirectory _posts of your project's publishing
;;; directory in the year-month-day-title.html format that Hyde
;;; expects. Properties are passed over as yaml front-matter in the
;;; exported files. The title of the subtree is the title of the
;;; entry. The title of the post is a link to the post's page.
(defvar org-hyde-category nil
"Specify a property which, if defined in the entry, is used as
a category: the post is written to category/_posts. Ignored if
nil. Use \"lang\" if you want to send posts in different
languages to different directories.")
(defvar org-hyde-localize-dir nil
"If non-nil and the lang property is set in the entry,
org-hyde will look for a lang.yml file in this directory and
include it in the front matter of the exported entry.")
(defvar org-hyde-new-buffers nil
"Buffers created to visit org-publish project files looking for blog posts.")
(defun org-hyde-publish-dir (project &optional category)
"Where does the project go, by default a :blog-publishing-directory
entry in the org-publish-project-alist."
(let ((pdir (plist-get (cdr project) :blog-publishing-directory)))
(unless pdir
(setq pdir (plist-get (cdr project) :publishing-directory)))
(concat pdir
(if category (concat category "/") "")
"content/")))
(defun org-hyde-site-root (project)
"Site root, like http://yoursite.com, from which blog
permalinks follow. Needed to replace entry titles with
permalinks that RSS agregators and google buzz know how to
follow. Looks for a :site-root entry in the org-publish-project-alist."
(or (plist-get (cdr project) :site-root)
""))
(defun org-get-hyde-file-buffer (file)
"Get a buffer visiting FILE. If the buffer needs to be
created, add it to the list of buffers which might be released
later. Copied from org-get-agenda-file-buffer, and modified
the list that holds buffers to release."
(let ((buf (org-find-base-buffer-visiting file)))
(if buf
buf
(progn (setq buf (find-file-noselect file))
(if buf (push buf org-hyde-new-buffers))
buf))))
(defun org-hyde-slurp-yaml (fname)
(remove "---" (if (file-exists-p fname)
(split-string (with-temp-buffer
(insert-file-contents fname)
(buffer-string))
"\n" t))))
(defun ensure-directories-exist (fname)
(let ((dir (file-name-directory fname)))
(unless (file-accessible-directory-p dir)
(make-directory dir t)))
fname)
(defun org-hyde-sanitize-string (str project)
(if (plist-get (cdr project) :hyde-sanitize-permalinks)
(progn (setq str (downcase str))
(dolist (c '(("á" . "a")
("é" . "e")
("í" . "i")
("ó" . "o")
("ú" . "u")
("à" . "a")
("è" . "e")
("ì" . "i")
("ò" . "o")
("ù" . "u")
("ñ" . "n")
("ç" . "s")
("\\$" . "S")
("€" . "E")))
(setq str (replace-regexp-in-string (car c) (cdr c) str)))
(setq str (replace-regexp-in-string
"\\(^-\\|-$\\)" ""
(replace-regexp-in-string
"-+" "-"
(replace-regexp-in-string
"[^a-z0-9-]" ""
(replace-regexp-in-string " +" "-" str))))))
str))
(defun org-hyde-convert-pre (html)
"Replace pre blocks with syntax blocks for pygments."
(save-excursion
(let (pos info params src-re code-re)
(with-temp-buffer
(insert html)
(goto-char (point-min))
(save-match-data
(while (re-search-forward
"<pre\\(.*?\\)>\\(\\(.\\|[[:space:]]\\|\\\n\\)*?\\)</pre.*?>"
nil t 1)
(setq code (match-string-no-properties 2))
(if (save-match-data
(string-match "example" (match-string-no-properties 1)))
(setq lang "html")
(setq lang (substring
(match-string-no-properties 1) 16 -1))
;; handling emacs-lisp separately. pygments raises error when language
;; is unknown. list of languages variable should be added?
(if (string= "emacs-lisp" lang)
(setq lang "common-lisp")))
(save-match-data
(setq code (replace-regexp-in-string "<.*?>" "" code))
(while (string-match ">" code)
(setq code (replace-match ">" t t code)))
(while (string-match "<" code)
(setq code (replace-match "<" t t code)))
(while (string-match "&" code)
(setq code (replace-match "&" t t code))))
(replace-match
(format "\n{%% syntax %s %%}\n%s\n{%% endsyntax %%}" lang code)
nil t)))
(setq html (buffer-substring-no-properties (point-min) (point-max))))))
html)
(defun org-hyde-export-entry (project)
(let* ((props (org-entry-properties nil 'standard))
(time (or (org-entry-get (point) "POST_DATE")
(org-entry-get (point) "SCHEDULED")
(org-entry-get (point) "DEADLINE")
(org-entry-get (point) "TIMESTAMP_IA")))
(lang (cdr (or (assoc "lang" props)
(assoc "LANG" props))))
(category (if org-hyde-category
(cdr (assoc org-hyde-category props))
nil)))
(when time
(let* ((heading (org-get-heading t))
;; Get the tags from the headline
(tags (concat "["
(mapconcat
(lambda (tag) tag)
(sort
(mapcar
'downcase
(delete "noexport"
(delete "blog"
(org-get-tags-at
(point) nil))))
'string<)
", ")
"]" ))
(title (replace-regexp-in-string "[:=\(\)\?/.&'!#\"]" ""
(replace-regexp-in-string
"[ \t/]" "-" heading)))
;; Save the time used as POST_DATE. SCHEDULED etc may change.
(str-time
(format-time-string "%Y-%m-%d %T"
(if time
(apply 'encode-time
(org-parse-time-string time))
(current-time)
(org-entry-put (point)
"POST_DATE" cur-time))))
(to-file (format "%s.html"
(org-hyde-sanitize-string title project)))
(org-buffer (current-buffer))
(yaml-front-matter (cons (cons "title"
(if (string-match "[#:]" heading)
(concat "'" heading "'")
heading)) nil))
(yaml-front-matter (cons (cons "categories" tags)
yaml-front-matter))
(yaml-front-matter (cons (cons "created" str-time)
yaml-front-matter))
html)
(org-narrow-to-subtree)
(let ((level (- (org-reduced-level (org-outline-level)) 1))
(contents (buffer-substring (point-min) (point-max)))
(site-root (org-hyde-site-root project)))
;; Without the promotion the header with which the headline
;; is exported depends on the level. With the promotion it
;; fails when the entry is not visible (ie, within a folded
;; entry).
(dotimes (n level nil) (org-promote-subtree))
(setq html
(replace-regexp-in-string
"<h2 id=\"sec-1\">\\(.+\\)</h2>"
(concat "<h2 id=\"sec-1\"><a href=\"" site-root
"{{ page.url }}\">\\1</a></h2>")
(org-export-region-as-html
(1+ (and (org-back-to-heading) (line-end-position)))
(org-end-of-subtree)
t 'string)))
(set-buffer org-buffer)
(setq html (org-hyde-convert-pre html))
(delete-region (point-min) (point-max))
(insert contents)
(save-buffer))
(widen)
(with-temp-file (ensure-directories-exist
(expand-file-name
to-file (org-hyde-publish-dir project category)))
(when yaml-front-matter
(insert "{% extends \"_post.html\" %}\n")
(insert "{%hyde \n")
(mapc (lambda (pair)
(insert (format " %s: %s\n" (car pair) (cdr pair))))
yaml-front-matter)
(if (and org-hyde-localize-dir lang)
(mapc (lambda (line)
(insert (format "%s\n" line)))
(org-hyde-slurp-yaml (concat org-hyde-localize-dir
lang ".yml"))))
(insert "%}\n\n"))
(insert "{% block article %}\n{%article%}\n")
(insert html)
(insert "{%endarticle%}\n{% endblock %}\n\n"))))))
; Evtl. needed to keep compiler happy:
(declare-function org-publish-get-project-from-filename "org-publish"
(filename &optional up))
(defun org-hyde-export-current-entry ()
(interactive)
(save-excursion
(let ((project (org-publish-get-project-from-filename buffer-file-name)))
(org-back-to-heading t)
(org-hyde-export-entry project))))
(defun org-hyde-export-blog (&optional filename)
"Export all entries in project files that have a :blog: keyword
and an :on: datestamp. Property drawers are exported as
front-matters, outline entry title is the exported document
title. "
(interactive)
(save-excursion
(setq org-hyde-new-buffers nil)
(let ((project (org-publish-get-project-from-filename
(if filename
filename
(buffer-file-name)))))
(mapc
(lambda (jfile)
(if (string= (file-name-extension jfile) "org")
(with-current-buffer (org-get-hyde-file-buffer jfile)
;; It fails for non-visible entries, CONTENT visibility
;; mode ensures that all of them are visible.
(org-content)
(org-map-entries (lambda () (org-hyde-export-entry project))
"blog|BLOG"))))
(org-publish-get-base-files project)))
(org-release-buffers org-hyde-new-buffers)))
(provide 'org-hyde)