模組:TV program order

模組解[]
local p = {}

local yesno = require('Module:Yesno')

local firstitem = false

local function union(t1, t2, t3)
    local vals = {}
    for k, v in pairs(t1) do
        vals[v] = true
    end
    for k, v in pairs(t2) do
        vals[v] = true
    end
    for k, v in pairs(t3) do
        vals[v] = true
    end
    local ret = {}
    for k, v in pairs(vals) do
        table.insert(ret, k)
    end
    return ret
end

local function getArgNums(args, suffix)
    local nums = {}
    for k, v in pairs(args) do
        local num = mw.ustring.match(tostring(k), '^' .. '([1-9]%d*)' .. suffix .. '$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function makeTitle(rowArgs)
    if not rowArgs.title then return end
    local row = mw.html.create('tr')
    row:tag('th')
        :attr('colspan', 3)
        :wikitext(rowArgs.title)
    return row
end

local function makeNote(notes)
    if not notes.notea then return end
    local noteCell = mw.html.create('ul')
    noteCell
        :css('font-size', 'smaller')
        :css('font-weight', 'normal')
        :css('text-align', 'left')
        :tag('li')
            :wikitext(notes.notea)
        if notes.noteb then
            noteCell
                :tag('li')
                    :wikitext(notes.noteb)
        end
    return noteCell
end

local function makeSimpleCell(cells)
    local dataCell = mw.html.create('td')
    if firstitem == true then
        dataCell
            :attr('rowspan', 2)
            :css('width', '33.33%')
            :css('font-weight', 'bold')
    end
    dataCell
        :cssText(cells.datastyle)
        :wikitext(cells.data)
        :node(makeNote({
            notea = cells.notea,
            noteb = cells.noteb
        }))
    return dataCell
end

local function makeContentRow(contents)
    local row = mw.html.create('tr')
    row:node(makeSimpleCell({
        data = contents.dataa,
        notea = contents.notea1,
        noteb = contents.noteb1
    }))
    row:node(makeSimpleCell({
        data = contents.datab,
        datastyle = 'font-weight:bold;',
        notea = contents.notea2,
        noteb = contents.noteb2
    }))
    row:node(makeSimpleCell({
        data = contents.datac,
        notea = contents.notea3,
        noteb = contents.noteb3
    }))
    return row
end

function p._main(args)
    local root = mw.html.create()

    if args["hide"] then
        args["隱藏"] = args["hide"]
    elseif args["摺疊"] then
        args["隱藏"] = args["摺疊"]
    end
    if args["電視臺"] then
        args["電視台"] = args["電視臺"]
    end

    args["隱藏"] = yesno(args["隱藏"], false)

    local tableRoot = root:tag('table')
    tableRoot
        :addClass('wikitable')
        :addClass(args["隱藏"] and 'mw-collapsible mw-collapsed' or nil)
        :css('margin', '1em auto')
        :css('font-size', '90%')
        :css('table-layout', 'fixed')
        :css('width', args["寬度"] or '598pt')
        :css('text-align', 'center')

    local s = ''

    if args["播放檔次"] then
        s = args["播放檔次"]
    end
    if args["電視台"] then
        s = args["電視台"] .. ' ' .. s
    end

    if s == '' then
        s = '{{{播放檔次}}}'
    end

    tableRoot:node(makeTitle({
        title = s
    }))

    local headerRow = tableRoot:tag('tr')
    local contentRow = tableRoot:tag('tr')

    firstitem = true

    headerRow
        :tag('th')
            :css('width', '33.33%')
            :wikitext('上個節目')

    headerRow:node(makeSimpleCell({
        data = (args["節目名稱"] or mw.title.getCurrentTitle().text),
        notea = args["節目名稱備註A"],
        noteb = args["節目名稱備註B"]
    }))

    headerRow
        :tag('th')
            :css('width', '33.33%')
            :wikitext('下個節目')

    firstitem = false

    contentRow:node(makeSimpleCell({
        data = (args["上一節目"] or '—'),
        notea = args["上一節目備註A"],
        noteb = args["上一節目備註B"]
    }))

    contentRow:node(makeSimpleCell({
        data = (args["下一節目"] or '—'),
        notea = args["下一節目備註A"],
        noteb = args["下一節目備註B"]
    }))

    local nums = union(getArgNums(args, '播放檔次'), getArgNums(args, '上一節目'), getArgNums(args, '下一節目'))
    table.sort(nums)

    for _, num in ipairs(nums) do
        if tonumber(num) >=2 then
            if args[tostring(num) .. "電視臺"] then
                args[tostring(num) .. "電視台"] = args[tostring(num) .. "電視臺"]
            end

            local sn = nil
            if args[tostring(num) .. '播放檔次'] then
                sn = args[tostring(num) .. '播放檔次']
                if args[tostring(num) .. '電視台'] then
                    sn = args[tostring(num) .. '電視台'] .. ' ' .. sn
                end
            end

            tableRoot:node(makeTitle({
                title = sn
            }))

            tableRoot:node(makeContentRow({
                dataa = (args[tostring(num) .. '上一節目'] or '—'),
                datab = (args[tostring(num) .. '節目名稱'] or '第' .. tostring(num) .. '季'),
                datac = (args[tostring(num) .. '下一節目'] or '—'),
                notea1 = args[tostring(num) .. '上一節目備註A'],
                noteb1 = args[tostring(num) .. '上一節目備註B'],
                notea2 = args[tostring(num) .. '節目名稱備註A'],
                noteb2 = args[tostring(num) .. '節目名稱備註B'],
                notea3 = args[tostring(num) .. '下一節目備註A'],
                noteb3 = args[tostring(num) .. '下一節目備註B']
            }))
        end
    end

    return tostring(root)
end

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        wrappers = 'Template:電視節目變遷'
    })
    return p._main(args)
end

return p