Commit 8980a330 authored by Cherie Yang's avatar Cherie Yang

Refactor code organization

parent 17feca53
......@@ -12,7 +12,11 @@
:licence "TBD"
:description "Static site generator for CLnet, written in CL"
:depends-on (:cl-mustache)
:depends-on (:asdf :cl-mustache)
:components ((:file "main"))
:serial t
:components ((:file "globals")
(:file "helpers")
(:file "process")
(:file "main"))
)
\ No newline at end of file
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(in-package :cl-site)
;; Location of template files
(defparameter *LAYOUT-DIR* (asdf:system-relative-pathname :cl-site "layout/templates/"))
;; Where generated output should be written to
(defparameter *OUTPUT-DIR* (asdf:system-relative-pathname :cl-site "output/"))
;; Default template to use unless another is explicitly specified.
;; (More template-related variables may go here in the future)
(defparameter *DEFAULT-PAGE-TEMPLATE* "primary.html")
;; Location and list of styles
;; *STYLES* should be list of strings representing relative filenames.
;; @TO-DO: replace this with better css preprocessing.
(defparameter *STYLES-DIR* (asdf:system-relative-pathname :cl-site "content/css/"))
(defparameter *STYLES*
(list "main.css"))
;; Location & list of pages
;; Each page is an alist containing info to be sent to the template via the context.
;; The only REQUIRED field is :content which must be the filename of the page content
;; The :content field will NOT be sent to the template.
(defparameter *PAGES-DIR* (asdf:system-relative-pathname :cl-site "content/"))
(defparameter *PRIVATE-KEYS* '(:slug :content))
(defparameter *PAGES*
(list
'((:title . "Test Page") (:slug . "test-page") (:content . "testpage.html"))))
;; Initialize global context (will be appended to all individual page contexts)
;; Used to store things like stylesheets, etc...
(defparameter *GLOBAL-CONTEXT* ())
\ No newline at end of file
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(in-package :cl-site)
; Makes pathname from prefix + page object
(defun make-path (prefix page)
(let ((path (cdr (assoc :content page))))
(if (pathnamep prefix)
(merge-pathnames path prefix)
(pathname (concatenate 'string
prefix path)))))
; Slurps file from a string
(defun file-to-string (filepath)
(with-open-file (stream filepath)
(let ((data (make-string (file-length stream))))
(read-sequence data stream)
data)))
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(in-package :cl-site)
(require :asdf)
;; Locations for various files
(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/"))
(defparameter *PRIVATE-KEYS* '(:slug :content))
(defparameter *PAGES*
(list
'((:title . "Test Page")
(:slug . "test-page")
(:content . "testpage.html"))))
;; Default template to use unless another is explicitly specified.
(defparameter *DEFAULT-PAGE-TEMPLATE* "primary.html")
(defun make-path (prefix page)
(let ((path (cdr (assoc :content page))))
(if (pathnamep prefix)
(merge-pathnames path prefix)
(pathname (concatenate 'string
prefix path)))))
(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)))
(with-open-file (output-stream output-path :direction :output
:if-exists :supersede)
(mustache:render template-path page-context output-stream))))
(defun make-site ()
(let ((template-path (pathname (merge-pathnames *LAYOUT-DIR* *DEFAULT-PAGE-TEMPLATE*))))
(loop for page in *PAGES*
do (render-page page template-path))))
(process-pages))
\ No newline at end of file
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(in-package :cl-site)
;; Copies/renders stylesheets + adds to global context
(defun process-styles () )
;; Copies/renders static assets (imgs, etc)
(defun process-static () )
;; process-pages : Copies/renders pages - called last
(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)))
(with-open-file (output-stream output-path :direction :output
:if-exists :supersede)
(mustache:render template-path page-context output-stream))))
(defun process-pages ()
(let
((template-path (pathname (merge-pathnames *LAYOUT-DIR* *DEFAULT-PAGE-TEMPLATE*))))
(progn (ensure-directories-exist *OUTPUT-DIR*)
(loop for page in *PAGES*
do (render-page page template-path)))))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment