# Generate documentation
(def- prelude
```
Janet Language Documentation
```)
(def- postlude
```
```)
(def- escapes
{10 "
"
09 " "
38 "&"
60 "<"
62 ">"
34 """
39 "'"
47 "/"})
(defn- trim-lead
"Trim leading newlines"
[str]
(var i 0)
(while (= 10 str.i) (++ i))
(string/slice str i))
(defn- html-escape
"Escape special characters for HTML encoding."
[str]
(def buf @"")
(loop [byte :in str]
(if-let [rep escapes.byte]
(buffer/push-string buf rep)
(buffer/push-byte buf byte)))
buf)
(defn- gen-one
"Generate documentation for a binding. Returns an
html fragment."
[key docstring]
(if-let [index (string/find "\n" docstring)]
(let [first-line (html-escape (string/slice docstring 0 index))
rest (html-escape (trim-lead (string/slice docstring (inc index))))]
(string "" first-line "
\n"
"" rest "
\n"))
(string "" (html-escape key) "
\n"
"" (html-escape docstring) "
\n")))
# Generate parts and print them to stdout
(def parts (seq [[k {:doc d :private p}]
:in (sort (pairs (table/getproto _env)))
:when (and d (not p))]
(gen-one k d)))
(print prelude ;parts postlude)