模块:TemplateDemo:修订间差异

来自「荏苒之境」
无编辑摘要
无编辑摘要
 
(未显示同一用户的2个中间版本)
第17行: 第17行:
end
end


local function append_template_argument(t, k, v)
local function append_template_argument(t, k, v, last_num_param)
local num = tonumber(k)
local num = tonumber(k)
if num ~= nil then
if num ~= nil then
第24行: 第24行:
else
else
k = tostring(num - 1)
k = tostring(num - 1)
v = trim(v)
end
end
end
end
if k ~= nil then
if k ~= nil then
t[#t+1] = "| "
t[#t+1] = "| "
t[#t+1] = k
if num == nil or last_num_param ~= num - 1 then
t[#t+1] = " = "
t[#t+1] = k
t[#t+1] = " = "
end
t[#t+1] = v
t[#t+1] = v
t[#t+1] = "\n"
t[#t+1] = "\n"
end
end
return num
end
end


第48行: 第52行:
local t = {"{{", trim(template_name), "\n"}
local t = {"{{", trim(template_name), "\n"}
local last_num_param = 1
if template_data ~= nil and type(template_data.paramOrder) == "table" then
if template_data ~= nil and type(template_data.paramOrder) == "table" then
第55行: 第60行:
local v = args[k]
local v = args[k]
if v ~= nil then
if v ~= nil then
append_template_argument(t, k, v)
last_num_param =
append_template_argument(t, k, v, last_num_param)
end
end
end
end
else
else
for k, v in pairs(args) do
for k, v in pairs(args) do
append_template_argument(t, k, v)
last_num_param =
append_template_argument(t, k, v, last_num_param)
end
end
end
end

2025年8月8日 (五) 01:20的最新版本

此模块的文档可以在模块:TemplateDemo/doc创建

local html = mw.html

local template_demo = {}

local function trim(str)
	return string.gsub(str, "^%s*(.-)%s*$", "%1")
end

local function parse_template_data(title)
	local content = mw.title.new(title).content
	local raw = string.match(content, "<templatedata>(.-)</templatedata>")
	if raw == nil then
		return nil
	end
	local succ, res = pcall(mw.text.jsonDecode, raw)
	return succ and res or nil
end

local function append_template_argument(t, k, v, last_num_param)
	local num = tonumber(k)
	if num ~= nil then
		if num == 1 then
			k = nil
		else
			k = tostring(num - 1)
			v = trim(v)
		end
	end
	if k ~= nil then
		t[#t+1] = "| "
		if num == nil or last_num_param ~= num - 1 then
			t[#t+1] = k
			t[#t+1] = " = "
		end
		t[#t+1] = v
		t[#t+1] = "\n"
	end
	return num
end

template_demo.show = function(frame)
	local args = frame.args
	local template_name = args[1]

	if template_name == nil or template_name == '' then
		return html.create("p"):addClass("error")
			:wikitext("没有指定模板名!")
	end
	
	local template_data = parse_template_data("Template:"..template_name)
	local args = frame:getParent().args
	
	local t = {"{{", trim(template_name), "\n"}
	local last_num_param = 1
	
	if template_data ~= nil and type(template_data.paramOrder) == "table" then
		local order = template_data.paramOrder
		for i = 1, #order do
			local k = order[i]
			local v = args[k]
			if v ~= nil then
				last_num_param =
					append_template_argument(t, k, v, last_num_param)
			end
		end
	else
		for k, v in pairs(args) do
			last_num_param =
				append_template_argument(t, k, v, last_num_param)
		end
	end
	
	t[#t+1] = "}}"
	
	local source = table.concat(t)
	local result = frame:preprocess(source)
	return result.."示例代码:\n<pre>"..source.."\n</pre>"
end

return template_demo