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 (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
52 (with-temp-file (file-name-concat my-blog-source-folder "tag-index.org")
53 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
54 (insert (concat "#+TITLE: Blog - All tags\n"))
55 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
57 (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) ))
58 (insert (concat "- [[file:" (file-name-concat my-blog-posts-folder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n"))))
59 (dolist (tag (my-org-get-all-filetags))
60 (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
61 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
62 (insert (concat "#+TITLE: " (car tag) "\n"))
63 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
65 (insert (concat "# " (car tag) "\n\n"))
66 (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist))
67 (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
68 (message (concat "Processing " tagfile))
69 (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
70 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
72 (org-publish-find-title tagfile plist))
74 ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
76 (defun my-org-publish-sitemap-default-entry (entry style project)
77 "My format for site map ENTRY, as a string.
78 ENTRY is a file name. STYLE is the style of the sitemap.
79 PROJECT is the current project."
80 (cond ((not (directory-name-p entry))
81 (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
82 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
84 (org-publish-find-title entry project)))
86 ;; Return only last subdir.
87 (file-name-nondirectory (directory-file-name entry)))
90 (defun my-blog-parse-sitemap-list (l)
91 "Convert the sitemap list in to a list of filenames."
93 (let ((link (with-temp-buffer
94 (let ((org-inhibit-startup nil))
97 (goto-char (point-min))
98 (org-element-link-parser)))))
100 (plist-get (cadr link) :path))))
103 (defun my-blog-sort-article-list (l p)
104 "sort the article list anti-chronologically."
105 (sort l #'(lambda (a b)
106 (let ((d-a (org-publish-find-date a p))
107 (d-b (org-publish-find-date b p)))
108 (not (time-less-p d-a d-b))))))
110 (defun my-blog-get-preview (file)
111 "Clips a section of a post in FILE to be used as preview in the sitemap.
112 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
114 (insert-file-contents file)
115 (goto-char (point-min))
116 (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
117 (re-search-forward "^$")))
118 (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
120 (progn (goto-char (+ 1 beg))
121 (re-search-forward "^$" nil t 2)))))
122 (buffer-substring beg end))))
124 (defun my-blog-sitemap (title list)
125 "Generate the sitemap landing page for my blog."
126 (my-plain-publish-sitemap-default title list) ; Create additional sitemap
128 ;; mangle the parsed list given to us into a plain lisp list of files
129 (let* ((filenames (my-blog-parse-sitemap-list list))
130 (project-plist (assoc "posts" org-publish-project-alist))
131 (articles (my-blog-sort-article-list filenames project-plist)))
132 (dolist (file filenames)
133 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
134 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
135 (title (org-publish-find-title file project-plist))
136 (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
137 (preview (my-blog-get-preview abspath))
139 ;; insert a horizontal line before every post, kill the first one
141 (insert (concat "* [[file:" relpath "][" title "]]\n"))
142 ;; add properties for `ox-rss.el' here
143 (let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
144 (rss-pubdate (format-time-string (cdr org-time-stamp-formats) (org-publish-find-date file project-plist))))
146 (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
147 (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
148 (org-set-property "RSS_PERMALINK" rss-permalink)
149 (org-set-property "PUBDATE" rss-pubdate)
150 (org-set-property "RSS_TITLE" title))
151 ;; insert the date, preview, & read more link
152 (insert "#+ATTR_HTML: :class card-header\n")
153 (insert (concat "Published: " date "\n\n"))
154 (insert "#+BEGIN_export html\n")
155 (insert "<section class=\"card-body\">\n")
156 (insert "#+END_export\n")
158 ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
160 (insert (concat "[[file:" relpath "][Read More...]]\n"))
161 (insert "#+BEGIN_export html\n")
162 (insert "<\/section>\n")
163 (insert "#+END_export\n")
165 ;; kill the first hrule to make this look OK
166 ;(goto-char (point-min))
167 ;(let ((kill-whole-line t)) (kill-line))
168 ;; insert a title and save
169 (insert "#+OPTIONS: title:nil\n")
170 (insert "#+TITLE: Frederik Vanrenterghem's blog\n")
171 (insert "#+AUTHOR: Frederik Vanrenterghem\n")
172 (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
173 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
174 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
178 (defun my-plain-publish-sitemap-default (title list)
179 "Create a simple site map, as a string.
180 TITLE is the title of the site map. LIST is an internal
181 representation for the files to include, as returned by
183 (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
184 (let* ((filenames (my-blog-parse-sitemap-list list))
185 (project-plist (assoc "posts" org-publish-project-alist))
186 (articles (my-blog-sort-article-list filenames project-plist)))
187 (dolist (file filenames)
188 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
189 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
190 (title (org-publish-find-title file project-plist))
191 (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
192 (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
193 (goto-char (point-min))
194 (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
196 (defun my-blog-cleanup-sitemaps (plist)
197 "Clean up temporary files created in the process of publishing"
198 ;; Create a body-only version of the tags index.
199 (with-temp-file (file-name-concat
200 (plist-get (cdr (assoc "posts" org-publish-project-alist)) :publishing-directory)
201 "tag-index-body.html")
202 (insert-file-contents (file-name-concat
203 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)
205 (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil))
206 (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
207 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
208 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org~")))
210 (setq org-publish-project-alist
212 :base-directory ,my-blog-source-folder
213 :base-extension "org"
214 :publishing-directory ,my-blog-target-folder
215 :publishing-function org-mustache-html-publish-to-html
216 :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
221 :html-content-class nil
222 :html-head-include-default-style nil
230 :base-directory ,my-blog-posts-folder
231 :base-extension "org"
232 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
233 :publishing-function org-mustache-html-publish-to-html
234 :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
235 :preparation-function my-blog-create-tags-files
236 :exclude "html*\\|assets*\\|index.org\\|sitemap.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
237 :html-content-class nil
241 :sitemap-title "All posts"
242 :html-head-include-default-style nil
249 :sitemap-sort-folders ignore-errors
250 :sitemap-function my-blog-sitemap
251 :sitemap-sort-files anti-chronologically
252 :sitemap-filename "sitemap.org"
256 :base-directory ,(file-name-concat my-blog-source-folder "tags")
257 :base-extension "org"
258 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
259 :publishing-function org-mustache-html-publish-to-html
260 :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
261 :html-content-class nil
265 :html-head-include-default-style nil
275 :base-directory ,(file-name-concat my-blog-source-folder "assets")
278 :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
279 :publishing-function org-publish-attachment
280 :completion-function my-blog-cleanup-sitemaps
284 :base-directory ,my-blog-posts-folder
285 :base-extension "org"
286 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
287 :publishing-function org-rss-publish-to-rss
289 :title "Frederik Vanrenterghem's blog"
290 :html-link-home ,my-blog-target-url
291 :html-link-use-abs-url t
294 :include ("sitemap.org")
295 :table-of-contents nil
298 ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
300 (org-publish-initialize-cache "website")