跳转到内容

模組:TemplateVariadicArgumentSingle

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2023年12月7日 (四) 02:10编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local lib_arg = {}
function p.build_template(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {parentFirst=true})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
	local template_obj = mw.title.new( "Template:US_county_navigation_box/core" )
	local template_body = template_obj:getContent()
	local noinclude = mw.ustring.find(template_body,"<%s*noinclude%s*>")
	while noinclude do
		_, noinclude_end = mw.ustring.find(template_body,"<%s*/%s*noinclude%s*>")
		noinclude_end = noinclude_end or -1
		template_body = mw.ustring.sub(template_body, 1, noinclude-1) .. ((noinclude_end < 0) and '' or mw.ustring.sub(template_body, noinclude_end + 1, -1))
		noinclude = mw.ustring.find(template_body,"<noinclude>")
	end
	local row_list = {} --array
	local max_id = -1
	for key, value in pairs(args) do
		local par_name_id, _ = mw.ustring.find(key,"%d+$")
		local par_name = par_name_id and mw.ustring.sub(key, 1, par_name_id - 1) or nil
		local par_id = par_name_id and mw.ustring.sub(key, par_name_id, -1) or nil
		local par_num = tonumber(par_id)
		if par_name and par_id then
			local par_name_check = mw.ustring.lower(par_name)
			if par_num > max_id then max_id = par_num end
			if par_name_check == 'body' then
				--new row
				if type(row_list[par_num]) == type(nil) then row_list[par_num] = {}end
				row_list[par_num].body = value
			elseif par_name_check == 'title' then
				--new row
				if type(row_list[par_num]) == type(nil) then row_list[par_num] = {}end
				row_list[par_num].title = value
			end
		end
	end
	local body = ''
	for i = 1, max_id do
		local para_obj = row_list[i]
		if para_obj then
			if para_obj.body or para_obj.title then
				body = body .. "\n\n" ..
					"|group"..i.." = {{{title"..i.."|}}}\n"..
					"|list"..i.." = {{#if:{{{body"..i.."|}}}|<div>\n"..
					"{{{body"..i.."}}}\n"..
					"</div>}}"
			end
		end
	end
	template_body = mw.ustring.gsub(template_body, "__LUA_INSERT_CORE__", body)
	working_frame = working_frame:getParent() or working_frame
	template_body = working_frame:preprocess(template_body)
	return template_body
end

return p