Module:Template link with magic/sandbox explained

local export =

local variables_nullary =

local variables_nonnullary =

local parser_functions =

-- rudimentarylocal function is_valid_pagename(pagename) if (pagename

"") or pagename:match("[%[%]%|%#\127<>]") then return false end return trueend

local function hook_special(page) if is_valid_pagename(page) then return "" .. page .. "" else return page endend

local function hook_wdprop(name) if mw.wikibase.resolvePropertyId(name) then return ("%s"):format(mw.wikibase.resolvePropertyId(name), name) else return name end end

local parser_function_hooks =

function export.format_link(frame) local args = frame:getParent.args local output = local templ = args[1] local noargs = true if not templ then if mw.title.getCurrentTitle.fullText

frame:getParent:getTitle then -- demo mode return "{{<var>{{{1}}}</var>|<var>{{{2}}}</var>|...}}" else error("The template name must be given.") end end

for key, value in pairs(args) do if key ~= 1 then noargs = false break end end

local function render_title(templ) local marker, rest

marker, rest = templ:match("^([Ss][Uu][Bb][Ss][Tt]):(.*)") if not marker then marker, rest = templ:match("^([Ss][Aa][Ff][Ee][Ss][Uu][Bb][Ss][Tt]):(.*)") end if marker then templ = rest table.insert(output, ("%s:"):format(marker)) end if noargs and variables_nullary[templ] then table.insert(output, ("%s"):format(variables_nullary[templ], templ)) return end marker, rest = templ:match("^([Mm][Ss][Gg][Nn][Ww]):(.*)") if marker then templ = rest -- not the most accurate documentation ever table.insert(output, ("%s:"):format(marker)) else marker, rest = templ:match("^([Mm][Ss][Gg]):(.*)") if marker then templ = rest table.insert(output, ("%s:"):format(marker)) -- ditto end end marker, rest = templ:match("^([Rr][Aa][Ww]):(.*)") if marker then table.insert(output, ("%s:"):format(marker)) -- missingno. templ = rest end if templ:match("^%s*/") then table.insert(output, ("%s"):format(templ)) return end marker, rest = templ:match("^(.-):(.*)") if marker then local lcmarker = marker:lower if parser_functions[lcmarker] then if parser_function_hooks[lcmarker] then rest = parser_function_hooks[lcmarker](rest) end table.insert(output, ("%s:%s"):format(mw.uri.encode(parser_functions[lcmarker], "WIKI"), marker, rest)) return elseif variables_nonnullary[marker] then table.insert(output, ("%s:%s"):format(variables_nonnullary[marker], marker, rest)) return end end if templ:match("[%[%]%|%#\127<>]") then table.insert(output, templ) return end

if marker then if mw.site.namespaces[marker] then if (title

"") or (mw.title.getCurrentTitle.fullText

templ) then table.insert(output, templ) else table.insert(output, ("%s"):format(templ, templ)) end return elseif mw.site.interwikiMap[marker:lower] then -- XXX: not sure what to do now… table.insert(output, ("%s:%s"):format(marker, marker, rest)) return end end

if (templ

"") or (mw.title.getCurrentTitle.fullText

("Template:" .. templ)) then table.insert(output, templ) else table.insert(output, ("%s"):format(templ, templ)) end end

render_title(templ)

local i = 2 while args[i] do table.insert(output, "|" .. args[i]) i = i + 1 end for key, value in pairs(args) do if type(key)

"string" then table.insert(output, "|" .. key .. "=" .. value) end end table.insert(output, frame.args.nested and "}}" or "}}") return table.concat(output)end

return export