X-Git-Url: http://git.vanrenterghem.biz/www2.vanrenterghem.biz.git/blobdiff_plain/fc9fb1e406c4f9b75e074a9fbc34bd0e526448d4..4f561115943ffa0cb68579e3276f8b599c38eb4f:/maak-website.el?ds=sidebyside diff --git a/maak-website.el b/maak-website.el index 1bb4897..c93f1cc 100644 --- a/maak-website.el +++ b/maak-website.el @@ -1,41 +1,88 @@ +(require 'cl-lib) +(require 'org) (require 'ox-publish) +(add-to-list 'load-path "~/.emacs.d/elpa/ox-rss-20230408.231") +(add-to-list 'load-path "~/.emacs.d/elpa/mustache-20230713.514") +(add-to-list 'load-path "~/.emacs.d/elpa/s-20220902.1511") +(add-to-list 'load-path "~/.emacs.d/elpa/dash-20240510.1327") +(add-to-list 'load-path "~/.emacs.d/elpa/f-20240308.906 " ) +(add-to-list 'load-path "~/.emacs.d/elpa/htmlize-20240527.1456") +(require 'ox-rss) +(load "~/.emacs.d/lisp/mustache-html.el") (setq org-html-doctype "html5") -(setq org-html-divs '((preamble "header" "top") - (content "main" "content") - (postamble "footer" "postamble")) - org-html-container-element "section") (setq org-html-head-include-default-style nil) -;(setq org-html-htmlize-output-type 'css) ; default: 'inline-css +(setq org-html-htmlize-output-type 'css) ; default: 'inline-css +(setq org-time-stamp-custom-formats '("%A %e %B %Y" . "%A %e %B %Y at %H:%M")) +(setq org-display-custom-times t) +(setq org-html-container-element "div") ;; TODO - check +(setq my-blog-base-folder "~/websites/stage.vanrenterghem.biz") +(setq my-blog-source-folder "~/websites/stage.vanrenterghem.biz/source") +(setq my-blog-target-folder "~/websites/stage.vanrenterghem.biz/target") +(setq my-blog-target-url "https://www.vanrenterghem.biz/posts/") +(setq my-blog-mustache-folder (file-name-concat my-blog-base-folder "html")) +(setq my-blog-tags-folder (file-name-concat my-blog-source-folder "tags")) +(setq my-blog-posts-folder (file-name-concat my-blog-source-folder "posts")) -(defun website-header (info) - (with-temp-buffer - (insert-file-contents "~/websites/stage.vanrenterghem.biz/html/navbar.html") - (buffer-string))) +(setq org-export-time-stamp-file nil) +(setq org-rss-use-entry-url-as-guid nil) -(defun post-header (exp-plist) - (with-temp-buffer - (insert (format "" - (or (org-export-data (plist-get exp-plist ':title) exp-plist) - "none"))) - (goto-char (point-min)) - (insert-file-contents "~/websites/stage.vanrenterghem.biz/html/post-header.html") - ;(goto-char (point-max)) - ;(insert (format "
Tags: %s
" - ; (or (org-export-data (plist-get exp-plist :filetags) exp-plist) - ; "none"))) - (buffer-string))) - -(defun website-footer (info) - (with-temp-buffer - (insert-file-contents "~/websites/stage.vanrenterghem.biz/html/footer.html") - (buffer-string))) +(defun my-org-get-all-filetags () + "Get list of filetags from all org-files in my-blog-posts-folder." + (let ((files (directory-files my-blog-posts-folder t nil nil nil)) + tagslist x) + (save-window-excursion + (while (setq x (pop files)) + (set-buffer (find-file-noselect x)) + (mapc + (lambda (y) + (let ((tagfiles (assoc y tagslist))) + (if tagfiles + (setcdr tagfiles (cons x (cdr tagfiles))) + (add-to-list 'tagslist (list y x))))) + (my-org-get-filetags))) + tagslist))) + +(defun my-org-get-filetags () + "Get list of filetags for current buffer" + (let ((ftags org-file-tags) + x) + (mapcar + (lambda (x) + (org-no-properties x)) + ftags))) + +(defun my-blog-create-tags-files (plist) + "Create org files for each tag defined in FILETAGS in posts, storing them in my-blog-tags-folder." + (let* ((tagfolder (file-name-concat + (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory))) + (postfolder (file-name-concat + (plist-get (cdr (assoc "posts" org-publish-project-alist)) :base-directory))) + (relpostfolder (file-relative-name postfolder tagfolder))) + (unless (file-directory-p my-blog-tags-folder) (make-directory my-blog-tags-folder)) + (with-temp-file (file-name-concat tagfolder "tag-index.org") + (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags-index.mustache") "\n")) + (insert (concat "#+TITLE: Blog - All tags\n")) + (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript + (insert "\n") + (dolist (tag (sort (my-org-get-all-filetags) (lambda (x y) (string-lessp (car x) (car y))) )) + (insert (concat "- [[file:" (file-name-concat relpostfolder (concat "tag-" (car tag) ".org")) "][" (car tag) "]]\n"))))) + (dolist (tag (my-org-get-all-filetags)) + (with-temp-file (file-name-concat my-blog-tags-folder (concat "tag-" (car tag) ".org")) + (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "tags.mustache") "\n")) + (insert (concat "#+TITLE: " (car tag) "\n")) + (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript + (insert "\n") + (insert (concat "# " (car tag) "\n\n")) + (dolist (tagfile (my-blog-sort-article-list (cdr tag) plist)) + (let ((relpath (file-relative-name tagfile my-blog-posts-folder)));;not used + (message (concat "Processing " tagfile)) + (insert (concat "- " (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry + (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date tagfile plist)) + relpath + (org-publish-find-title tagfile plist)) + "\n"))))))) + ;(my-org-publish-sitemap-default-entry tagfile nil plist) "\n"))))))) (defun my-org-publish-sitemap-default-entry (entry style project) "My format for site map ENTRY, as a string. @@ -43,7 +90,7 @@ ENTRY is a file name. STYLE is the style of the sitemap. PROJECT is the current project." (cond ((not (directory-name-p entry)) (format "%s - [[file:%s][%s]]" ;;the date and filename are added after the entry - (format-time-string "%e %b %Y" (org-publish-find-date entry project)) + (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date entry project)) entry (org-publish-find-title entry project))) ((eq style 'tree) @@ -51,54 +98,215 @@ PROJECT is the current project." (file-name-nondirectory (directory-file-name entry))) (t entry))) +(defun my-blog-parse-sitemap-list (l) + "Convert the sitemap list in to a list of filenames." + (mapcar #'(lambda (i) + (let ((link (with-temp-buffer + (let ((org-inhibit-startup nil)) + (insert (car i)) + (org-mode) + (goto-char (point-min)) + (org-element-link-parser))))) + (when link + (plist-get (cadr link) :path)))) + (cdr l))) + +(defun my-blog-sort-article-list (l p) + "sort the article list anti-chronologically." + (sort l #'(lambda (a b) + (let ((d-a (org-publish-find-date a p)) + (d-b (org-publish-find-date b p))) + (not (time-less-p d-a d-b)))))) + +(defun my-blog-get-preview (file) + "Clips a section of a post in FILE to be used as preview in the sitemap. +Either the section between #+BEGIN_PREVIEW and +#END_PREVIEW is used, or the first section between 2 blank lines." + (with-temp-buffer + (insert-file-contents file) + (goto-char (point-min)) + (let* ((beg (or (re-search-forward "^#\\+BEGIN_PREVIEW$" nil t 1) + (re-search-forward "^$"))) + (end (or (if (re-search-forward "^#\\+END_PREVIEW$" nil t 1) + (match-beginning 0)) + (progn (goto-char (+ 1 beg)) + (re-search-forward "^$" nil t 2))))) + (buffer-substring beg end)))) + +(defun my-blog-sitemap (title list) + "Generate the sitemap landing page for my blog." + (my-plain-publish-sitemap-default title list) ; Create additional sitemap + (with-temp-buffer + ;; mangle the parsed list given to us into a plain lisp list of files + (let* ((filenames (my-blog-parse-sitemap-list list)) + (project-plist (assoc "posts" org-publish-project-alist)) + (articles (my-blog-sort-article-list filenames project-plist))) + (dolist (file filenames) + (let* ((abspath (file-name-concat "/home/frederik/websites/stage.vanrenterghem.biz/source/posts" file)) + (relpath (file-relative-name abspath "/home/frederik/websites/stage.vanrenterghem.biz/source/posts/")) + (title (org-publish-find-title file project-plist)) + (date (format-time-string (cdr org-time-stamp-custom-formats) (org-publish-find-date file project-plist))) + (preview (my-blog-get-preview abspath)) + ) + ;; insert a horizontal line before every post, kill the first one + ;; before saving + (insert (concat "* [[file:" relpath "][" title "]]\n")) + ;; add properties for `ox-rss.el' here + (let ((rss-permalink (concat (file-name-sans-extension relpath) ".html")) + (rss-pubdate (format-time-string (cdr org-time-stamp-formats) (org-publish-find-date file project-plist)))) + (org-mode) + (org-set-property "HTML_CONTAINER_CLASS" "card mb-2") ;Bootstrap margin border 2 + (org-set-property "HTML_HEADLINE_CLASS" "card-header card-title border-bottom-0 fs-5 fw-bold text-decoration-none") + (org-set-property "RSS_PERMALINK" rss-permalink) + (org-set-property "PUBDATE" rss-pubdate) + (org-set-property "RSS_TITLE" title)) + ;; insert the date, preview, & read more link + (insert "#+ATTR_HTML: :class card-header\n") + (insert (concat "Published: " date "\n\n")) + (insert "#+BEGIN_export html\n") + (insert "