模块:TemplateData

来自「荏苒之境」
Sicusa留言 | 贡献2025年8月16日 (六) 16:10的版本

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

local utils_str = require("Module:Utils/String")

local template_data = {}

local TD_CACHE = {}

local function extract(title)
	local td = TD_CACHE[title]
	if td == nil then
		local content = mw.title.new(title).content
		if not content then
			error("模板页面不存在:"..title)
		end
		td = utils_str.match_xml_noattr(content, "templatedata")
		TD_CACHE[title] = td
	end
	return td
end

template_data.extract = extract

template_data.parse = function(title)
	local raw = extract(title)
	if raw == nil then
		return nil
	end
	local succ, res = pcall(mw.text.jsonDecode, raw)
	return succ and res or nil
end

template_data.show = function(frame)
	local title = frame.args.title
	return "<templatedata>"..extract(title).."</templatedata>"
end

return template_data