write literate haskell programs in typst cdn.oppi.li/typst-unlit.pdf
haskell typst

regen readme

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li f8a53ade 79988ad1

verified
+14 -8
+14 -8
readme.md
··· 1 1 # Typst-Unlit 2 2 3 + Write literate Haskell programs in Typst 4 + 3 5 *[tangled.org/@oppi.li/typst-unlit](https://tangled.org/@oppi.li/typst-unlit)* 4 6 5 - *Serves: 1 Prep Time: 10min Compile Time: 10ms* 7 + **Serves: 1 Prep Time: 10min Compile Time: 10ms** 6 8 7 9 A literate program is one where comments are first-class citizens, and 8 10 code is explicitly demarcated, as opposed to a regular program, where ··· 12 14 preprocessor to extract code from documents. This preprocessor is known 13 15 as *unlit*[^1]. GHC also supports *custom* preprocessors, which can be 14 16 passed in via the `-pgmL` flag. This very document you are reading, is 15 - one such preprocessor that allows embedding Haskell code inside typst 16 - files[^2]. 17 + one such preprocessor program that allows extracting Haskell from Typst 18 + code (although it has been rendered to HTML, PDF or markdown depending 19 + on where you are reading it)[^2]! 17 20 18 21 This recipe not only gives you a fish (the typst-unlit preprocessor), 19 22 but also, teaches you how to fish (write your own preprocessors). ··· 53 56 with an empty line! To detect lines that are Haskell, we look for the 54 57 ```` ```haskell ```` directive and stop at the end of the code fence. 55 58 Simple enough! Annoyingly, Haskell requires that imports be declared at 56 - the top of the file. This results in literate haskell programs always 59 + the top of the file. This results in literate Haskell programs always 57 60 starting with a giant block of imports: 58 61 59 62 > -- So first we need to get some boilerplate and imports out of the way. ··· 73 76 - `-h`: ignore this for now 74 77 - `<label>`: ignore this for now 75 78 - `<infile>`: the input lhaskell source code 76 - - `<outfile>`: the output haskell source code 79 + - `<outfile>`: the output Haskell source code 77 80 78 81 Invoke the runes to handle CLI arguments: 79 82 ··· 210 213 ghc -o typst-unlit typst-unlit.hs 211 214 ``` 212 215 213 - And now, we can execute our preprocessor on literate haskell files! 216 + And now, we can execute our preprocessor on literate Haskell files! 214 217 215 218 ## Serving 216 219 217 - To test our preprocessor, first, write a literate haskell file 220 + To test our preprocessor, first, write a literate Haskell file 218 221 containing your typst code: 219 222 220 223 ```` ··· 265 268 ``` 266 269 #!/usr/bin/env bash 267 270 268 - # this does the same thing as typst-unlit.lhs, but depends on `typst` and `jq` 271 + # this does the same thing as typst-unlit.lhs, but depends on typst and jq 272 + # this script does clobber the line numbers, so users beware 269 273 270 274 typst query "$3" 'raw.where(lang: "haskell-top")' | jq -r '.[].text' > "$4" 271 275 typst query "$3" 'raw.where(lang: "haskell")' | jq -r '.[].text' >> "$4" 272 276 ``` 277 + 278 + This document mentions the word “Haskell” 60 times. 273 279 274 280 [^1]: <https://gitlab.haskell.org/ghc/ghc/-/tree/master/utils/unlit> 275 281