From f84c14ffa0a997b92b07de2f3b87951012804a6c Mon Sep 17 00:00:00 2001 From: Frederik Vanrenterghem Date: Sat, 9 Dec 2023 14:37:34 +0800 Subject: [PATCH 1/1] Voeg eerste werkbare Mustache versie toe. --- html/footer.html | 0 html/footer.mustache | 1 + html/{navbar.html => navbar.mustache} | 0 maak-website.el | 163 +++++++++++++++++--------- 4 files changed, 111 insertions(+), 53 deletions(-) delete mode 100644 html/footer.html create mode 100644 html/footer.mustache rename html/{navbar.html => navbar.mustache} (100%) diff --git a/html/footer.html b/html/footer.html deleted file mode 100644 index e69de29..0000000 diff --git a/html/footer.mustache b/html/footer.mustache new file mode 100644 index 0000000..3642471 --- /dev/null +++ b/html/footer.mustache @@ -0,0 +1 @@ + diff --git a/html/navbar.html b/html/navbar.mustache similarity index 100% rename from html/navbar.html rename to html/navbar.mustache diff --git a/maak-website.el b/maak-website.el index 1bb4897..f20dc4b 100644 --- a/maak-website.el +++ b/maak-website.el @@ -1,41 +1,9 @@ (require 'ox-publish) (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 -(defun website-header (info) - (with-temp-buffer - (insert-file-contents "~/websites/stage.vanrenterghem.biz/html/navbar.html") - (buffer-string))) - -(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-publish-sitemap-default-entry (entry style project) "My format for site map ENTRY, as a string. @@ -51,47 +19,136 @@ 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) + (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/")) + (title (org-publish-find-title file project-plist)) + (date (format-time-string (car org-time-stamp-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 "-----\n") + (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 date)) + ; (org-set-property "RSS_PERMALINK" rss-permalink) + ; (org-set-property "PUBDATE" rss-pubdate)) + ;; insert the date, preview, & read more link + (insert (concat "Published: " date "\n\n")) + (insert preview) + ;(insert (concat "#+INCLUDE: \"" relpath "\" :only-contents t :lines \"1-10\"\n")) + (insert "\n") + (insert (concat "[[file:" relpath "][Read More...]]\n")))) + ;; kill the first hrule to make this look OK + (goto-char (point-min)) + (let ((kill-whole-line t)) (kill-line)) + ;; insert a title and save + (insert "#+OPTIONS: title:nil\n") + (insert "#+TITLE: Blog\n") + (insert "#+AUTHOR: Frederik Vanrenterghem\n") + (insert "#+EMAIL: frederik@vanrenterghem.biz\n") + (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript + (insert "#+mustache-template: ~/websites/stage.vanrenterghem.biz/html/post-index.mustache\n") + (insert "\n") + (buffer-string)))) + +(defun my-plain-publish-sitemap-default (title list) + "Default site map, as a string. +TITLE is the title of the site map. LIST is an internal +representation for the files to include, as returned by +`org-list-to-lisp'. PROJECT is the current project." + (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org" + (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript + (insert (concat "#+TITLE: " title "\n\n")) + (insert (org-list-to-org list)))) + (setq org-publish-project-alist - '(("pages" + '(("landing" :base-directory "~/websites/stage.vanrenterghem.biz/source/" :base-extension "org" + :include ("posts/sitemap.org") :publishing-directory "~/websites/stage.vanrenterghem.biz/target" - :publishing-function org-html-publish-to-html + :publishing-function org-mustache-html-publish-to-html + :mustache-template "~/websites/stage.vanrenterghem.biz/html/landing.mustache" :headline-levels 3 :section-numbers nil :with-toc nil :html-head-include-default-style nil - :html-head "" - :html-preamble t - :html-preamble website-header - :html-postamble website-footer + :html-head nil + :html-divs nil + :html-preamble nil + :html-postamble nil ) ("posts" :base-directory "~/websites/stage.vanrenterghem.biz/source/posts/" :base-extension "org" - :publishing-directory "~/websites/stage.vanrenterghem.biz/target" - :publishing-function org-html-publish-to-html - :exclude "assets*\\|sitemap.org\\|index.org" ;; regexp - :html-content-class "container content inlinepage card h-entry" - :headline-levels 3 + :publishing-directory "~/websites/stage.vanrenterghem.biz/target/posts" + :publishing-function org-mustache-html-publish-to-html + :mustache-template "~/websites/stage.vanrenterghem.biz/html/post.mustache" + :exclude "assets*\\|index.org" ;"assets*\\|sitemap.org\\|index.org" ;; regexp + :html-content-class nil :section-numbers nil :with-toc nil :with-title nil + :sitemap-title "All posts" :html-head-include-default-style nil - :html-head "\n - " - :html-preamble t - :html-divs ((preamble "header" "preamble") - (content "article" "inlinepage card h-entry") - (postamble "footer" "postamble")) + :html-head nil + :html-divs nil :recursive t :auto-sitemap t - :html-preamble post-header - :html-postamble website-footer + :html-preamble nil + :html-postamble nil :sitemap-sort-folders ignore-errors - :sitemap-format-entry my-org-publish-sitemap-default-entry + :sitemap-function my-blog-sitemap + ;:sitemap-function org-publish-sitemap-default + ;:sitemap-format-entry my-org-publish-sitemap-default-entry :sitemap-sort-files anti-chronologically + :sitemap-filename "sitemap.org" ) ("assets" @@ -101,4 +158,4 @@ PROJECT is the current project." :publishing-directory "~/websites/stage.vanrenterghem.biz/target/assets/" :publishing-function org-publish-attachment) - ("website" :components ("posts" "pages" "assets")))) + ("website" :components ("posts" "landing" "assets")))) -- 2.39.5