61 points · by lelanthran · 3y ago · 61 comments
(h1 :disabled Hello world!)
(div :class="alert"
So long, and (b thanks) for all the (em fish))
I hate that syntax for attributes.:class="alert" is strictly worse than ((class . "alert")) or any other variation that actually leverages s-expressions instead of merging key and value into the same symbol. Ew.
Not a fan of the bare words instead of strings either, which means this cannot be naively evaluated by a Lisp.
I wouldn't say this is producing html from s-exps, rather that it is vaguely inspired by them.
I hear you, but I wasn't aiming for leveraging the power of macros. Using `((class . "alert"))` does not provide any benefits over :class="alert".
> Not a fan of the bare words instead of strings either, which means this cannot be naively evaluated by a Lisp.
I traded off ergonomics against "Evaluation by a Lisp processor".Instead of
(div "My first " (b "hello") (em " world"))
I'd rather do (div My first (b hello) (em world))
Thank you for your input.Of course it does. It is a s-exp, like everything else in this language, so it's just another list you can map, apply or otherwise transform. The keyword approach instead requires a weird incantation of
(string-trim (cadr (string-split (keyword->string :class="alert") ?=)) ?")
to extract the value from the attribute. For what benefit? If the goal is using s-exps, why not use s-exps for everything?My goal wasn't "Use s-expressions", it's "More readable and writable HTML trees."
This isn't a Lisp interpreter either, TBH. There's plenty of those around and they require the user to use a Lisp implementation, and write Lisp code.
map, string-trim, car, cadr etc are not required for s-expressions, they're required for Lisp.
Also, what if you don't want a space between the bold hello and emphasized world? Or between first and <b>hello</b>?
Templating wasn't mentioned (yet. looking at the feedback I got, some avenues are open for some sort of templating).
> Also, what if you don't want a space between the bold hello and emphasized world?
Then you don't put one in?
$ echo "(div My first (b hello)(em world)) " | ./l2h -s
<div>My first <b>hello</b><em>world</em></div>
> Or between first and <b>hello</b>?Again, don't put one in:
echo "(div My first(b hello) (em world))" | ./l2h -s
<div>My first<b>hello</b> <em>world</em></div>
I think that that is is intuitive as it can get for people who know HTML: space-emission matches what you would expect for HTML.If you look at the git history, I spent a lot of time making sure that the parsing of whitespace and newlines made sense from an HTML perspective, not from a Lisp perspective.
(div (class "alert") So long, and (b thanks) for all the (em fish))
or (div {class "alert"} So long, and (b thanks) for all the (em fish))(body (style "styleinfo")) mean
<body style="styleinfo"></body>
Or <body><style>styleinfo</style></body>?
(div (class [alert someothercss]) [so long and (b [thanks]) for all the (em [fish and chips])])Most Lisps (all that I know of) have a way to convert symbols to strings. The hard part is resolving the ambiguity between what should be text, and what should be treated as an HTML node. Like, what if I wanted to actually write the string "(b thanks)" on a page? R7RS could probably do this with the pipe identifier syntax[0], but I think R7RS is fairly unique in its capability for identifiers (and thus symbols) with such a wide range of supported characters.
[0] https://standards.scheme.org/corrected-r7rs/r7rs-Z-H-4.html#...
This doesn't have variables. Or functions.
It is consistent with HTML however, especially the fact that both collapse whitespace.
Do Lisp implementations usually garbage-collect interned symbols when they're no longer used?
TXR Lisp:
This is the TXR Lisp interactive listener of TXR 291.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
I'm confused; is this piano recital Rimsky-Korsakov or Wernicke–Korsakoff?
1> (progn [finalize (gensym "foo-") prinl] nil)
nil
2> (sys:gc)
#:foo-0012
t
3> (sys:gc)
t
We make a gensym whose name starts with foo-. We register a GC finalizer for it which is the prinl function (print, with newline).Then when we manually invoke GC, it gets collected; we see the print.
Internet symbols cannot be collected, normally, because they are always used: they are registered in their package.
TXR Lisp has weak packages, though. A weak package will purge a symbol that is not reachable other than through the package.
1> (make-package "abc" t) ;; t argument here means weak
#<package: abc>
2> (progn [finalize (intern "xyz" *1) prinl] nil)
nil
3> (sys:gc)
abc:xyz
t
4> (package-symbols *1)
nil
5>
Don't know if other Lisp people thunk of this one, but there it is.I don't think I exposed this weak property through the defpackage macro. It's intended for specific scenarios, like reading data in the context of a package.
The initial driver for adoption was that Oleg made an excellent XML parser (SSAX) that used SXML. Then a bunch of people (including me) built new tools to work with SXML, and changed existing tools to use it.
Later, I realized that SXML permitting unnecessary list nesting, which initially seemed sloppy and inefficient, actually has a very useful efficiency property, once you're using immutable lists: you can compose larger XML with a small allocation and no mutation like:
(list some-huge-tree another-huge-tree)
Racket-specific starting point: https://docs.racket-lang.org/sxml-intro/index.htmlSee Reagent which uses Hiccup syntax: https://reagent-project.github.io/
(defn simple-component []
[:div
[:p "I am a component!"]
[:p.someclass
"I have " [:strong "bold"]
[:span {:style {:color "red"}} " and red "] "text."]]) <div disabled>
represents the attribute value rather than the attribute name. The value has to be declared in an enumeration and must be different from all other enumerated values (of other elements) for the parser to infer the attribute and emit canonical markup such as <div disabled="disabled">
In HTML, it just so happens that the attribute name in what the WHATWG HTML spec calls a "Boolean attribute" is always the same as the single allowed value. However, WHATWG managed to break that in the latest spec, allowing "until-found" as additional value for the "hidden" attribute [1].Nothing is a faster converter than avoiding a conversion step altogether... but that is not why I did it; I used that technique in my Ph.D. thesis back in 2007 [1,2] to avoid directories full with hundreds of auxiliary files (xml/ html.tmp/ ..), which may be confusing to manage, slow to copy, and eventually lead to the file system running out of i-nodes.
EDIT:
[1] https://www.sciencedirect.com/science/article/abs/pii/S01989...
[2] pp. 132-134 in https://era.ed.ac.uk/bitstream/handle/1842/1849/leidner-2007...
I don't know what that means - it currently uses the first element of the list as the tagname, so you can use whatever tags you want to.
> and imports and this becomes a very, very neat static site generation tool.
I've toyed with the idea of adding more functionality (like imports, environment variables, conditionals, etc).
Not too sure I want to go in that direction.
If the parser comes across a tag it doesn't know, it emits it verbatim. If it comes across a tag that is defined as a custom tag, then it replaces the tag expression with the expanded tag template body ("expanded" here meaning the parameters are substituted).
Similar to JSX, make an implicit parameter called "children" that holds all child elements, and then allow the templates to refer to attributes with a special expression (e.g. `(attr children)` or something, or maybe just `(:children)` if that doesn't break syntax rules).
Not sure how it might be supported in this library, but Hiccup (in the Clojure) ecosystem is similar and lets you do this with functions. This is a simple example of a scrollable div with a title block.
(defelem scroll-column [ id title & contents ]
[:div.scroll-column
[:div.fixed title]
[:div.scrollable { :id id :data-preserve-scroll "true" }
contents]])
Note this (and Hiccup in general) heavily depends on Clojure array and hash literals. This wouldn't look as clean in a Lisp without those things.This sounds like a good idea (could call it a `macro` but it might annoy readers who expect Lisp-type macros).
Something to DRY the input a little, maybe. I don't want it veering off too much into LispLand because then it's bound to be annoying to devs who wanted a more Lisp-conformant tool.
And still I can't find one that I like (which is why I also wrote one myself :D).
m("h1", {class: "title"}, "My first app")Here’s a simplified implementation: https://github.com/uxtely/js-utils/tree/main/react-create-el...
I've tried to engage with everyone - I apologise to those I didn't engage with or answer adequately.
You are quite correct, my goal appeared to be "Lisp interpreted HTML", my actual goal is "Write HTML tag trees easier".
I want to use them but I cannot find a spec like for JSON
High praise indeed.
Apart from scheme, I have only done XML things in Python and C#, and that was a less than stellar experience.