Modul:TemplateDocumentation

Z Wikipedie, otevřené encyklopedie
require 'Modul:No globals'

local function getValue(data, key, params)
	while not data[key] and data.inherits do
		data = params[data.inherits] or {}
	end
	return data[key]
end

local function getTemplateTitle(title)
	local subpage = mw.message.new('templatedata-doc-subpage'):plain()
	if title.subpageText == subpage then
		return title.rootText
	else
		return title.text
	end
end

local function loadTD(title)
	local subpage = mw.message.new('templatedata-doc-subpage'):plain()
	if title.subpageText ~= subpage then
		title = title:subPageTitle(subpage)
	end
	local content = mw.text.trim(mw.ustring.match(title:getContent() or '', '<templatedata>(.-)</templatedata>'))
	local data
	if content then
		data = mw.text.jsonDecode(content)
	end
	return data
end

local function getExampleArguments(data)
	local args = {}
	for arg, arg_data in pairs(data.params or {}) do
		local value = arg_data.example -- or getValue(arg_data, 'example', data.params)?
		if value then
			args[arg] = value
		end
	end
	return args
end

local function formatTemplate(template, data, format, order)
	if not format or format == 'inline' then
		format = '{{_|_=_}}'
	elseif format == 'block' then
		format = '{{_\n| _ = _\n}}'
	end
	local split = mw.text.split(format, '|', true)
	local str = mw.ustring.gsub(split[1], '_', template)
	local pattern = split[2]
	pattern = mw.ustring.gsub(pattern, '%}%}.*', '')
	pattern = mw.ustring.gsub(pattern, '_+', '$%0')
	local callback = function (param, value)
		local ret = mw.ustring.gsub(pattern, '%$(_+)', function (m)
			local diff = mw.ustring.len(m) - mw.ustring.len(param)
			if diff > 0 then
				return param .. mw.ustring.rep(' ', diff)
			else
				return param
			end
		end, 1)
		ret = mw.ustring.gsub(ret, '%$_', value)
		return ret
	end
	if #(order or {}) > 0 then
		for _, param in ipairs(order) do
			if data[param] then
				str = str .. '|' .. callback(param, data[param])
			end
		end
	else
		for param, value in pairs(data) do
			str = str .. '|' .. callback(param, value)
		end
	end
	return str .. '}}'
end

local p = {}

function p.getEmptyPreload(template, data)
	local args = {}
	for arg, arg_data in pairs(data.params or {}) do
		if getValue(arg_data, 'deprecated', data.params) == nil then
			args[arg] = getValue(arg_data, 'autovalue', data.params) or ''
		end
	end
	return formatTemplate(template, args, data.format, data.paramOrder)
end

function p.getExampleCode(template, data)
	local args = getExampleArguments(data)
	return formatTemplate(template, args, data.format, data.paramOrder)
end

function p.emptyPreload(frame)
	local title = mw.title.getCurrentTitle()
	local data = loadTD(title)
	if data then
		return p.getEmptyPreload(getTemplateTitle(title), data)
	end
	return ''
end

function p.example(frame)
	return frame:preprocess(p.exampleCode(frame))
end

function p.exampleCode(frame)
	local title = mw.title.getCurrentTitle() 
	local data = loadTD(title)
	if data then
		local args = getExampleArguments(data)
		return formatTemplate(getTemplateTitle(title), args, data.format, data.paramOrder)
	end
	return ''
end

return p