5 ms·
There is a port of the CL-WHO HTML templater or TXR Lisp called TL-WHO. It has a deftag macro for defining new tags. $ txr -i tl-who.tl If you get your ma
by kazinator 3y ago
There is a port of the CL-WHO HTML templater or TXR Lisp called TL-WHO.
It has a deftag macro for defining new tags.
$ txr -i tl-who.tl
If you get your macros hot enough, you get syntactic caramel!
1> (in-package :tl-who)
#<package: tl-who>
We can define :atag, which destructures the first two items of its body as url and class:
2> (deftag :atag other-attrs (url cls . body)
^(:a :href ,url :class ,cls ,*other-attrs ,*body))
#<interpreted fun: lambda (#:g0203 . other-attrs)>
Then use that in the definition of :page:
3> (deftag :page () (url . body)
^(:atag ,url "pages" ,*body))
#<interpreted fun: lambda (#:g0217 . #:g0232)>
So now that we have :page, wrap that in :li:
4> (with-html-output (*stdout*)
(:li (:page "./writing" "My writing")))
<li><a href='./writing' class='pages'>My writing</a></li>
TL-WHO, like CL-WHO, compiles the with-html-output form into something that uses constant string literals that are as long as possible. It closes all your tags, HTML-escapes what has to be escaped and can pretty-print with indentation.
Code can be freely used: attributes and tags are distinguished by virtue of being Lisp keywords. Anything that is not a keyword is evaluated as code.
Only when code produces a string is that string interpolated into the output, making it possible to write code with useful side effects and no HTML output, by not returning a string.
Side-effecting code can explicitly write into the HTML stream; the output will go to the right place in the generated HTML:
5> (with-html-output (*stdout*)
(:div :class "foo"
(:pre (:code (sh "ls -l /etc/a*.conf")))))
<div class='foo'><pre><code>-rw-r--r-- 1 root root 3026 Dec 16 2019 /etc/adduser.conf
-rw-r--r-- 1 root root 433 Oct 1 2017 /etc/apg.conf
-rw-r--r-- 1 root root 769 Apr 4 2018 /etc/appstream.conf
</code></pre></div>
Note that the output of "ls -l" isn't being HTML escaped; this example just shows how something can write to the stream as an alternative to interpolation, not that it's a good idea to dump ls -l into that HTML stream.