Difference between revisions of "Module:Frequency range"

From Amateur Radio Equipment Database
Jump to: navigation, search
(created basic frequency list module)
(No difference)

Revision as of 13:43, 23 April 2017

This Module implements the {{Frequency range}} template. Please see the template page for usage instructions.


local p = {}

local args = {}
local root

local function union(t1, t2)
    -- Returns the union of the values of two tables, as a sequence.
    local vals = {}
    for k, v in pairs(t1) do
        vals[v] = true
    end
    for k, v in pairs(t2) 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(prefix)
    -- Returns a table containing the numbers of the arguments that exist
    -- for the specified prefix. For example, if the prefix was 'data', and
    -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function addRow(rowArgs)
    local item = root:tag('li')
    item:wikitext(tostring(rowArgs.FreqStart) .. 'MHz - ' .. tostring(rowArgs.FreqEnd) .. 'MHz')
    if(rowArgs.note) then
        item:wikitext(' ('..rowArgs.note..')')
    end

end

local function renderRows()
    -- builds the frequency detail rows
    local rownums = union(getArgNums('start'), getArgNums('RXstart'))
    table.sort(rownums)
    for k, num in ipairs(rownums) do
        addRow({
            number = tostring(num),
            FreqStart = args['start' .. tostring (num)],
            FreqEnd = args['end' .. tostring(num)],
            note = args['note' .. tostring(num)]
        })
    end
end

function p.table( frame )
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
    else
        args = frame
    end

    root = mw.html.create('ul')

    renderRows()

    mw.log(tostring(root))
    return tostring(root)
end

return p