Initialization of the repository - cl-yag - Common Lisp Yet Another website Generator HTML git clone git://bitreich.org/cl-yag/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/cl-yag/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 66a2b83f5bceef484963bd78efc2a2da98004f4e DIR parent 33a711b470f9c3b71345444fff96f7103f933a77 HTML Author: Solene Rapenne <solene@srv.dataswamp.org> Date: Sat, 30 Apr 2016 17:21:31 +0200 Initialization of the repository Diffstat: A Makefile | 11 +++++++++++ A data/1.txt | 1 + A data/articles.lisp | 15 +++++++++++++++ A generator.lisp | 82 +++++++++++++++++++++++++++++++ A static/style.css | 0 A template/article.tpl | 7 +++++++ A template/layout.tpl | 23 +++++++++++++++++++++++ 7 files changed, 139 insertions(+), 0 deletions(-) --- DIR diff --git a/Makefile b/Makefile @@ -0,0 +1,11 @@ +all: + mkdir -p output/static + cp -fr static/* output/static/ + sbcl --dynamic-space-size 60 --script generator.lisp + +clean: + rm -fr output/* + +css: + mkdir -p output/static + cp -fr static/* output/static/ DIR diff --git a/data/1.txt b/data/1.txt @@ -0,0 +1 @@ +<p>This contains the text of the article with id 1</p> DIR diff --git a/data/articles.lisp b/data/articles.lisp @@ -0,0 +1,15 @@ +(defvar *config* + (list + :webmaster "Your author name here" + :title "Your blog title here" + )) + +;; describes articles (ordered) +;; exemple => (list :id "4" :date "2015-05-04" :title "The article title" :author "Me" :tiny "Short description for home page") +;; :author can be omitted and will be replaced by webmaster value +;; :tiny can be omitted and will be replaced by the full article text +(defvar *articles* + (list + (list :id "1" :date "29 April 2016" :title "My first message" :short "This is my first message" :author "Solène") + )) + DIR diff --git a/generator.lisp b/generator.lisp @@ -0,0 +1,82 @@ +(load "data/articles.lisp") + +(defun replace-all (string part replacement &key (test #'char=)) + "Returns a new string in which all the occurences of the part +is replaced with replacement." + (with-output-to-string (out) + (loop with part-length = (length part) + for old-pos = 0 then (+ pos part-length) + for pos = (search part string + :start2 old-pos + :test test) + do (write-string string out + :start old-pos + :end (or pos (length string))) + when pos do (write-string replacement out) + while pos))) +; load a file as a string +(defun slurp-file(path) + (with-open-file (stream path) + (let ((data (make-string (file-length stream)))) + (read-sequence data stream) + data))) + +; save a string in a file +(defun save-file(path data) + (with-open-file (stream (concatenate 'string "output/" path) :direction :output :if-exists :supersede) + (format stream data))) + +;; simplify the str replace work +(defmacro template(before &body after) + `(progn + (setf output (replace-all output ,before ,@after)))) + +;; simplify the declaration of a new page type +(defmacro prepare(template &body code) + `(progn + (let ((output (slurp-file ,template))) + ,@code + output))) + +;; generates the html of one only article +(defun create-article(article &optional &key (tiny t)) + (prepare "template/article.tpl" + (template "%%Author%%" (if (member :author article) (getf article :author) (getf *config* :webmaster))) + (template "%%Date%%" (getf article :date)) + (template "%%Title%%" (getf article :title)) + (template "%%Id%%" (getf article :id)) + (template "%%Text%%" (if (and tiny (member :tiny article)) + (getf article :tiny) (slurp-file (format nil "data/~d.txt" (getf article :id))))))) + +;; Layout generation +(defun generate-layout(body) + (let ((output (slurp-file "template/layout.tpl"))) + (template "%%Title%%" (getf *config* :title)) + (template "%%Body%%" body) + output)) + +; Homepage generation +(defun generate-mainpage() + (format nil "~{~d~}" + (loop for article in *articles* collect + (create-article article :tiny t)))) + +; produce index.html +(defun generate-file-index() + (save-file "index.html" + (generate-layout (generate-mainpage)))) + +; produce html files for articles +(defun generate-file-article(article) + (save-file (format nil "article-~d.html" (getf article :id)) + (generate-layout (create-article article :tiny nil)))) + +; ENGINE START ! +(defun generate-site() + (generate-file-index) + (dolist (article *articles*) (generate-file-article article)) + ;;(generate-file-rss) + ) + + +(generate-site) DIR diff --git a/static/style.css b/static/style.css DIR diff --git a/template/article.tpl b/template/article.tpl @@ -0,0 +1,7 @@ +<div class="article"> + <div class="informations"> + <b><a href="article-%%Id%%.html"><em>%%Author%%</em> wrote "%%Title%%" on %%Date%%</a></b> + </div> + <p>%%Text%%</p> +</div> + DIR diff --git a/template/layout.tpl b/template/layout.tpl @@ -0,0 +1,23 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html > + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <link rel="stylesheet" type="text/css" href="static/style.css" /> + <title>%%Title%%</title> + </head> + <body> + + <div id="top"> + <a href="index.html">Home</a> <a href="rss.xml">Rss</a> + </div> + + <div id="content"> + %%Body%% + </div> + + <div id="foot" > + This blog is powered by cl-yag ! + </div> + + </body> +</html>