forked from emacsorphanage/org-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op-template.el
271 lines (248 loc) · 12 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
;;; 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 'mustache)
(autoload 'mustache-render "mustache")
(require 'op-util)
(require 'op-vars)
(require 'op-git)
(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', 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)
"Firstly get item from `op/item-cache' with KEY, if item 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/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/template-directory "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/template-directory "nav.mustache")))
(or param-table
(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" (capitalize cat))))
(sort (remove-if
#'(lambda (cat)
(or (string= cat "index")
(string= cat "about")))
(op/get-file-category nil))
'string-lessp)))
("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)))))))
(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'."
(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/template-directory
(or template "post.mustache"))))
(or param-table
(ht ("title" (or (op/read-org-option "TITLE") "Untitled"))
("content" (flet ((org-html-fontify-code
(code lang)
(when code (org-html-encode-plain-text code))))
(org-export-as 'html 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/template-directory "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"))
(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" date)
("mod-date" (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))))))
("tag-links" (if (not tags) "N/A"
(mapconcat
#'(lambda (tag-name)
(mustache-render
"<a href=\"{{link}}\">{{name}}</a>"
(ht ("link" (op/generate-tag-uri tag-name))
("name" tag-name))))
(delete "" (mapcar
'trim-string
(split-string tags "[:,]+" t))) ", ")))
("author" (or (op/read-org-option "AUTHOR")
user-full-name
"Unknown Author"))
("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'."
(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