aboutsummaryrefslogtreecommitdiff
path: root/publish.el
blob: 2f62127f549effa2965baf1c7e78cecb5b144169 (plain) (blame)
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
;; Copyright (C) 2021 Yuchen Pei.

;; This file is part of site generator for Yuchen's personal website (abbreviated to sg4y).

;; sg4y is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; sg4y 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 Affero General Public License for more details.

;; You should have received a copy of the GNU Affero General Public License
;; along with sg4y.  If not, see <https://www.gnu.org/licenses/>.


(package-initialize)

(require 'ox-publish)

(defvar this-date-format "%Y-%m-%d")

(defun me/html-preamble (plist)
  "PLIST: An entry."
  (if (org-export-get-date plist this-date-format)
        (plist-put plist
             :subtitle (format "Published on %s"
                               (org-export-get-date plist this-date-format)
			       )))
  ;; Preamble
  (with-temp-buffer
    (insert-file-contents "../html-templates/preamble.html") (buffer-string)))

(defun me/html-postamble (plist)
  (with-temp-buffer
    (insert-file-contents "../html-templates/postamble.html") (buffer-string)))

(defun me/org-posts-sitemap-format-entry (entry style project)
  "Format posts with author and published data in the index page.

ENTRY: file-name
STYLE:
PROJECT: `posts in this case."
  (cond ((not (directory-name-p entry))
         (format "%s - *[[file:posts/%s][%s]]*"
                 (format-time-string this-date-format
                                     (org-publish-find-date entry project))
                 entry
                 (org-publish-find-title entry project)
		 ))
        ((eq style 'tree) (file-name-nondirectory (directory-file-name entry)))
        (t entry)))

(defun org-publish-find-content (file project)
  "Finds the content of an org file.
The content starts with the first empty line."
  (let ((file (org-publish--expand-file-name file project)))
    (when (and (file-readable-p file) (not (directory-name-p file)))
      (with-temp-buffer
	(insert-file-contents file)
	(goto-char (point-min))
	(let ((beg (+ 1 (re-search-forward "^$"))))
	  (buffer-substring beg (point-max)))))))
    
(defun me/org-microposts-sitemap-format-entry (entry style project)
  "Format microposts with author and published data in the index page.

ENTRY: file-name
STYLE:
PROJECT: `microposts in this case."
  (cond ((not (directory-name-p entry))
         (format "*[[%s][%s]]* - %s\n<<%s>>\n\n%s"
		 (file-name-sans-extension entry)
                 (format-time-string this-date-format
                                     (org-publish-find-date entry project))
                 (org-publish-find-title entry project)
		 (file-name-sans-extension entry)
		 (org-publish-find-content entry project)
		 ))
        ((eq style 'tree) (file-name-nondirectory (directory-file-name entry)))
        (t entry)))

(defun me/org-publish-microblog (plist filename pub-dir)
  "Publish RSS with PLIST, only when FILENAME is '../pages/microblog.org'.
PUB-DIR is when the output will be placed."
  (if (equal "../pages/microblog.org" (file-name-nondirectory filename))
      (org-html-publish-to-html plist filename pub-dir)))

(setq org-publish-project-alist
      '(("posts"
         :base-directory "posts"
         :base-extension "org"
         :publishing-directory "site/posts"
         :recursive nil
         :publishing-function org-html-publish-to-html
         :auto-sitemap t
	 :section-numbers nil
	 :sitemap-format-entry me/org-posts-sitemap-format-entry
	 :sitemap-title "Yuchen's Blog"
	 :sitemap-sort-files anti-chronologically
	 :sitemap-filename "../pages/blog.org"
	 :html-head "<link rel='stylesheet' href='../css/default.css' type='text/css'/>
<link rel='alternate' title=\"Yuchen's blog\" href='/blog-feed.xml' type='application/rss+xml'>"
	 :html-preamble me/html-preamble
	 :html-self-link-headlines t
	 :author ("Yuchen Pei")
	 :html-postamble me/html-postamble
	 :html-mathjax-options ((path "/js/mathjax/MathJax.js?config=TeX-AMS_CHTML"))
	 :html-mathjax-template "<script type=\"text/javascript\" src=\"%PATH\"></script>"
	 )
	("microposts"
         :base-directory "microposts"
         :base-extension "org"
         :publishing-directory "site/microposts"
         :recursive t
         :publishing-function me/org-publish-microblog
         :auto-sitemap t
	 :sitemap-format-entry me/org-microposts-sitemap-format-entry
	 :sitemap-title "Yuchen's Microblog"
	 :sitemap-sort-files anti-chronologically
	 :sitemap-filename "../pages/microblog.org"
	 :html-head "<link rel='stylesheet' href='../css/default.css' type='text/css'/>"
	 :html-preamble me/html-preamble
	 :author ("Yuchen Pei")
	 :html-postamble me/html-postamble
	 :html-mathjax-options ((path "/js/mathjax/MathJax.js?config=TeX-AMS_CHTML"))
	 :html-mathjax-template "<script type=\"text/javascript\" src=\"%PATH\"></script>"
	 )
	("pages"
         :base-directory "pages"
         :base-extension "org"
         :publishing-directory "site"
         :recursive t
         :publishing-function org-html-publish-to-html
	 :html-head "<link rel='stylesheet' href='../css/default.css' type='text/css'/>
                     <link rel='stylesheet' href='../css/page.css' type='text/css'/>
                     <link rel='alternate' title=\"Yuchen's blog\" href='/blog-feed.xml' type='application/rss+xml'>
                     <link rel='alternate' title=\"Yuchen's microblog\" href='/microblog-feed.xml' type='application/rss+xml'>"
	 :html-preamble me/html-preamble
	 :author ("Yuchen Pei")
	 :section-numbers nil
	 :with-toc nil
	 :html-postamble me/html-postamble
	 :html-prefer-user-labels t
	 :html-mathjax-options ((path "/js/mathjax/MathJax.js?config=TeX-AMS_CHTML"))
	 :html-mathjax-template "<script type=\"text/javascript\" src=\"%PATH\"></script>"
	 )	 
	("css"
          :base-directory "css"
          :base-extension "css"
          :publishing-directory "site/css"
          :publishing-function org-publish-attachment
          :recursive t
	  )
	("js"
          :base-directory "js"
          :base-extension "js"
          :publishing-directory "site/js"
          :publishing-function org-publish-attachment
          :recursive t
	  )
	("assets"
	 :base-directory "assets"
	 :base-extension any
	 :publishing-directory "site/assets"
	 :publishing-function org-publish-attachment
	 :recursive t
	 )
        ("all" :components ("posts" "microposts" "pages" "css" "js" "assets"))))