# Generate documentation
# TODO - make tool reusable
(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- make-title
"Generate title"
[]
(string "Janet Core API
"
"Version " janet/version "-" janet/build "
"
"Generated "
(string/number (os/time) :f 0 20)
" seconds after epoch
"
"
"))
(defn- emit-item
"Generate documentation for one entry."
[key env-entry]
(let [{:macro macro
:value val
:ref ref
:source-map sm
:doc docstring} env-entry
binding-type (cond
macro :macro
ref (string :var " (" (type (get ref 0)) ")")
(type val))
source-ref (if-let [[path start end] sm]
(string "" path " (" start ":" end ")")
"")]
(string "" (html-escape key) "
\n"
"" binding-type "\n"
"" (trim-lead (html-escape docstring)) "
\n"
source-ref)))
# Generate parts and print them to stdout
(def parts (seq [[k entry]
:in (sort (pairs (table/getproto _env)))
:when (and entry:doc (not entry:private))]
(emit-item k entry)))
(print
prelude
(make-title)
;(interpose "
\n" parts)
postlude)