Module:CatMark

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

This module copies a template's parameter into an "invisible" HTML marker, so that categorizer modules can read the value later easily. It is meant for values that can't be reliably detected from prose, such as the country code in {{Nationality}} or post count given in {{Boorusearch}}. it just saves a whole lot of time from writing annoying regex and patterning when the template can just report its code with an earmark phrase

Usage

Place this inside the template somewhere:

{{#invoke:CatMark|mark|markername|parameter}}

For example, in {{Nationality}}:

{{#invoke:CatMark|mark|nationality|1}}

if the template uses "DE", it will report "DE" in the mark.

The marker renders nothing visible to the reader and is harmless. Markers will generate in the order they are in wikitext.


local p = {}

p.prefix = 'data-cat-'
-- earmark for modules to identify
local function trim(value)
	if value == nil then return '' end
	return mw.text.trim(tostring(value))
end

function p._build(name, value)
	name = string.lower(trim(name))
	if not string.match(name, '^[%w%-]+$') then return '' end

	local marker = mw.html.create('span'):attr(p.prefix .. name, trim(value))

	return tostring(marker)
end

function p.mark(frame)
	local parent = frame:getParent()
	if not parent then return '' end

	local param = trim(frame.args[2])
	if param == '' then return '' end

	return p._build(frame.args[1], parent.args[tonumber(param) or param])
end

return p