1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-01 06:26:17 +00:00
janet/tools/gendoc.janet

115 lines
2.8 KiB
Plaintext
Raw Normal View History

# Generate documentation
(def- prelude
```
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Janet Language Documentation</title>
<meta name="description" content="API Documentation for the janet programming language.">
<style>
.docstring {
font-family: monospace;
}
.binding-type {
color: blue;
}
.source-map {
color: steelblue;
font-size: 0.8em;
}
</style>
</head>
```)
(def- postlude
```
</html>
```)
(def- escapes
{10 "<br>"
09 "&nbsp;&nbsp;&nbsp;&nbsp;"
38 "&amp;"
60 "&lt;"
62 "&gt;"
34 "&quot;"
39 "&#39;"
47 "&#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)
2019-02-16 23:35:29 +00:00
(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)))
2018-12-17 06:57:09 +00:00
(defn- make-title
"Generate title"
[]
(string "<h1>Janet Core API</h1>"
"<p>Version " janet/version "-" janet/build "</p>"
2019-02-16 23:38:34 +00:00
"<p>Generated "
2019-02-16 23:35:29 +00:00
(nice-date)
"</p>"
2018-12-17 06:57:09 +00:00
"<hr>"))
(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 "<span class=\"source-map\">" path " (" start ":" end ")</span>")
"")]
(string "<h2 class=\"binding\"><a id=\"" key "\">" html-key "</a></h2>\n"
"<span class=\"binding-type\">" binding-type "</span>\n"
"<p class=\"docstring\">" (trim-lead (html-escape docstring)) "</p>\n"
source-ref)))
# Generate parts and print them to stdout
(def parts (seq [[k entry]
:in (sort (pairs (table/getproto (fiber/getenv (fiber/current)))))
2019-06-19 13:48:33 +00:00
:when (symbol? k)
:when (and (get entry :doc) (not (get entry :private)))]
(emit-item k entry)))
2018-12-17 06:57:09 +00:00
(print
prelude
(make-title)
;(interpose "<hr>\n" parts)
2018-12-17 06:57:09 +00:00
postlude)