Module:Dialogue

From Chicory: A Colorful Tale Wiki

Documentation for this module may be created at Module:Dialogue/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}

local function make_tr(parent, icon, alt, font, text, classname)
    local tr = parent
        :tag("tr")
            :tag("td")
                :wikitext("[[" .. icon .. "|12px|alt=".. alt .."]]")
                :done()
            :tag("td")
                :wikitext(text)
                :css("font-family", font)
                :done()
    if classname then
        tr:addClass(classname)
    end
end

local function make_tree(parent, args, argnum, argslength, trees)
    while argnum <= argslength do
        if args[argnum] == "!BRANCH!" then
            parent
                :tag("tr")
                :tag("td")
                    :wikitext(args[argnum+1])
                    :attr("colspan", "2")
            argnum = argnum + 2
        elseif args[argnum] == "!TREE!" then
            trees = trees + 1
            argnum, trees = make_tree(parent, args, argnum+1, argslength, trees)
        elseif args[argnum] == "!ENDTREE!" then
            break
        else
            make_tr(parent, args[argnum], args[argnum+1], args[argnum+2], args[argnum+3], "dialoguebranch")
            argnum = argnum + 4
        end
    end
    return argnum+1, trees
end

function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end

function p._main(args)
    local argslength = 0
    for i, v in pairs(args) do
        if tonumber(i) and i > argslength then
            argslength = i
        end
    end
    local htmltable = mw.html.create("table"):addClass("dialoguetable")
    local argnum = 1
    local trees = 0
    while argnum <= argslength do
        if args[argnum] == "!TREE!" then
            trees = trees + 1
            argnum, trees = make_tree(htmltable, args, argnum+1, argslength, trees)
        else
            make_tr(htmltable, args[argnum], args[argnum+1], args[argnum+2], args[argnum+3])
            argnum = argnum + 4
        end
    end
    return tostring(htmltable)
end

return p