-
Notifications
You must be signed in to change notification settings - Fork 11
/
ego-config.el
589 lines (444 loc) · 18 KB
/
ego-config.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
;;; ego-config.el --- Variables and Functions dealing with ego configure
;; Copyright (C) 2015 Feng Shu, Kuangdash
;; 2012, 2013, 2014, 2015 Kelvin Hu
;; Author: Feng Shu <tumashu AT 163.com>
;; Kelvin Hu <ini DOT kelvin AT gmail DOT com>
;; Kuangdash <kuangdash AT 163.com>
;; Keywords: org-mode, convenience, beautify
;; Homepage: https://github.com/emacs-china/EGO
;; 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:
;; ego-config.el contains almost all variable definitions and configurations.
;;; Code:
(require 'ox)
(require 'ht)
(require 'subr-x)
(require 'vc-git)
(defgroup ego nil
"Options for generating static pages using ego."
:tag "Org static page generator"
:group 'org)
(defcustom ego-default-project-name nil
"If set, `ego-do-publication' will directly publish this project
and `ego-new-post' will directly add new post to this project."
:group 'ego
:type 'string)
(defcustom ego-project-config-alist nil
"Association list to control ego publishing behavior.
Each element of the alist is a ego 'project.' The CAR of
each element is a string, uniquely identifying the project. The
CDR of each element is a well-formed property list with an even
number of elements, alternating keys and values, specifying
parameters for the publishing process.
\(:property value :property value ... )
Most properties are optional, but some should always be set:
`:repository-directory'
The git repository directory, where org files stored on branch
`:repository-org-branch', and generated html files stored on branch
`:repository-html-branch'.
1. Type: string
2. Example1: \"~/.emacs.d/projects/tumashu.github.com/\"
`:site-domain'
The domain name of entire site, it is recommended to assign with prefix
http:// or https://, http will be considered if not assigned.
1. Type: string
2. Example1: \"http://tumashu.github.com\"
`:site-main-title'
The main title of entire site.
1. Type: string
2. Example1: \"Tumashu's website\"
`:site-sub-title'
The subtitle of entire site.
1. Type: string
2. Example1: \"======> My personal blog site.\"
`:preparation-function'
Function to be called before publishing this project. This may also
be a list of functions.
1. Type: function
2. Example: eh-convert-el-to-org
`:repository-org-branch'
The branch where org files stored on, it is within repository presented by
`:repository-directory'.
1. Type: string
2. Example1: \"source\"
3. Example2: \"master\"
`:repository-html-branch'
The branch where generated html files stored on, it is within repository
presented by `:repository-directory'.
1. Type: string
2. Example1: \"master\"
3. Example2: \"gh-pages\"
`:theme-root-directory'
The root directory list that stores themes for page rendering. By default, it
points to the directory `themes' in ego installation directory.
1. Type: list
2. Example1: (\"/path/to/dir1\" \"/path/to/dir2\" \"/path/to/dir3\" ...)
3. Example2: nil
When set this option to `nil', ego will find two paths as fallback:
1. <Directory which contain ego.el>/themes
2. <Your project repository directory>/themes
`:theme'
The theme used for page generation.
1. Type: list
2. Example1: (worg killjs)
3. Example2: nil
When set this option to `nil', default theme will be used.
`:source-browse-url'
The personal github link.
1. Type: list
2. Example1: (\"GitHub\" \"https://github.com/emacs-china/ego\")
`:personal-avatar'
The link to an avatar image.
1. Type: string
2. Example1: \"/media/img/horse.jpg\"
2. Example2: \"http://tumashu.github.com/ego-media/img/horse.jpg\"
`:personal-disqus-shortname'
The personal disqus shortname.
1. Type: string
2. Example1: \"my-disqus-name\"
`:personal-duoshuo-shortname'
The personal duoshuo shortname.
1. Type: string
2. Example1: \"my-duoshuo-name\"
`:personal-google-analytics-id'
Personal google analytics id.
The personal duoshuo shortname.
1. Type: string
2. Example1: \"my-google-analytics-id\"
`:confound-email'
Determine whether email addresses should be confounded or not.
1. Type: boolean
2. Example1: t
When set this option to `t', \"[email protected]\" will be converted to \"myname <at> 163 <dot> com\"
`:force-absolute-url'
Force convert relative url to absolute url in html files by append site domain.
1. Type: boolean
2. Example1: t
When set this option to `t', all url like \"/path/to/file.html\" will be
converted to \"http://yourdomain.com/path/to/file.html\".
`:default-category'
If org files don't set category, default category will be used.
1. Type: string
2. Example1: \"blog\"
3. Example2: \"wiki\"
4. Example3: \"documents\"
`:about'
About page of org-website.
1. Type: list
2. Example1: (\"About\" \"/about/\")
`:rss'
RSS page of org-website.
1. Type: list
2. Example1: (\"RSS\" \"/rss.xml\")
`:summary'
A summary is a statistic page, Which can be used show pages
based on \"tags\" , \"data\" , \"author\" and so on.
it is similar Microsoft Excel pivot table feature.
1. Type: alist
2. Example1: ((\"tags\" :tags) (\"years\" :year) (\"authors\" :authors))
2. Example2: ((\"按标签分类\" :tags) (\"按年度分类\" :year) (\"按作者分类\" :authors))
`:category-ignore-list'
Ignore subdirs/categories for navigation.
1. Type: list
2. Example1: (\"themes\" \"assets\")
Names in this list will not showed in webpage navbar.
`:ignore-file-name-regexp'
the file whose name(include path) match the regexp won't be exported.
`:get-title-function'
A function used to retrieve an org file's Title, it has no parameter and
run org file buffer.
1. Type: function
2. Example1: ego--get-title
`:retrieve-category-function'
A function used to retrieve an org file's category, its parameter is the
org file's path, if parameter is nil, it should return all categories.
1. Type: function
2. Example1: ego--get-file-category
`:org-export-function'
Set the default function by which ego export org file to html.
1. Type: function
2. Example1: ego--default-org-export
`:html-creator-string'
Information about the creator of the HTML document.
1. Type: string
2. Example1: \"This is an example creator string\"
`:repo-files-function'
The function used to get all org files exported.
1. Type: function
2. Example1: ego-git-get-all-files
`:addition-files-function'
The function used to get addition org files exported, for example:
org files ignored by git, which are generated from other files.
1. Type: function
2. Example1: ego--addition-all-files
`:store-dir'
ego can start a web server to test publish, this
set the server document root.
1. Type: string
2. Example1: \"~/.emacs.d/org-website-server/ego-\"
You can see fallback value of above option in `ego-config-fallback'"
:group 'ego
:type 'alist)
(defcustom ego--get-config-option-function
'ego--get-config-option-from-alist
"The function used to get config option."
:group 'ego
:type 'function)
(defcustom ego-db-file-name
".ego.db"
"The file used to store ego related information."
:group 'ego
:type 'string)
(defcustom ego-auto-commit nil
"Specify auto commit repo-dir or not"
:group 'ego
:type 'boolean)
(defcustom ego-auto-push t
"Whether auto push repo-dir and store-dir"
:group 'ego
:type 'boolean)
(defconst ego--temp-buffer-name "*EGO Output*"
"Name of the temporary buffer used by ego.")
(defconst ego-load-directory
(cond
(load-file-name (file-name-directory load-file-name))
((symbol-file 'ego--temp-buffer-name)
(file-name-directory (symbol-file 'ego--temp-buffer-name)))
((string= (file-name-nondirectory buffer-file-name) "ego-config.el")
(file-name-directory buffer-file-name))
(t nil))
"The directory where ego is loaded from.")
(defvar ego--category-config-alist
'(("blog"
:show-meta t
:show-comment t
:uri-generator ego--generate-uri
:uri-template "/blog/%y/%m/%d/%t/"
:sort-by :date ;; how to sort the posts
:category-index nil) ;; generate category index or not
("index"
:show-meta nil
:show-comment nil
:uri-generator ego--generate-uri
:uri-template "/"
:sort-by :date
:category-index nil)
("about"
:show-meta nil
:show-comment nil
:uri-generator ego--generate-uri
:uri-template "/about/"
:sort-by :date
:category-index nil))
"Configurations for different categories, can and should be customized.")
(defvar ego--category-show-list nil
"the list of category names(string) which will be showed in the navigation-bar")
(defvar ego-current-project-name "")
(defvar ego--item-cache nil
"The cache for general purpose.")
(defconst ego--rss-template "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\">
<channel>
<title>{{title}}</title>
<link>{{link}}</link>
<description>{{description}}</description>
<pubDate>{{date}}</pubDate>
<lastBuildDate>{{date}}</lastBuildDate>
<docs>http://www.rssboard.org/rss-specification</docs>
<generator>ego static site generator \
(https://github.com/emacs-china/ego)</generator>
{{#items}}
<item>
<title>{{item-title}}</title>
<link>{{item-link}}</link>
<description>{{item-description}}</description>
<pubDate>{{item-update-date}}</pubDate>
<guid>{{item-link}}</guid>
</item>
{{/items}}
</channel>
</rss>"
"Template for RSS rendering.")
(defvar ego-config-fallback
`(:repository-directory nil
:site-domain nil
:site-main-title "ego"
:site-sub-title "static site generator"
:repository-org-branch "source"
:repository-html-branch "master"
:theme-root-directory nil
:theme (default)
:source-browse-url nil
:personal-avatar nil
:personal-disqus-shortname nil
:personal-duoshuo-shortname nil
:personal-google-analytics-id nil
:default-category "blog"
:about ("About" "/about/")
:rss ("RSS" "/rss.xml")
:category-ignore-list ("themes" "assets")
:ignore-file-name-regexp "\n"
:summary (("tags" :tags))
:confound-email t
:force-absolute-url nil
:preparation-function nil
:get-title-function ego--get-title
:retrieve-category-function ego--get-file-category
:repo-files-function ego-git-get-all-files
:addition-files-function nil
:org-export-function ego--default-org-export
:store-dir "~/.emacs.d/ego-server/default"
:html-creator-string ,(format "<a href=\"http://www.gnu.org/software/emacs/\">Emacs</a> %s\
(<a href=\"http://orgmode.org\">Org mode</a> %s)"
(format "%s.x" emacs-major-version)
(if (fboundp 'org-version)
(replace-regexp-in-string "\\..*" ".x" (org-version))
"Unknown Version"))
"If User don't set an option, ego will use fallback value of this option."))
(defcustom ego-pre-publish-hooks nil
"run hook before publish changed org file to html file(by calling `ego--publish-modified-file') with attr-plist as the only argument.
attr-plist contains meta data of the org file such as :title :date :mod-date :thumb :description :year :tags :authors :category :uri :pub-dir"
:group 'ego
:type 'list)
(defcustom ego-post-publish-hooks nil
"run hook before publish changed org file to html file(by calling `ego--publish-modified-file') with attr-plist as the only argument.
attr-plist contains meta data of the org file such as :title :date :mod-date :thumb :description :year :tags :authors :category :uri :pub-dir"
:group 'ego
:type 'list)
(defun ego--get-config-option (option)
"The function used to read ego config"
(when (functionp ego--get-config-option-function)
(funcall ego--get-config-option-function option)))
(defun ego--get-config-option-from-alist (option)
"The default ego config read function,
which can read `option' from `ego-project-config-alist'
if `option' is not found, get fallback value from
`ego-config-fallback'."
(let ((project-plist (cdr (assoc ego-current-project-name
ego-project-config-alist))))
(if (plist-member project-plist option)
(plist-get project-plist option)
(plist-get ego-config-fallback option))))
(defun ego--get-repository-directory ()
"The function, which can return repository directory string."
(let ((dir (vc-git-root (ego--get-config-option :repository-directory))))
(when dir
(file-name-as-directory
(expand-file-name dir)))))
(defun ego--get-site-domain ()
"The function, which can return site-domain string."
(let ((site-domain (ego--get-config-option :site-domain)))
(when site-domain
(if (or (string-prefix-p "http://" site-domain)
(string-prefix-p "https://" site-domain))
(directory-file-name
(file-name-as-directory site-domain))
(directory-file-name
(file-name-as-directory
(concat "http://" site-domain)))))))
(defun ego--get-theme-dirs (&optional root-dir theme type)
"The function ,return ego theme type paths list.
ego organizes its themes by directory:
| Directory | Argument | Value |
+---------------------+-------------+------------------------+
| /path/to/directory | <root-dir> | \"/path/to/directory\" |
| \--mdo | <theme> | 'mdo |
| |-- templates | <type> | 'templates |
| \- resources | <type> | 'resources |
`root-dir' and `theme' can be lists, for example:
`(\"path/to/dir1\" \"path/to/dir2\" \"path/to/dir3\")'
`(theme1 theme2 theme3)'
At this time, `ego--get-theme-dirs' will find *all possible*
<type> directorys by permutation way and return a list with
multi path."
(let* ((themes (delete-dups
(if theme
(list theme)
`(,@(ego--get-config-option :theme) default))))
(theme-root-dirs (delete-dups
(if root-dir
(list root-dir)
`(,@(ego--get-config-option :theme-root-directory)
,(concat (ego--get-repository-directory) "themes/")
,(concat ego-load-directory "themes/")))))
theme-dir theme-dirs)
(dolist (theme themes)
(dolist (root-dir theme-root-dirs)
(setq theme-dir
(file-name-as-directory
(expand-file-name
(format "%s/%s" (symbol-name theme)
(if type (symbol-name type) ""))
root-dir)))
(when (file-directory-p theme-dir)
(push theme-dir theme-dirs))))
(reverse theme-dirs)))
(defun ego--get-html-creator-string ()
"The function, which can return creator string."
(or (ego--get-config-option :html-creator-string) ""))
(defun ego--get-category-setting (category)
"The function , which can return category config of `category'"
(or (assoc category ego--category-config-alist)
`(,category
:show-meta t
:show-comment t
:uri-generator ego--generate-uri
:uri-template ,(format "/%s/%%t/" category)
:sort-by :date
:category-index t)))
(defun ego-get-db-file ()
"Return the absolute path of db-file in `ego-current-project-name''s repo root directory"
(expand-file-name ego-db-file-name (ego--get-config-option :store-dir)))
(defun ego-get-org-html-mapping ()
"Return org-html-mapping-alist which stored in ego-db-file(use ego-get-db-file function to get it)."
(let* ((ego-db-file (ego-get-db-file))
(org-html-mapping-lines (when (file-readable-p ego-db-file)
(delete "" (split-string (ego--file-to-string ego-db-file) "[\r\n]+"))))
(org-html-mapping-alist (mapcar #'read org-html-mapping-lines)))
org-html-mapping-alist))
(defun ego-save-org-html-mapping (org-html-mapping-alist)
"Save org-html-mapping-alist to ego-db-file(use ego-get-db-file function to get it)."
(let ((ego-db-file (ego-get-db-file)))
(with-temp-file ego-db-file
(let ((standard-output (current-buffer)))
(mapc #'print org-html-mapping-alist)))))
(defun ego-update-org-html-mapping (org-path html-uri &optional del)
"update org-path and html-uri mapping relationship stored in ego-db-file(use ego-get-db-file function to get it).
If DEL is not nil, then it will delete origin-html-uri as well.
Return the origin html uri of ORG-PATH."
(let* ((org-html-mapping-alist (ego-get-org-html-mapping))
(origin-html-uri (assoc-default org-path org-html-mapping-alist)))
(if origin-html-uri
(setf (cdr (assoc org-path org-html-mapping-alist)) html-uri)
(push (cons org-path html-uri) org-html-mapping-alist))
(ego-save-org-html-mapping org-html-mapping-alist)
(when (and del
origin-html-uri
(not (string= html-uri origin-html-uri)))
(delete-file origin-html-uri))
origin-html-uri))
(defun ego-delete-org-html-mapping (org-path &optional del)
"delete org-path and html-uri mapping relationship stored in ego-db-file(use ego-get-db-file function to get it).
If DEL is not nil, then it will delete origin-html-uri as well.
Return the origin html uri of ORG-PATH"
(message "EGO DEBUG:ego-delete-org-html-mapping(%s %s)" org-path del)
(let* ((org-html-mapping-alist (ego-get-org-html-mapping))
(origin-html-uri (assoc-default org-path org-html-mapping-alist)))
(setq org-html-mapping-alist (delete (assoc org-path org-html-mapping-alist) org-html-mapping-alist))
(ego-save-org-html-mapping org-html-mapping-alist)
(when (and del
origin-html-uri)
(message "EGO DEBUG:delete-file %s" origin-html-uri)
(delete-file origin-html-uri))
origin-html-uri))
(provide 'ego-config)
;;; ego-config.el ends here