Jump to content

Module:Mwlocal

Unchecked
From Wikipedia

Documentation for this module may be created at Module:Mwlocal/doc

local extensiontags = {
    nowiki = true,
    ref = true,
    gallery = true,
    pre = true,
    source = true,
    categorytree = true,
    charinsert = true,
    hiero = true,
    imagemap = true,
    inputbox = true,
    math = true,
    poem = true,
    ref = true,
    references = true,
    syntaxhighlight = true,
    timeline = true,
}

local text = {
    -- This returns a string with HTML character entities for wikitext markup characters.
    -- FIXME: Space at the start of a line isn't handled.
    escape = function (text)
        local chars = {}
        for i=1,#text do
            local char = text:sub(i,i)
            local byte = char:byte()
            if ( byte == 38 or byte == 39 or byte == 91 or byte == 93 or byte == 123 or byte == 124 or byte == 125 ) then
                table.insert(chars, "&#" .. tostring(byte) .. ";")
            else
                table.insert(chars, char)
            end
        end
        return table.concat(chars)
    end,
}

text.tag = function (t, frame)
    local name = t.name or "!-- --"
    local content = t.contents or ""
    if ( extensiontags[name] ) then
        -- We have to preprocess these, so that they are properly turned into so-called "strip markers" in the generated wikitext.
        if ( not frame ) then error ("Please supply an extra frame argument to the mwlocal.text.tag() function.") end
        local params = {}
        for n,v in pairs(t.params) do
            table.insert(params, "|" .. n .. "=" .. v)
        end
        return frame:preprocess("{{#tag:" .. name .. "|" .. content .. table.concat(params) .. "}}")
    else
        -- Everything else we can just generate directly, without calling the preprocessor.
        local attrs = {}
        for n,v in pairs(t.params) do
            if (v) then
                table.insert(attrs, n .. "=\"" .. text.escape(v) .. "\"")
            else
                table.insert(attrs, n)
            end
        end
        if ("" == content) then
            return "<" .. name .. " " .. table.concat(attrs, " ") .. "/>"
        else
            return "<" .. name .. " " .. table.concat(attrs, " ") .. ">" .. content .. "</" .. name .. ">"
        end
    end
end

local url = {
    server = "test2.wikipedia.org",
    -- Return a string encoded for use in a URL, equivalent to the parser function {{urlencode:}}.
    --   0-9A-Za-z --> no change
    --   -._       --> no change
    --   ' ' --> '+'
    --   '*' --> '%XX' where XX is hex value of character '*' other than those above
    encode = function (t)
        local chars = {}
        for i=1,#t do
            local byte = t:sub(i,i):byte()
            if (byte == 32) then
                table.insert(chars, '+')
            elseif ( (byte >= 65 and byte <= 90) or (byte >= 97 and byte <= 122) or
                     (byte == 45 or byte == 46 or byte == 95) ) then
                table.insert(chars, string.char(byte))
            else
                table.insert(chars, "%" .. string.format("%02X", byte))
            end
        end
        return table.concat(chars)
    end,
    -- This returns a string encoded for use in a URL, equivalent to the parser function {{anchorencode:}}.
    encodeAnchor = function (t)
        local chars = {}
        for i=1,#t do
            local byte = t:sub(i,i):byte()
            if ( byte == 32 or (byte >= 8 and byte <= 13) or byte == 0 ) then
                table.insert(chars, "_")
            elseif ( (byte >= 65 and byte <= 91) or ( byte >= 97 and byte <= 122 ) ) then
                table.insert(chars, string.char(byte))
            else
                table.insert(chars, "." .. string.format("%02X", byte))
            end
        end
        return table.concat(chars)
    end,
}

url["local"] = function (title, query)
    return "/w/index.php?title=" .. url.encode(title) .. "&" .. query
end
url.full = function (title, query)
    return "//" .. url.server .. "/w/index.php?title=" .. url.encode(title) .. "&" .. query
end

-- Insert as the global functions if they haven't been supplied by Scribunto.
if ( nil == mwlocal ) then mwlocal = {} end
if ( nil == mwlocal.text ) then mwlocal.text = text end
if ( nil == mwlocal.url ) then mwlocal.url = url end

-- Return our replacement functions as this module's own exported function table.
return { url = url, text = text }