i- Add RSS generation - Code enhancement - Escaping ~ which were problematic - cl-yag - Common Lisp Yet Another website Generator Err bitreich.org 70 hgit clone git://bitreich.org/cl-yag/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/cl-yag/ URL:git://bitreich.org/cl-yag/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/cl-yag/ bitreich.org 70 1Log /scm/cl-yag/log.gph bitreich.org 70 1Files /scm/cl-yag/files.gph bitreich.org 70 1Refs /scm/cl-yag/refs.gph bitreich.org 70 1Tags /scm/cl-yag/tag bitreich.org 70 1README /scm/cl-yag/file/README.md.gph bitreich.org 70 1LICENSE /scm/cl-yag/file/LICENSE.gph bitreich.org 70 i--- Err bitreich.org 70 1commit a6f07ecaad846e608500efa5cb5b3913a43db7e0 /scm/cl-yag/commit/a6f07ecaad846e608500efa5cb5b3913a43db7e0.gph bitreich.org 70 1parent e0a4614444c18e9840e4fa30d832da9fafedfa5e /scm/cl-yag/commit/e0a4614444c18e9840e4fa30d832da9fafedfa5e.gph bitreich.org 70 hAuthor: Solene Rapenne URL:mailto:solene@dataswamp.org bitreich.org 70 iDate: Sun, 1 May 2016 00:43:31 +0200 Err bitreich.org 70 i Err bitreich.org 70 i- Add RSS generation Err bitreich.org 70 i- Code enhancement Err bitreich.org 70 i- Escaping ~ which were problematic Err bitreich.org 70 i Err bitreich.org 70 iDiffstat: Err bitreich.org 70 i M README.md | 8 ++++++-- Err bitreich.org 70 i M data/articles.lisp | 11 ++++++++++- Err bitreich.org 70 i M generator.lisp | 80 ++++++++++++++++++++++--------- Err bitreich.org 70 i A template/rss-item.tpl | 5 +++++ Err bitreich.org 70 i A template/rss.tpl | 10 ++++++++++ Err bitreich.org 70 i Err bitreich.org 70 i5 files changed, 88 insertions(+), 26 deletions(-) Err bitreich.org 70 i--- Err bitreich.org 70 1diff --git a/README.md b/README.md /scm/cl-yag/file/README.md.gph bitreich.org 70 i@@ -39,7 +39,7 @@ I tried to make it "hacking friendly" so it's very extensible. Err bitreich.org 70 i Here is an example code if you want to add something like a panel on the layout. Err bitreich.org 70 i Err bitreich.org 70 i + Add a string for the replacement to occure, like %%Panel%% in **template/layout.tpl** (because we want the panel on every page) Err bitreich.org 70 i-+ In **generator.lisp** modify the function *generate-layout* to add "**(template "%%Panel%%" (slurp-file "template/panel.tpl"))**" after one template function call Err bitreich.org 70 i++ In **generator.lisp** modify the function *generate-layout* to add "**(template "%%Panel%%" (load-file "template/panel.tpl"))**" after one template function call Err bitreich.org 70 i + Create **template/panel.tpl** with the html Err bitreich.org 70 i Err bitreich.org 70 i (note : you can also directly add your text inside the layout template file instead of including another file) Err bitreich.org 70 i@@ -50,7 +50,7 @@ You may want to have some dedicated page for some reason, reusing the website la Err bitreich.org 70 i Err bitreich.org 70 i In **generate-site** function we can load a file, apply the template and save it in the output. It may look like this Err bitreich.org 70 i Err bitreich.org 70 i- (generate "somepage.html" (slurp-file "data/mypage.html")) Err bitreich.org 70 i+ (generate "somepage.html" (load-file "data/mypage.html")) Err bitreich.org 70 i Err bitreich.org 70 i This will produce the file somepage.html in the output folder Err bitreich.org 70 i Err bitreich.org 70 i@@ -66,3 +66,7 @@ Here is a tip to produce html files from markdown using emacs Err bitreich.org 70 i 4. run *make* to update your site Err bitreich.org 70 i Err bitreich.org 70 i The generator don't do it natively because I didn't want it to have dependencies. You can use what you want to produces the html files. Err bitreich.org 70 i+ Err bitreich.org 70 i+# Known limitation Err bitreich.org 70 i+ Err bitreich.org 70 i+The application will crash if you use a single "**~**" caracter inside one data structure in **articles.lisp** files. This is due to the format function trying to interpret the ~ symbol while we just one a ~ symbol. This symbol in the others files are automatically replaced by ~~ which produce a single ~. So, if you want to have a "~" as a title/url/author/description/short/date you have to double it. It may be interestind to sanitize it in the tool maybe. Err bitreich.org 70 1diff --git a/data/articles.lisp b/data/articles.lisp /scm/cl-yag/file/data/articles.lisp.gph bitreich.org 70 i@@ -1,15 +1,24 @@ Err bitreich.org 70 i+;; WARNING caracter "~" must be escaped when used in this file Err bitreich.org 70 i+;; you have to type ~~ for one ~ to escape it Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 i+;; define informations about your blog Err bitreich.org 70 i+;; used for the RSS generation and some variables replacements in the layout Err bitreich.org 70 i (defvar *config* Err bitreich.org 70 i (list Err bitreich.org 70 i :webmaster "Your author name here" Err bitreich.org 70 i :title "Your blog title here" Err bitreich.org 70 i+ :description "Yet another website on the net" Err bitreich.org 70 i+ :url "https://my.website/~~user/" Err bitreich.org 70 i )) Err bitreich.org 70 i Err bitreich.org 70 i-;; describes articles (ordered) Err bitreich.org 70 i+;; describes articles (ordered on the website as they are displayed here, the first in list is the top of the website) Err bitreich.org 70 i ;; exemple => (list :id "4" :date "2015-05-04" :title "The article title" :author "Me" :tiny "Short description for home page") Err bitreich.org 70 i ;; :author can be omitted and will be replaced by webmaster value Err bitreich.org 70 i ;; :tiny can be omitted and will be replaced by the full article text Err bitreich.org 70 i (defvar *articles* Err bitreich.org 70 i (list Err bitreich.org 70 i+ (list :id "2" :date "30 April 2016" :title "Another message" :short "New version available") Err bitreich.org 70 i (list :id "1" :date "29 April 2016" :title "My first message" :short "This is my first message" :author "Solène") Err bitreich.org 70 i )) Err bitreich.org 70 i Err bitreich.org 70 1diff --git a/generator.lisp b/generator.lisp /scm/cl-yag/file/generator.lisp.gph bitreich.org 70 i@@ -13,12 +13,16 @@ Err bitreich.org 70 i :end (or pos (length string))) Err bitreich.org 70 i when pos do (write-string replacement out) Err bitreich.org 70 i while pos))) Err bitreich.org 70 i-;; load a file and return it as a string Err bitreich.org 70 i-(defun slurp-file(path) Err bitreich.org 70 i- (with-open-file (stream path) Err bitreich.org 70 i- (let ((data (make-string (file-length stream)))) Err bitreich.org 70 i- (read-sequence data stream) Err bitreich.org 70 i- data))) Err bitreich.org 70 i+ Err bitreich.org 70 i+;; load a file as a string Err bitreich.org 70 i+;; we escape ~ to avoid failures with format Err bitreich.org 70 i+(defun load-file(path) Err bitreich.org 70 i+ (replace-all Err bitreich.org 70 i+ (with-open-file (stream path) Err bitreich.org 70 i+ (let ((data (make-string (file-length stream)))) Err bitreich.org 70 i+ (read-sequence data stream) Err bitreich.org 70 i+ data)) Err bitreich.org 70 i+ "~" "~~")) Err bitreich.org 70 i Err bitreich.org 70 i ;; save a string in a file Err bitreich.org 70 i (defun save-file(path data) Err bitreich.org 70 i@@ -33,54 +37,84 @@ Err bitreich.org 70 i ;; simplify the declaration of a new page type Err bitreich.org 70 i (defmacro prepare(template &body code) Err bitreich.org 70 i `(progn Err bitreich.org 70 i- (let ((output (slurp-file ,template))) Err bitreich.org 70 i+ (let ((output (load-file ,template))) Err bitreich.org 70 i ,@code Err bitreich.org 70 i output))) Err bitreich.org 70 i Err bitreich.org 70 i+;; simplify the file saving by using the layout Err bitreich.org 70 i+(defmacro generate(name &body data) Err bitreich.org 70 i+ `(progn Err bitreich.org 70 i+ (save-file ,name Err bitreich.org 70 i+ (generate-layout ,@data)))) Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 i ;; generates the html of one only article Err bitreich.org 70 i ;; this is called in a loop to produce the homepage Err bitreich.org 70 i (defun create-article(article &optional &key (tiny t)) Err bitreich.org 70 i (prepare "template/article.tpl" Err bitreich.org 70 i- (template "%%Author%%" (if (member :author article) (getf article :author) (getf *config* :webmaster))) Err bitreich.org 70 i+ (template "%%Author%%" (getf article :author (getf *config* :webmaster))) Err bitreich.org 70 i (template "%%Date%%" (getf article :date)) Err bitreich.org 70 i (template "%%Title%%" (getf article :title)) Err bitreich.org 70 i (template "%%Id%%" (getf article :id)) Err bitreich.org 70 i (template "%%Text%%" (if (and tiny (member :tiny article)) Err bitreich.org 70 i- (getf article :tiny) (slurp-file (format nil "data/~d.txt" (getf article :id))))))) Err bitreich.org 70 i+ (getf article :tiny) (load-file (format nil "data/~d.txt" (getf article :id))))))) Err bitreich.org 70 i Err bitreich.org 70 i ;; return a html string Err bitreich.org 70 i ;; produce the code of a whole page with title+layout with the parameter as the content Err bitreich.org 70 i (defun generate-layout(body) Err bitreich.org 70 i- (let ((output (slurp-file "template/layout.tpl"))) Err bitreich.org 70 i- (template "%%Title%%" (getf *config* :title)) Err bitreich.org 70 i- (template "%%Body%%" body) Err bitreich.org 70 i- output)) Err bitreich.org 70 i+ (prepare "template/layout.tpl" Err bitreich.org 70 i+ (template "%%Title%%" (getf *config* :title)) Err bitreich.org 70 i+ (template "%%Body%%" body) Err bitreich.org 70 i+ output)) Err bitreich.org 70 i Err bitreich.org 70 i Err bitreich.org 70 i ;; Homepage generation Err bitreich.org 70 i-;; generate each article and concatenate the whole Err bitreich.org 70 i (defun generate-mainpage() Err bitreich.org 70 i+ (prepare "template/layout.tpl" Err bitreich.org 70 i+ (template "%%Body%%" Err bitreich.org 70 i+ (format nil "~{~d~}" Err bitreich.org 70 i+ (loop for article in *articles* collect Err bitreich.org 70 i+ (create-article article :tiny t)))) Err bitreich.org 70 i+ (template "%%Title%%" (getf *config* :title)))) Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 i+;; Generate the items for the xml Err bitreich.org 70 i+(defun generate-rss-item() Err bitreich.org 70 i (format nil "~{~d~}" Err bitreich.org 70 i (loop for article in *articles* collect Err bitreich.org 70 i- (create-article article :tiny t)))) Err bitreich.org 70 i+ (prepare "template/rss-item.tpl" Err bitreich.org 70 i+ (template "%%Title%%" (getf article :title)) Err bitreich.org 70 i+ (template "%%Description%%" (getf article :short "")) Err bitreich.org 70 i+ (template "%%Url%%" Err bitreich.org 70 i+ (format nil "~d/article-~d.html" Err bitreich.org 70 i+ (getf *config* :url) Err bitreich.org 70 i+ (getf article :id))))))) Err bitreich.org 70 i+ Err bitreich.org 70 i+;; Generate the rss xml data Err bitreich.org 70 i+(defun generate-rss() Err bitreich.org 70 i+ (prepare "template/rss.tpl" Err bitreich.org 70 i+ (template "%%Description%%" (getf *config* :description)) Err bitreich.org 70 i+ (template "%%Title%%" (getf *config* :title)) Err bitreich.org 70 i+ (template "%%Url%%" (getf *config* :url)) Err bitreich.org 70 i+ (template "%%Items%%" (generate-rss-item)))) Err bitreich.org 70 i Err bitreich.org 70 i Err bitreich.org 70 i ;; ENGINE START ! Err bitreich.org 70 i ;; This is function called when running the tool Err bitreich.org 70 i (defun generate-site() Err bitreich.org 70 i Err bitreich.org 70 i- ; produce index.html Err bitreich.org 70 i- (generate "index.html" Err bitreich.org 70 i- (generate-mainpage)) Err bitreich.org 70 i- Err bitreich.org 70 i- ; produce each article file Err bitreich.org 70 i+ ;; produce index.html Err bitreich.org 70 i+ (save-file "index.html" Err bitreich.org 70 i+ (generate-mainpage)) Err bitreich.org 70 i+ Err bitreich.org 70 i+ ;; produce each article file Err bitreich.org 70 i (dolist (article *articles*) Err bitreich.org 70 i (generate (format nil "article-~d.html" (getf article :id)) Err bitreich.org 70 i (create-article article :tiny nil))) Err bitreich.org 70 i- Err bitreich.org 70 i+ Err bitreich.org 70 i ;;(generate-file-rss) Err bitreich.org 70 i- ;;not done yet Err bitreich.org 70 i+ (save-file "rss.xml" Err bitreich.org 70 i+ (generate-rss)) Err bitreich.org 70 i ) Err bitreich.org 70 i Err bitreich.org 70 i- Err bitreich.org 70 i (generate-site) Err bitreich.org 70 1diff --git a/template/rss-item.tpl b/template/rss-item.tpl /scm/cl-yag/file/template/rss-item.tpl.gph bitreich.org 70 i@@ -0,0 +1,5 @@ Err bitreich.org 70 i+ Err bitreich.org 70 i+ %%Title%% Err bitreich.org 70 i+ %%Description%% Err bitreich.org 70 i+ %%Url%% Err bitreich.org 70 i+ Err bitreich.org 70 1diff --git a/template/rss.tpl b/template/rss.tpl /scm/cl-yag/file/template/rss.tpl.gph bitreich.org 70 i@@ -0,0 +1,10 @@ Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 i+ %%Title%% Err bitreich.org 70 i+ %%Description%% Err bitreich.org 70 i+ %%Url%% Err bitreich.org 70 i+ Err bitreich.org 70 i+ %%Items%% Err bitreich.org 70 i+ Err bitreich.org 70 i+ Err bitreich.org 70 .