Module:TemplateParameters:修订间差异

删除的内容 添加的内容
从 zhwiki 导入完整版
 
从 zhwiki 导入并适配
 
第4行:
local lib_editstate={};
function p.containsNumber(frame)
-- For calling from #invoke.
local args
local can_call_parse = false
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.
args = frame.args
can_call_parse = true
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
if type(args) ~= type({}) then args = {frame} end
第27行 ⟶ 第32行:
 
function p.getNumberValue(frame)
-- For calling from #invoke.
local args
local can_call_parse = false
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.
args = frame.args
can_call_parse = true
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
if type(args) ~= type({}) then args = {frame} end
第49行 ⟶ 第59行:
 
function p.argTrim(frame)
-- For calling from #invoke.
local args
local can_call_parse = false
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.
args = frame.args
can_call_parse = true
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
if type(args) ~= type({}) then args = {frame} end
第134行 ⟶ 第149行:
end
 
function p.makeTemplateParameter(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.
args = {}
for key,value in frame:argumentPairs() do
第147行 ⟶ 第164行:
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()
第172行 ⟶ 第191行:
end
 
function p.call(frame) --轉換模板參數為lua參數
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()
第188行 ⟶ 第211行:
if args.isJSON then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args.isJSON or 'no') then --local delnowiki=require('Module:Delcat').delnowiki --備用
isJSON = true
end
第208行 ⟶ 第231行:
if num_args[i] then
local try_to_get_obj = mw.clone(num_args[i])
if not xpcall( function() --try
try_to_get_obj = get_obj(num_args[i])
end, function(msg)return msg end ) then --catch
call_args[i-1] = num_args[i]
else --finally
call_args[i-1] = try_to_get_obj
end
第320行 ⟶ 第343行:
end
 
--外部讀取不定參數
function p.FormatingArguments(frame)
-- For calling from #invoke.
local args
--是否存在有效的frame可供呼叫
local can_call_parse = false
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.
args = frame.args
can_call_parse = true
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
local input_str = '' --輸入的格式化字串
local count = 1 --預設參數數量為1個一組
if args["format"] or args["Format"] or args["格式"] then input_str = args["format"] or args["Format"] or args["格式"] or '' end
if args["count"] then count = tonumber(args["count"] or 1) or 1 end
local display_all = false --是否顯示跳過的參數 (如輸入1,2,5參數,決定是否輸出3,4參數)
local delnowiki = false --是否從nowiki中解除包裝
local hasPrefixArg = false
local arrPrefixArg = {}
local allexpr = false --是否每組參數全部都呼叫一次帶參數的新解析器 (高開銷,最多100次)
local allexpr_count = 1 --紀錄呼叫帶參數的新解析器次數,以免超過100次lua停止運作
if args["displayAll"] then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
第350行 ⟶ 第380行:
end
end
 
