Jump to content

Module:template parser/templates

From Wiktionary, the free dictionary

This module implements {{temp}}, {{paramref}} and {{wikitag}}.


-- Prevent substitution.
if mw.isSubsting() then
	return require("Module:unsubst")
end

local export = {}

local m_template_parser = require("Module:template parser")

local display_parameter = m_template_parser.displayParameter
local process_params = require("Module:parameters").process
local template_link = m_template_parser.templateLink
local wikitag_link = m_template_parser.wikitagLink

local function get_offset_template_args(frame)
	-- Process parameters with the return_unknown flag set. `title` contains
	-- the title at key 1; everything else goes in `args`.
	local title, args = process_params(frame:getParent().args, {
		[1] = {required = true, allow_empty = true, no_trim = true}
	}, true)
	title = title[1]
	-- Shift all implicit arguments down by 1. Non-sequential numbered
	-- parameters don't get shifted; however, this offset means that if the
	-- input contains (e.g.) {{tl|l|en|3=alt}}, representing {{l|en|3=alt}},
	-- the parameter at 3= is instead treated as sequential by this module,
	-- because it's indistinguishable from {{tl|l|en|alt}}, which represents
	-- {{l|en|alt}}. On the other hand, {{tl|l|en|4=tr}} is handled correctly,
	-- because there's still a gap before 4=.
	-- Unfortunately, there's no way to know the original input, so
	-- there's no clear way to fix this; the only difference is that explicit
	-- parameters have whitespace trimmed from their values while implicit ones
	-- don't, but we can't assume that every input with no whitespace was given
	-- with explicit numbering.
	-- This also causes bigger problems for any parser functions which treat
	-- their inputs as arrays, or in some other nonstandard way (e.g.
	-- {{#IF:foo|bar=baz|qux}} treats "bar=baz" as parameter 1). Without
	-- knowing the original input, these can't be reconstructed accurately.
	-- The way around this is to use <nowiki> tags in the input, since this
	-- module won't unstrip them by design.
	local i = 2
	repeat
		local arg = args[i]
		args[i - 1] = arg
		i = i + 1
	until arg == nil
	return title, args
end

function export.template_link_t(frame)
	local iargs = process_params(frame.args, {
		["annotate"] = true,
		["nolink"] = {type = "boolean"},
	})
	-- iargs.annotate allows a template to specify the title, so the input
	-- arguments will match the output.
	local title = iargs.annotate
	if title then
		return template_link(title, frame:getParent().args, iargs.nolink)
	end
	-- Otherwise, get template arguments offset by 1.
	local args
	title, args = get_offset_template_args(frame)
	return template_link(title, args, iargs.nolink)
end

function export.template_demo_t(frame)
	local title, args = get_offset_template_args(frame)
	return template_link(title, args) .. " ⇒<br style=\"line-height: 200%;\" />" .. frame:expandTemplate{title = title, args = args}
end

function export.parameter_t(frame)
	return display_parameter(unpack(process_params(frame:getParent().args, {
		[1] = {required = true, allow_empty = true, no_trim = true},
		[2] = {allow_empty = true, no_trim = true},
	})))
end

function export.wikitag_link_t(frame)
	return wikitag_link(process_params(frame:getParent().args, {
		[1] = {required = true, allow_empty = true, no_trim = true}
	})[1])
end

return export