Module:Template link: Difference between revisions

From Rhythm Heaven Wiki
Jump to navigation Jump to search
(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...")
 
m (Replace " " with "\t")
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:


local function trim(str)
local function trim(str)
    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


function p.main(frame)
function p.main(frame)
    local args = {}
local args = {}
    for k, v in pairs(frame.args) do
for k, v in pairs(frame.args) do
        args[k] = v
args[k] = v
    end
end
    for k, v in pairs(frame:getParent().args) do
for k, v in pairs(frame:getParent().args) do
        args[k] = v
args[k] = v
    end
end
    return p.makeLink(args)
return p.makeLink(args)
end
end


Line 23: Line 29:
end
end


    local linkedTitle = mw.title.new(templateName, "Template")
local subst
    if linkedTitle == nil then
if mw.ustring.sub(templateName, 1, 6) == "subst:" then
        error(mw.ustring.format([["%s" isn't a valid title]], templateName))
subst = "subst"
    end
templateName = mw.ustring.sub(templateName, 7)
elseif mw.ustring.sub(templateName, 1, 10) == "safesubst:" then
subst = "safesubst"
templateName = mw.ustring.sub(templateName, 11)
end
 
local linkedTitle = mw.title.new(templateName, "Template")
if linkedTitle == nil then
error(mw.ustring.format([["%s" isn't a valid title]], templateName))
end
 
local slash = mw.ustring.sub(templateName, 1, 1) == "/"
if slash then
linkedTitle = mw.title.new(templateName)
end
 
local titleText = linkedTitle.fullText
local linkText = linkedTitle.text
if slash then
-- Do nothing
elseif linkedTitle.nsText == "" then
titleText = ":" .. titleText
linkText = ":" .. linkText
elseif linkedTitle.nsText ~= "Template" then
linkText = linkedTitle.prefixedText
end


    local slash = mw.ustring.sub(templateName, 1, 1) == "/"
local output
    if slash then
if yes(args.nolink) then output = linkText
        linkedTitle = mw.title.new(templateName)
else output = mw.ustring.format("[[%s|%s]]", titleText, linkText) end
    end


    local titleText = linkedTitle.fullText
if subst ~= nil then
    local linkText = linkedTitle.text
if yes(args.substhelp) then
    if slash then
subst = "[[mw:Help:Substitution|" .. subst .. "]]"
        -- Do nothing
end
    elseif linkedTitle.nsText == "" then
output = subst .. ":" .. output
        titleText = ":" .. titleText
end
        linkText = ":" .. linkText
    elseif linkedTitle.nsText ~= "Template" then
        linkText = linkedTitle.prefixedText
    end


    local templateArgs = ""
if args[2] ~= nil then
    if args[2] ~= nil then
output = output .. "|" .. tostring(args[2])
        templateArgs = "|" .. tostring(templateArgs)
end
    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
    end
html = mw.html.create("span"):css("font-family", "monospace")
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
end
end


return p
return p

Latest revision as of 10:20, 3 August 2025

Documentation for this module may be created at Module:Template link/doc

local p = {}

local function trim(str)
	if str == nil then return nil end
	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

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

	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

	local linkedTitle = mw.title.new(templateName, "Template")
	if linkedTitle == nil then
		error(mw.ustring.format([["%s" isn't a valid title]], templateName))
	end

	local slash = mw.ustring.sub(templateName, 1, 1) == "/"
	if slash then
		linkedTitle = mw.title.new(templateName)
	end

	local titleText = linkedTitle.fullText
	local linkText = linkedTitle.text
	if slash then
		-- Do nothing
	elseif linkedTitle.nsText == "" then
		titleText = ":" .. titleText
		linkText = ":" .. linkText
	elseif linkedTitle.nsText ~= "Template" then
		linkText = linkedTitle.prefixedText
	end

	local output
	if yes(args.nolink) then output = linkText
	else output = mw.ustring.format("[[%s|%s]]", titleText, linkText) 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 html
	if yes(args.code) then
		html = mw.html.create("code")
	elseif yes(args.mono) then
		html = mw.html.create("span"):css("font-family", "monospace")
	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
end

return p