This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ohl.el
310 lines (255 loc) · 8.53 KB
/
ohl.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
;;; ohl.el --- A workflow from org to hugo -*- lexical-binding: t -*-
;; Author: Fingker Knight <[email protected]>
;; URL: https://github.com/fingerknight/ohl.el
;; Version: 0.0.1
;; Package-Requires: ((emacs "28") (ox-hugo "0.12.1") (dash "2.19.1") (f "0.20.0") (s "1.13.1"))
;; Keywords: org-mode hugo
;; TODO
;; - [-] output filename to md5 by using `slug' or `filename'
;; - [ ] next/prev page in special pages
;; - [X] after exporting, don't not close buffers open before
;; - [-] way to sort in certain category.
;; - [X] do not use org-project-plist as communication channel, its copy instead.
(require 'f)
(require 's)
(require 'ohl-gitalk)
(require 'ohl-db)
(require 'ohl-git)
(defcustom ohl-project-plist nil
"A plist containing export projects.
Each project:
(\"project name\"
:base-directory
STRING
Where the Hugo project is
:static-relative-directory
STRING assets
The relavtive path of statics to /
:source-directory
STRING
Where the org files locate
:repository-directory
STRING
Where the GitHub repo is
:url
STRING
Blog's URL
:gitalk
BOOL, nil
If automatically handle with Gitalk
:paried-shortcodes
STING \"\"
Allowed shortcodes, which is seperated by space
:md5 - discarded
BOOL nil
use md5 replacing output md file's name.
:md5-include - discarded
LIST[REGEX-STRING] nil
when :md5 is nil, specify what files rename with md5
:md5-exclude - discarded
LIST[REGEX-STRING] nil
when :md5 is t, specify what files do not rename with md5
:auto-lastmod
BOOL t
:with-toc
BOOL
)"
:group 'ohl
:type 'alist)
(defvar ohl--loaded nil
"A sign used to check if deps loaded")
(defun eh-org-clean-space-for-md (text backend info)
"在export为 MarkDown时,删除中文之间不必要的空格"
(when (or (org-export-derived-backend-p backend 'hugo))
(let ((regexp "[[:multibyte:]]")
(string text))
;; org默认将一个换行符转换为空格,但中文不需要这个空格,删除.
(setq string
(replace-regexp-in-string
(format "\\(%s\\) *\n *\\(%s\\)" regexp regexp)
"\\1\\2" string))
;; 删除粗体之前的空格
(setq string
(replace-regexp-in-string
(format "\\(%s\\) +\\(\\*\\*%s\\)" regexp regexp)
"\\1\\2" string))
;; 删除粗体之后的空格
(setq string
(replace-regexp-in-string
(format "\\(%s\\*\\*\\) +\\(%s\\)" regexp regexp)
"\\1\\2" string))
string)))
(defun ohl-transform-tab-to-space-in-trim (text backend info)
"将落在行首或行尾的 \t 转换为 4 个空格"
(when (or (org-export-derived-backend-p backend 'hugo))
(let ((regexp "^\\(\t+\\)\\|\\(\t\\)$")
(string text))
(setq string
(replace-regexp-in-string
regexp
#'(lambda (match) (s-repeat (s-count-matches "\t" match) " "))
string))
string)))
(defun ohl-load ()
"A activating function"
(interactive)
(unless ohl--loaded
(message "[Org Hugo Lazy] Loading the configurations...")
(require 'ox)
(require 'ox-hugo)
(add-to-list 'org-hugo-special-block-type-properties '("mermaid" . (:raw t)))
;; 修改 center ,不加 <style> ,在 css 里面调样式
(setq org-blackfriday-center-block
(lambda (_center-block contents _info)
"Center-align the text in CONTENTS using CSS."
(let* ((class "org-center"))
(format "<div class=\"%s\">\n%s\n</div>"
class contents))))
(add-to-list 'org-export-filter-paragraph-functions
'eh-org-clean-space-for-md)
(add-to-list 'org-export-filter-src-block-functions
'ohl-transform-tab-to-space-in-trim)
(setq ohl--loaded t)))
(defun ohl--gernerate-single (project filename plist)
"Export a single org file FILE-NAME to md.
PLIST is communication property-list."
(let* ((file-short-name (f-relative filename
(plist-get plist :source-directory)))
(file-time (org-time-convert-to-integer (f-modification-time filename)))
(delete-list (plist-get plist :delete-list))
(file-time-in-db (ohl-db-get file-short-name))
(outfile ""))
(message "[Org Hugo Lazy] Processing: %s" filename)
(if (and file-time-in-db
(= file-time file-time-in-db))
;; This file has not been modified.
(message "Skipping unmodified file: %s" filename)
;; New file or modified file
(with-temp-buffer
(find-file filename)
(setq outfile (org-hugo-export-wim-to-md t nil))
(when (plist-get plist :gitalk)
;; Try to create new issue
(ohl-gitalk--git-add-issue (plist-get plist :repository-directory)
(s-trim (nth 1 (s-match "#\\+[tT][iI][tT][lL][eE]:\\([^\n]+\\)\n"
(buffer-string))))
(downcase (concat (plist-get plist :url)
(f-base filename) "/"))
(md5 (f-relative outfile
(f-expand "content/"
(plist-get plist :base-directory))))))
(unless (or (eql (buffer-name)
(plist-get plist :current-buffer))
(member (buffer-file-name)
(plist-get plist :buffer-list)))
(kill-buffer)))
(let ((lst (plist-get plist :update-list)))
(push (cons (intern file-short-name) file-time) lst)
(plist-put plist :update-list lst))
;; (when (or (and (plist-get plist :md5)
;; (--every-p (not (s-matches-p it
;; (f-filename filename)))
;; (plist-get plist :md5-exclude)))
;; (and (not (plist-get plist :md5))
;; (--any-p (s-matches-p it
;; (f-filename filename))
;; (plist-get plist :md5-include))))
;; (rename-file outfile
;; (f-expand (concat (md5 file-short-name)
;; ".md")
;; (f-dirname outfile))
;; t))
(message "Done: %s" filename))
;; remove name from the table
;; since the rest will be deleted from database
(plist-put plist :delete-list (remove file-short-name delete-list))))
;;;###autoload
(defun ohl-generate (&optional project plist)
"Transform all the org files in project to markdown.
PLIST is communication channel.
IF this function is called interactively, then GITALK will \
be forbidden.
`ohl--gernerate-single' is used for each file."
(interactive)
(when (interactive-p)
(ohl-load)
(setq project (completing-read
"Project: "
(mapcar #'car
ohl-project-plist)
nil t)
plist (copy-alist (cdr (assoc-string
project
ohl-project-plist))))
(plist-put plist :gitalk nil))
(setq org-hugo-base-dir
(plist-get plist :base-directory)
org-hugo-default-static-subdirectory-for-externals
(if (plist-member plist :static-relative-directory)
(plist-get plist :static-relative-directory)
"assets")
org-hugo-auto-set-lastmod
(if (plist-member plist :auto-lastmod)
(plist-get plist :auto-lastmod)
t)
org-hugo-export-with-toc
(if (plist-member plist :with-toc)
(plist-get plist :with-toc)
nil)
org-hugo-paired-shortcodes
(if (plist-member plist :paired-shortcodes)
(plist-get plist :paired-shortcodes)
""))
(plist-put plist :current-buffer (buffer-name))
(plist-put plist :buffer-list (remove nil (-map #'buffer-file-name (buffer-list))))
(plist-put plist :update-list nil)
(plist-put plist :delete-list (ohl-db-load project))
(--map
(ohl--gernerate-single project it plist)
(f-files (plist-get plist :source-directory)
(lambda (file) (s-equals-p (f-ext file) "org"))
t))
(setq dbg plist)
;; clean old items
(ohl-db-delete-files (plist-get plist :delete-list))
;; update items
(ohl-db-update-files (plist-get plist :update-list))
;; save json
(ohl-db-save project))
;;;###autoload
(defun ohl-publish (&optional project wait)
(interactive)
(ohl-load)
(let* ((project-name
(if project project
(completing-read "Project: "
(mapcar #'car
ohl-project-plist)
nil t)))
(plist (copy-alist (cdr (assoc-string project-name
ohl-project-plist)))))
(when (plist-get plist :gitalk)
(ohl-gitalk--get-issue-list (plist-get plist :repository-directory)))
;; Export org to md
(ohl-generate project-name plist)
;; Export md to HTML
(message (shell-command-to-string (format "cd %s; hugo"
(plist-get plist :base-directory))))
(message "[Org Hugo Lazy] HTML exported")
;; Git commit
(let ((script (ohl-git-format-script (plist-get plist :repository-directory))))
(if wait script
(async-shell-command script)))
))
;;;###autoload
(defun ohl-publish-all ()
"Publish all projects"
(interactive)
(async-shell-command
(--reduce-from
(concat acc (ohl-publish (car it) t))
""
ohl-project-plist))
(message "[Org Hugo Lazy] All published."))
(provide 'ohl)
;;; ohl.el ends