模块:Timeline

来自维阿百科
跳转至: 导航搜索

该模块实现{{Timeline}}的功能。请不要直接调用此模块。


-- Module:Timeline
-- Made with ♥ by User:Leranjun

-- This module implements {{tl|Timeline}}.
-- Please refrain from invoking this module directly.

local p = {}

local getArgs = require("Module:Arguments").getArgs

local function notempty(s)
    return (s and s ~= "")
end

function p.main(frame)
    return p._main(getArgs(frame))
end

function p._main(args)
    local anchor = (mw.ustring.lower((args.anchor or "yes")) ~= "no")

    local r = mw.html.create("table"):addClass("timeline"):addClass(args.class):cssText(args.style)

    local data = p.parse(args)

    for _, period in ipairs(data[1]) do
        local timelist = data[period]
        if (notempty(period)) then
            local row = mw.html.create("tr")
            local header =
                mw.html.create("th"):attr("colspan", 3):cssText(args.periodstyle):cssText(args[period .. "style"])
            if (anchor) then
                local heading =
                    mw.html.create("h5"):cssText("display:inline;font-weight:inherit;color:inherit"):wikitext(period)
                header:node(tostring(heading))
            else
                header:wikitext(period)
            end
            row:node(tostring(header))
            r:node(tostring(row))
        end

        for _, time in ipairs(timelist[1]) do
            local events = timelist[time]
            local long = events.l
            local cnt = #events
            local full = period .. time
            for cur, event in ipairs(events) do
                local row = mw.html.create("tr")
                local cell = mw.html.create("td"):cssText(args.textstyle):cssText(args[full .. "textstyle"])
                if (not long) then
                    cell:attr("colspan", 2)
                end
                -- Add line breaks to fix compatibility issues with unordered lists
                cell:wikitext(event .. "\n")

                if (cur == 1) then
                    local header =
                        mw.html.create("th"):css("font-weight", args.timeweight):cssText(args.timestyle):cssText(
                        args[full .. "style"]
                    )

                    if (long) then
                        header:attr("colspan", 2)
                    end

                    if (cnt > 1) then
                        header:attr("rowspan", cnt)
                    end

                    if (args.period == "on") then
                        header:wikitext(period)
                    end
                    header:wikitext(time)
                    header:wikitext(args.sepr or "")

                    row:node(tostring(header))
                end
                row:node(tostring(cell))
                r:node(tostring(row))
            end
        end
    end

    return r
end

function p.parse(args)
    local raw = args.list
    local data = {[1] = {}} -- index

    for v in mw.text.gsplit(raw, ";") do
        if (not notempty(v)) then
            break
        end

        local timelist = mw.text.split(v, ":")
        local period = timelist[1]
        timelist = timelist[2]

        data[period] = {[1] = {}}
        table.insert(data[1], period)

        for time in mw.text.gsplit(timelist, ",") do
            if (not notempty(time)) then
                break
            end

            local value = args["in" .. period .. time]
            time = mw.text.split(time, "#")
            local needSort = false
            if (#time == 1) then
                needSort = true
            end
            time = time[1]

            time = mw.text.split(time, "in")
            if (#time > 1) then
                time = time[2]
                data[period][time] = data[period][time] or {l = true}
            else
                time = time[1]
                data[period][time] = data[period][time] or {}
            end

            if (needSort) then
                table.insert(data[period][1], time)
            end

            table.insert(data[period][time], value)
        end
    end

    return data
end

return p