Blob


1 Some time ago I wrote a text/gemini parser for Clojure. More than a real parser was a hack, an extremely verbose one, just to play with this transducer thing.
3 Well, I got tired of that and rewrote it as a standalone library that is now available on clojars.
5 => https://clojars.org/com.omarpolo/gemtext/ com.omarpolo/gemtext on Clojars.
7 There’s still a transducer at the heart of the library, but it’s a more reasonable one this time. It allows to build up pipelines where parsing text/gemini is only one of the steps, or to parse streaming text/gemini, which is a cool thing (even tho not widespread.)
9 Since this is a library and not a hack inside this blog codebase, ‘parse’ is now a multimethod (Clojure own generic functions if you came from CL-land) with default implementations for strings, sequence of strings and java Reader.
11 Since the parser is built using nothing more than the clojure stdlib, I thought “why not” and called the file ‘core.cljc’, so it’s available also for ClojureScript. (The beforementioned multimethod is available also there, with a default implementation for vectors, lists and strings.)
13 The library emits “almost usual” hiccup:
15 ```clojure
16 user=> (gemtext/parse "some\nlines\nof\ntext")
17 [[:text "some"] [:text "lines"] [:text "of"] [:text "text"]]
19 user=> (gemtext/parse (repeat 3 "* test"))
20 [[:item "test"] [:item "test"] [:item "test"]]
21 ```
23 and is able to turn ’em back to strings:
25 ```clojure
26 user=> (gemtext/unparse [[:link "/foo" "A link"]])
27 "=> /foo A link\n"
28 ```
30 but also to return “HTML” hiccup
32 ```clojure
33 user=> (gemtext/to-hiccup [[:header-1 "text/gemini"] [:text "..."]])
34 [[:h1 "text/gemini"] [:p "..."]]
35 ```
37 so you can use it with other Clojure/script libraries, and to convert text/gemini to HTML.
39 It was fun: I use clojure a lot, but never actually wrote a library, so this was a chance to play with different things. First of, the (small) documentation is available also on cljdoc, and second I played with ‘seancorfield/readme’, a Clojure library that transforms your README to a REPL session!
41 A final note: the design is done, but in the following weeks I may slighly change something here and there (for instance, only now I realize that you can parse text/gemini on the fly, but not convert it to HTML one bit at a time, i.e. “convert text/gemini to html streamingly” (?) which can be useful, or unparse into anything other than a string.)
44 (P.S. I took the chance to also to restyle the capsule. I removed the ASCII banners and followed the subscription spec, yay!)