Skip to content

Commit d1c7a67

Browse files
committed
Add modules documentation generator
1 parent c76ef4a commit d1c7a67

File tree

3 files changed

+184
-3
lines changed

3 files changed

+184
-3
lines changed

docs/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.temp
33
.cache
4-
/docs/*/modules/
4+
docs/.vuepress/configs/modules.js
5+
docs/*/modules/

docs/docs/.vuepress/configs/pages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import modules from './modules'
12
export default {
23
'/': {
34
text: {'en': 'OwnLang', 'ru': 'OwnLang'},
@@ -24,7 +25,6 @@ export default {
2425

2526
'/modules/': {
2627
text: {'en': 'Modules', 'ru': 'Модули'},
27-
pages: [
28-
]
28+
pages: modules
2929
}
3030
}

docs/src/docgen-md.own

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
use std, types, files, yaml, functional
2+
3+
INPUT_PATH_FMT = "./modules/%s.yml"
4+
OUTPUT_DIR_FMT = "../docs/%s/modules"
5+
OUTPUT_PATH_FMT = OUTPUT_DIR_FMT + "/%s.md"
6+
7+
LANGS = ["en", "ru"]
8+
MODULES = [
9+
"std",
10+
"types",
11+
"math",
12+
"date",
13+
"files",
14+
"http",
15+
"socket",
16+
"downloader",
17+
"base64",
18+
"json",
19+
"yaml",
20+
"zip",
21+
"gzip",
22+
"functional",
23+
"robot",
24+
"ounit",
25+
"canvas",
26+
"canvasfx",
27+
"forms",
28+
"java",
29+
"jdbc",
30+
"regex",
31+
"android",
32+
"canvas_android",
33+
"forms_android",
34+
"imageprocessing_android",
35+
"gps_android"
36+
]
37+
messages = {
38+
"constants": {"en": "Constants", "ru": "Константы"},
39+
"functions": {"en": "Functions", "ru": "Функции"},
40+
"types": {"en": "Types", "ru": "Типы"},
41+
"example": {"en": "Example", "ru": "Пример"},
42+
"since": {"en": "since", "ru": "начиная с"}
43+
}
44+
45+
// Write modules pages to vuepress config
46+
f = fopen("../docs/.vuepress/configs/modules.js", "w")
47+
writeLine(f, "export default [")
48+
writeLine(f, stream(MODULES).map(def(m) = " \"%s.md\"".sprintf(m)).joining(",\n"))
49+
writeLine(f, "]")
50+
flush(f)
51+
fclose(f)
52+
53+
// Create output dirs
54+
for lang : LANGS {
55+
mkdirs(sprintf(OUTPUT_DIR_FMT, lang))
56+
}
57+
58+
for moduleName : MODULES {
59+
module = readYml(sprintf(INPUT_PATH_FMT, moduleName))
60+
for lang : LANGS {
61+
println "" + module.name + " / " + lang
62+
file = sprintf(OUTPUT_PATH_FMT, lang, moduleName)
63+
f = fopen(file, "w")
64+
65+
writeHeader(f, module, lang)
66+
writeConstants(f, module.constants ?? [], lang)
67+
writeFunctions(f, module.functions ?? [], lang, 2);
68+
writeTypes(f, module.types ?? [], lang);
69+
70+
flush(f)
71+
fclose(f)
72+
}
73+
}
74+
75+
// -- write
76+
def writeHeader(f, module, lang) {
77+
writeText(f, header(module.name, 1))
78+
if length(module.scope ?? "") && (module.scope != "both") {
79+
writeText(f, " (" + module.scope + ")")
80+
}
81+
writeLine(f, "\n")
82+
if length(module.since ?? "") {
83+
writeSince(f, module.since, lang)
84+
}
85+
writeDescription(f, module, lang, "\n%s\n")
86+
}
87+
88+
def writeConstants(f, constants, lang) {
89+
if (constants.isEmpty()) return 0
90+
91+
writeLine(f, "\n\n## " + messages.constants[lang])
92+
for info : constants {
93+
writeText(f, "\n`%s` : *%s*".sprintf(info.name, info.typeName))
94+
writeScope(f, info.scope ?? "")
95+
writeText(f, " = ")
96+
constValue = getValue(info, "value", lang)
97+
if (info.type != MAP && info.type != CLASS) {
98+
writeText(f, "`%s`".sprintf(constValue))
99+
} else {
100+
mapValues = constValue.substring(1, constValue.length - 1).split(", ")
101+
if (mapValues.length >= 7) {
102+
writeText(f, "\n\n```own\n{\n ");
103+
writeText(f, mapValues.joinToString(",\n "));
104+
writeText(f, "\n}\n```");
105+
} else {
106+
writeText(f, "`%s`".sprintf(constValue));
107+
}
108+
}
109+
writeLine(f, "")
110+
writeDescription(f, info, lang, "\n%s\n")
111+
}
112+
113+
}
114+
115+
def writeFunctions(f, functions, lang, level = 2) {
116+
if (functions.isEmpty()) return 0
117+
118+
writeLine(f, "\n\n" + header(messages.functions[lang], level))
119+
for info : functions {
120+
writeText(f, "\n`%s(%s)`".sprintf(info.name, info.args))
121+
writeScope(f, info.scope ?? "")
122+
if length(info.since ?? "") {
123+
writeSince(f, info.since, lang)
124+
}
125+
writeDescription(f, info, lang, " — %s")
126+
writeLine(f, "")
127+
128+
example = getValue(info, "example", lang)
129+
if (length(example ?? "")) {
130+
writeLine(f, "\n```own")
131+
writeLine(f, example)
132+
writeLine(f, "```")
133+
}
134+
}
135+
}
136+
137+
def writeTypes(f, types, lang) {
138+
if (types.isEmpty()) return 0
139+
140+
writeLine(f, "\n\n" + header(messages.types[lang]))
141+
for info : types {
142+
writeText(f, "\n\n" + header("`%s`".sprintf(info.name), 3))
143+
writeScope(f, info.scope ?? "")
144+
writeDescription(f, info, lang, "%s\n")
145+
writeFunctions(f, info.functions ?? [], lang, 4);
146+
writeLine(f, "")
147+
}
148+
}
149+
150+
def writeScope(f, scope) = match(scope) {
151+
case "android": writeText(f, " <Badge text=\"Android\" />")
152+
case "desktop": writeText(f, " <Badge text=\"Desktop\" />")
153+
case _: { }
154+
}
155+
156+
def writeDescription(f, obj, lang, format = "%s") {
157+
str = getValue(obj, "desc", lang)
158+
if (str != "") {
159+
writeText(f, sprintf(format, str))
160+
}
161+
}
162+
163+
def writeSince(f, since, lang) {
164+
writeText(f, "<Badge text=\"%s %s\" />".sprintf(messages.since[lang], since))
165+
}
166+
167+
// -- utils
168+
def getValue(object, key, lang = "en") {
169+
newKey = (lang != "en") ? (key + "_" + lang) : key
170+
return object[newKey] ?? object[key] ?? ""
171+
}
172+
173+
def header(text, level = 2) = ("#" * level) + " " + text
174+
175+
def readYml(filename) {
176+
f = fopen(filename, "r")
177+
s = readText(f)
178+
fclose(f)
179+
return yamldecode(s)
180+
}

0 commit comments

Comments
 (0)