Module:LinkTemplate

-- @protect -- @module      LinkTemplate -- @description Module for creating link templates. -- @version     1.0 -- @usage       require("Dev:LinkTemplate") -- @author      TheGoldenPatrik1 -- @release     Beta

local p = {}

-- @method      p.external -- @description Creates an external link with URL parameters -- @param       {String} link - The page to link to -- @param        {String} params - The URL parameters -- @param       {String} text - The link text function p.external(link, params, text) return '['..tostring(mw.uri.canonicalUrl(link, params)) ..' '..(text or link)..']' end

-- @method      p.link -- @description Creates an internal link -- @param       {String} link - The page to link to -- @param        {String} text - The link text function p.link(link, text) return ..(text or link).. end

-- @method      p.interwiki -- @description Generates interwiki links -- @param       {String} wiki - The wiki to link to -- @param        {String} wikiName - The wiki's name -- @param       {String} paramA - The first arg -- @param       {String} paramB - The second arg -- @param       {String} url - The url arg function p.interwiki(wiki, wikiName, paramA, paramB, url) if url then local uri = url:gsub('-', '=') if not paramA then return p.external('w:c:'..wiki, uri, paramB or wikiName) else return p.external('w:c:'..wiki..':'..paramA, uri, paramB or paramA) end else if not paramA then return p.link('w:c:'..wiki, paramB or wikiName) else return p.link('w:c:'..wiki..':'..paramA, paramB or paramA) end end end

-- @method      p.namespace -- @description Generates namespace links -- @param       {String} namespace - The namespace to link to -- @param        {String} paramA - The first arg -- @param       {String} paramB - The second arg -- @param       {String} url - The url arg function p.namespace(namespace, paramA, paramB, url) local ns = mw.site.namespaces[tonumber(namespace)].name if url then local uri = url:gsub('-', '=') if not paramA then return p.external('Special:PrefixIndex/'..ns..':', uri, paramB or ''.. ns..' ') else if paramB == 'f' or paramB == 'F' then return p.external(ns..':'..paramA, uri) else return p.external(ns..':'..paramA, uri, paramB or paramA) end end else if not paramA then return p.link('Special:PrefixIndex/'..ns..':', paramB or ''.. ns..' ') else if paramB == 'f' or paramB == 'F' then return p.link(ns..':'..paramA) else return p.link(ns..':'..paramA, paramB or paramA) end end end end

-- @method      p.main -- @description The main function; determines what to do -- @param        {String} frame - User input function p.main(frame) local linkType = frame.args[1] local data = frame.args[2] local pargs = frame:getParent.args local paramA = pargs[1] local paramB = pargs[2] local url = pargs['url'] if linkType == 'ns' then return p.namespace(data, paramA, paramB, url) else if linkType == 'wiki' then return p.interwiki(data, frame.args[3], paramA, paramB, url) end end end return p