-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblog-minimal.el
287 lines (232 loc) · 10.4 KB
/
blog-minimal.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
;;; blog-minimal.el --- a simple static site generator based on org mode
;; Copyright (C) Thank Fly
;; Author: Thank Fly <thiefuniverses@gmail.com>
;; Version: 0.1
;; Package-Requires: ((ht "1.5") (simple-httpd "1.4.6") (mustache "0.22") (s "1.11.0") (org "9.0.3"))
;; Keywords: tools
;; URL: https://github.com/thiefuniverse/blog-minimal
;; This file is part of blog-minimal.
;; Blog-minimal 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.
;; Blog-minimal 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Blog-minimal is a simple static site generator based on org mode.
;; It's so simple that others can hack it quickly and then create and
;; modify their own sites.
;; you need add (require 'blog-minimal) to you .emacs file
;; then config your blog-minimal-vars, execute (blog-minimal-init)
;;; Code:
(require 'mustache)
(require 'ht)
(require 's)
(require 'simple-httpd)
(require 'blog-minimal-vars)
(require 'blog-minimal-utils)
;;; set templates dir
(defun blog-minimal-init ()
"Init directory structure for blog minimal."
(interactive)
(blog-minimal-set-package-dir)
(if (string= blog-minimal-blog-main-dir "")
(message-box "blog-minimal-blog-main-dir is nil\n Please set a dir for your blog.")
(progn
(make-directory (concat blog-minimal-blog-main-dir "org") t)
(copy-directory (concat blog-minimal-package-dir "/templates") blog-minimal-blog-main-dir)
(copy-directory (concat blog-minimal-package-dir "/media") blog-minimal-blog-main-dir)
(copy-file (concat blog-minimal-package-dir "/about.org") blog-minimal-blog-main-dir t)
(blog-minimal-render-all-org-files))))
(defun blog-minimal-get-org-article-property (org-file-name)
"ORG-FILE-NAME: org-file.
Get property (with title,uri,date) from org-file."
(let ((pt-list (ht)))
(with-temp-buffer
(insert-file-contents org-file-name)
(ht-set pt-list "title" (blog-minimal-read-org-option "title"))
(ht-set pt-list "date" (blog-minimal-read-org-option "date"))
;;; you can add other property for your org file.
(ht-set pt-list "title-uri" (concat (blog-minimal-read-org-option "uri") ".html"))
;;; (ht-set pt-list "tags" (split-string (blog-minimal-read-org-option "tags") ","))
(ht-set pt-list "keywords" (split-string (blog-minimal-read-org-option "keywords") ",")))
pt-list))
(defun blog-minimal-get--articles-property-for-index ()
"Get all articles properties for index."
(let* ((articles-property ())
(org-prefix-dir (concat blog-minimal-blog-main-dir "org/"))
(real-org-files (blog-minimal-real-org-files)))
(dolist (real-org real-org-files)
(push (blog-minimal-get-org-article-property (concat org-prefix-dir real-org))
articles-property))
;;; sort org index by date
(sort articles-property 'blog-minimal-comparator-org-by-date)))
(defun blog-minimal-real-org-files ()
"Return real org files."
(let ((org-files (directory-files (concat blog-minimal-blog-main-dir "org/")))
(real-org-files ()))
(dolist (org-file org-files)
;;; filter files whose name begin with . or # and end with ~
(when (not (or (s-starts-with? "." org-file)
(s-starts-with? "#" org-file)
(s-ends-with? "~" org-file)))
(push org-file real-org-files)))
real-org-files))
(defun blog-minimal-render-all-org-files ()
"Render all org files."
(interactive)
(update-config-info)
(blog-minimal-update-vars)
(blog-minimal-render-main-index)
(blog-minimal-render-about)
(blog-minimal-render-404)
(let ((real-org-files (blog-minimal-real-org-files)))
(dolist (real-orgf real-org-files)
(let ((org-file-name (concat blog-minimal-blog-main-dir "org/" real-orgf)))
(blog-minimal-render-current-article org-file-name)))))
(defun blog-minimal-update-index-vars (index-or-tags)
"If INDEX-OR-TAGS is t, return article index ; nil return tags index."
(ht-set blog-minimal-blog-index-vars "blog-info" blog-minimal-blog-info)
(ht-remove blog-minimal-blog-index-vars "blog-index")
;;; (ht-remove blog-minimal-blog-index-vars "blog-tags")
(if index-or-tags
(ht-set blog-minimal-blog-index-vars "blog-index" (blog-minimal-get--articles-property-for-index))
;;; (ht-set blog-minimal-blog-index-vars "blog-tags" (blog-minimal-get--tags-property-for-index))
))
(defun blog-minimal-render-main-index ()
"Render main index for blog minimal."
(blog-minimal-update-vars)
(ht-remove blog-minimal-header-vars "high-dir")
(blog-minimal-update-index-vars t)
(let ((main-vars
(ht-merge blog-minimal-header-vars blog-minimal-person-zone-vars blog-minimal-nav-vars blog-minimal-blog-index-vars blog-minimal-footer-vars)))
(blog-minimal-string-to-file (mustache-render
"{{> main-index}}"
main-vars)
(concat blog-minimal-blog-main-dir "index.html"))))
(defun blog-minimal-update-self-info ()
"Transfer about.org to self-info.mustache."
(interactive)
(with-temp-buffer
(insert-file-contents (concat blog-minimal-blog-main-dir "about.org"))
(let ((current-html (blog-minimal-org-content-to-html-file)))
(blog-minimal-string-to-file current-html (concat blog-minimal-mustache-templates-dir "self-info.mustache")))))
(defun blog-minimal-render-about ()
"Render about me for blog minimal."
(ht-remove blog-minimal-header-vars "high-dir")
(blog-minimal-update-self-info)
(let ((about-vars
(ht-merge blog-minimal-header-vars blog-minimal-person-zone-vars blog-minimal-nav-vars blog-minimal-footer-vars)))
(blog-minimal-string-to-file (mustache-render
"{{> about}}"
about-vars)
(concat blog-minimal-blog-main-dir "about.html"))))
(defun blog-minimal-render-404 ()
"Render 404 for blog minimal."
(ht-remove blog-minimal-header-vars "high-dir")
(let ((main-vars
(ht-merge blog-minimal-header-vars blog-minimal-person-zone-vars blog-minimal-nav-vars blog-minimal-footer-vars)))
(blog-minimal-string-to-file (mustache-render
"{{> 404}}"
main-vars)
(concat blog-minimal-blog-main-dir "404.html"))))
;; TODO add tags index? if you need it?
;; (defun blog-minimal-render-main-tags ()
;; "render tags index for blog minimal"
;; (ht-remove blog-minimal-header-vars "high-dir")
;; (blog-minimal-update-index-vars nil)
;; (let ((main-vars
;; (ht-merge blog-minimal-header-vars blog-minimal-person-zone-vars blog-minimal-nav-vars blog-minimal-blog-index-vars)))
;; (blog-minimal-string-to-file (mustache-render
;; "{{> main-index}}"
;; main-vars)
;; (concat blog-minimal-blog-main-dir "about.html"))))
(defun blog-minimal-render-current-article (&optional org-file-name)
"Render blog content for blog minimal."
(interactive)
(update-config-info)
(let ((target-buffer (if org-file-name
(generate-new-buffer "target-buffer")
(current-buffer))))
(save-current-buffer
(set-buffer target-buffer)
;;; insert file content we invoke it with a org file name
(if org-file-name
(insert-file-contents org-file-name))
;;; render blog content
(save-current-buffer
(let ((current-html (blog-minimal-org-content-to-html-file)))
(blog-minimal-string-to-file current-html (concat blog-minimal-mustache-templates-dir "blog-content.mustache"))))
;;; set vars for rendering
(ht-set blog-minimal-header-vars "high-dir" "/")
(ht-set blog-minimal-content-title-vars "current-title" (blog-minimal-read-org-option "title"))
(ht-set blog-minimal-content-title-vars "uri" (blog-minimal-read-org-option "uri"))
(ht-set blog-minimal-content-title-vars "article-keywords"
(let ((all-keywords ())
(keywords_org (blog-minimal-read-org-option "keywords")))
(if keywords_org
(dolist (keyword (split-string keywords_org ","))
(push (ht ("keyword" keyword)) all-keywords)))
all-keywords))
(let ((blog-vars
(ht-merge blog-minimal-header-vars blog-minimal-content-title-vars blog-minimal-footer-vars blog-minimal-blog-comment-vars
blog-minimal-nav-vars))
(date-list (split-string (blog-minimal-read-org-option "date") "-") ))
;;; create date dirs for articles
(blog-minimal-create-date-dir date-list)
(blog-minimal-string-to-file (mustache-render
"{{> blog-article}}"
blog-vars)
;;; write html to date dir
(concat blog-minimal-blog-main-dir (ht-get blog-vars "uri") ".html")))
(if org-file-name
(kill-buffer target-buffer))
(delete-window)))
;;; update main index
(blog-minimal-render-main-index))
(defun blog-minimal-create-new-article ()
"Create new article for blog minimal."
(interactive)
(update-config-info)
(let* ((title (read-string "Article Title: "))
(uri (read-string "Uri: ")) ;;; automatically add date
(real-uri (concat (format-time-string "articles/%Y/%m/") uri))
;;; (tags (read-string "Tags(separated by comma and space) [,]): "))
(keywords (read-string "Keywords(separated by comma and space) [,]: "))
(file-name (concat (format-time-string "%Y-%m-%d-") uri ".org"))
(time-list (split-string (format-time-string "%Y-%m-%d") "-"))
(buffer-name (concat blog-minimal-blog-main-dir "org/" file-name)))
(blog-minimal-create-date-dir time-list)
(switch-to-buffer (find-file buffer-name))
(insert (format
"#+TITLE: %s
#+AUTHOR: %s
#+EMAIL: %s
#+DATE: %s
#+URI: %s
#+KEYWORDS: %s
#+LANGUAGE: %s
#+OPTIONS: toc:%s num:nil \n\n\n
"
;;;#+TAGS: %s
title
blog-minimal-author
blog-minimal-blog-email
(format-time-string "%Y-%m-%d")
real-uri
keywords
;;; tags
org-export-default-language
blog-minimal-export-with-toc))))
(defun blog-minimal-preview-blog ()
"Preivew blog minimal."
(interactive)
(blog-minimal-render-main-index)
(httpd-serve-directory blog-minimal-blog-main-dir)
(browse-url (format "http://%s:%d" "127.0.0.1" httpd-port)))
(provide 'blog-minimal)
;;; blog-minimal.el ends here