tag support - 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 b6217bd19691e99255d6ea2ae57c14c6d48b073a DIR parent 304d6befcc4319581253099320092cb6cbee5bd1 HTML Author: solene rapenne <solene@dataswamp.org> Date: Tue, 3 May 2016 14:03:25 +0200 tag support Diffstat: A data/2.txt | 1 + M data/articles.lisp | 4 ++-- M generator.lisp | 51 +++++++++++++++++++++++++++++-- M template/layout.tpl | 1 + A template/one-tag.tpl | 1 + 5 files changed, 54 insertions(+), 4 deletions(-) --- DIR diff --git a/data/2.txt b/data/2.txt @@ -0,0 +1 @@ +<p>hello</p> DIR diff --git a/data/articles.lisp b/data/articles.lisp @@ -18,7 +18,7 @@ ;; :tiny can be omitted and will be replaced by the full article text (defvar *articles* (list - (list :id "2" :date "30 April 2016" :title "Another message" :short "New version available") - (list :id "1" :date "29 April 2016" :title "My first message" :short "This is my first message" :author "Solène") + (list :id "2" :date "30 April 2016" :tag "lisp" :title "Another message" :short "New version available") + (list :id "1" :date "29 April 2016":tag "pony code" :title "My first message" :short "This is my first message" :author "Solène") )) DIR diff --git a/generator.lisp b/generator.lisp @@ -14,6 +14,20 @@ when pos do (write-string replacement out) while pos))) +;; common-lisp don't have a split string function natively +;; thanks https://gist.github.com/siguremon/1174988 +(defun split-str (string &optional (separator " ")) + (split-str-1 string separator)) +(defun split-str-1 (string &optional (separator " ") (r nil)) + (let ((n (position separator string + :from-end t + :test #'(lambda (x y) + (find y x :test #'string=))))) + (if n + (split-str-1 (subseq string 0 n) separator (cons (subseq string (1+ n)) r)) + (cons string r)))) + + ;; load a file as a string ;; we escape ~ to avoid failures with format (defun load-file(path) @@ -47,6 +61,21 @@ (save-file ,name (generate-layout ,@data)))) +;; generate the list of tags +(defun the-tags() + (let ((tag-list)) + (loop for article in *articles* do + (if (getf article :tag nil) ;; we don't want an error if no tag + (loop for tag in (split-str (getf article :tag)) do ;; for each word in tag keyword + (setf (getf tag-list (intern tag "KEYWORD")) ;; we create the keyword is inexistent and add ID to :value + (list + :name tag + :value (push (getf article :id) (getf (getf tag-list (intern tag "KEYWORD")) :value))))))) + (loop for i from 1 to (length tag-list) by 2 collect ;; removing the keywords + (nth i tag-list)))) + + + ;; generates the html of one only article ;; this is called in a loop to produce the homepage @@ -64,18 +93,28 @@ (defun generate-layout(body) (prepare "template/layout.tpl" (template "%%Title%%" (getf *config* :title)) + (template "%%Tags%%" + (format nil "~{~d~}" (loop for tag in (the-tags) collect + (prepare "template/one-tag.tpl" + (template "%%Name%%" (getf tag :name)))))) (template "%%Body%%" body) output)) -;; Homepage generation +;; html generation of index homepage (defun generate-semi-mainpage() (format nil "~{~d~}" (loop for article in *articles* collect (create-article article :tiny t)))) +;; html generation of a tag homepage +(defun generate-tag-mainpage(articles-in-tag) + (format nil "~{~d~}" + (loop for article in *articles* + when (member (getf article :id) articles-in-tag :test #'equal) + collect (create-article article :tiny t)))) -;; Generate the items for the xml +;; xml generation of the items for the rss (defun generate-rss-item() (format nil "~{~d~}" (loop for article in *articles* collect @@ -107,9 +146,17 @@ (dolist (article *articles*) (generate (format nil "article-~d.html" (getf article :id)) (create-article article :tiny nil))) + + ;; produce index file for each tag + (loop for tag in (the-tags) do + (generate (format nil"tag-~d.html" (getf tag :NAME)) + (generate-tag-mainpage (getf tag :VALUE)))) ;;(generate-file-rss) (save-file "rss.xml" (generate-rss)) + ) (generate-site) + + DIR diff --git a/template/layout.tpl b/template/layout.tpl @@ -9,6 +9,7 @@ <div id="top"> <a href="index.html">Home</a> <a href="rss.xml">Rss</a> + <p>Tags : %%Tags%%</p> </div> <div id="content"> DIR diff --git a/template/one-tag.tpl b/template/one-tag.tpl @@ -0,0 +1 @@ + <a href="tag-%%Name%%.html">%%Name%%</a>