]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - maak-website.el
Geef duidelijkere conclusie na het experiment.
[www2.vanrenterghem.biz.git] / maak-website.el
1 (require 'cl-lib)
2 (require 'org)
3 (require 'ox-publish)
4 (let ((default-directory "~/.emacs.d/elpa/"))
5   (normal-top-level-add-subdirs-to-load-path))
6 (require 'mustache)
7 (require 's)
8 (require 'dash)
9 (require 'f)
10 (require 'htmlize)
11 (require 'ox-rss)
12 (load "~/.emacs.d/lisp/mustache-html.el")
14 (setq org-html-doctype "html5")
15 (setq org-html-head-include-default-style nil)
16 (setq org-html-htmlize-output-type 'css) ; default: 'inline-css
17 (setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M"))
18 (setq org-display-custom-times t)
19 (setq org-html-container-element "div") ;; TODO - check
20 (setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz")
21 (setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source")
22 (setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target")
23 (setq my-blog-target-url "https://www.vanrenterghem.biz/posts/")
24 (setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html"))
25 (setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags"))
26 (setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts"))
28 (setq org-export-time-stamp-file nil)
29 (setq org-rss-use-entry-url-as-guid t)
30 (setq make-backup-files nil)
32 (defun my-org-get-all-filetags ()
33   "Get list of filetags from all org-files in my-blog-posts-folder."
34   (let ((files (directory-files my-blog-posts-folder t nil nil nil))
35         tagslist x)
36     (save-window-excursion
37       (while (setq x (pop files))
38         (set-buffer (find-file-noselect x))
39         (mapc
40          (lambda (y)
41            (let ((tagfiles (assoc y tagslist)))
42              (if tagfiles
43                  (setcdr tagfiles (cons x (cdr tagfiles)))
44                (add-to-list 'tagslist (list y x)))))
45          (my-org-get-filetags)))
46       tagslist)))
48 (defun my-org-get-filetags ()
49   "Get list of filetags for current buffer"
50   (let ((ftags org-file-tags)
51         x)
52     (mapcar
53      (lambda (x)
54        (org-no-properties x))
55      ftags)))
57 (defun my-blog-create-tags-files (plist)
58   "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder."
59   (let* ((tagfolder (file-name-concat
60                     (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)))
61          (postfolder (file-name-concat
62                     (plist-get (cdr (assoc "posts" org-publish-project-alist)) :base-directory)))
63          (relpostfolder (file-relative-name  postfolder tagfolder)))
64     (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder))
65     (with-temp-file (file-name-concat tagfolder "tag-index.org")
66       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n"))
67       (insert (concat "#+TITLE: Blog - All tags\n"))
68       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
69       (insert "\n")
70       (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) ))
71         (insert (concat "- [[file:" (file-name-concat relpostfolder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n"))))) 
72   (dolist (tag (my-org-get-all-filetags))
73     (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org"))
74       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n"))
75       (insert (concat "#+TITLE: " (car tag) "\n"))
76       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
77       (insert "\n")
78       (insert (concat "# " (car tag) "\n\n"))
79       (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist))
80         (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used
81           (message (concat "Processing " tagfile))
82           (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
83                  (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist))
84                  relpath
85                  (org-publish-find-title tagfile plist))
86                           "\n")))))))
87                                         ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n")))))))
89 (defun my-org-publish-sitemap-default-entry (entry style project)
90   "My format for site map ENTRY, as a string.
91 ENTRY is a file name.  STYLE is the style of the sitemap.
92 PROJECT is the current project."
93   (cond ((not (directory-name-p entry))
94          (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry
95                  (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project))
96                  entry
97                  (org-publish-find-title entry project)))
98         ((eq style 'tree)
99          ;; Return only last subdir.
100          (file-name-nondirectory (directory-file-name entry)))
101         (t entry)))
103 (defun my-blog-parse-sitemap-list (l)
104   "Convert the sitemap list in to a list of filenames."
105   (mapcar #'(lambda (i)
106               (let ((link (with-temp-buffer
107                             (let ((org-inhibit-startup nil))
108                               (insert (car i))
109                               (org-mode)
110                               (goto-char (point-min))
111                               (org-element-link-parser)))))
112                 (when link
113                   (plist-get (cadr link) :path))))
114           (cdr l)))
116 (defun my-blog-sort-article-list (l p)
117   "sort the article list anti-chronologically."
118   (sort l #'(lambda (a b)
119               (let ((d-a (org-publish-find-date a p))
120                     (d-b (org-publish-find-date b p)))
121                 (not (time-less-p d-a d-b))))))
123 (defun my-blog-get-preview (file)
124   "Clips a section of a post in FILE to be used as preview in the sitemap.
125 Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines."
126   (with-temp-buffer
127     (insert-file-contents file)
128     (goto-char (point-min))
129     (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1)
130                     (re-search-forward "^$")))
131            (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1)
132                         (match-beginning 0))
133                     (progn (goto-char (+ 1 beg))
134                            (re-search-forward "^$" nil t 2)))))
135       (buffer-substring beg end))))
137 (defun my-blog-sitemap (title list)
138   "Generate the sitemap landing page for my blog."
139   (my-plain-publish-sitemap-default title list) ; Create additional sitemap
140   (with-temp-buffer
141     ;; mangle the parsed list given to us into a plain lisp list of files
142     (let* ((filenames (my-blog-parse-sitemap-list list))
143            (project-plist (assoc "posts" org-publish-project-alist))
144            (articles (my-blog-sort-article-list filenames project-plist)))
145       (dolist (file filenames)
146         (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
147                (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/"))
148                (title (org-publish-find-title file project-plist))
149                (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))
150                (preview (my-blog-get-preview abspath))
151                )
152           (insert (concat "* [[file:" relpath "][" title "]]\n"))
153           (org-mode)
154           (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2
155           (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none")
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")
162           (insert preview)
163           (insert "\n")
164           (insert (concat "[[file:" relpath "][Read More...]]\n"))
165           (insert "#+BEGIN_export html\n")
166           (insert "</section> <!-- END CARD-BODY-->\n")
167           (insert "#+END_export\n")
168           ))
169       ;; insert a title and save
170       (insert "#+OPTIONS: title:nil\n")
171       (insert "#+TITLE: Frederik Vanrenterghem's blog\n")
172       (insert "#+AUTHOR: Frederik Vanrenterghem\n")
173       (insert "#+EMAIL: frederik@vanrenterghem.biz\n")
174       (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript
175       (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n"))
176       (insert "\n")
177       (buffer-string))))
179 (defun my-plain-publish-sitemap-default (title list)
180   "Create a simple site map, as a string.
181 TITLE is the title of the site map.  LIST is an internal
182 representation for the files to include, as returned by
183 `org-list-to-lisp'."
184   (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org"
185     (let* ((filenames (my-blog-parse-sitemap-list list))
186            (project-plist (assoc "posts" org-publish-project-alist))
187            (articles (my-blog-sort-article-list filenames project-plist)))
188       (dolist (file filenames)
189         (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file))
190                (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/"))
191                (title (org-publish-find-title file project-plist))
192                (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist))))
193           (insert (concat "* " date " - [[file:" relpath "][" title "]]\n"))))
194       (goto-char (point-min))
195       (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript
197 (defun my-blog-cleanup-sitemaps (plist)
198   "Clean up temporary files created in the process of publishing"
199   ;; Create a body-only version of the tags index. Needs absolute path to posts folder.
200   (with-temp-file (file-name-concat
201                    (plist-get (cdr (assoc "landing" org-publish-project-alist)) :publishing-directory)
202                    "tag-index-body.html")
203     (insert-file-contents (file-name-concat
204                            (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory)
205                            "tag-index.org"))
206     (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil)
207     (replace-string "href=\"posts" "href=\"/posts"))
208   ;; Delete the sitemap and rss files created so they don't get picked up as original files in future publish actions.
209   (delete-file (file-name-concat my-blog-source-folder "sitemap.org"))
210   (delete-file (file-name-concat my-blog-posts-folder "sitemap.org"))
211   (delete-file (file-name-concat my-blog-posts-folder "rss.org")))
213 ;; Define some custom functions to create the RSS feed. Main reason is the website has "read more" functionality,
214 ;; which is not nice to have in RSS.
216 (defun fv/format-rss-feed (title list)
217   "Generate RSS feed, as a string.
218 TITLE is the title of the RSS feed.  LIST is an internal
219 representation for the files to include, as returned by
220 `org-list-to-lisp'.  PROJECT is the current project."
221   (concat "#+TITLE: " title "\n"
222           "#+AUTHOR: Frederik Vanrenterghem\n"
223           "#+EMAIL: frederik@vanrenterghem.biz\n"
224           "#+OPTIONS: ^:nil\n" "\n"
225           (org-list-to-subtree list 1 '(:icount "" :istart ""))))
227 (defun fv/format-rss-feed-entry (entry style project)
228   "Format ENTRY for the RSS feed.
229 ENTRY is a file name.  STYLE is either 'list' or 'tree'.
230 PROJECT is the current project."
231   (cond ((not (directory-name-p entry))
232          (let* ((file (org-publish--expand-file-name entry project))
233                 (title (org-publish-find-title entry project))
234                 (date (format-time-string "%Y-%m-%d %H:%M" (org-publish-find-date entry project)))
235                 (link (concat (file-name-sans-extension entry) ".html"))
236                 (tags (cadar (with-temp-buffer
237                                (org-mode)
238                                (insert-file-contents file)
239                                (org-collect-keywords '("FILETAGS"))))))
240            (with-temp-buffer
241              (org-mode)
242              (insert (format "* [[file:%s][%s]]  %s\n" file title tags))
243              ;; add properties for `ox-rss.el' here
244              (org-set-property "RSS_PERMALINK" link)
245              (org-set-property "PUBDATE" date)
246              (org-set-property "RSS_TITLE" title)
247              ;; We simply chuck in the entire file.
248              ;; This assumes the file doesn't for instance start with a PROPERTIES drawer.
249              (insert-file-contents file)
250              ;; Ensure the list doesn't accidentally end due to blank lines at the end of
251              ;; entries. Still leaves problems when the entry has a double blank line in it
252              ;; as that ends an org list.
253              (goto-char (point-max))
254              (delete-blank-lines)
255              (buffer-string))))
256         ((eq style 'tree)
257          ;; Return only last subdir.
258          (file-name-nondirectory (directory-file-name entry)))
259         (t entry)))
261 (setq org-publish-project-alist
262       `(("landing"
263          :base-directory ,my-blog-source-folder
264          :base-extension "org"
265          :publishing-directory ,my-blog-target-folder
266          :publishing-function org-mustache-html-publish-to-html
267          :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache")
268          :headline-levels 2
269          :section-numbers nil
270          :with-toc nil
271          :with-title nil
272          :html-content-class nil
273          :html-head-include-default-style nil
274          :html-head nil
275          :html-divs nil
276          :html-preamble nil
277          :html-postamble nil
278          )
280         ("posts"
281          :base-directory ,my-blog-posts-folder
282          :base-extension "org"
283          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
284          :publishing-function org-mustache-html-publish-to-html
285          :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache")
286          :preparation-function my-blog-create-tags-files
287          :exclude "html*\\|assets*\\|rss.org\\|index.org\\|sitemap.org\\|.org~"
288          :html-content-class nil
289          :section-numbers nil
290          :with-toc nil
291          :with-title nil
292          :sitemap-title "All posts"
293          :html-head-include-default-style nil
294          :html-head nil
295          :html-divs nil
296          :recursive t
297          :auto-sitemap t
298          :html-preamble nil
299          :html-postamble nil
300          :sitemap-sort-folders ignore-errors
301          :sitemap-function my-blog-sitemap
302          :sitemap-sort-files anti-chronologically
303          :sitemap-filename "sitemap.org"
304          )
306         ("tags"
307          :base-directory ,(file-name-concat my-blog-source-folder "tags")
308          :base-extension "org"
309          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
310          :publishing-function org-mustache-html-publish-to-html
311          :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache")
312          :html-content-class nil
313          :section-numbers nil
314          :with-toc nil
315          :with-title nil
316          :html-head-include-default-style nil
317          :html-head nil
318          :html-divs nil
319          :recursive nil
320          :auto-sitemap nil
321          :html-preamble nil
322          :html-postamble nil
323          )
325         ("assets"
326          :base-directory ,(file-name-concat my-blog-source-folder "assets")
327          :base-extension any
328          :recursive t
329          :publishing-directory ,(file-name-concat my-blog-target-folder "assets")
330          :publishing-function org-publish-attachment
331          :completion-function my-blog-cleanup-sitemaps
332          )
333         
334         ("rss"
335          :base-directory ,my-blog-posts-folder
336          :base-extension "org"
337          :recursive nil
338          :exclude ,(regexp-opt '("rss.org" "index.org" "sitemap.org"))
339          :publishing-directory ,(file-name-concat my-blog-target-folder "posts")
340          :publishing-function org-rss-publish-to-rss
341          :with-author t
342          :title "Frederik Vanrenterghem's blog"
343          :html-link-home ,my-blog-target-url
344          :html-link-use-abs-url t
345          :html-link-org-files-as-html t
346          :section-numbers nil
347          :exclude ".*"
348          :include ("rss.org")
349          :table-of-contents nil
350          :auto-sitemap t
351          :sitemap-filename "rss.org"
352          :sitemap-title "Frederik Vanrenterghem's blog"
353          :sitemap-style list
354          :sitemap-sort-files anti-chronologically
355          :sitemap-function fv/format-rss-feed
356          :sitemap-format-entry fv/format-rss-feed-entry
357          )
358         
359         ("website" :components ("posts" "rss" "tags" "landing" "assets"))))
361 (org-publish-initialize-cache "website")