Module:template parser/templates

From Wiktionary, the free dictionary
Jump to navigation Jump to search

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


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

local is_substing = mw.isSubsting
local process_params = require("Module:parameters").process
local template_link = m_template_parser.templateLink
local wikitag_link = m_template_parser.wikitagLink

local export = {}

function export.template_link_t(frame)
	if is_substing() then
		return require("Module:unsubst").unsubst_template("show")
	end
	
	local iargs = process_params(frame.args, {
		["annotate"] = {},
		["nested"] = {type = "boolean"}
	})
	
	-- iargs.annotate allows a template to specify the title, so the input
	-- arguments will match the output. Otherwise, we treat parameter 1 as the
	-- template name and offset any implicit arguments.
	local title = iargs.annotate
	if title then
		return template_link(title, require("Module:table").shallowcopy(frame:getParent().args), iargs.nested)
	end
	-- Process parameters with the return_unknown flag set. `title` contains
	-- the title at key 1; everything else goes in `args`.
	local args
	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 template_link(title, args, iargs.nested)
end

function export.wikitag_link_t(frame)
	if is_substing() then
		return require("Module:unsubst").unsubst_template("show")
	end
	
	return wikitag_link(process_params(frame:getParent().args, {
		[1] = {required = true, allow_empty = true, no_trim = true}
	})[1])
end

return export