4 (add-to-list 'load-path "~/.emacs.d/elpa/ox-rss-20230408.231")
5 (add-to-list 'load-path "~/.emacs.d/elpa/mustache-20230713.514")
6 (add-to-list 'load-path "~/.emacs.d/elpa/s-20220902.1511")
7 (add-to-list 'load-path "~/.emacs.d/elpa/dash-20240510.1327")
8 (add-to-list 'load-path "~/.emacs.d/elpa/f-20240308.906 " )
9 (add-to-list 'load-path "~/.emacs.d/elpa/htmlize-20240527.1456")
11 (load "~/.emacs.d/lisp/mustache-html.el")
13 (setq org-html-doctype "html5")
14 (setq org-html-head-include-default-style nil)
15 (setq org-html-htmlize-output-type 'css) ; default: 'inline-css
16 (setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M"))
17 (setq org-display-custom-times t)
18 (setq org-html-container-element "div") ;; TODO - check
19 (setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz")
20 (setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source")
21 (setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target")
22 (setq my-blog-target-url "https://www.vanrenterghem.biz/posts/")
23 (setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html"))
24 (setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags"))
25 (setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts"))
27 (setq org-export-time-stamp-file nil)
28 (setq org-rss-use-entry-url-as-guid nil)
30 (defun my-org-get-all-filetags ()
31 "Get list of filetags from all org-files in my-blog-posts-folder."
32 (let ((files (directory-files my-blog-posts-folder t nil nil nil))
34 (save-window-excursion
35 (while (setq x (pop files))
36 (set-buffer (find-file-noselect x))
39 (let ((tagfiles (assoc y tagslist)))
41 (setcdr tagfiles (cons x (cdr tagfiles)))
42 (add-to-list 'tagslist (list y x)))))
43 (my-org-get-filetags)))
46 (defun my-org-get-filetags ()
47 "Get list of filetags for current buffer"
48 (let ((ftags org-file-tags)
52 (org-no-properties x))
55 (defun my-blog-create-tags-files (plist)
56 "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder."
57 (let* ((tagfolder (file-name-concat
58 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)))
59 (postfolder (file-name-concat
60 (plist-get (cdr (assoc "posts" org-publish-project-alist)) :base-directory)))
61 (relpostfolder (file-relative-name postfolder tagfolder)))
62 (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
63 (with-temp-file (file-name-concat tagfolder "tag-index.org")
64 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
65 (insert (concat "#+TITLE: Blog - All tags\n"))
66 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
68 (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) ))
69 (insert (concat "- [[file:" (file-name-concat relpostfolder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n")))))
70 (dolist (tag (my-org-get-all-filetags))
71 (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
72 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
73 (insert (concat "#+TITLE: " (car tag) "\n"))
74 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
76 (insert (concat "# " (car tag) "\n\n"))
77 (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist))
78 (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
79 (message (concat "Processing " tagfile))
80 (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
81 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
83 (org-publish-find-title tagfile plist))
85 ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
87 (defun my-org-publish-sitemap-default-entry (entry style project)
88 "My format for site map ENTRY, as a string.
89 ENTRY is a file name. STYLE is the style of the sitemap.
90 PROJECT is the current project."
91 (cond ((not (directory-name-p entry))
92 (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
93 (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
95 (org-publish-find-title entry project)))
97 ;; Return only last subdir.
98 (file-name-nondirectory (directory-file-name entry)))
101 (defun my-blog-parse-sitemap-list (l)
102 "Convert the sitemap list in to a list of filenames."
103 (mapcar #'(lambda (i)
104 (let ((link (with-temp-buffer
105 (let ((org-inhibit-startup nil))
108 (goto-char (point-min))
109 (org-element-link-parser)))))
111 (plist-get (cadr link) :path))))
114 (defun my-blog-sort-article-list (l p)
115 "sort the article list anti-chronologically."
116 (sort l #'(lambda (a b)
117 (let ((d-a (org-publish-find-date a p))
118 (d-b (org-publish-find-date b p)))
119 (not (time-less-p d-a d-b))))))
121 (defun my-blog-get-preview (file)
122 "Clips a section of a post in FILE to be used as preview in the sitemap.
123 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
125 (insert-file-contents file)
126 (goto-char (point-min))
127 (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
128 (re-search-forward "^$")))
129 (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
131 (progn (goto-char (+ 1 beg))
132 (re-search-forward "^$" nil t 2)))))
133 (buffer-substring beg end))))
135 (defun my-blog-sitemap (title list)
136 "Generate the sitemap landing page for my blog."
137 (my-plain-publish-sitemap-default title list) ; Create additional sitemap
139 ;; mangle the parsed list given to us into a plain lisp list of files
140 (let* ((filenames (my-blog-parse-sitemap-list list))
141 (project-plist (assoc "posts" org-publish-project-alist))
142 (articles (my-blog-sort-article-list filenames project-plist)))
143 (dolist (file filenames)
144 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
145 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
146 (title (org-publish-find-title file project-plist))
147 (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
148 (preview (my-blog-get-preview abspath))
150 ;; insert a horizontal line before every post, kill the first one
152 (insert (concat "* [[file:" relpath "][" title "]]\n"))
153 ;; add properties for `ox-rss.el' here
154 (let ((rss-permalink (concat (file-name-sans-extension relpath) ".html"))
155 (rss-pubdate (format-time-string (cdr org-time-stamp-formats) (org-publish-find-date file project-plist))))
157 (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
158 (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
159 (org-set-property "RSS_PERMALINK" rss-permalink)
160 (org-set-property "PUBDATE" rss-pubdate)
161 (org-set-property "RSS_TITLE" title))
162 ;; insert the date, preview, & read more link
163 (insert "#+ATTR_HTML: :class card-header\n")
164 (insert (concat "Published: " date "\n\n"))
165 (insert "#+BEGIN_export html\n")
166 (insert "<section class=\"card-body\">\n")
167 (insert "#+END_export\n")
169 ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n"))
171 (insert (concat "[[file:" relpath "][Read More...]]\n"))
172 (insert "#+BEGIN_export html\n")
173 (insert "</section> <!-- END CARD-BODY-->\n")
174 (insert "#+END_export\n")
176 ;; kill the first hrule to make this look OK
177 ;(goto-char (point-min))
178 ;(let ((kill-whole-line t)) (kill-line))
179 ;; insert a title and save
180 (insert "#+OPTIONS: title:nil\n")
181 (insert "#+TITLE: Frederik Vanrenterghem's blog\n")
182 (insert "#+AUTHOR: Frederik Vanrenterghem\n")
183 (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
184 (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
185 (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
189 (defun my-plain-publish-sitemap-default (title list)
190 "Create a simple site map, as a string.
191 TITLE is the title of the site map. LIST is an internal
192 representation for the files to include, as returned by
194 (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
195 (let* ((filenames (my-blog-parse-sitemap-list list))
196 (project-plist (assoc "posts" org-publish-project-alist))
197 (articles (my-blog-sort-article-list filenames project-plist)))
198 (dolist (file filenames)
199 (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
200 (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
201 (title (org-publish-find-title file project-plist))
202 (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
203 (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
204 (goto-char (point-min))
205 (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
207 (defun my-blog-cleanup-sitemaps (plist)
208 "Clean up temporary files created in the process of publishing"
209 ;; Create a body-only version of the tags index. Needs absolute path to posts folder.
210 (with-temp-file (file-name-concat
211 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :publishing-directory)
212 "tag-index-body.html")
213 (insert-file-contents (file-name-concat
214 (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)
216 (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil)
217 (replace-string "href=\"posts" "href=\"/posts"))
218 (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
219 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
220 (delete-file (file-name-concat my-blog-posts-folder "sitemap.org~")))
222 (setq org-publish-project-alist
224 :base-directory ,my-blog-source-folder
225 :base-extension "org"
226 :publishing-directory ,my-blog-target-folder
227 :publishing-function org-mustache-html-publish-to-html
228 :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
233 :html-content-class nil
234 :html-head-include-default-style nil
242 :base-directory ,my-blog-posts-folder
243 :base-extension "org"
244 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
245 :publishing-function org-mustache-html-publish-to-html
246 :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
247 :preparation-function my-blog-create-tags-files
248 :exclude "html*\\|assets*\\|index.org\\|sitemap.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp
249 :html-content-class nil
253 :sitemap-title "All posts"
254 :html-head-include-default-style nil
261 :sitemap-sort-folders ignore-errors
262 :sitemap-function my-blog-sitemap
263 :sitemap-sort-files anti-chronologically
264 :sitemap-filename "sitemap.org"
268 :base-directory ,(file-name-concat my-blog-source-folder "tags")
269 :base-extension "org"
270 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
271 :publishing-function org-mustache-html-publish-to-html
272 :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
273 :html-content-class nil
277 :html-head-include-default-style nil
287 :base-directory ,(file-name-concat my-blog-source-folder "assets")
290 :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
291 :publishing-function org-publish-attachment
292 :completion-function my-blog-cleanup-sitemaps
296 :base-directory ,my-blog-posts-folder
297 :base-extension "org"
298 :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
299 :publishing-function org-rss-publish-to-rss
301 :title "Frederik Vanrenterghem's blog"
302 :html-link-home ,my-blog-target-url
303 :html-link-use-abs-url t
306 :include ("sitemap.org")
307 :table-of-contents nil
310 ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
312 (org-publish-initialize-cache "website")