Module:User:Theknightwho/template link

local export = {}

local m_str_utils = require("Module:string utilities") local string = string local table = table

local concat = table.concat local encode_uri = mw.uri.encode local format = string.format local format_link -- defined as export.format_link below local html_create = mw.html.create local is_substing = mw.isSubsting local lower = string.lower local make_title = mw.title.makeTitle -- namespace cannot be overriden by prefixes (except mainspace) local new_title = mw.title.new -- namespace can be overriden by prefixes local parse_template_name = require("Module:template parser").parseTemplateName local php_trim = m_str_utils.php_trim local process_params = require("Module:parameters").process local select = select local sorted_pairs = require("Module:table").sortedPairs local split = m_str_utils.split local insert = table.insert local type = type

local data = mw.loadData("Module:template link/data") local magic_words = data.magic_words local link_parameter_0 = data.link_parameter_0 local link_parameter_1 = data.link_parameter_1 local parser_extension_tags = mw.loadData("Module:data/parser extension tags")

do local function link_page(page, namespace) if not namespace then return page end local title = make_title(namespace, page) if not title then return page end return format(			"%s",			encode_uri(title.prefixedText, "WIKI"),			page		) end

local function render_title(name, args) local arg1 = args[1] local chunks, pf_arg0 = parse_template_name(name, arg1) local chunks_len = #chunks local rawchunks = split(name, ":") for i = 1, chunks_len - 1 do			chunks[i] = format(				"%s",				encode_uri(magic_words[chunks[i]], "WIKI"),				rawchunks[i]			) end local chunk = chunks[chunks_len] local link = magic_words[chunk] -- Template. if not link then chunks[chunks_len] = format(				"%s",				encode_uri(new_title(chunk, 10).prefixedText, "WIKI"),				concat(rawchunks, ":", chunks_len)			) return concat(chunks, ":") elseif type(link) == "table" then link = arg1 and link[2] or link[1] end chunks[chunks_len] = format(			"%s",			encode_uri(link, "WIKI"),			rawchunks[chunks_len]		) if pf_arg0 then if chunk == "#TAG:" then local tag = parser_extension_tags[lower(php_trim(pf_arg0))] if tag then pf_arg0 = format(						"%s",						encode_uri(tag, "WIKI"),						pf_arg0					) end else pf_arg0 = link_page(pf_arg0, link_parameter_0[chunk]) end chunks[chunks_len + 1] = pf_arg0 end if arg1 then args[1] = link_page(arg1, link_parameter_1[chunk]) end return concat(chunks, ":") end

function export.format_link(title, args, nested) local output = {render_title(title, args)} local iter = sorted_pairs(args) local i, k, v = 1, iter while k == i do			insert(output, v)			k, v = iter i = i + 1 end if k ~= nil then repeat insert(output, k .. "=" .. v)				k, v = iter until k == nil end output = "&#123;&#123;" .. concat(output, "&#124;") .. "&#125;&#125;"		if nested then return output end return html_create("code"):wikitext(output) end format_link = export.format_link end

function export.show(frame) if is_substing then return require("Module:unsubst").unsubst_template("format_link") end local iargs = process_params(frame.args, {		["annotate"] = {},		["nested"] = {type = "boolean"}	}) local title, args = iargs.annotate if title then args = select(2, process_params(frame:getParent.args, {}, true)) else title, args = process_params(frame:getParent.args, {			[1] = {required = true}		}, true) title = title[1] -- Shift all implicit arguments down by 1. local i = 2 repeat local arg = args[i] args[i - 1] = arg i = i + 1 until arg == nil end return format_link(title, args, iargs.nested) end

return export