www-gopher/generator.lisp

424 lines
17 KiB
Common Lisp
Raw Permalink Normal View History

2017-12-13 23:23:40 +00:00
;;;; GLOBAL VARIABLES
2017-12-12 18:37:45 +00:00
(defparameter *articles* '())
(defparameter *converters* '())
(defparameter *days* '("Monday" "Tuesday" "Wednesday" "Thursday"
"Friday" "Saturday" "Sunday"))
(defparameter *months* '("January" "February" "March" "April"
"May" "June" "July" "August" "September"
"October" "November" "December"))
2017-12-12 18:37:45 +00:00
;; structure to store links
2017-12-14 23:49:21 +00:00
(defstruct article title tag date id tiny author rawdate converter)
(defstruct converter name command extension)
2017-12-12 18:37:45 +00:00
2017-12-13 23:23:40 +00:00
;;;; FUNCTIONS
2017-12-12 18:37:45 +00:00
2017-12-14 23:49:21 +00:00
(require 'asdf)
;; return the day of the week
(defun get-day-of-week(day month year)
(multiple-value-bind
2017-12-13 23:23:40 +00:00
(second minute hour date month year day-of-week dst-p tz)
(decode-universal-time (encode-universal-time 0 0 0 day month year))
(declare (ignore second minute hour date month year dst-p tz))
day-of-week))
;; parse the date to
(defun date-parse(date)
(if (= 8 (length date))
(let* ((year (parse-integer date :start 0 :end 4))
(monthnum (parse-integer date :start 4 :end 6))
(daynum (parse-integer date :start 6 :end 8))
(day (nth (get-day-of-week daynum monthnum year) *days*))
(month (nth (- monthnum 1) *months*)))
(list
:dayname day
:daynumber daynum
:monthname month
:monthnumber monthnum
:year year))
2017-12-13 23:23:40 +00:00
nil))
(defun post(&optional &key title tag date id (tiny nil) (author (getf *config* :webmaster)) (converter nil))
2017-12-13 23:23:40 +00:00
(push (make-article :title title
:tag tag
:date (date-parse date)
2018-10-12 09:22:34 +00:00
:rawdate date
2017-12-13 23:23:40 +00:00
:tiny tiny
:author author
2017-12-14 23:49:21 +00:00
:id id
2018-10-12 09:22:34 +00:00
:converter converter)
2017-12-13 23:23:40 +00:00
*articles*))
;; we add a converter to the list of the one availables
(defun converter(&optional &key name command extension)
2017-12-14 23:49:21 +00:00
(setf *converters*
2018-10-12 09:22:34 +00:00
(append
(list name
(make-converter :name name
:command command
:extension extension))
*converters*)))
2017-12-13 23:23:40 +00:00
;; load data from metadata and load config
(load "data/articles.lisp")
(setf *articles* (reverse *articles*))
2016-04-30 16:21:48 +00:00
;; common-lisp don't have a replace string function natively
2016-04-30 15:21:31 +00:00
(defun replace-all (string part replacement &key (test #'char=))
(with-output-to-string (out)
2018-10-12 09:22:34 +00:00
(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)))
2016-05-03 12:03:25 +00:00
;; common-lisp don't have a split string function natively
2017-11-28 06:21:33 +00:00
(defun split-str(text &optional (separator #\Space))
"this function split a string with separator and return a list"
(let ((text (concatenate 'string text (string separator))))
(loop for char across text
2018-10-12 09:22:34 +00:00
counting char into count
when (char= char separator)
collect
;; we look at the position of the left separator from right to left
(let ((left-separator-position (position separator text :from-end t :end (- count 1))))
(subseq text
;; if we can't find a separator at the left of the current, then it's the start of
;; the string
(if left-separator-position (+ 1 left-separator-position) 0)
(- count 1))))))
2016-05-03 12:03:25 +00:00
2016-05-03 13:54:02 +00:00
;; load a file as a string
;; we escape ~ to avoid failures with format
(defun load-file(path)
2017-01-23 14:00:40 +00:00
(if (probe-file path)
(replace-all
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
(with-open-file (stream path)
(loop for line = (read-line stream nil)
while line
collect
(format nil "~a~%" line))))
2017-01-23 14:00:40 +00:00
"~" "~~")
(progn
(format t "ERROR : file ~a not found. Aborting~%" path)
(quit))))
2016-05-03 13:54:02 +00:00
2016-04-30 16:21:48 +00:00
;; save a string in a file
2016-04-30 15:21:31 +00:00
(defun save-file(path data)
2016-08-11 12:51:05 +00:00
(with-open-file (stream path :direction :output :if-exists :supersede)
2017-11-03 13:08:48 +00:00
(format stream data)))
2016-04-30 15:21:31 +00:00
;; simplify the str replace work
(defmacro template(before &body after)
`(progn
(setf output (replace-all output ,before ,@after))))
2017-12-14 23:49:21 +00:00
;; get the converter object of "article"
(defmacro with-converter(&body code)
`(progn
2018-05-15 07:26:20 +00:00
(let ((converter-name (if (article-converter article)
(article-converter article)
(getf *config* :default-converter))))
2017-12-14 23:49:21 +00:00
(let ((converter-object (getf *converters* converter-name)))
,@code))))
;; generate the html file from the source file
;; using the converter associated with the post
2017-12-14 23:49:21 +00:00
(defun use-converter-to-html(article)
(with-converter
(let ((output (converter-command converter-object)))
(let* ((src-file (format nil "~a~a" (article-id article) (converter-extension converter-object)))
(dst-file (format nil "temp/data/~a.html" (article-id article) ))
(full-src-file (format nil "data/~a" src-file)))
;; skip generating if the destination exists
;; and is more recent than source
(unless (and
(probe-file dst-file)
(>=
(file-write-date dst-file)
(file-write-date full-src-file)))
(ensure-directories-exist "temp/data/")
(template "%IN" src-file)
(template "%OUT" dst-file)
(format t "~a~%" output)
(uiop:run-program output))))))
2017-12-14 23:49:21 +00:00
;; format the date
(defun date-format(format date)
(let ((output format))
(template "%DayName" (getf date :dayname))
2017-12-13 23:23:40 +00:00
(template "%DayNumber" (format nil "~2,'0d" (getf date :daynumber)))
(template "%MonthName" (getf date :monthname))
2017-12-13 23:23:40 +00:00
(template "%MonthNumber" (format nil "~2,'0d" (getf date :monthnumber)))
(template "%Year" (write-to-string (getf date :year )))
output))
2016-04-30 15:21:31 +00:00
;; simplify the declaration of a new page type
(defmacro prepare(template &body code)
`(progn
(let ((output (load-file ,template)))
2016-04-30 15:21:31 +00:00
,@code
output)))
;; simplify the file saving by using the layout
(defmacro generate(name &body data)
`(progn
2016-05-03 13:54:02 +00:00
(save-file ,name (generate-layout ,@data))))
2018-10-12 09:22:34 +00:00
;; generate a gopher index file
(defun generate-gopher-index(articles)
(let ((output (load-file "templates/gopher_head.tpl")))
(dolist (article articles)
(setf output
(string
(concatenate 'string output
(format nil (getf *config* :gopher-format)
0 ;;;; gopher type, 0 for text files
;; here we create a 80 width char string with title on the left
;; and date on the right
;; we truncate the article title if it's too large
(let ((title (format nil "~80a"
(if (< 80 (length (article-title article)))
(subseq (article-title article) 0 80)
(article-title article)))))
(replace title (article-rawdate article) :start1 (- (length title) (length (article-rawdate article)))))
(concatenate 'string
(getf *config* :gopher-path) "/article-" (article-id article) ".txt")
(getf *config* :gopher-server)
(getf *config* :gopher-port)
)))))
output))
2016-05-03 12:03:25 +00:00
;; generate the list of tags
(defun articles-by-tag()
2016-05-03 12:03:25 +00:00
(let ((tag-list))
(loop for article in *articles* do
2017-12-12 18:37:45 +00:00
(when (article-tag article) ;; we don't want an error if no tag
(loop for tag in (split-str (article-tag article)) 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
2017-12-12 18:37:45 +00:00
:value (push (article-id article) (getf (getf tag-list (intern tag "KEYWORD")) :value)))))))
2016-05-03 12:03:25 +00:00
(loop for i from 1 to (length tag-list) by 2 collect ;; removing the keywords
(nth i tag-list))))
2017-11-03 13:08:48 +00:00
;; generates the html of the list of tags for an article
(defun get-tag-list-article(&optional article)
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
(mapcar #'(lambda (item)
(prepare "templates/one-tag.tpl" (template "%%Name%%" item)))
2017-12-12 18:37:45 +00:00
(split-str (article-tag article)))))
;; generates the html of the whole list of tags
(defun get-tag-list()
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
(mapcar #'(lambda (item)
(prepare "templates/one-tag.tpl"
(template "%%Name%%" (getf item :name))))
(articles-by-tag))))
2017-11-03 13:08:48 +00:00
2017-12-13 23:23:40 +00:00
;; generates the html of only one article
2016-04-30 16:21:48 +00:00
;; this is called in a loop to produce the homepage
2017-01-21 15:53:27 +00:00
(defun create-article(article &optional &key (tiny t) (no-text nil))
2017-11-17 14:51:35 +00:00
(prepare "templates/article.tpl"
2017-12-12 18:37:45 +00:00
(template "%%Author%%" (let ((author (article-author article)))
(or author (getf *config* :webmaster))))
(template "%%Date%%" (date-format (getf *config* :date-format)
2017-12-13 23:23:40 +00:00
(article-date article)))
(template "%%Raw-Date%%" (article-rawdate article))
(template "%%Title%%" (article-title article))
(template "%%Id%%" (article-id article))
2017-11-03 13:08:48 +00:00
(template "%%Tags%%" (get-tag-list-article article))
2017-12-13 23:23:40 +00:00
(template "%%Date-Url%%" (date-format "%Year-%MonthNumber-%DayNumber"
(article-date article)))
2017-11-03 13:08:48 +00:00
(template "%%Text%%" (if no-text
""
2017-12-12 18:37:45 +00:00
(if (and tiny (article-tiny article))
(article-tiny article)
(load-file (format nil "temp/data/~d.html" (article-id article))))))))
2016-04-30 15:21:31 +00:00
2016-04-30 16:21:48 +00:00
;; return a html string
;; produce the code of a whole page with title+layout with the parameter as the content
(defun generate-layout(body &optional &key (title nil))
2017-11-17 14:51:35 +00:00
(prepare "templates/layout.tpl"
2018-05-15 07:26:20 +00:00
(template "%%Title%%" (if title title (getf *config* :title)))
(template "%%Tags%%" (get-tag-list))
(template "%%Body%%" body)
output))
2016-04-30 16:21:48 +00:00
2016-05-03 12:03:25 +00:00
;; html generation of index homepage
2017-01-21 15:53:27 +00:00
(defun generate-semi-mainpage(&key (tiny t) (no-text nil))
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
(loop for article in *articles* collect
(create-article article :tiny tiny :no-text no-text))))
2016-05-03 12:03:25 +00:00
;; html generation of a tag homepage
(defun generate-tag-mainpage(articles-in-tag)
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
2018-10-12 09:22:34 +00:00
(loop for article in *articles*
2017-12-12 18:37:45 +00:00
when (member (article-id article) articles-in-tag :test #'equal)
2017-11-28 06:33:25 +00:00
collect (create-article article :tiny t))))
2016-05-03 12:03:25 +00:00
;; xml generation of the items for the rss
2018-04-17 06:06:04 +00:00
(defun generate-rss-item(&key (gopher nil))
2017-11-28 06:33:25 +00:00
(apply #'concatenate 'string
(loop for article in *articles*
for i from 1 to (min (length *articles*) (getf *config* :rss-item-number))
2017-11-28 06:33:25 +00:00
collect
(prepare "templates/rss-item.tpl"
2017-12-12 18:37:45 +00:00
(template "%%Title%%" (article-title article))
(template "%%Description%%" (load-file (format nil "temp/data/~d.html" (article-id article))))
2017-12-13 23:23:40 +00:00
(template "%%Date%%" (format nil
(date-format "~a, %DayNumber ~a %Year 00:00:00 GMT"
(article-date article))
(subseq (getf (article-date article) :dayname) 0 3)
(subseq (getf (article-date article) :monthname) 0 3)))
2017-11-28 06:33:25 +00:00
(template "%%Url%%"
2018-04-17 06:06:04 +00:00
(if gopher
2018-05-15 07:26:20 +00:00
(format nil "gopher://~a:~d/0~a/article-~a.txt"
2018-04-17 06:06:04 +00:00
(getf *config* :gopher-server)
(getf *config* :gopher-port)
(getf *config* :gopher-path)
(article-id article))
(format nil "~d~d-~d.html"
(getf *config* :url)
(date-format "%Year-%MonthNumber-%DayNumber"
(article-date article))
(article-id article))))))))
2017-11-28 06:33:25 +00:00
;; Generate the rss xml data
2018-04-17 06:06:04 +00:00
(defun generate-rss(&key (gopher nil))
2017-11-17 14:51:35 +00:00
(prepare "templates/rss.tpl"
(template "%%Description%%" (getf *config* :description))
(template "%%Title%%" (getf *config* :title))
(template "%%Url%%" (getf *config* :url))
2018-04-17 06:06:04 +00:00
(template "%%Items%%" (generate-rss-item :gopher gopher))))
2016-04-30 15:21:31 +00:00
2016-08-11 12:51:05 +00:00
;; We do all the website
(defun create-html-site()
2017-01-21 15:53:27 +00:00
;; produce each article file
2017-12-13 23:23:40 +00:00
(loop for article in *articles*
do
2017-12-14 23:49:21 +00:00
;; use the article's converter to get html code of it
(use-converter-to-html article)
2017-12-13 23:23:40 +00:00
(generate (format nil "output/html/~d-~d.html"
(date-format "%Year-%MonthNumber-%DayNumber"
(article-date article))
(article-id article))
(create-article article :tiny nil)
:title (concatenate 'string (getf *config* :title) " : " (article-title article))))
2017-12-14 23:49:21 +00:00
;; produce index.html
(generate "output/html/index.html" (generate-semi-mainpage))
;; produce index-titles.html where there are only articles titles
(generate "output/html/index-titles.html" (generate-semi-mainpage :no-text t))
2018-10-12 09:22:34 +00:00
2016-05-03 12:03:25 +00:00
;; produce index file for each tag
(loop for tag in (articles-by-tag) do
2018-10-12 09:22:34 +00:00
(generate (format nil "output/html/tag-~d.html" (getf tag :NAME))
2016-05-03 12:03:25 +00:00
(generate-tag-mainpage (getf tag :VALUE))))
2018-04-17 06:06:04 +00:00
;; generate rss gopher in html folder if gopher is t
(when (getf *config* :gopher)
(save-file "output/html/rss-gopher.xml" (generate-rss :gopher t)))
2016-04-30 15:21:31 +00:00
;;(generate-file-rss)
2016-08-11 12:51:05 +00:00
(save-file "output/html/rss.xml" (generate-rss)))
;; we do all the gopher hole
(defun create-gopher-hole()
2018-04-17 06:06:04 +00:00
;;(generate-file-rss)
(save-file "output/gopher/rss.xml" (generate-rss :gopher t))
2016-08-11 12:51:05 +00:00
;; produce the gophermap file
(save-file (concatenate 'string "output/gopher/" (getf *config* :gopher-index))
2018-10-12 09:22:34 +00:00
(generate-gopher-index *articles*))
;; produce a tag list menu
(let* ((directory-path "output/gopher/_tags_/")
(index-path (concatenate 'string directory-path (getf *config* :gopher-index))))
(ensure-directories-exist directory-path)
(save-file index-path
(let ((output (load-file "templates/gopher_head.tpl")))
2018-10-12 09:50:16 +00:00
(loop for tag in
;; sort tags per articles in it
(sort (articles-by-tag) #'>
:key #'(lambda (x) (length (getf x :value))))
2018-10-12 09:22:34 +00:00
do
(setf output
(string
(concatenate
'string output
(format nil (getf *config* :gopher-format)
1 ;; gopher type, 1 for menus
2018-10-12 09:50:16 +00:00
;; here we create a 72 width char string with title on the left
;; and number of articles on the right
;; we truncate the article title if it's too large
(let ((title (format nil "~72a"
(if (< 72 (length (getf tag :NAME)))
(subseq (getf tag :NAME) 0 80)
(getf tag :NAME))))
(article-number (format nil "~d article~p" (length (getf tag :value)) (length (getf tag :value)))))
(replace title article-number :start1 (- (length title) (length article-number))))
2018-10-12 09:22:34 +00:00
(concatenate 'string
(getf *config* :gopher-path) "/" (getf tag :NAME) "/")
(getf *config* :gopher-server)
(getf *config* :gopher-port)
)))))
output)))
;; produce each tag gophermap index
(loop for tag in (articles-by-tag) do
(let* ((directory-path (concatenate 'string "output/gopher/" (getf tag :NAME) "/"))
(index-path (concatenate 'string directory-path (getf *config* :gopher-index)))
(articles-with-tag (loop for article in *articles*
when (member (article-id article) (getf tag :VALUE) :test #'equal)
collect article)))
(ensure-directories-exist directory-path)
(save-file index-path (generate-gopher-index articles-with-tag))))
;; produce each article file (adding some headers)
2017-12-14 23:49:21 +00:00
(loop for article in *articles*
do
(with-converter
(let ((id (article-id article)))
(save-file (format nil "output/gopher/article-~d.txt" id)
(format nil "~{~a~}"
(list
"Title: " (article-title article) "~%"
"Author: " (article-author article) "~%"
"Date: " (date-format (getf *config* :date-format) (article-date article)) "~%"
"Tags: " (article-tag article) "~%"
"==========~%~%"
(load-file (format nil "data/~d~d" id (converter-extension converter-object))))))))))
2016-04-30 15:21:31 +00:00
2016-08-11 12:51:05 +00:00
;; This is function called when running the tool
(defun generate-site()
(if (getf *config* :html)
(create-html-site))
(if (getf *config* :gopher)
(create-gopher-hole)))
2017-12-13 23:23:40 +00:00
;;;; EXECUTION
2017-12-12 18:37:45 +00:00
2016-04-30 15:21:31 +00:00
(generate-site)
2017-12-12 18:37:45 +00:00
2017-11-28 06:21:33 +00:00
(quit)