From c48cde4aedfae253a14163f28477c131afc9be43 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 20 Oct 2017 14:29:04 +0200 Subject: [PATCH] docs: move write_include out of the writer backend This is a large chunk shared between backends. --- src/scripts/elua/apps/docgen/util.lua | 4 +- src/scripts/elua/apps/docgen/writer.lua | 150 ++++++++++++------------ 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/src/scripts/elua/apps/docgen/util.lua b/src/scripts/elua/apps/docgen/util.lua index 7e14a37b6d..48e375da95 100644 --- a/src/scripts/elua/apps/docgen/util.lua +++ b/src/scripts/elua/apps/docgen/util.lua @@ -22,8 +22,8 @@ M.nspace_to_path = function(ns) return ns:gsub(":", path_sep):gsub(rep_sep, path_sep):lower() end -M.make_page = function(path) - return M.path_join(doc_root, "auto", path .. ".txt") +M.make_page = function(path, ext) + return M.path_join(doc_root, "auto", path .. "." .. ext) end M.get_root_ns = function() diff --git a/src/scripts/elua/apps/docgen/writer.lua b/src/scripts/elua/apps/docgen/writer.lua index 6dc47c1e39..67b6765596 100644 --- a/src/scripts/elua/apps/docgen/writer.lua +++ b/src/scripts/elua/apps/docgen/writer.lua @@ -63,6 +63,79 @@ local Buffer = { end } +local write_include = function(self, tp, name, flags) + local it_to_tp = { + [self.INCLUDE_PAGE] = "page", + [self.INCLUDE_SECTION] = "section", + [self.INCLUDE_NAMESPACE] = "namespace", + [self.INCLUDE_TAG] = "tagtopic" + } + if type(name) == "table" then + if name[#name] == true then + name[#name] = nil + name = ":" .. root_nspace .. ":auto:" + .. table.concat(name, ":") + elseif name[#name] == false then + name[#name] = nil + name = ":" .. root_nspace .. ":user:" + .. table.concat(name, ":") + else + name = table.concat(name, ":") + end + end + self:write_raw("{{", it_to_tp[tp], ">", name); + if flags then + if tp == self.INCLUDE_SECTION and flags.section then + self:write_raw("#", flags.section) + end + flags.section = nil + local flstr = {} + for k, v in pairs(flags) do + local allow = allowed_incflags[k] + if allow ~= nil then + if type(allow) == "boolean" then + flstr[#flstr + 1] = k + elseif type(allow) == "number" or type(allow) == "string" then + if type(v) ~= type(allow) then + error("invalid value type for flag " .. k) + end + flstr[#flstr + 1] = k .. "=" .. v + elseif type(allow) == "table" then + if type(allow[1]) == "table" then + local valid = false + for i, vv in ipairs(allow[1]) do + if v == vv then + flstr[#flstr + 1] = k .. "=" .. v + valid = true + break + end + end + if not valid then + error("invalid value " .. v .. " for flag " .. k) + end + elseif type(allow[1]) == "string" and + type(allow[2]) == "string" then + if v then + flstr[#flstr + 1] = allow[1] + else + flstr[#flstr + 1] = allow[2] + end + end + end + else + error("invalid include flag: " .. tostring(k)) + end + end + flstr = table.concat(flstr, "&") + if #flstr > 0 then + self:write_raw("&", flstr) + end + end + self:write_raw("}}") + self:write_nl() + return self +end + M.set_backend = function(bend) M.Writer = assert(writers[bend], "invalid generation backend") M.Buffer = M.Writer:clone(Buffer) @@ -82,7 +155,7 @@ writers["dokuwiki"] = util.Object:clone { subs = dutil.nspace_to_path(path) end dutil.mkdir_p(subs) - self.file = assert(io.open(dutil.make_page(subs), "w")) + self.file = assert(io.open(dutil.make_page(subs, "txt"), "w")) if title then if M.has_feature("title") then self:write_raw("", title, "") @@ -112,79 +185,8 @@ writers["dokuwiki"] = util.Object:clone { return self end, - write_include = function(self, tp, name, flags, nonl) - local it_to_tp = { - [self.INCLUDE_PAGE] = "page", - [self.INCLUDE_SECTION] = "section", - [self.INCLUDE_NAMESPACE] = "namespace", - [self.INCLUDE_TAG] = "tagtopic" - } - if type(name) == "table" then - if name[#name] == true then - name[#name] = nil - name = ":" .. root_nspace .. ":auto:" - .. table.concat(name, ":") - elseif name[#name] == false then - name[#name] = nil - name = ":" .. root_nspace .. ":user:" - .. table.concat(name, ":") - else - name = table.concat(name, ":") - end - end - self:write_raw("{{", it_to_tp[tp], ">", name); - if flags then - if tp == self.INCLUDE_SECTION and flags.section then - self:write_raw("#", flags.section) - end - flags.section = nil - local flstr = {} - for k, v in pairs(flags) do - local allow = allowed_incflags[k] - if allow ~= nil then - if type(allow) == "boolean" then - flstr[#flstr + 1] = k - elseif type(allow) == "number" or type(allow) == "string" then - if type(v) ~= type(allow) then - error("invalid value type for flag " .. k) - end - flstr[#flstr + 1] = k .. "=" .. v - elseif type(allow) == "table" then - if type(allow[1]) == "table" then - local valid = false - for i, vv in ipairs(allow[1]) do - if v == vv then - flstr[#flstr + 1] = k .. "=" .. v - valid = true - break - end - end - if not valid then - error("invalid value " .. v .. " for flag " .. k) - end - elseif type(allow[1]) == "string" and - type(allow[2]) == "string" then - if v then - flstr[#flstr + 1] = allow[1] - else - flstr[#flstr + 1] = allow[2] - end - end - end - else - error("invalid include flag: " .. tostring(k)) - end - end - flstr = table.concat(flstr, "&") - if #flstr > 0 then - self:write_raw("&", flstr) - end - end - self:write_raw("}}") - if not nonl then - self:write_nl() - end - return self + write_include = function(self, tp, name, flags) + return write_include(self, tp, name, flags) end, write_editable = function(self, ns, name)