跳转到内容

模組:Va

维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2022年8月18日 (四) 14:20 (技術限制需求 Special:PermaLink/73258479#Template: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