Skip to content

Instantly share code, notes, and snippets.

@enshiromashiro
Created May 13, 2017 14:01
Show Gist options
  • Select an option

  • Save enshiromashiro/984e0b913488181cb5f5a62b0b99c7e0 to your computer and use it in GitHub Desktop.

Select an option

Save enshiromashiro/984e0b913488181cb5f5a62b0b99c7e0 to your computer and use it in GitHub Desktop.
#!/bin/sh
#|-*- mode:lisp -*-|#
#| generate my site
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
(ros:ensure-asdf)
(ql:quickload '(:cl-arrows :cl-ppcre :djula :rosa) :silent t))
(defpackage :ros.script.sitegen.3703245828
(:use :cl))
(in-package :ros.script.sitegen.3703245828)
(use-package :cl-arrows)
(defun render-markup (body)
(-<> body
(string-trim '(#\newline) <>)
(ppcre:regex-replace-all "\\n" <> (format nil "<br />~%"))
(ppcre:regex-replace-all "\{([^{}|]+)\\|([^{}|]+)\}"
<>
"<ruby>\\1<rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>")
(ppcre:regex-replace-all "\\+([^+]+)\\+"
<>
"<span class=\"emphasis\">\\1</span>")
(ppcre:regex-replace-all "(^|\\n)# ([^\\n]+)\\n+"
<>
(format nil "~%<h2>\\2</h2>~%"))
(ppcre:regex-replace-all "(^|\\n)## ([^\\n]+)\\n+"
<>
(format nil "~%<h3>\\2</h3>~%"))
(ppcre:regex-replace-all "(^|\\n)### ([^\\n]+)\\n+"
<>
(format nil "~%<h4>\\2</h4>~%"))
(ppcre:regex-replace-all "(^|\\n)>+ ([^\\n]+)*"
<>
(format nil "~%   \\2~%"))))
(defun page-from (pathname)
(let ((data (with-open-file (in pathname)
(rosa:peruse-as-plist in))))
(loop
:for (k v) :on data :by #'cddr
:if (not (member k '(:|person-name| :|person-description|)))
:append (list k (aref v 0))
:else
:append (list k v))))
(defun main (&rest argv)
(if (< (length argv) 2)
(format t "usage sitegen.ros TEMPLATE TEXT~%")
(let* ((template (djula:compile-template* (pathname (nth 0 argv))))
(input-pathname (pathname (nth 1 argv)))
(output-pathname (make-pathname :directory (pathname-directory input-pathname)
:name (pathname-name input-pathname)
:type "html"))
(txt (page-from input-pathname)))
(when (and (getf txt :|person-name|)
(getf txt :|person-description|)
(= (length (getf txt :|person-name|))
(length (getf txt :|person-description|))))
(setf (getf txt :|persons|)
(loop
:for name :across (getf txt :|person-name|)
:for desc :across (getf txt :|person-description|)
:for image-url :across (let ((image-urls (getf txt :|person-image-url|)))
(if image-urls
image-urls
(apply #'vector
(loop
:for n :across (getf txt :|person-name|)
:collect "/default.png"))))
:collect (list :|name| name
:|description| (render-markup desc)
:|image-url| image-url))))
(setf (getf txt :|body|) (render-markup (getf txt :|body|)))
(setf (getf txt :|postscript|) (render-markup (getf txt :|postscript|)))
(with-open-file (out output-pathname :direction :output :if-exists :supersede)
(apply #'djula:render-template* `(,template ,out ,@txt))))))
;;; vim: set ft=lisp lisp:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment