跳转到内容

模組:TemplateVariadicArgumentSingle

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

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

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" ) --載入模板樣板
	if not template_obj then return '' end --如模板樣板載入失敗,返回空
	local template_body = template_obj:getContent() --讀取模板樣板
	if mw.text.trim(template_body or '') == '' then return '' end --如果讀取不到模板樣板,返回空
	--摘除模板樣板中的noinclude
	local noinclude = mw.ustring.find(template_body,"<%s*noinclude%s*>")
	while noinclude do
		local _, 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 = {} --用於儲存有多少組不定參數
	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) --統一以小寫匹配
			local loaded_value = false --等一下檢查有無讀到有效的body或title參數
			if par_name_check == 'body' then --匹配到body參數
				loaded_value = true --有效的body參數
				--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 --匹配到title參數
				loaded_value = true --有效的title參數
				--new row
				if type(row_list[par_num]) == type(nil) then row_list[par_num] = {}end
				row_list[par_num].title = value --存入參數
			end
			if loaded_value and (par_num > max_id) then max_id = par_num 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和title參數其一
				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)
	--解析要使用外層解析器 (才讀得到那些不定參數,內層是{{#invoke:}})
	working_frame = working_frame:getParent() or working_frame
	--將包含了不定參數的模板展開
	template_body = working_frame:preprocess(template_body)
	return template_body --回傳展開了不定參數的模板結果
end

return p