模块:Va
外观
local p={}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local lib_redirect = require('Module:Redirect')
local lib_zhcvt = require('Module:ZhConversion')
function p.main(frame,...)
local args
local 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 = getArgs(frame, {
parentFirst=true,
}) --frame.args
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 = (type(frame) == type({"table"})) and frame or {frame,...}
working_frame = mw.getCurrentFrame()
end
local title = args[1] or ''
local article_type = args[2] or ''
local final_title = mw.ustring.gsub(title,"%|.*$",'')
local size = p.article_size(final_title)
local template_name
if mw.ustring.match(article_type,"[Ff][Aa]") then template_name = "Va-fa"
elseif mw.ustring.match(article_type,"[Gg][Aa]") then template_name = "Va-ga"
else
if size <= 2999 then template_name = "Va-1"
elseif size >= 3000 and size <=9999 then template_name = "Va-2"
elseif size >= 10000 and size <=29999 then template_name = "Va-3"
else template_name = "Va-4" end
end
if template_name then return working_frame:expandTemplate{ title = template_name}
.. ' [[' .. title .. ']]' .. (yesno(args.with_size) and ("("..p._number_formating(size)..")") or "")end
return ''
end
local function getTitle(...)
local success, titleObj = pcall(mw.title.new, ...)
if success then
return titleObj
else
return nil
end
end
function p.article_size(title)
local input_title = title
if type(title) == type({"table"}) then
input_title = (title.args or {})[1] or title[1] or ''
elseif type(title) ~= type("string") then
input_title = tostring(title)
end
local final_title = title
local success = xpcall(function()
final_title = p.redirect_target(title)
end,function()end)
if not success then mw.addWarning("[[Module:Va]] 超出了模板限制 於標題 \"" .. title .. "\" 顯示的結果可能會不正確。") end
local title_obj = getTitle(final_title)
if not title_obj then return 0 end
local content = title_obj:getContent()
if not content then return 0 end
return string.len(content)
end
--此函數有多個模組/模塊調用,請勿隨意更動
function p.redirect_target(title)
local input_title = title
if type(title) == type({"table"}) then
input_title = (title.args or {})[1] or title[1] or ''
elseif type(title) ~= type("string") then
input_title = tostring(title)
end
local page_code
local checked_title = input_title
xpcall(function()
local titleObj = getTitle(input_title)
local ori_code = titleObj:getContent()
if ori_code then
checked_title = input_title
page_code = ori_code
else
local zh_hant = lib_zhcvt.to_hant(input_title)
local titleHantObj = getTitle(zh_hant)
local hant_code = titleHantObj:getContent()
if hant_code then
checked_title = zh_hant
page_code = hant_code
else
local zh_hans = lib_zhcvt.to_hans(input_title)
local titleHansObj = getTitle(zh_hans)
local hans_code = titleHansObj:getContent()
if hans_code then
checked_title = zh_hans
page_code = hans_code
end
end
end
end,function()end)
if not page_code then return title end
local target = checked_title
mw.ustring.gsub(page_code, "^[%s\n\r]*#[^%[%]]+%[%[([^%[%]]+)%]%]", function(str) target = str end)
return target
end
function p._number_formating(num)
local num_str = tostring(num)
local str_len = num_str:len()
local start_pos = str_len % 3
local result = num_str:sub(1, start_pos)
local flag = false
local i = start_pos
while i < str_len do
result = result .. ',' .. num_str:sub(i+1, i+3)
i = i + 3
end
if result:sub(1, 1) == ',' then result = result:sub(2, -1) end
return result
end
return p