Difference between revisions of "Module:Frequency range"

From Amateur Radio Equipment Database
Jump to: navigation, search
(fixed issue with wrong frequencies being used)
 
Line 74: Line 74:
 
             FreqRXStart = args['RXstart' .. tostring(num)],
 
             FreqRXStart = args['RXstart' .. tostring(num)],
 
             FreqTXStart = args['TXstart' .. tostring(num)],
 
             FreqTXStart = args['TXstart' .. tostring(num)],
             FreqRXEnd = args['RXstart' .. tostring(num)],
+
             FreqRXEnd = args['RXend' .. tostring(num)],
             FreqTXEnd = args['TXstart' .. tostring(num)],
+
             FreqTXEnd = args['TXend' .. tostring(num)],
 
             note = args['note' .. tostring(num)]
 
             note = args['note' .. tostring(num)]
 
         })
 
         })

Latest revision as of 20:50, 22 March 2018

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')
    -- Combined TX/RX ranges
    if(rowArgs.FreqStart) then
        item:wikitext(tostring(rowArgs.FreqStart) .. 'MHz - ' .. tostring(rowArgs.FreqEnd) .. 'MHz')
    -- separate ranges
    else
        -- Recieve frequencies provided
        if(rowArgs.FreqRXStart) then
            -- TXRX
            if(rowArgs.FreqTXStart) then
                item:wikitext(tostring(rowArgs.FreqRXStart) .. 'MHz - ' .. tostring(rowArgs.FreqRXEnd) .. 'MHz RX ')
                item:wikitext(tostring(rowArgs.FreqTXStart) .. 'MHz - ' .. tostring(rowArgs.FreqTXEnd) .. 'MHz TX')
            -- RX only
            else
                item:wikitext(tostring(rowArgs.FreqRXStart) .. 'MHz - ' .. tostring(rowArgs.FreqRXEnd) .. 'MHz RX only')
            end
        -- TX only
        else
            item:wikitext(tostring(rowArgs.FreqTXStart) .. 'MHz - ' .. tostring(rowArgs.FreqTXEnd) .. 'MHz TX only')
        end
        
    end

    if(rowArgs.note) then
        item:wikitext(' ('..rowArgs.note..')')
    end

end

local function renderRows()
    -- builds the frequency detail rows
    local rownums = union(union(getArgNums('start'), getArgNums('RXstart')), getArgNums('TXstart'))
    table.sort(rownums)
    for k, num in ipairs(rownums) do
        addRow({
            number = tostring(num),
            FreqStart = args['start' .. tostring (num)],
            FreqEnd = args['end' .. tostring(num)],
            FreqRXStart = args['RXstart' .. tostring(num)],
            FreqTXStart = args['TXstart' .. tostring(num)],
            FreqRXEnd = args['RXend' .. tostring(num)],
            FreqTXEnd = args['TXend' .. 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