模組:Va
外观
local p={}
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]
local article_type = args[2]
local final_title = p.redirect_target(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 .. ']]' 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 = p.redirect_target(title)
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 zh_hant = lib_zhcvt.to_hant(input_title)
local zh_hans = lib_zhcvt.to_hans(input_title)
local success, select_title = xpcall(function()
if (getTitle(input_title) or {}).exists then return input_title
elseif (getTitle(zh_hant) or {}).exists then return zh_hant
elseif (getTitle(zh_hans) or {}).exists then return zh_hans end
end, function()end)
if not select_title then return title end
return lib_redirect.luaMain(select_title)
end
return p