blob: 20f141fe41a5e8b64af2d4fe03b61c7d5d9498bb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
;;; 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)
;; Article fetching & rendering functions
(defun render-org-buffer (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"))
;; Don't re-render articles unless forced.
(must-render (or force
(not (get-buffer output-buffer)))))
(if (and input-buffer must-render)
(with-current-buffer input-buffer
(org-export-to-buffer 'html output-buffer nil nil t)))
(if input-buffer output-buffer nil)))
(defun get-buffer-string (buffer)
"Returns the contents of the specified buffer as a string."
(with-current-buffer (get-buffer buffer)
(buffer-string)))
(defvar-local article-not-found
'(404 . "<html><body><p>Oh no, the article was not found.</p></body></html>"))
(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"))))
(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)
(elnode-start 'elblog-handler
:port elblog-port
:host elblog-host))
(defun stop-elblog ()
(interactive)
(elnode-stop elblog-port))
|