-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
gendoc.lua
479 lines (444 loc) · 17.4 KB
/
gendoc.lua
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
--!A cross-platform document build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author charlesseizilles
-- @file gendoc.lua
--
-- imports
import("core.base.option")
import("shared.md4c")
function _load_apimetadata(filecontent, opt)
opt = opt or {}
local content = {}
local apimetadata = {}
local ismeta = false
for idx, line in ipairs(filecontent:split("\n", {strict = true})) do
if idx == 1 then
local _, _, includepath = line:find("${include (.+)}")
if includepath then
local apientrydata = io.readfile(path.join(os.projectdir(), "doc", opt.locale, includepath))
local apimetadata, content = _load_apimetadata(apientrydata, opt)
return apimetadata, content, includepath
elseif idx == 1 and line == "---" then
ismeta = true
end
elseif ismeta then
if line == "---" then
ismeta = false
else
local key, value = line:match("(.+): (.+)")
apimetadata[key] = value
end
else
table.insert(content, line)
end
end
if apimetadata.api then
local api = apimetadata.api
if api ~= "true" and api ~= "false" then
for idx, line in ipairs(content) do
if line:startswith("### ") then
table.insert(content, idx + 1, "`" .. api .. "`")
break
end
end
end
end
if apimetadata.version then
local names = {
["en-us"] = "Introduced in version",
["zh-cn"] = "被引入的版本"
}
local name = names[opt.locale]
if name then
table.insert(content, "#### " .. name .. " " .. apimetadata.version)
end
end
if apimetadata.refer then
local names = {
["en-us"] = "See also",
["zh-cn"] = "参考"
}
local name = names[opt.locale]
if name then
table.insert(content, "#### " .. name)
local refers = {}
for _, line in ipairs(apimetadata.refer:split(",%s+")) do
table.insert(refers, "${link " .. line .. "}")
end
table.insert(content, table.concat(refers, ", "))
end
end
return apimetadata, table.concat(content, "\n")
end
function _make_db()
local db = {}
local docroot = path.join(os.projectdir(), "doc")
for _, pagefilepath in ipairs(os.files(path.join(os.projectdir(), "doc", "*", "pages.lua"))) do
local locale = path.basename(path.directory(pagefilepath))
local localizeddocroot = path.join(docroot, locale)
db[locale] = io.load(path.join(localizeddocroot, "pages.lua"))
db[locale].apis = {}
db[locale].pages = {}
for _, pagegroup in ipairs(db[locale].categories) do
for _, page in ipairs(pagegroup.pages) do
table.insert(db[locale].pages, page)
for _, apientryfile in ipairs(os.files(path.join(localizeddocroot, page.docdir, "*.md"))) do
local apientrydata = io.readfile(apientryfile)
local apimetadata, _, includepath = _load_apimetadata(apientrydata, {locale = locale})
if apimetadata.key and not includepath then
assert(db[locale].apis[apimetadata.key] == nil, "keys must be unique (\"" .. apimetadata.key .. "\" was already inserted) (" .. apientryfile .. ")")
db[locale].apis[apimetadata.key] = apimetadata
db[locale].apis[apimetadata.key].page = page
end
end
end
end
end
return db
end
function _join_link(...)
return table.concat(table.pack(...), "/")
end
function _make_anchor(db, key, locale, siteroot, page, text)
assert(db and key and locale and siteroot and db[locale])
if db[locale].apis[key] then
text = text or db[locale].apis[key].name
return '<a href="' .. _join_link(siteroot, locale, page) .. '#' .. db[locale].apis[key].key .. '" id="' .. db[locale].apis[key].key .. '">' .. text .. '</a>'
else
text = text or key
return '<s>' .. text .. '</s>'
end
end
function _make_link(db, key, locale, siteroot, text)
assert(db and key and locale and siteroot and db[locale])
if db[locale].apis[key] then
text = text or db[locale].apis[key].name
return '<a href="' .. _join_link(siteroot, locale, db[locale].apis[key].page.docdir .. ".html") .. '#' .. db[locale].apis[key].key .. '">' .. text .. '</a>'
else
text = text or key
return '<s>' .. text .. '</s>'
end
end
function _make_editlink(markdownpath, includepath, locale)
local siteroot = "https://github.com/xmake-io/xmake-gendoc/edit/main/doc"
if includepath then
local pos = markdownpath:find(locale, 1, true)
if pos then
markdownpath = _join_link(markdownpath:sub(1, pos - 1), locale, includepath)
end
end
return '<a href="' .. _join_link(siteroot, markdownpath) .. '" target="_blank">edit</a>'
end
function _build_language_selector(db, locale, siteroot, page)
local languageSelect = [[
<select name="language" onchange='changeLanguage(this, "]] .. locale .. [[");'>
]]
local odrereddbkeys = table.orderkeys(db)
for _, localename in ipairs(odrereddbkeys) do
local dblocaleentry = db[localename]
local selected = localename == locale and "selected" or ""
languageSelect = languageSelect .. string.format([[ <option %s value="%s">%s %s</option>
]], selected, localename, dblocaleentry.flag, dblocaleentry.name)
end
languageSelect = languageSelect .. [[
</select>
<script>
function changeLanguage(select, currentLang) {
if(select.value != currentLang) {
var newUrl = "%s/" + select.value + "/%s";
var splitUrl = window.location.href.split('#');
if (splitUrl.length > 1)
newUrl = newUrl + '#' + splitUrl[splitUrl.length - 1];
window.location.href = newUrl;
}
}
</script>
]]
return string.format(languageSelect, siteroot, page)
end
function _write_header(sitemap, siteroot, title)
sitemap:write(string.format([[
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="resource-type" content="document">
<link rel="stylesheet" href="%s/prism.css" type="text/css" media="all">
<link rel="stylesheet" href="%s/xmake.css" type="text/css" media="all">
<title>%s</title>
</head>
<body>
]], siteroot, siteroot, title))
end
function _write_api(sitemap, db, locale, siteroot, page, apimetalist, apientrydata, markdownpath)
local apimetadata, content, includepath = _load_apimetadata(apientrydata, {locale = locale})
assert(apimetadata.api ~= nil, "entry api is nil value")
assert(apimetadata.key ~= nil, "entry key is nil value")
assert(apimetadata.name ~= nil, "entry name is nil value")
table.insert(apimetalist, apimetadata)
vprint("apimetadata", apimetadata)
-- TODO auto generate links
-- do not match is_arch before matching os.is_arch
local orderedapikeys = table.orderkeys(db[locale].apis, function(lhs, rhs) return #lhs > #rhs end)
local contentlines = content:split("\n", {strict = true})
for idx, line in ipairs(contentlines) do
if line:find("^### " .. apimetadata.name .. "$") then
contentlines[idx] = "### " .. _make_anchor(db, apimetadata.key, locale, siteroot, page)
else
-- local lastfoundidx = 1
-- for _, key in ipairs(orderedapikeys) do
-- local api = db[locale].apis[key]
-- local foundstart, foundend, match = line:find(api.name, lastfoundidx, true)
-- if match then
-- local linebegin
-- line:gsub(match, _make_link(db, api.key, locale, siteroot), 1)
-- end
-- end
end
end
content = table.concat(contentlines, '\n')
local htmldata, errors = md4c.md2html(content)
assert(htmldata, errors)
-- add edit link
local editlink = _make_editlink(markdownpath, includepath, locale)
if editlink then
htmldata = htmldata .. "\n" .. editlink
end
local findstart, findend
repeat
local anchor
findstart, findend, anchor = htmldata:find("%${anchor ([^%s${%}]+)}")
if findstart == nil then break end
htmldata = htmldata:gsub("%${anchor [^%s${%}]+}", _make_anchor(db, anchor, locale, siteroot, page), 1)
until not findstart
repeat
local anchor, text
findstart, findend, anchor, text = htmldata:find("%${anchor ([^%s${%}]+) ([^${%}]+)}")
if findstart == nil then break end
htmldata = htmldata:gsub("%${anchor [^%s${%}]+ [^${%}]+}", _make_anchor(db, anchor, locale, siteroot, page, text), 1)
until not findstart
repeat
local link
findstart, findend, link = htmldata:find("%${link ([^%s${%}]+)}")
if findstart == nil then break end
htmldata = htmldata:gsub("%${link [^%s${%}]+}", _make_link(db, link, locale, siteroot), 1)
until not findstart
repeat
local link, text
findstart, findend, link, text = htmldata:find("%${link ([^%s${%}]+) ([^%{%}]+)}")
if findstart == nil then break end
htmldata = htmldata:gsub("%${link [^%s${%}]+ [^%{%}]+}", _make_link(db, link, locale, siteroot, text), 1)
until not findstart
sitemap:write(htmldata)
end
function _write_table_of_content(sitemap, db, locale, siteroot, page, apimetalist)
local names = {
["en-us"] = "Interfaces",
["zh-cn"] = "接口"
}
local interfaces = names[locale]
sitemap:write(string.format([[
<div id="toc">
<table>
<thead>
<tr><td>%s</td></tr>
</thead>
<tbody id="toc-body">]] .. "\n", interfaces))
for _, apimetadata in ipairs(apimetalist) do
if apimetadata.api ~= "false" then
sitemap:write(' <tr><td><a href="' .. _join_link(siteroot, locale, page) .. '#' .. apimetadata.key .. '">' .. apimetadata.name .. "</a></td></tr>\n")
end
end
sitemap:write([[
</tbody>
</table>
</div>]])
end
function _write_footer(sitemap, siteroot, jssearcharray)
sitemap:write(string.format("\n" .. [[
<script src="%s/prism.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/index.min.js"></script>
<script type="text/javascript">
const documents = [
]] .. jssearcharray .. [[
]
let miniSearch = new MiniSearch({
fields: ['key', 'name'],
storeFields: ['key', 'name', 'url'],
searchOptions: {
boost: { name: 2 },
prefix: true,
fuzzy: 0.4
}
})
miniSearch.addAll(documents)
function changeSearch(input) {
var result = ""
var found = miniSearch.search(input)
found.forEach((e) => {
result = result + "<tr><td><a href='" + e.url + "#" + e.key + "'>" + e.name + "</a></td></tr>"
})
document.getElementById("search-table-body").innerHTML = result
}
</script>
<script type="text/javascript">
function locationHashChanged(e) {
var tocbody = document.getElementById("toc-body")
if (tocbody) {
var tocLinks = tocbody.getElementsByTagName("a")
for (let i = 0;i < tocLinks.length; i++) {
if (tocLinks[i].href == window.location.href) {
tocLinks[i].style = "font-weight:bold"
tocLinks[i].parentElement.style = "background-color:#d6ffed"
} else {
tocLinks[i].style = ""
tocLinks[i].parentElement.style = ""
}
}
}
var navLinks = document.getElementById("sidebar-nav").getElementsByTagName("a")
for (let i = 0;i < navLinks.length; i++) {
const urlbase = window.location.href.split('#')
if (navLinks[i].href == urlbase[0] || navLinks[i].href == window.location.href) {
navLinks[i].style = "font-weight:bold"
navLinks[i].parentElement.style = "background-color:#d6ffed"
} else {
navLinks[i].style = ""
navLinks[i].parentElement.style = ""
}
}
}
window.onhashchange = locationHashChanged;
locationHashChanged({})
</script>
</body>
</html>
]], siteroot))
end
function _build_html_page(docdir, title, db, sidebar, jssearcharray, opt)
opt = opt or {}
local locale = opt.locale or "en-us"
local page = docdir .. ".html"
local isindex = false
if title == "index" and docdir == "." then
page = "index.html"
isindex = true
end
local outputfiledir = path.join(opt.outputdir or "", locale)
local outputfile = path.join(outputfiledir, page)
local sitemap = io.open(outputfile, 'w')
local siteroot = opt.siteroot:gsub("\\", "/")
_write_header(sitemap, siteroot, title)
sitemap:write('<div id="sidebar">\n')
sitemap:write([[
<input type="search" id="search-input" placeholder="search" name="search" oninput="changeSearch(this.value);">
<table><tbody id="search-table-body"></tbody></table>
]])
sitemap:write(_build_language_selector(db, locale, siteroot, page))
sitemap:write(sidebar)
sitemap:write('</div>\n')
sitemap:write('<div id="content">\n')
local isfirst = true
local apimetalist = {}
local docroot = path.join(os.projectdir(), "doc")
local localeroot = path.join(docroot, locale)
local files = {}
for _, file in ipairs(os.files(path.join(localeroot, docdir, "*.md"))) do
local filename = path.filename(file)
if not filename:startswith("_") then
if filename == "0_intro.md" then
table.insert(files, 1, file)
else
table.insert(files, file)
end
end
end
table.sort(files)
for _, file in ipairs(files) do
if isfirst then
isfirst = false
else
sitemap:write("<hr />")
end
vprint("loading " .. file)
local apientrydata = io.readfile(file)
local markdownpath = path.relative(file, docroot)
_write_api(sitemap, db, locale, siteroot, page, apimetalist, apientrydata, markdownpath)
end
sitemap:write("</div>\n")
if not isindex then
_write_table_of_content(sitemap, db, locale, siteroot, page, apimetalist)
end
_write_footer(sitemap, siteroot, jssearcharray)
sitemap:close()
end
function _make_search_array(db, opt)
local jssearcharray = ""
local odreredapikeys = table.orderkeys(db[opt.locale].apis)
local id = 1
for _, apikey in ipairs(odreredapikeys) do
local api = db[opt.locale].apis[apikey]
if api.api ~= "false" then
jssearcharray = jssearcharray .. " {id: " .. tostring(id) .. ", key: \"" .. api.key .. "\", name: \"" .. api.name .. "\", url: \"" .. _join_link(opt.siteroot, opt.locale, api.page.docdir) .. ".html\" },\n"
id = id + 1
end
end
return jssearcharray:gsub("\\", "/")
end
function _build_html_pages(opt)
opt = opt or {}
os.tryrm(opt.outputdir)
local db = _make_db()
for _, pagefile in ipairs(os.files(path.join(os.projectdir(), "doc", "*", "pages.lua"))) do
opt.locale = path.basename(path.directory(pagefile))
local jssearcharray = _make_search_array(db, opt)
local sidebar = '<div id="sidebar-nav">'
for _, category in ipairs(db[opt.locale].categories) do
sidebar = sidebar .. "\n<p>" .. category.title .. "</p>\n<ul>\n"
for _, page in ipairs(category.pages) do
local pagepath = page.docdir
if pagepath == "." then
pagepath = page.title
end
sidebar = sidebar .. '<li><a href="' .. _join_link(opt.siteroot, opt.locale, pagepath .. ".html") .. '">' .. page.title .. "</a></li>\n"
end
sidebar = sidebar .. "</ul>\n"
end
sidebar = sidebar .. "</div>\n"
for _, page in ipairs(db[opt.locale].pages) do
_build_html_page(page.docdir, page.title, db, sidebar, jssearcharray, opt)
end
end
for _, htmlfile in ipairs(os.files(path.join(os.projectdir(), "doc", "*.html"))) do
os.trycp(htmlfile, opt.outputdir)
io.gsub(path.join(opt.outputdir, path.filename(htmlfile)), "%${siteroot}", opt.siteroot)
end
os.trycp(path.join(os.projectdir(), "resources", "*"), opt.outputdir)
end
function main()
local outputdir = path.absolute(option.get("outputdir"))
local siteroot = option.get("siteroot")
if not siteroot:startswith("http") then
siteroot = "file://" .. path.absolute(siteroot)
end
if #siteroot > 1 and siteroot:endswith("/") then
siteroot = siteroot:sub(1, -2)
end
_build_html_pages({outputdir = outputdir, siteroot = siteroot})
cprint("Generated document: ${bright}%s${clear}", outputdir)
cprint("Siteroot: ${bright}%s${clear}", siteroot)
end