Jump to content

Module:Sandbox/isaacl/ExpandPageAbbreviation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Isaacl (talk | contribs) at 16:51, 26 March 2013 (rename variable; remove unnecessary require statement). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- For unit tests, see [[Module:Sandbox/isaacl/ExpandPageAbbreviation/tests]]

local me = { 
    outputForAbbr = { },
    }

-- if mw.loadData() not supported, use require() instead
if mw.loadData then
    me.phraseForAbbr = mw.loadData('Module:Sandbox/isaacl/ExpandPageAbbreviation/data')
else
    me.phraseForAbbr = require('Module:Sandbox/isaacl/ExpandPageAbbreviation/data')
end

function me._expand(abbr)
    if me.outputForAbbr[abbr] ~= nil then
        return me.outputForAbbr[abbr]
    else
        local outputString
        if me.phraseForAbbr[abbr] == nil then
            outputString = string.format("[[%s]]", abbr)
            me.outputForAbbr[abbr] = outputString
        else
            me.outputForAbbr[abbr] = string.format("[[%s|%s]]",
                abbr, me.phraseForAbbr[abbr])
            outputString = string.format("%s (%s)",
                me.outputForAbbr[abbr], abbr)
        end
        return outputString
    end
end  -- function _expand()

function me.expand(frame)
    local abbr = frame.args[1]
    return me._expand(abbr)
end

return me