# 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 (get 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 (get escapes byte)]
(buffer/push-string buf rep)
(buffer/push-byte buf byte)))
buf)
(def- months '("January" "February" "March" "April" "May" "June" "July" "August" "September"
"October" "November" "December"))
(defn nice-date
"Get the current date nicely formatted"
[]
(let [date (os/date)
M (months (date :month))
D (+ (date :month-day) 1)
Y (date :year)
HH (date :hours)
MM (date :minutes)
SS (date :seconds)]
(string/format "%s %d, %d at %.2d:%.2d:%.2d"
M D Y HH MM SS)))
(defn- make-title
"Generate title"
[]
(string "Janet Core API
"
"Version " janet/version "-" janet/build "
"
"Generated "
(nice-date)
"
"
"
"))
(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
html-key (html-escape key)
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 "\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 (get entry :doc) (not (get entry :private)))]
(emit-item k entry)))
(print
prelude
(make-title)
;(interpose "
\n" parts)
postlude)