feat(blog): Register blog articles in hash-table

* adds a hash-table stored in a variable called `elblog-articles` that
  defines a map of article names (used as URI fragments) to file names
  of org-mode files
* adds a custom variable `elblog-article-directory` which must be set
  to the base path of the org-mode files representing elblog articles
* refactors the article-rendering functions to look up articles in the
  articles hash-table and renders them from there

After this change elblog is almost functional as a blog software, only
missing index generation.
This commit is contained in:
Vincent Ambo 2017-11-13 18:40:59 +01:00
parent 5065f5395b
commit 2ba11f5c03

26
blog.el
View file

@ -1,8 +1,10 @@
;;; blog.el --- A simple org-mode & elnode blog software.
;;; -*- lexical-binding: t; -*-
(require 'dash)
(require 'elnode)
(require 'f)
(require 'ht)
;; Definition of customization options
@ -25,6 +27,17 @@
:group 'elblog
:type 'string)
(defcustom elblog-article-directory nil
"Directory in which elblog articles are stored"
:group 'elblog
:type 'string)
;; Declare user-configurable variables needed at runtime.
(defvar elblog-articles (ht-create)
"A hash-table of blog articles. This is used for looking up articles from
URL fragments as well as for rendering the index.")
;; HTML templating setup
(defun template-preamble ()
@ -42,10 +55,9 @@
;; Article fetching & rendering functions
(defun render-org-buffer (buffer &optional force)
(defun render-org-buffer (input-buffer &optional force)
"Renders an org-mode buffer as HTML and returns the name of the output buffer."
(letrec ((input-buffer (get-buffer buffer))
(output-buffer (concat buffer "-rendered"))
(letrec ((output-buffer (concat (buffer-name input-buffer) "-rendered"))
;; Don't re-render articles unless forced.
(must-render (or force
(not (get-buffer output-buffer)))))
@ -66,8 +78,12 @@
(defun render-article (article)
"Renders an article, if it exists."
(let ((output-buffer (render-org-buffer (concat article ".org") t)))
(if output-buffer `(200 . ,(get-buffer-string output-buffer))
(letrec ((rendered (-some->>
(ht-get elblog-articles article)
(concat elblog-article-directory)
(find-file)
(render-org-buffer))))
(if rendered `(200 . ,(get-buffer-string rendered))
article-not-found)))
(defun blog-post-handler (httpcon)