(require 'cl-lib) (require 'org) (require 'ox-publish) (let ((default-directory "~/.emacs.d/elpa/")) (normal-top-level-add-subdirs-to-load-path)) (require 'mustache) (require 's) (require 'dash) (require 'f) (require 'htmlize) (require 'ox-rss) (load "~/.emacs.d/lisp/mustache-html.el") (setq org-html-doctype "html5") (setq org-html-head-include-default-style nil) (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")) (setq org-export-time-stamp-file nil) (setq org-rss-use-entry-url-as-guid t) (setq make-backup-files nil) (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. 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 (car org-time-stamp-custom-formats) (org-publish-find-date entry project)) entry (org-publish-find-title entry project))) ((eq style 'tree) ;; Return only last subdir. (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 (concat "* [[file:" relpath "][" title "]]\n")) (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") ;; 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 "
\n") (insert "#+END_export\n") (insert preview) (insert "\n") (insert (concat "[[file:" relpath "][Read More...]]\n")) (insert "#+BEGIN_export html\n") (insert "
\n") (insert "#+END_export\n") )) ;; insert a title and save (insert "#+OPTIONS: title:nil\n") (insert "#+TITLE: Frederik Vanrenterghem's blog\n") (insert "#+AUTHOR: Frederik Vanrenterghem\n") (insert "#+EMAIL: frederik@vanrenterghem.biz\n") (insert "#+OPTIONS: ^:nil\n") ; do not use underscores as subscript (insert (concat "#+mustache-template: " (file-name-concat my-blog-mustache-folder "post-index.mustache") "\n")) (insert "\n") (buffer-string)))) (defun my-plain-publish-sitemap-default (title list) "Create a simple 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'." (with-temp-file "~/websites/stage.vanrenterghem.biz/source/sitemap.org" (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/")) (title (org-publish-find-title file project-plist)) (date (format-time-string (car org-time-stamp-custom-formats) (org-publish-find-date file project-plist)))) (insert (concat "* " date " - [[file:" relpath "][" title "]]\n")))) (goto-char (point-min)) (insert "#+OPTIONS: ^:nil\n")))) ; do not use underscores as subscript (defun my-blog-cleanup-sitemaps (plist) "Clean up temporary files created in the process of publishing" ;; Create a body-only version of the tags index. Needs absolute path to posts folder. (with-temp-file (file-name-concat (plist-get (cdr (assoc "landing" org-publish-project-alist)) :publishing-directory) "tag-index-body.html") (insert-file-contents (file-name-concat (plist-get (cdr (assoc "landing" org-publish-project-alist)) :base-directory) "tag-index.org")) (org-export-to-buffer 'mustache-html (current-buffer) nil nil nil t nil) (replace-string "href=\"posts" "href=\"/posts")) ;; Delete the sitemap and rss files created so they don't get picked up as original files in future publish actions. (delete-file (file-name-concat my-blog-source-folder "sitemap.org")) (delete-file (file-name-concat my-blog-posts-folder "sitemap.org")) (delete-file (file-name-concat my-blog-posts-folder "rss.org"))) ;; Define some custom functions to create the RSS feed. Main reason is the website has "read more" functionality, ;; which is not nice to have in RSS. (defun fv/format-rss-feed (title list) "Generate RSS feed, as a string. TITLE is the title of the RSS feed. LIST is an internal representation for the files to include, as returned by `org-list-to-lisp'. PROJECT is the current project." (concat "#+TITLE: " title "\n" "#+AUTHOR: Frederik Vanrenterghem\n" "#+EMAIL: frederik@vanrenterghem.biz\n" "#+OPTIONS: ^:nil\n" "\n" (org-list-to-subtree list 1 '(:icount "" :istart "")))) (defun fv/format-rss-feed-entry (entry style project) "Format ENTRY for the RSS feed. ENTRY is a file name. STYLE is either 'list' or 'tree'. PROJECT is the current project." (cond ((not (directory-name-p entry)) (let* ((file (org-publish--expand-file-name entry project)) (title (org-publish-find-title entry project)) (date (format-time-string "%Y-%m-%d %H:%M" (org-publish-find-date entry project))) (link (concat (file-name-sans-extension entry) ".html")) (tags (cadar (with-temp-buffer (org-mode) (insert-file-contents file) (org-collect-keywords '("FILETAGS")))))) (with-temp-buffer (org-mode) (insert (format "* [[file:%s][%s]] %s\n" file title tags)) ;; add properties for `ox-rss.el' here (org-set-property "RSS_PERMALINK" link) (org-set-property "PUBDATE" date) (org-set-property "RSS_TITLE" title) ;; We simply chuck in the entire file. ;; This assumes the file doesn't for instance start with a PROPERTIES drawer. (insert-file-contents file) ;; Ensure the list doesn't accidentally end due to blank lines at the end of ;; entries. Still leaves problems when the entry has a double blank line in it ;; as that ends an org list. (goto-char (point-max)) (delete-blank-lines) (buffer-string)))) ((eq style 'tree) ;; Return only last subdir. (file-name-nondirectory (directory-file-name entry))) (t entry))) (setq org-publish-project-alist `(("landing" :base-directory ,my-blog-source-folder :base-extension "org" :publishing-directory ,my-blog-target-folder :publishing-function org-mustache-html-publish-to-html :mustache-template ,(file-name-concat my-blog-mustache-folder "landing.mustache") :headline-levels 2 :section-numbers nil :with-toc nil :with-title nil :html-content-class nil :html-head-include-default-style nil :html-head nil :html-divs nil :html-preamble nil :html-postamble nil ) ("posts" :base-directory ,my-blog-posts-folder :base-extension "org" :publishing-directory ,(file-name-concat my-blog-target-folder "posts") :publishing-function org-mustache-html-publish-to-html :mustache-template ,(file-name-concat my-blog-mustache-folder "post.mustache") :preparation-function my-blog-create-tags-files :exclude "html*\\|assets*\\|rss.org\\|index.org\\|sitemap.org\\|.org~" :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 nil :html-divs nil :recursive t :auto-sitemap t :html-preamble nil :html-postamble nil :sitemap-sort-folders ignore-errors :sitemap-function my-blog-sitemap :sitemap-sort-files anti-chronologically :sitemap-filename "sitemap.org" ) ("tags" :base-directory ,(file-name-concat my-blog-source-folder "tags") :base-extension "org" :publishing-directory ,(file-name-concat my-blog-target-folder "posts") :publishing-function org-mustache-html-publish-to-html :mustache-template ,(file-name-concat my-blog-mustache-folder "tags.mustache") :html-content-class nil :section-numbers nil :with-toc nil :with-title nil :html-head-include-default-style nil :html-head nil :html-divs nil :recursive nil :auto-sitemap nil :html-preamble nil :html-postamble nil ) ("assets" :base-directory ,(file-name-concat my-blog-source-folder "assets") :base-extension any :recursive t :publishing-directory ,(file-name-concat my-blog-target-folder "assets") :publishing-function org-publish-attachment :completion-function my-blog-cleanup-sitemaps ) ("rss" :base-directory ,my-blog-posts-folder :base-extension "org" :recursive nil :exclude ,(regexp-opt '("rss.org" "index.org" "sitemap.org")) :publishing-directory ,(file-name-concat my-blog-target-folder "posts") :publishing-function org-rss-publish-to-rss :with-author t :title "Frederik Vanrenterghem's blog" :html-link-home ,my-blog-target-url :html-link-use-abs-url t :html-link-org-files-as-html t :section-numbers nil :exclude ".*" :include ("rss.org") :table-of-contents nil :auto-sitemap t :sitemap-filename "rss.org" :sitemap-title "Frederik Vanrenterghem's blog" :sitemap-style list :sitemap-sort-files anti-chronologically :sitemap-function fv/format-rss-feed :sitemap-format-entry fv/format-rss-feed-entry ) ("website" :components ("posts" "rss" "tags" "landing" "assets")))) (org-publish-initialize-cache "website")