-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
op-template.el
358 lines (330 loc) · 15.7 KB
/
op-template.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
;;; op-template.el --- templating system based on mustache, required by org-page
;; Copyright (C) 2012, 2013, 2014 Kelvin Hu
;; Author: Kelvin Hu <ini DOT kelvin AT gmail DOT com>
;; Keywords: convenience
;; Homepage: https://github.com/kelvinh/org-page
;; 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:
;; templating system based on mustache.el, to replace `format-spec'.
;;; Code:
(require 'ox)
(require 'cl-lib)
;; (require 'mustache)
(autoload 'mustache-render "mustache")
(require 'op-util)
(require 'op-vars)
(require 'op-git)
(defun op/read-org-option (option)
"Read option value of org file opened in current buffer.
e.g:
#+TITLE: this is title
will return \"this is title\" if OPTION is \"TITLE\""
(let ((match-regexp (org-make-options-regexp `(,option))))
(save-excursion
(goto-char (point-min))
(when (re-search-forward match-regexp nil t)
(match-string-no-properties 2 nil)))))
(defun op/generate-tag-uri (tag-name)
"Generate tag uri based on TAG-NAME."
(concat "/tags/" (encode-string-to-url tag-name) "/"))
(defun op/get-file-category (org-file)
"Get org file category presented by ORG-FILE.
When ORG-FILE is nil, return all the categories.
This is the default function used to get a file's category, see
`op/retrieve-category-function'. How to judge a file's category is
based on its name and its root folder name under
`op/repository-directory'."
(cond ((not org-file)
(let ((cat-list '("index" "about" "blog"))) ;; 3 default categories
(dolist (f (directory-files op/repository-directory))
(when (and (not (equal f "."))
(not (equal f ".."))
(not (equal f ".git"))
(not (member f op/category-ignore-list))
(not (equal f "blog"))
(file-directory-p
(expand-file-name f op/repository-directory)))
(setq cat-list (cons f cat-list))))
cat-list))
((string= (expand-file-name "index.org" op/repository-directory)
(expand-file-name org-file)) "index")
((string= (expand-file-name "about.org" op/repository-directory)
(expand-file-name org-file)) "about")
((string= (file-name-directory (expand-file-name org-file))
op/repository-directory) "blog")
(t (car (split-string (file-relative-name (expand-file-name org-file)
op/repository-directory)
"[/\\\\]+")))))
(defun op/get-template-dir ()
"Return the template directory.
It is determined by variable `op/theme-root-directory' with `op/theme'
or `op/template-directory'."
(or op/template-directory
(file-name-as-directory
(expand-file-name
(format "%s/templates" (symbol-name op/theme))
op/theme-root-directory))))
(defun op/get-cache-item (key)
"Get the item associated with KEY in `op/item-cache'.
If `op/item-cache' is nil or there is no item associated with KEY in it,
return nil."
(and op/item-cache
(plist-get op/item-cache key)))
(defun op/update-cache-item (key value)
"Update the item associated with KEY in `op/item-cache' to VALUE.
If `op/item-cache' is nil, initialize it."
(if op/item-cache
(plist-put op/item-cache key value)
(setq op/item-cache `(,key ,value)))
value)
(defmacro op/get-cache-create (key &rest body)
"Get item from `op/item-cache' with KEY.
If item is not found, evaluate BODY and push the result into cache and
return it."
`(or (op/get-cache-item ,key)
(op/update-cache-item ,key (funcall (lambda () ,@body)))))
(defun op/get-category-name (category)
"Return the name of CATEGORY.
Use :label property in `op/category-config-alist'.
Default to capitalized CATEGORY name if no :label property found."
(let* ((config (cdr (or (assoc category op/category-config-alist)
(assoc "blog" op/category-config-alist)))))
(or (plist-get config :label)
(capitalize category))))
(defun op/render-header (&optional param-table)
"Render the header on each page.
PARAM-TABLE is the hash table from mustache to render the template. If
it is not set or nil, this function will try to build a hash table
accordint to current buffer."
(mustache-render
(op/get-cache-create
:header-template
(message "Read header.mustache from file")
(file-to-string (concat (op/get-template-dir) "header.mustache")))
(or param-table
(ht ("page-title" (concat (or (op/read-org-option "TITLE") "Untitled")
" - " op/site-main-title))
("author" (or (op/read-org-option "AUTHOR")
user-full-name "Unknown Author"))
("description" (op/read-org-option "DESCRIPTION"))
("keywords" (op/read-org-option "KEYWORDS"))))))
(defun op/render-navigation-bar (&optional param-table)
"Render the navigation bar on each page.
It will be read firstly from `op/item-cache', if there is no cached
content, it will be rendered and pushed into cache from template.
PARAM-TABLE is the hash table for mustache to render the template. If
it is not set or nil, this function will try to render from a default
hash table."
(op/get-cache-create
:nav-bar-html
(message "Render navigation bar from template")
(mustache-render
(op/get-cache-create
:nav-bar-template
(message "Read nav.mustache from file")
(file-to-string (concat (op/get-template-dir) "nav.mustache")))
(or param-table
(ht-merge (ht ("site-main-title" op/site-main-title)
("site-sub-title" op/site-sub-title)
("nav-categories"
(mapcar
#'(lambda (cat)
(ht ("category-uri"
(concat "/" (encode-string-to-url cat) "/"))
("category-name" (op/get-category-name cat))))
(sort (cl-remove-if
#'(lambda (cat)
(or (string= cat "index")
(string= cat "about")))
(op/get-file-category nil))
'string-lessp)))
("github" op/personal-github-link)
("avatar" op/personal-avatar)
("site-domain" (if (string-match
"\\`https?://\\(.*[a-zA-Z]\\)/?\\'"
op/site-domain)
(match-string 1 op/site-domain)
op/site-domain)))
(if op/organization (ht ("authors-li" t)) (ht ("avatar" op/personal-avatar))))))))
(defun op/render-content (&optional template param-table)
"Render the content on each page.
TEMPLATE is the template name for rendering, if it is not set of nil,
will use default post.mustache instead. PARAM-TABLE is similar to
`op/render-header'. `op/highlight-render' is `js' or `htmlize'."
(mustache-render
(op/get-cache-create
(if template
(intern (replace-regexp-in-string "\\.mustache$" "-template" template))
:post-template)
(message (concat "Read " (or template "post.mustache") " from file"))
(file-to-string (concat (op/get-template-dir)
(or template "post.mustache"))))
(or param-table
(ht ("title" (or (op/read-org-option "TITLE") "Untitled"))
("content"
(cond ((eq op/highlight-render 'js)
(progn
(cl-letf (((symbol-function'org-html-fontify-code)
#'(lambda (code lang)
(when code
(org-html-encode-plain-text code)))))
(org-export-as op/export-backend nil nil t nil))))
((eq op/highlight-render 'htmlize)
(org-export-as op/export-backend nil nil t nil))))))))
(defun op/render-footer (&optional param-table)
"Render the footer on each page.
PARAM-TABLE is similar to `op/render-header'."
(mustache-render
(op/get-cache-create
:footer-template
(message "Read footer.mustache from file")
(file-to-string (concat (op/get-template-dir) "footer.mustache")))
(or param-table
(let* ((filename (buffer-file-name))
(title (or (op/read-org-option "TITLE") "Untitled"))
(date (fix-timestamp-string
(or (op/read-org-option "DATE")
(format-time-string "%Y-%m-%d"))))
(tags (op/read-org-option "TAGS"))
(tags (if tags
(mapcar
#'(lambda (tag-name)
(ht ("link" (op/generate-tag-uri tag-name))
("name" tag-name)))
(delete "" (mapcar 'trim-string (split-string tags "[:,]+" t))))))
(category (funcall (or op/retrieve-category-function
#'op/get-file-category)
filename))
(config (cdr (or (assoc category op/category-config-alist)
(assoc "blog" op/category-config-alist))))
(uri (funcall (plist-get config :uri-generator)
(plist-get config :uri-template) date title)))
(ht ("show-meta" (plist-get config :show-meta))
("show-comment" (plist-get config :show-comment))
("date" (funcall op/date-final-format date))
("mod-date" (funcall
op/date-final-format
(if (not filename)
(format-time-string "%Y-%m-%d")
(or (op/git-last-change-date
op/repository-directory
filename)
(format-time-string
"%Y-%m-%d"
(nth 5 (file-attributes filename)))))))
("tags" tags)
("tag-links" (if (not tags) "N/A"
(mapconcat
#'(lambda (tag)
(mustache-render
"<a href=\"{{link}}\">{{name}}</a>" tag))
tags ", ")))
("author" (or (op/read-org-option "AUTHOR")
user-full-name
"Unknown Author"))
("hashover-comment" (and (boundp 'op/hashover-comments)
op/hashover-comments))
("disqus-id" uri)
("disqus-url" (get-full-url uri))
("disqus-comment" (and (boundp 'op/personal-disqus-shortname)
op/personal-disqus-shortname))
("disqus-shortname" op/personal-disqus-shortname)
("duoshuo-comment" (and (boundp 'op/personal-duoshuo-shortname)
op/personal-duoshuo-shortname))
("duoshuo-shortname" op/personal-duoshuo-shortname)
("google-analytics" (and (boundp 'op/personal-google-analytics-id)
op/personal-google-analytics-id))
("google-analytics-id" op/personal-google-analytics-id)
("creator-info" op/html-creator-string)
("email" (confound-email (or (op/read-org-option "EMAIL")
user-mail-address
"Unknown Email"))))))))
;;; this function is deprecated
(defun op/update-default-template-parameters ()
"Update the default template parameters.
It is only needed when user did some customization to relevant
variables."
(ht-update
op/default-template-parameters
(ht ("site-main-title" op/site-main-title)
("site-sub-title" op/site-sub-title)
("github" op/personal-github-link)
("site-domain" (if (string-match "\\`https?://\\(.*[a-zA-Z]\\)/?\\'"
op/site-domain)
(match-string 1 op/site-domain)
op/site-domain))
("disqus-shortname" op/personal-disqus-shortname)
("disqus-comment" (if op/personal-disqus-shortname t nil))
("duoshuo-shortname" op/personal-duoshuo-shortname)
("duoshuo-comment" (if op/personal-duoshuo-shortname t nil))
("google-analytics-id" op/personal-google-analytics-id)
("google-analytics" (if op/personal-google-analytics-id t nil))))
op/default-template-parameters)
;;; this function is deprecated
(defun op/compose-template-parameters (attr-plist content)
"Compose parameters for org file represented in current buffer.
ATTR-PLIST is the attribute plist of the buffer, retrieved by the combination of
`org-export--get-inbuffer-options' and `op/get-inbuffer-extra-options'.
The return value is a hash table mapping attributes from ATTR-PLIST to
their values. In addition, \"content\" attribute will be set to CONTENT."
(let* ((info
(org-combine-plists
(org-export--get-global-options 'html)
attr-plist))
(title (org-element-interpret-data (plist-get info :title)))
(author (org-element-interpret-data
(or (plist-get info :author) user-full-name)))
(email (confound-email (or (plist-get info :email)
user-mail-address)))
(description (or (plist-get info :description) nil))
(keywords (or (plist-get info :keywords) nil))
(category (plist-get info :category))
(show-meta-info (and (not (eq category 'index))
(not (eq category 'about))
(not (eq category 'none))))
(creation-date (if (plist-get info :date)
(fix-timestamp-string
(org-element-interpret-data
(plist-get info :date)))
"N/A"))
(mod-date (or (plist-get info :mod-date) "N/A"))
(tag-links (mapconcat
#'(lambda (tag-name)
(mustache-render
"<a href=\"{{link}}\">{{name}}</a>"
(ht ("link" (op/generate-tag-uri tag-name))
("name" tag-name))))
(plist-get info :tags) ", "))
(show-comment (eq category 'blog))
(disqus-id (plist-get info :uri))
(disqus-url (get-full-url disqus-id))
(param-table (ht-create)))
(ht-update param-table op/default-template-parameters)
(ht-update
param-table
(ht ("page-title" (concat title " - " op/site-main-title))
("author" author)
("description" description)
("keywords" keywords)
("title" title)
("content" content)
("show-meta-info" show-meta-info)
("creation-date" creation-date)
("modification-date" mod-date)
("tags" tag-links)
("show-comment" show-comment)
("disqus-id" disqus-id)
("disqus-url" disqus-url)
("email" email)))
param-table))
(provide 'op-template)
;;; op-template.el ends here