if args["delnowiki"] then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args["delnowiki"] or 'no') then --local delnowiki=require('Module:Delcat').delnowiki --備用
input_str = mw.text.unstripNoWiki( input_str )
end
第362行 ⟶ 第393行:
end
end
if args["allexpr"] then --較耗費資源
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
allexpr = yesno(args["allexpr"] or 'no')
end
count = math.floor(count) --不處理小數
if count < 1 then count = 1 end --非正整數當作1
local other_num_args = {} --紀錄範圍外參數
if args["checkMoreArg"] then
if args["checkMoreArg"] then --處理範圍外參數 (如已定義每組3參數,但想用{{{4}}}存取下一組的第一參數)
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args["checkMoreArg"] or 'no') then --較耗費資源
local allnumarg_checker = mw.ustring.gmatch(input_str,"%{%{%{[^%{%}%|]+[%}%|]")
local pre_arg_str = allnumarg_checker() --匹配模板參數
local find_num_args = {}
while(pre_arg_str) do
第383行 ⟶ 第415行:
pre_arg_str = allnumarg_checker()
end
for check_is_other_num_args, _ in pairs(find_num_args) do --紀錄範圍外參數
if check_is_other_num_args > count or check_is_other_num_args < 1 then
other_num_args[#other_num_args + 1] = check_is_other_num_args
第390行 ⟶ 第422行:
end
end
 
local usingConditionalExpressions = false --是否額外再呼叫解析器函數
if args.usingConditionalExpressions then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
第398行 ⟶ 第431行:
end
end
local out_frame = mw.getCurrentFrame():getParent() --模板外層
local out_args = {} --紀錄來自模板外層提供的參數
local body = ''
if out_frame then out_args = out_frame.args or out_args end
local valueTable = {}
local valueTable = {} --紀錄可用值 (參數) 提供填入參數之用
local max_count, i, j = 1, 0, 0 for index, arg_val in pairs(out_args) do if (tonumber(index) or 0) > max_count then max_count = (tonumber(index) or 0) end valueTable[#valueTable + 1] = arg_val end
max_count = math.ceil(max_count / count); --獲輸入參數之最大數量 (以組為單位)
if hasPrefixArg then max_count = getMaxArgCount(out_args, max_count, arrPrefixArg) end
 
local last_one = false
for i = 1, max_count do
local local_arg = {} --紀錄本組的參數
local_arg.isLast, local_arg["isFirst"]= '', '' --提供判斷的魔術參數
local_arg.count=tostring(max_count)
local_arg.argGroupID = tostring(i)
第417行 ⟶ 第453行:
if i == 1 then local_arg["isFirst"] = '1' end
if allexpr and allexpr_count > 99 then
allexpr = false --紀錄呼叫帶參數的新解析器次數,超過100次停止運作,改用純字串替換完成輸出
allexpr = false
mw.addWarning( "指定 \"allexpr\" 參數全部展開已超過展開上限,後面的參數將使用低消耗函式填入數值。" )
end
if(#valueTable > 1) then --提供部分須輪播展示的參數組
local_arg.allRandom = valueTable[math.random( 1, #valueTable )]
local rand_local = math.random(1, count)
local rand_it = (i-1) * count + rand_local --查無參數則從外層抓
local_arg.random = out_args[tostring(rand_it)] or out_args[rand_it] or args[tostring(rand_local)] or args[rand_local]
if local_arg.random == nil and not allexpr then local_arg.random=('{{{' .. tostring(rand_local) .. '}}}') end
end
local is_ready = false --本組參數是否有值
for j = 1, count do
local it = (i-1) * count + j
第433行 ⟶ 第469行:
local_arg[j] = out_args[tostring(it)] or out_args[it] or args[tostring(j)] or args[j]
if local_arg[j] == nil and not allexpr then local_arg[j]=('{{{' .. tostring(j) .. '}}}') end
--找到本組參數的其中一個值
if out_args[tostring(it)] or out_args[it] then is_ready = true end
end
if hasPrefixArg then local_arg, is_ready = addPrefixArgToArgList(out_args, args, local_arg, tostring(i), arrPrefixArg, allexpr, is_ready) end
for _, other_arg_it in ipairs(other_num_args) do --處理範圍外參數
local it = (i-1) * count + other_arg_it
local_arg[other_arg_it] = out_args[tostring(it)] or out_args[it] or args[tostring(other_arg_it)] or args[other_arg_it]
if local_arg[other_arg_it] == nil and not allexpr then local_arg[other_arg_it]=('{{{' .. tostring(other_arg_it) .. '}}}') end
end
if is_ready or display_all then --本組參數有值存在才輸出; 如果設定全部顯示也顯示
local output_str = tostring(input_str)
if allexpr then
第448行 ⟶ 第487行:
end
local working_frame = frame:newChild{ title = frame:getTitle(), args = local_arg }
body=body..working_frame:preprocess( output_str ) --呼叫帶參數的新解析器
allexpr_count=allexpr_count+1
else
body=body..p._getFormatingStringByArgument(output_str, local_arg) --使用一般字串取代填入參數
end
end
end
 
if usingConditionalExpressions and can_call_parse then
body = frame:preprocess( body )
第461行 ⟶ 第502行:
end
 
--內部讀取確定參數
function p.getFormatingStringByArgument(frame)
-- For calling from #invoke.
local args
local can_call_parse = false
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) --frame.args
can_call_parse = true
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
第507行 ⟶ 第554行:
end
 
--格式化字串的處理,會將特殊的 {{{}}} 轉換
function p._getEscapeString(str) str = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(str, "%%", "%%%%"), "%]", "%]"), "%{", "%{"), "%}", "%}"), "%[", "%["), "%)", "%)"), "%-", "%-"), "%^", "%^"), "%$", "%$"), "%(", "%("), "%.", "%."), "%*", "%*"), "%+", "%+"), "%|", "%|"); return str; end function p._getFormatingStringByArgument(str, args)
local result = str or "*{{{1}}}\n"
第528行 ⟶ 第576行:
 
function p.arg_to_spstr(frame)
-- For calling from #invoke.
local args
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)
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
if type(frame) ~= type({}) then args = {frame} end
第544行 ⟶ 第597行:
 
function p.pass_spstr(frame)
-- For calling from #invoke.
local args
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) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
if type(frame) ~= type({}) then args = {frame}
else args = frame end
第555行 ⟶ 第613行:
if args["delnowiki"] then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
if yesno(args["delnowiki"] or 'no') then --local delnowiki=require('Module:Delcat').delnowiki --備用
input_text = mw.text.unstripNoWiki( input_text )
end
第584行 ⟶ 第642行:
local nkey = tonumber(key)
if key ~= '' then input_args[key],child_args[key] = spilt_kv[2],spilt_kv[2] end
--if nkey ~= nil then input_args[nkey],child_args[nkey] = spilt_kv[2],spilt_kv[2] end
end
end
第603行 ⟶ 第662行:
local args, new_name
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.
new_name = frame.args['1'] or frame.args[1] or frame.args.name or frame.args.Name
local parent = frame:getParent() or {['args']={}}
args = parent.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
if type(frame) == type('string') then new_name = frame
elseif type(frame) == type({}) then new_name = frame['1'] or frame[1] or frame.name or frame.Name