;;; blog.el --- A simple org-mode & elnode blog software.
;;; -*- lexical-binding: t; -*-
(require 'elnode)
(require 'f)
;; Definition of customization options
(defgroup elblog nil
"Configuration for the Emacs Lisp blog software"
:link '(url-link "https://github.com/tazjin/elblog"))
(defcustom elblog-port 8010
"Port to run elblog's HTTP server on"
:group 'elblog
:type 'integer)
(defcustom elblog-host "localhost"
"Host for elblog's HTTP server to listen on"
:group 'elblog
:type 'string)
;; org-mode settings need to be configured a certain way for elblog's HTML
;; templating to work correctly.
(defun configure-org-html-export ()
(setq org-html-postamble t)
(setq org-html-doctype "html5")
(setq org-html-head-include-scripts nil)
(setq org-html-style-default (f-read-text "blog.css"))
(setq org-html-preamble-format
'(("en" "Tazjin's blog
Oh no, the article was not found.
")) (defvar-local text-html '("Content-Type" . "text/html")) (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)) article-not-found))) (defun blog-post-handler (httpcon) "This handler servers a blog post from the configured blog post directory." (let ((response (render-article (elnode-http-mapping httpcon 1)))) (elnode-http-start httpcon (car response) text-html) (elnode-http-return httpcon (cdr response)))) ;; Web server implementation (defvar-local elblog-routes '(("^.*//en/\\(.*\\)" . blog-post-handler))) (defun elblog-handler (httpcon) (elnode-hostpath-dispatcher httpcon elblog-routes)) (defun start-elblog () (interactive) (configure-org-html-export) (elnode-start 'elblog-handler :port elblog-port :host elblog-host)) (defun stop-elblog () (interactive) (elnode-stop elblog-port))