meta: publishing this website

This is a always up-to-date version of my initial post about publishing this website using org-mode. This uses the tangle feature of org-mode and will span from the Makefile of the repository to any emacs-lisp code required.

This is part of the Meta entry.

Parts of the website

Let’s look at the different part of the website and for each where I do get the information I want to publish.

this is just a file ( that I maintain manually.
this is what we call blog these days : short to medium article that are valid at a point of time, as may contain deprecated content, or content that do not reflect my views at a later point in time.
medium to long article about a topic. Those should be up-to-date or explicitly mark as deprecated or invalid. This is my ready for the public knowledge database, a bit like Jethro’s Braindump. It is managed using org-roam and I just need to get the latests somewhere to publish it.
medium to long article about my configurations. Those are base sources for my home configuration mono-repository, and usually follow literate programming principles. The are managed using org-roam with the config: prefix.
a dump of random files, it is actually on another domain name, completely unmanaged by this.
an about page about the author of the website (aka me), linking external contributions (GitHub/Gitlab/… profiles, Talks, …).

In a nutshell, the folder hierarchy is something like :

|-- about           # the about folder (with one
|-- articles        # <- comes from ~/desktop/org/notes~
|-- css             # the css
|-- images          # the images
|--       # the index 🎉
|-- posts           # the posts (format YYYY-MM-DD-{title}.org)
`-- public          # the output that get deployed later on


Make it happen

In order to publish this website, I am using make. In a nutshell, I am going to define a few target to get the content from my notes, export org files into html and copy more or less everything to the public folder. I will also define a clean and a publish target.

The first part of my Makefile will be to define some constants that I want to use later on. Those are mainly to easily change where to look for the notes or where the emacs configuration is.

ifndef EMACS
EMACS = "emacs"

DOTEMACS = "~/.config/emacs"

ifndef PUBLISH

NOTES = ~/desktop/org/notes
SRCHOME = ~/src/home

The default target will be name build.

all: build

Building public/ and publishing it

To build the website, we will be using Emacs in batch mode, with some shared library and the actual the publish scrits.

.PHONY: build
build: publish.el publish-common.el build-articles
        @echo "Publishing... with current Emacs configurations."
        ${EMACS} --batch --directory $(DOTEMACS)/lisp/ --directory $(DOTEMACS)/lisp/vorg/ \
                --load publish-common.el --load publish.el \
                --funcall org-publish-all

.PHONY: build-articles
build-articles: $(NOTES)
        rsync -arv --delete --exclude='*' --exclude='*.db' $(NOTES)/ articles/

        $(error $(NOTES) doesn't exists…)

The publish target is gonna be really simple: I just need to copy the content to ~/desktop/sites on the current machine, and the rest is automated.

.PHONY: publish
publish: build
        rsync -a --progress --delete public/assets/.fancyindex/ ~/desktop/sites/
        rsync -a --progress --delete public/ ~/desktop/sites/${PUBLISH}/

Local server

Let’s use miniserve (using Nix with nix-shell) to serve the static website locally to validate my changes.

.PHONY: serve
        nix-shell -p miniserve --command "miniserve --port=8181 --index=index.html public/"

Final nits of the Makefile

One of the final step is to install the git hooks if any. I tend to have this target in all my personal Makefile at least. Let’s also define a pre-commit target that will hold anything we need to do at pre-commit.

.PHONY: install-hooks
        if [ -e .git ]; then nix-shell -p git --run 'git config core.hooksPath .githooks'; fi

.PHONY: pre-commit

The file is actually generated from my The main reason is that most tools (cgit, …) and do not support the org file. $(SRCHOME)
        @echo "Updating…"
        $(EMACS) --batch --directory $(DOTEMACS)/lisp/ \
                --load $(SRCHOME)/lib/lisp/docs.el \
                --funcall update-readme-md

And the final target is the clean one. This will remove any compile emacs-lisp file (*.elc), the public folder, and some org-mode metadata.

.PHONY: clean
        @echo "Cleaning up.."
        @-rm -rvf *.elc
        @-rm -rvf public
        @-rm -rv ~/.org-timestamps/*

The publish scrits

I’ve imported the script directly in here, I’ll slowly split this and document it.


;;; publish.el --- Publish www project -*- lexical-binding: t; -*-
;; Author: Vincent Demeester <>

;;; Commentary:
;; This script will convert the org-mode files in this directory into
;; html.

;;; Code:
(require 'package)
(require 'publish-common)

(setq org-publish-project-alist
         :base-directory "posts"
         :base-extension "org"
         :recursive t
         :publishing-function org-html-publish-to-html
         :publishing-directory "./public/posts"
         :exclude ,(regexp-opt '("" "draft"))
         :auto-sitemap t
         :with-footnotes t
         :with-toc nil
         :with-drawers t
         :sitemap-filename ""
         :sitemap-title "Posts"
         :sitemap-format-entry sbr/org-sitemap-format-entry
         :sitemap-style list
         :sitemap-sort-files anti-chronologically
         :sitemap-function sbr/org-publish-sitemap
         :html-head-include-scripts nil
         :html-head-include-default-style nil
         :html-head ,sbr-website-html-head
         :html-preamble sbr-website-html-preamble
         :html-postamble ,sbr-website-html-postamble)
         :base-directory "posts"
         :base-extension "org"
         :recursive t
         :html-link-home ""
         :rss-link-home ""
         :html-link-use-abs-url t
         :rss-extension "xml"
         :publishing-directory "./public"
         :publishing-function (sbr/org-rss-publish-to-rss)
         :section-number nil
         :exclude ".*"
         :include (""))
         :base-directory "articles"
         :base-extension "org"
         :recursive t
         :publishing-function org-html-publish-to-html
         :publishing-directory "./public/articles"
         :exclude ,(regexp-opt '("" "draft"))
         :auto-sitemap t
         :with-footnotes t
         :with-toc nil
         :with-drawers t
         :sitemap-filename ""
         :sitemap-title "Articles"
         :sitemap-style tree
         :sitemap-sort-files anti-chronologically
         ;;:sitemap-format-entry sbr/org-sitemap-format-entry
         ;;:sitemap-function sbr/org-publish-sitemap
         :html-head-include-scripts nil
         :html-head-include-default-style nil
         :html-head ,sbr-website-html-head
         :html-preamble sbr-website-html-preamble
         :html-postamble ,sbr-website-html-postamble)
         :exclude ,(regexp-opt '("*.org"))
         :base-directory "articles"
         :base-extension ,site-attachments
         :publishing-directory "./public/articles"
         :publishing-function org-publish-attachment
         :recursive t)
         :base-directory "about"
         :base-extension "org"
         :exclude ,(regexp-opt '("" "draft"))
         :index-filename ""
         :recursive nil
         :with-footnotes t
         :with-toc nil
         :with-drawers t
         :publishing-function org-html-publish-to-html
         :publishing-directory "./public/about"
         :html-head-include-scripts nil
         :html-head-include-default-style nil
         :html-head ,sbr-website-html-head
         :html-preamble sbr-website-html-preamble
         :html-postamble ,sbr-website-html-postamble)
         :base-directory ""
         :base-extension "org"
         :exclude ,(regexp-opt '("" "draft"))
         :index-filename ""
         :recursive nil
         :with-footnotes t
         :with-toc nil
         :with-drawers t
         :with-title nil
         :publishing-function org-html-publish-to-html
         :publishing-directory "./public"
         :html-head-include-scripts nil
         :html-head-include-default-style nil
         :html-head ,sbr-website-html-head
         :html-preamble sbr-website-html-preamble
         :html-postamble ,sbr-website-html-postamble)
         :base-directory "./css"
         :base-extension ,site-attachments
         :recursive t
         :publishing-directory "./public/css"
         :publishing-function org-publish-attachment
         :recursive t)
         :base-directory "./images"
         :base-extension ,site-attachments
         :publishing-directory "./public/images"
         :publishing-function org-publish-attachment
         :recursive t)
         :base-directory "./assets"
         :base-extension ,site-attachments
         :publishing-directory "./public/assets"
         :publishing-function org-publish-attachment
         :recursive t)
         :base-directory "./legacy"
         :base-extension ,site-attachments
         :publishing-directory "./public/"
         :publishing-function org-publish-attachment
         :recursive t)
        ("all" :components ("posts" "about" "index" "articles" "articles-assets" "css" "images" "assets" "legacy" "posts-rss"))))

(provide 'publish)
;;; publish.el ends here


;;; publish-common.el --- Commons code for www publishing projects -*- lexical-binding: t; -*-
;; Author: Vincent Demeester <>

;;; Commentary:
;;; Code:
;; load org
(require 'org)
;; load org export functions
(require 'ox-publish)
(require 'ox-rss)
(require 'ox-html)
;; load org link functions
(require 'ol-man)
(require 'ol-git-link)
;; Those are mine
(require 'ol-github)
(require 'ol-gitlab)
(require 'org-attach)
;; load additional libraries
(require 'go-mode)
(require 'css-mode)
(require 'yaml-mode)
(require 'nix-mode)

(require 's)

(setq org-export-use-babel nil)
(setq org-link-abbrev-alist '(("att" . org-attach-expand-link)))

;; setting to nil, avoids "Author: x" at the bottom
(setq org-export-with-section-numbers nil
      org-export-with-smart-quotes t
      org-export-with-toc nil)

(defvar sbr-date-format "%b %d, %Y")

(setq org-html-divs '((preamble "header" "top")
                      (content "main" "content")
                      (postamble "footer" "postamble"))
      org-html-container-element "section"
      org-html-metadata-timestamp-format sbr-date-format
      org-html-checkbox-type 'unicode
      org-html-html5-fancy t
      org-html-doctype "html5"
      org-html-htmlize-output-type 'css
      org-html-htmlize-font-prefix "org-"
      org-src-fontify-natively t
      org-html-coding-system 'utf-8-unix)

(defun sbr/org-export-format-drawer (name content)
  "HTML export of drawer with NAME and CONTENT.
name is the name of the drawer, that will be used as class.
content is the content of the drawer"
  (format "<div class='drawer %s'>\n<h6>%s</h6>\n%s</div>"
          (downcase name)
          (capitalize name)
(setq org-html-format-drawer-function 'sbr/org-export-format-drawer)

(defun read-file (filePath)
  "Return FILEPATH's file content."
    (insert-file-contents filePath)

(defvar sbr-website-html-head
  "<link rel='icon' type='image/x-icon' href='/images/favicon.ico'/>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' href='/css/new.css' type='text/css'/>
<link rel='stylesheet' href='/css/syntax.css' type='text/css'/>
<link href='/index.xml' rel='alternate' type='application/rss+xml' title='Vincent Demeester' />")

(defun sbr-website-html-preamble (plist)
  "PLIST: An entry."
  ;; Skip adding subtitle to the post if :KEYWORDS don't have 'post' has a
  ;; keyword
  (when (string-match-p "post" (format "%s" (plist-get plist :keywords)))
    (plist-put plist
               :subtitle (format "Published on %s by %s."
                                 (org-export-get-date plist sbr-date-format)
                                 (car (plist-get plist :author)))))

  ;; Below content will be added anyways
<img src=\"/images/favicon.ico\" id=\"sitelogo\"/> <a href='/'>home</a> /
<a href='/posts/'>posts</a> (<a href='/index.xml'>rss</a>) /
<a href='/articles/'>articles</a> /
<a href='/articles/config_configurations.html'>configurations</a> /
<a href=''>files</a> /
<a href='/about/'>about</a></li>

(defvar sbr-website-html-postamble
     <span class='questions'>Questions, comments ? Please use my <a href=\"\">public inbox</a> by sending a plain-text email to <a href=\"mailto:~vdemeester/\">~vdemeester/</a>.</span>
     <span class='opinions'>Opinions stated here are my own and do not express the views of my employer, spouse, children, pets, neighbors, secret crushes, favorite authors, or anyone else who is not me. And maybe not even me, depending on how old this is.</span>
     <span class='copyright'>
      Content and design by Vincent Demeester
      (<a rel='licence' href=''>Some rights reserved</a>)
    </span><br />
    <span class='engine'>
      Powered by <a href=''>Gnu Emacs</a> and <a href=''>orgmode</a>
(defvar site-attachments
  (regexp-opt '("jpg" "jpeg" "gif" "png" "svg"
                "ico" "cur" "css" "js" "woff" "html" "pdf" "otf"))
  "File types that are published as static files.")

(defun sbr/org-sitemap-format-entry (entry style project)
  "Format posts with author and published data in the index page.

ENTRY: file-name
PROJECT: `posts in this case."
  (cond ((not (directory-name-p entry))
         (format "%s — [[file:%s][%s]]
                 :PUBDATE: [%s]
                 (format-time-string "%Y-%m-%d"
                                     (org-publish-find-date entry project))
                 (org-publish-find-title entry project)
                 (format-time-string "%Y-%m-%d"
                                     (org-publish-find-date entry project))))
        ((eq style 'tree) (file-name-nondirectory (directory-file-name entry)))
        (t entry)))

(defun sbr/org-publish-sitemap (title list)
  (concat "#+TITLE: " title "\n\n"
          (org-list-to-subtree list)))

(defun sbr/org-get-first-paragraph (file)
  "Get string content of first paragraph of file."
    (insert-file-contents file)
    (goto-char (point-min))
    (let ((first-begin (progn
                         (org-forward-heading-same-level 1)
          (first-end (progn
                       (org-next-visible-heading 1)
      (buffer-substring first-begin first-end)))))

(defun sbr/org-rss-publish-to-rss (plist filename pub-dir)
  "Prepare file before exporting."
  (let* ((postsdir (plist-get plist :base-directory)))
    (with-current-buffer (find-file filename)
      (insert "#+TITLE: Posts\n")
      (insert "#+AUTHOR: Vincent Demeester\n")
      (insert "#+OPTIONS: toc:nil\n")
      (let* ((files-all
              (reverse (directory-files "." nil
             (files (subseq files-all 0 (min (length files-all) 30))))
        (message (format "foo: %s" filename))
        (dolist (post files)
          (let* ((post-file post)
                 (post-title (org-publish-find-title post-file plist))
                 (preview-str (sbr/org-get-first-paragraph post-file))
                 (date (replace-regexp-in-string
                        "\\1" post)))
            (insert (concat "* [[file:" postsdir "/" post "][" post-title "]]\n\n"))
            (org-set-property "ID" post)
            (org-set-property "RSS_TITLE" post-title)
            ;; ox-rss prepends html-link-home to permalink
            (org-set-property "RSS_PERMALINK"
                              (concat postsdir "/"
                                      (file-name-sans-extension post)
              "<%Y-%m-%d %a %H:%M>"
                "\\1" post))))
            (insert preview-str)
            (newline 1)
            (insert (concat "[[file:" postsdir "/" post "][(Read more)]]\n\n"))))
  (let ((user-mail-address "t")
        (org-export-with-broken-links t)
        (org-rss-use-entry-url-as-guid nil))
    (org-rss-publish-to-rss plist filename pub-dir)))

(provide 'publish-common)
;;; publish-common.el ends here