--!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 '' .. text .. ''
else
text = text or key
return '' .. text .. ''
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 '' .. text .. ''
else
text = text or key
return '' .. text .. ''
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 'edit'
end
function _build_language_selector(db, locale, siteroot, page)
local languageSelect = [[
]]
return string.format(languageSelect, siteroot, page)
end
function _write_header(sitemap, siteroot, title)
sitemap:write(string.format([[
| %s |
| ' .. apimetadata.name .. " |