Module:Sandbox/Dovid/Ezref

Source: Wikipedia, the free encyclopedia.
--Simplify reference markup  |  Temp test at User:Dovid/sandbox/test  |  Used by template User:Dovid/sandbox/Ezref
local ez = {}, paramlist
local notes , opsLeft , opsRight, dbgLeft , dbgRight = "" , "<" , ">" , "?" , "!"
local tagLeft , tagRight = opsLeft , opsRight -- dbgLeft , dbgRight

function errNoName(suffix)
    suffix = (suffix or '') -- .. (notes == '' ? '' : '' .. notes)
    if paramlist ~= nil then
        for i,j in pairs(paramlist) do
            suffix = suffix .. "  |  " .. i .. '=' .. j
        end
    else
        suffix = suffix .. ' [args not accessible for this run]'
    end
    return 'Error: no ref name/1 parameter (' .. suffix .. ').'
end

function ez.ref(frame)
    --    local args=frame.getParent(frame).args
    if not frame then return errNoName('f') end
    local parent = frame.getParent(frame)
    if not parent then return errNoName('p') end
    local args = frame.args
    paramlist = args --global, for debugging
    if not args then return errNoName('a') end
    local refname = args['name'] or args[1]
    if not refname then return errNoName('n') end
    -- notes = '1=' .. (frame.args[1] ? frame.args[1] ? 'nil') .. '; name=' .. (frame.args['name'] ? frame.args['name'] : 'nil')
    
    local content = (args['text'] or args[2]) or ""
    if content == '' then
        return tagLeft .. 'ref name="' .. refname .. '"/' ..tagRight
    else
        return tagLeft .. 'ref name="' .. refname .. '"' .. tagRight .. content .. tagLeft .. '/ref' .. tagRight
    end
end

return ez