Module:Links

From The Satanic Wiki
Jump to navigation Jump to search

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

--<pre> 
--| Creates links and changes wikitext when it detects external links
--| Syntax
--| {{#invoke:links|main|page/url|label|type=ext/local}}
--| {{#invoke:links|batch|url1 label1 type1|url2 label2 type2|separator=linkoutput separator |delimiter= separator for link entries}}
--| Example:
--| {{#invoke:links|batch|www.google.com > google > ext|luamods > List of lua modules|delimiter=> }}

local p = {}
--% This function generates a wiki link
--@ url (string) The url to convert to wikitext
--@ desc (string) The caption or description of the url
--@ typ (string) The type of link, (local or ext)
--: (string) A formated wikilink
function p.link(url, desc, typ)
    local url = url or ""
    local lenUrl = mw.ustring.len(url)
    local uMatch = mw.ustring.match

    if typ == "ext" and (not uMatch(url, "%.") or lenUrl < 3)
            or lenUrl < 1 then
        return
    end

    local prefix = "[["
    local suffix = "]]"
    local separator = "|"

    if url:match("^[Cc]ategory:") and typ ~= "ext" then
        prefix = "[[:"
    end

    -- Adapt if url is external
    if typ ~= "local" and (uMatch(url, "www%.") or uMatch(url, "^http"))
            or typ == "ext" then
        prefix = "[//"

        if uMatch(url, "^http") then
            prefix = "["
        end

        suffix = "]"
        separator = " "
    end

    local desc = desc or ""

    if mw.ustring.len(desc) < 1 then
        separator = ""
    end

    return prefix .. url .. separator .. desc .. suffix
end
--% Entry point for function that generates a wiki link
--@ frame (table) Contains frame from invoked module
--: (string) A formated wikilink
function p.main(frame)
    local args = require("Module::Arguments").getArgs(frame)

    return p.link(args[1] or args.page, args[2] or args.desc, args.type)
end
--% Entry point for function that generates several wiki links
--@ frame (table) Contains frame from invoked module
--: (string) A formated string containing one or more wikilinks
function p.batch(frame)
    local args = require("Module::Arguments").getArgs(frame)
    local separator = args.separator
    local delimiter = args.delimiter

    return p.createLinkBatch(args, separator, delimiter)
end

--% This creates and formats many links
--@ links (table) a table containing links, e.g. {'Lua label1 type', 'www.google.com google ext'}
--@ separator (string) a separator between each link
--@ delimiter (string) an optional delimiter that will separate the label, link & type
--: (string) A formated string containing one or more wikilinks
function p.createLinkBatch(links, separator, delimiter)
    local linkTable = {}
    local linkBatch = ""
    local linkUrl = ""
    local linkLabel = ""
    local linkType = ""
    local delimiter = delimiter or "%s"
    local separator = separator or " • "
    local linkLen = 0

    for _, linkData in ipairs(links) do
        linkLen = linkData:len()
        linkType = linkData:sub(linkLen - 2)

        if linkType ~= "ext" and linkType ~= "int" then
            linkType = ""
        else
            linkData = linkData:sub(1, linkLen - 4)
        end

        linkTable = mw.text.split(linkData, delimiter)

        linkUrl = linkTable[1]
        linkLabel = linkTable[2]
        linkBatch = linkBatch .. p.link(linkUrl, linkLabel, linkType) .. separator
    end

    return linkBatch
end

return p