Module:Template link: Difference between revisions

More flags
(Created page with "local p = {} local function trim(str) if str == nil then return nil end return mw.text.trim(tostring(str)) end function p.main(frame) local args = {} for k, v in pairs(frame.args) do args[k] = v end for k, v in pairs(frame:getParent().args) do args[k] = v end return p.makeLink(args) end function p.makeLink(args) local templateName = trim(args[1]) if templateName == nil then error("Template name not given") end...")
 
(More flags)
Line 4: Line 4:
     if str == nil then return nil end
     if str == nil then return nil end
     return mw.text.trim(tostring(str))
     return mw.text.trim(tostring(str))
end
local function yes(str)
if str == nil or str == false then return false end
if type(str) ~= "string" then return true end
return mw.text.trim(str) ~= ""
end
end


Line 21: Line 27:
if templateName == nil then
if templateName == nil then
error("Template name not given")
error("Template name not given")
end
local subst
if mw.ustring.sub(templateName, 1, 6) == "subst:" then
subst = "subst"
templateName = mw.ustring.sub(templateName, 7)
elseif mw.ustring.sub(templateName, 1, 10) == "safesubst:" then
subst = "safesubst"
templateName = mw.ustring.sub(templateName, 11)
end
end


Line 44: Line 59:
     end
     end


    local templateArgs = ""
local output
    if args[2] ~= nil then
if yes(args.nolink) then output = linkText
        templateArgs = "|" .. tostring(templateArgs)
else output = mw.ustring.format("[[%s|%s]]", titleText, linkText) end
    end
 
if subst ~= nil then
if yes(args.substhelp) then
subst = "[[mw:Help:Substitution|" .. subst .. "]]"
end
output = subst .. ":" .. output
end
 
if args[2] ~= nil then
output = output .. "|" .. tostring(args[2])
end
output = "{{" .. output .. "}}"


     local inCode = trim(args.code)
local html
    local output = mw.ustring.format("{{[[%s|%s]]%s}}", titleText, linkText, templateArgs)
     if yes(args.code) then
    if inCode ~= nil and inCode ~= "" then
html = mw.html.create("code")
        output = mw.ustring.format("<code>%s</code>", output)
elseif yes(args.mono) then
html = mw.html.create("span"):css("font-family", "monospace")
     end
     end
if yes(args.nowrap) then
if html == nil then html = mw.html.create("span") end
html:css("white-space", "nowrap")
end
if html ~= nil then
output = tostring(html:wikitext(output))
end


     return output
     return output
420

edits