Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:SplitAndLink

From Net 7 Portal Wiki

Module:SplitAndLink/doc

This module implements the template {{SplitAndLink}}, see the documentation there.


The above doc is transcluded from Module:SplitAndLink/doc. (edit | history)
Add categories to the /doc subpage, not here. Subpages of this template.


local getArgs
local yesno = require('Module:Yesno')
local error = require('Module:Vrix').error
require('strict')

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function _args(frame)
    if not getArgs then
        getArgs = require('Module:Arguments').getArgs
    end
    local args = getArgs(frame, {
        trim = true,
        removeBlanks = true,
        wrappers = {
            'Template:SplitAndLink',
        }
    })
    return args
end

--------------------------------------------------------------------------------
-- SplitAndLink class definition
--------------------------------------------------------------------------------

local SplitAndLink = {}
SplitAndLink.__index = SplitAndLink

function SplitAndLink.new(args)
    local obj = {}

    obj.wikitext = ""

    local replace="[[%1]]"
    if args.type then
        if args.type:find("n7") then
            -- Prefer a wiki link if available
            replace="{{#ifexist:%1|[[%1]]|{{N7Link|item=%1|named=yes}}}}"
        elseif args.type:find("sector") then
            replace="{{Sector/ValidateAndLink|sector=%1|skip_delink=yes}}"
        end
    end

    if args.clist then
        for item in mw.text.gsplit(args.clist, ',') do
            obj.wikitext = obj.wikitext .. mw.getCurrentFrame():preprocess(item:gsub("^(.+)$", replace .. "<br />"))
        end
    end

    return setmetatable(obj, SplitAndLink)
end

function SplitAndLink:export()
    return tostring(mw.html.create()
        :wikitext(self.wikitext)
    )
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p, mt = {}, {}

function p._exportClasses()
    -- For testing.
    return {
        SplitAndLink = SplitAndLink
    }
end

function p.main(k, args)
    local splitAndLink = SplitAndLink.new(args)
    return splitAndLink:export()
end

function mt.__index(t, k)
    return function (frame)
        return t.main(k, _args(frame))
    end
end

return setmetatable(p, mt)