main.lisp 1.77 KB
Newer Older
1
2
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(in-package :cl-site)
3
(require :asdf)
4
5

;; Locations for various files
6
7
8
(defparameter *PAGES-DIR* (asdf:system-relative-pathname :cl-site "content/"))
(defparameter *LAYOUT-DIR* (asdf:system-relative-pathname :cl-site "layout/templates/"))
(defparameter *OUTPUT-DIR* (asdf:system-relative-pathname :cl-site "output/"))
9
10
11
12
13
14
15
16

(defparameter *PRIVATE-KEYS* '(:slug :content))
(defparameter *PAGES* 
  (list 
    '((:title . "Test Page") 
      (:slug . "test-page") 
      (:content . "testpage.html"))))

17
18
19
20
21
;; Default template to use unless another is explicitly specified.
(defparameter *DEFAULT-PAGE-TEMPLATE* "primary.html")


(defun make-path (prefix page)
22
23
24
25
26
  (let ((path (cdr (assoc :content page))))
    (if (pathnamep prefix)
        (merge-pathnames path prefix)
        (pathname (concatenate 'string 
                               prefix path)))))
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

(defun file-to-string (filepath)
  (with-open-file (stream filepath)
    (let ((data (make-string (file-length stream))))
      (read-sequence data stream)
      data)))

(defun render-page (page template-path)
  (let* 
    ((page-path (make-path *PAGES-DIR* page))
     (output-path (make-path *OUTPUT-DIR* page))
     (page-content (file-to-string page-path))
     (page-context (remove-if (lambda (p) (find p *PRIVATE-KEYS*)) 
       (push (cons :page-content page-content) page) :key 'car)))

42
43
      (with-open-file (output-stream output-path :direction :output
				     :if-exists :supersede) 
44
        (mustache:render template-path page-context output-stream))))
45
46

(defun make-site () 
47
48
49
  (let ((template-path (pathname (merge-pathnames *LAYOUT-DIR* *DEFAULT-PAGE-TEMPLATE*))))
    (loop for page in *PAGES*
       do (render-page page template-path))))