模块:TemplateData:修订间差异

来自「荏苒之境」
创建页面,内容为“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 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 loca…”
 
无编辑摘要
 
(未显示同一用户的3个中间版本)
第9行: 第9行:
if td == nil then
if td == nil then
local content = mw.title.new(title).content
local content = mw.title.new(title).content
if not content then
error("模板页面不存在:"..title)
end
td = utils_str.match_xml_noattr(content, "templatedata")
td = utils_str.match_xml_noattr(content, "templatedata")
TD_CACHE[title] = td
TD_CACHE[title] = td
第28行: 第31行:
template_data.show = function(frame)
template_data.show = function(frame)
local title = frame.args.title
local title = frame.args.title
return "<templatedata>"..extract(title).."</templatedata>"
if title == '' or title == nil then
error("未指定模板标题。")
end
local td = extract(title)
return td and frame:extensionTag("templatedata", td) or ""
end
end


return template_data
return template_data

2025年8月16日 (六) 16:35的最新版本

此模块的文档可以在模块: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
	if title == '' or title == nil then
		error("未指定模板标题。")
	end
	local td = extract(title)
	return td and frame:extensionTag("templatedata", td) or ""
end

return template_data