-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
export-refs.glisp
110 lines (89 loc) · 3.18 KB
/
export-refs.glisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;; Template would be like below:
;; ###path/rect
;; Generates a rect path
;; - **Alias:** `rect `- **Parameters:**
;; | Name | Type | Description |
;; | ---- | ------ | :--------------------------------------------- |
;; | Pos | `vec2 `| coordinate of top-left corner of the rectangle |
;; | Size | `vec2 `| size of the rectangle |
(defn gen-param-column [idx param variadic]
(if (= param &)
nil
(format "| %-8s | %-9s | %-12s |\n"
(format "%s%s"
(if variadic "& " "")
(get param :label (format "%%%d" idx)))
(str "`" (get param :type "") "`")
(get param :desc ""))))
(defn gen-param-table [params]
(if (not (sequential? params))
nil
(if (sequential? (first params))
;; Multi arity function
(apply str
(map #(->> %
(gen-param-table)
(format "%s\n"))
params))
;; Generates Table
(str
"| Name | Type | Description |\n"
"| -------- | --------- | :----------- |\n"
(apply
str
(remove nil?
(map-indexed
(fn [i p]
(gen-param-column
i p (and (> i 0)
(= & (nth params (dec i))))))
params)))
"\n"))))
(defn upper-case [text]
(js-eval (format "'%s'.toUpperCase()" text)))
(defn capital-case [text]
(if (= 1 (count text))
text
(str (upper-case (subs text 0 1))
(subs text 1))))
(defn gen-doc [sym m f]
(apply
str
(remove
nil?
[(format "### %s\n\n" (name sym))
(if (contains? m :doc)
(format "%s\n\n" (get m :doc)))
;; Generate parameter table
(let [fparams (fn-params f)
params (or (get m :params)
(map #(hash-map) fparams))
params (if (and fparams (>= (count fparams) (count params)))
(for [p params :index i]
(if (map? p)
(apply hash-map
(apply concat
[:label (capital-case
(print-str (nth fparams i)))]
(entries p)))
p))
params)]
(if (pos? (count params))
(str
"**Parameter**\n\n"
(gen-param-table params))))
(if (contains? m :return)
(format "**Returns**: `%s`\n\n" (get (get m :return) :type)))
(if (contains? m :alias)
(format "**Alias:** `%s`\n\n" (get m :alias)))])))
(def md (->> (get-all-symbols)
(sort)
(map #(vector % (eval %)))
(map #(vector (first %) ; symbol
(meta (second %)) ; meta
(second %))) ; function reference
(remove #(nil? (second %))) ; Delete functions with no metadata
(remove #(get (second %) :alias-for)) ; Delete aliased functions
(map #(apply gen-doc %))))
(def txt (join "\n" md))
(spit "docs/ref.md" txt)