Scheme Gemtext Reader

siiky

2022/07/05

2022/07/05

en

Yesterday I made a Scheme library to read Gemtext into a simple AST: a list of lines of text, headers, links, list blocks, or code blocks.

Text

some text
"some text"

Just a string, not tagged in any way. Empty lines are kept.

Headers

^(#+)\s+(.*)$
`(header ,(string-length $1) ,$2)

If *strict-gemtext-headers* is enabled, header lines of level > 3 are not considered headers.

##### title
"##### title"       ; (w/ *strict-gemtext-headers* enabled)
'(header 5 "title") ; (w/ *strict-gemtext-headers* disabled)

Links

=> some-uri.gmi Optional alt text
'(link "some-uri.gmi" "Optional alt text")

If there's no alt text, the empty string is used:

=> some-uri.gmi
'(link "some-uri.gmi" "")

Lists

* item 1
* item 2
'(list "item 1"
       "item 2")

Code blocks

(pretend there's no space between the three backticks)

`` `some optional alt text
some
pre-formatted
text
`` `
'(code "some optional alt text"
       "some"
       "pre-formatted"
       "text")

If there's no alt text, the empty string is used:

`` `some optional alt text
some
pre-formatted
text
`` `
'(code ""
       "some"
       "pre-formatted"
       "text")

Example

# Factorial

There are two steps to compute the factorial of a number:

* Compute the list of integers from 1 up to the number
* Multiply all the integers of the list

=> gemini://gemi.dev/cgi-bin/wp.cgi/view/en?Factorial
=> gemini://gemi.dev/cgi-bin/wp.cgi/view/en?Factorial Factorial

## Haskell code

Here's the code in Haskell:

`` `hs
fact n = prod [1..n]
`` `
'((header 1 "Factorial")
  ""
  "There are two steps to compute the factorial of a number:"
  ""
  (list "Compute the list of integers from 1 up to the number"
        "Multiply all the integers of the list")
  ""
  (link "gemini://gemi.dev/cgi-bin/wp.cgi/view/en?Factorial" "")
  (link "gemini://gemi.dev/cgi-bin/wp.cgi/view/en?Factorial" "Factorial")
  ""
  (header 2 "Haskell code")
  ""
  "Here's the code in Haskell:"
  ""
  (code "hs"
        "fact n = prod [1..n]"))

Output Patterns

(header level title)
(link uri alt-text)
(code alt-text . lines)
(list . items)