ref: 15b36ff59f6a8e1b1c24d58c3c4ab247abb5f46 betterssg/betterssg.lisp -rw-r--r-- 2.6 KiB View raw
                                                                                
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
(mapcar #'ql:quickload '(cl-markdown alexandria cl-ppcre str))

(defun find-pages ()
  (directory "./content/**/*.md"))
(defparameter *testpage* (merge-pathnames "test.html" *default-pathname-defaults*))
(defparameter *examplepage* (third (find-pages)))
(defun extract-date (pathname)
    (let* ((fullname (namestring pathname))
           (trimmed-name (reverse (subseq (reverse (subseq fullname (+ (search "/" fullname :from-end t) 1) )) 3))))
      (cond ((digit-char-p (char trimmed-name 0)) (subseq trimmed-name 0 10))
            (t nil))
      ))
(defun extract-slug (pathname)
  (let* ((fullname (namestring pathname))
         (name-without-path (reverse (subseq (reverse (subseq fullname (+ (search "/" fullname :from-end t) 1) )) 3))))
    (cond ((digit-char-p (char name-without-path 0)) (subseq name-without-path 11))
          (t name-without-path))))
(defun extract-title (pathname)
  (let* ((slug (extract-slug pathname))
         (spaces-name (substitute #\SPACE #\- slug))
         (upcase-name (string-upcase spaces-name)))
    upcase-name))

(defun get-html-content (pathname)
  (with-output-to-string (*standard-output*) (markdown:render-to-stream (markdown:markdown pathname) :html t)))

;;(merge-pathnames (pathname "layouts/page.html") *default-pathname-defaults*)
(defun build-page (pathname)
    (let* ((date (extract-date pathname))
           (title (extract-title pathname))
           (head (alexandria:read-file-into-string (merge-pathnames (pathname "layout/head.html") *default-pathname-defaults*)))
          (content (get-html-content pathname))
           (template (alexandria:read-file-into-string (merge-pathnames (pathname "layout/page.html") *default-pathname-defaults*)))
          (template (cond (date (cl-ppcre:regex-replace-all "\{\{date\}\}" template date)) (t (cl-ppcre:regex-replace-all "\{\{date\}\}" template ""))))
           (template (cl-ppcre:regex-replace-all "\{\{title\}\}" template title))
           (template (cl-ppcre:regex-replace-all "\{\{head\}\}" template head))
          (template (cl-ppcre:regex-replace-all "\{\{content\}\}" template content)))
       template))

(defun build-site ()
  (dolist (pathname (find-pages))
    (progn (ensure-directories-exist (get-new-pathname pathname))
           (str:to-file (get-new-pathname pathname) (build-page pathname)))))


(defparameter *website-pathname* (merge-pathnames "website" *default-pathname-defaults*))
(defparameter *content-pathname* (merge-pathnames "content" *default-pathname-defaults*))
(defun get-new-pathname (pathname)
  (pathname (string-downcase (concatenate 'string (namestring *website-pathname*) "/" (extract-slug pathname) "/index.html"))))