5 (load "~/.emacs.d/lisp/mustache-html.el")
7 (setq org-html-doctype "html5")
8 (setq org-html-head-include-default-style nil)
9 (setq org-html-htmlize-output-type 'css) ; default: 'inline-css
10 (setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M"))
11 (setq org-display-custom-times t)
12 (setq org-html-container-element "section") ;; TODO - check
13 (setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz")
14 (setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source")
15 (setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target")
16 (setq my-blog-target-url "https://www.vanrenterghem.biz/posts/")
17 (setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html"))
18 (setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags"))
19 (setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts"))
21 (setq org-export-time-stamp-file nil)
22 (setq org-rss-use-entry-url-as-guid nil)
24 (defun my-org-get-all-filetags ()
25 "Get list of filetags from all org-files in my-blog-posts-folder."
26 (let ((files (directory-files my-blog-posts-folder t nil nil nil))
28 (save-window-excursion
29 (while (setq x (pop files))
30 (set-buffer (find-file-noselect x))
33 (let ((tagfiles (assoc y tagslist)))
35 (setcdr tagfiles (cons x (cdr tagfiles)))
36 (add-to-list 'tagslist (list y x)))))
37 (my-org-get-filetags)))
40 (defun my-org-get-filetags ()
41 "Get list of filetags for current buffer"
42 (let ((ftags org-file-tags)
46 (org-no-properties x))
49 (defun my-blog-create-tags-files (plist)
50 "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder."
51 (let* ((tagfolder (file-name-concat
52 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)))
53 (postfolder (file-name-concat
54 (plist-get (cdr (assoc "posts" org-publish-project-alist)) :base-directory)))
55 (relpostfolder (file-relative-name postfolder tagfolder)))
56 (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
57 (with-temp-file (file-name-concat tagfolder "tag-index.org")
58 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
59 (insert (concat "#+TITLE: Blog - All tags\n"))
60 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
62 (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) ))
63 (insert (concat "- [[file:" (file-name-concat relpostfolder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n")))))
64 (dolist (tag (my-org-get-all-filetags))
65 (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
66 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
67 (insert (concat "#+TITLE: " (car tag) "\n"))
68 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
70 (insert (concat "# " (car tag) "\n\n"))
71 (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist))
72 (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
73 (message (concat "Processing " tagfile))
74 (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
75 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
77 (org-publish-find-title tagfile plist))
79 ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
81 (defun my-org-publish-sitemap-default-entry (entry style project)
82 "My format for site map ENTRY, as a string.
83 ENTRY is a file name. STYLE is the style of the sitemap.
84 PROJECT is the current project."
85 (cond ((not (directory-name-p entry))
86 (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
87 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
89 (org-publish-find-title entry project)))
91 ;; Return only last subdir.
92 (file-name-nondirectory (directory-file-name entry)))
95 (defun my-blog-parse-sitemap-list (l)
96 "Convert the sitemap list in to a list of filenames."
98 (let ((link (with-temp-buffer
99 (let ((org-inhibit-startup nil))
102 (goto-char (point-min))
103 (org-element-link-parser)))))
105 (plist-get (cadr link) :path))))
108 (defun my-blog-sort-article-list (l p)
109 "sort the article list anti-chronologically."
110 (sort l #'(lambda (a b)
111 (let ((d-a (org-publish-find-date a p))
112 (d-b (org-publish-find-date b p)))
113 (not (time-less-p d-a d-b))))))
115 (defun my-blog-get-preview (file)
116 "Clips a section of a post in FILE to be used as preview in the sitemap.
117 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
119 (insert-file-contents file)
120 (goto-char (point-min))
121 (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
122 (re-search-forward "^$")))
123 (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
125 (progn (goto-char (+ 1 beg))
126 (re-search-forward "^$" nil t 2)))))
127 (buffer-substring beg end))))
129 (defun my-blog-sitemap (title list)
130 "Generate the sitemap landing page for my blog."
131 (my-plain-publish-sitemap-default title list) ; Create additional sitemap
133 ;; mangle the parsed list given to us into a plain lisp list of files
134 (let* ((filenames (my-blog-parse-sitemap-list list))
135 (project-plist (assoc "posts" org-publish-project-alist))
136 (articles (my-blog-sort-article-list filenames project-plist)))
137 (dolist (file filenames)
138 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
139 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
140 (title (org-publish-find-title file project-plist))
141 (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
142 (preview (my-blog-get-preview abspath))
144 ;; insert a horizontal line before every post, kill the first one
146 (insert (concat "* [[file:" relpath "][" title "]]\n"))
147 ;; add properties for `ox-rss.el' here
148 (let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
149 (rss-pubdate (format-time-string (cdr org-time-stamp-formats) (org-publish-find-date file project-plist))))
151 (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
152 (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
153 (org-set-property "RSS_PERMALINK" rss-permalink)
154 (org-set-property "PUBDATE" rss-pubdate)
155 (org-set-property "RSS_TITLE" title))
156 ;; insert the date, preview, & read more link
157 (insert "#+ATTR_HTML: :class card-header\n")
158 (insert (concat "Published: " date "\n\n"))
159 (insert "#+BEGIN_export html\n")
160 (insert "<section class=\"card-body\">\n")
161 (insert "#+END_export\n")
163 ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
165 (insert (concat "[[file:" relpath "][Read More...]]\n"))
166 (insert "#+BEGIN_export html\n")
167 (insert "<\/section>\n")
168 (insert "#+END_export\n")
170 ;; kill the first hrule to make this look OK
171 ;(goto-char (point-min))
172 ;(let ((kill-whole-line t)) (kill-line))
173 ;; insert a title and save
174 (insert "#+OPTIONS: title:nil\n")
175 (insert "#+TITLE: Frederik Vanrenterghem's blog\n")
176 (insert "#+AUTHOR: Frederik Vanrenterghem\n")
177 (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
178 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
179 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
183 (defun my-plain-publish-sitemap-default (title list)
184 "Create a simple site map, as a string.
185 TITLE is the title of the site map. LIST is an internal
186 representation for the files to include, as returned by
188 (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
189 (let* ((filenames (my-blog-parse-sitemap-list list))
190 (project-plist (assoc "posts" org-publish-project-alist))
191 (articles (my-blog-sort-article-list filenames project-plist)))
192 (dolist (file filenames)
193 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
194 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
195 (title (org-publish-find-title file project-plist))
196 (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
197 (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
198 (goto-char (point-min))
199 (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
201 (defun my-blog-cleanup-sitemaps (plist)
202 "Clean up temporary files created in the process of publishing"
203 ;; Create a body-only version of the tags index. Needs absolute path to posts folder.
204 (with-temp-file (file-name-concat
205 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :publishing-directory)
206 "tag-index-body.html")
207 (insert-file-contents (file-name-concat
208 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)
210 (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil)
211 (replace-string "href=\"posts" "href=\"/posts"))
212 (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
213 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
214 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org~")))
216 (setq org-publish-project-alist
218 :base-directory ,my-blog-source-folder
219 :base-extension "org"
220 :publishing-directory ,my-blog-target-folder
221 :publishing-function org-mustache-html-publish-to-html
222 :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
227 :html-content-class nil
228 :html-head-include-default-style nil
236 :base-directory ,my-blog-posts-folder
237 :base-extension "org"
238 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
239 :publishing-function org-mustache-html-publish-to-html
240 :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
241 :preparation-function my-blog-create-tags-files
242 :exclude "html*\\|assets*\\|index.org\\|sitemap.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
243 :html-content-class nil
247 :sitemap-title "All posts"
248 :html-head-include-default-style nil
255 :sitemap-sort-folders ignore-errors
256 :sitemap-function my-blog-sitemap
257 :sitemap-sort-files anti-chronologically
258 :sitemap-filename "sitemap.org"
262 :base-directory ,(file-name-concat my-blog-source-folder "tags")
263 :base-extension "org"
264 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
265 :publishing-function org-mustache-html-publish-to-html
266 :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
267 :html-content-class nil
271 :html-head-include-default-style nil
281 :base-directory ,(file-name-concat my-blog-source-folder "assets")
284 :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
285 :publishing-function org-publish-attachment
286 :completion-function my-blog-cleanup-sitemaps
290 :base-directory ,my-blog-posts-folder
291 :base-extension "org"
292 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
293 :publishing-function org-rss-publish-to-rss
295 :title "Frederik Vanrenterghem's blog"
296 :html-link-home ,my-blog-target-url
297 :html-link-use-abs-url t
300 :include ("sitemap.org")
301 :table-of-contents nil
304 ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
306 (org-publish-initialize-cache "website")