跳转到内容

模組:Vgname/testcases

维基百科,自由的百科全书
local languageList = mw.loadData('Module:Vgname/languages')
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

local function refSeparator( a )
	a = a or ''
	local refPattern = '\127UNIQ%x+%-ref%-%x+%-QINU\127'
	local p, _  = string.find( a, refPattern )
	local contentPart, refPart
	
	if p then
		contentPart, refPart = string.sub( a, 1, p - 1 ), string.sub( a, p )
	else
		contentPart, refPart = a, ''
	end
	
	return contentPart, refPart
end

local function bold( a )
	local cont, ref = refSeparator( a )
	
	if yesno( _bold ) then
		cont = '<b>' .. cont  .. '</b>' .. ref
	end
	
	return cont
end

local function italic( a )
	local cont, ref = refSeparator( a )
	
	if yesno( _italic ) then
		cont = '<i>' .. cont ..'</i>' .. ref
	end
	
	return cont
end

local function isDiff( args )
	if yesno( _diff ) or args.na or args.eu then
		return true
	end
end

local function lang( langcode, a )
	local span = mw.html.create( 'span' )
	local content, ref = refSeparator( a )
	
	span
		:attr( 'lang', langcode )
		:wikitext( '-{' .. content .. '}-' )
		
	return tostring( span )  .. ref 
end

local function regionName( region , args )
	local varient = {
		{ 'cn', '中国大陆', args.cn },
		{ 'hk', '香港', args.hk },
		{ 'tw', '台湾', args.hk },
		{ 'cn-hk', '中国大陆和香港', args.cn },
		{ 'cn-tw', '中国大陆和台湾', args.cn },
		{ 'hk-tw', '香港和台湾', args.tw },
	}
	local ret = ''
	
	for i, v in ipairs( varient ) do
		if region == v[1] then
			if v[3] == 'en' then
				ret = v[2] .. '常用英文'
			else
				ret = '-{zh; zh-hans; zh-hant;|' .. bold( v[3] ) .. '}-'
				ret = v[2] .. '译作' .. '「' .. ret .. '」'
			end
			break
		end
	end
	
	return ret
end

local function markSeparator( a )
	local cont, refs = refSeparator( a )
	cont = cont .. '、'
	
	a = string.gsub( cont, "(.-)、", '「' .. bold( '%1' ) .. '」、' )
	
	a = string.sub( a, 1, -4 )
	
	return a .. refs
end

--------------------------------------------------------------------------

local function intro( args )
	local ret = args[1]
	
	ret = bold( ret )
	
	if yesno( _bracket ) then
		ret = '《' .. ret .. '》'
	end
	
	return ret
end

local function original( args ) 
	local ret
	
	for i, v in ipairs( languageList ) do
		if args[v[1]] then
			if isDiff( args ) then
				ret = v[3] .. ':' .. lang( v[1], args[v[1]] )
			else
				ret = v[2] .. ':' .. lang( v[1], args[v[1]] )
			end
			
			break
		end
	end
	
	return ret
end

local function english( args )
	local ret
	
	if args.na then
		ret = '美版名:' .. italic( lang( 'en', args.na ) )
	end
	
	if args.eu then
		ret = ret and ( ret .. ',' ) or ''
		ret = ret .. '欧版名:' .. italic( lang( 'en', args.eu ) )
	end
		
	if args.en and ret == nil then
		ret = isDiff( args ) and '英文版名' or '英文名'
		ret = ret .. ':' .. italic( lang( 'en', args.en ) )
	end
	
	return ret
end

local function chinese( args )
	if args.cn or args.tw then else return nil end
	
	local ret, temp
	local varList = {
	['hk-tw'] = {
		['zh'] = { 'cn', 'hk-tw' },
		['zh-hans'] = { 'tw', 'cn-hk' },
		['zh-hant'] = { 'tw', 'cn-hk' },
		['zh-cn'] = { 'hk-tw' },
		['zh-hk'] = { 'cn' },	
		['zh-mo'] = { 'cn' },
		['zh-sg'] = { 'cn', 'hk-tw' },
		['zh-tw'] = { 'cn' }
	},
	
	['cn-hk'] = {
		['zh'] = { 'cn-hk', 'tw' },
		['zh-hans'] = { 'cn-hk', 'tw' },
		['zh-hant'] = { 'cn-hk', 'tw' },
		['zh-cn'] = { 'tw' },
		['zh-hk'] = { 'cn-hk' },
		['zh-mo'] = { 'tw' },
		['zh-sg'] = { 'cn-hk', 'tw' },
		['zh-tw'] = { 'cn-hk' },
	},

	['cn-tw'] = {
		['zh'] = { 'cn-tw', 'hk' },
		['zh-hans'] = { 'cn-tw', 'hk' },
		['zh-hant'] = { 'cn-tw', 'hk' },
		['zh-cn'] = { 'hk' },
		['zh-hk'] = { 'cn-tw' },	
		['zh-mo'] = { 'cn-tw' },
		['zh-sg'] = { 'cn-tw', 'hk' },
		['zh-tw'] = { 'hk' }
	},

	['default'] = {
		['zh'] = { 'cn', 'hk', 'tw' },
		['zh-hans'] = { 'cn', 'hk', 'tw' },
		['zh-hant'] = { 'cn', 'hk', 'tw' },
		['zh-cn'] = { 'hk', 'tw' },
		['zh-hk'] = { 'cn', 'tw' },	
		['zh-mo'] = { 'cn', 'tw' },
		['zh-sg'] =  { 'cn', 'hk', 'tw' },
		['zh-tw'] = { 'cn', 'hk' },
	}
	
}
	
	if args.hk == 'tw' then
		temp = 'hk-tw'
	elseif args.hk == 'cn' then
		temp = 'cn-hk'
	elseif args.tw == 'cn' then
		temp = 'cn-tw'
	else
		temp = 'default'
	end

	ret = '-{'
	for i, v in pairs( varList[temp] ) do
		ret = ret .. i .. ':'
		for j, w in ipairs( v ) do
			ret = ret .. regionName( w, args ) .. ','
		end
		ret = string.sub( ret, 1, -4 ) .. '; '
	end
	
	ret = ret .. '}-'

	
	return ret
end

local function others( args )
	local tab = { {'cn', '中国大陆'}, {'hk', '香港'}, {'tw', '台湾' } }
	local ret
 
	for i, v in ipairs( tab ) do
 
		if args[ 'aka' .. '-' .. v[1] ] then
			ret	= ret .. v[2] .. '又译' .. markSeparator( args['aka' .. '-' .. v[1]] ) .. ','
		end
 
		for j, w in ipairs( tab ) do
			if args[ 'aka' .. '-' .. v[1] .. w[1] ] then
				ret = ret .. v[2] .. '和' .. w[2] .. '又译' .. markSeparator( args['aka' .. '-' .. v[1] .. w[1] ] )  .. ','
			end
		end
 
	end
 
	if ret then
		return string.sub( ret, 1, -4 )
	else
		return nil
	end
	
end

--------------------------------------------------------------------------

local p = {}
 
function p.vgname(frame)
	local args = getArgs(frame)
	
	_bold = args.bold or 'yes'
	_italic = args.italic or 'yes'
	_bracket = args.italic or 'yes'
	
	args.hk = args.hk or 'tw'

	if args.cn == 'tw' then
		args.cn = args.tw
		args.tw = 'cn'
	end

	return p._main(args)
end
 
function p._main(args)
	local ret = ''
	local list ={ original( args ), english( args ), chinese( args ), others( args) ,args[2] }
	
	for i, v in ipairs( list ) do
		if v then
			ret = ret .. v .. ','
		end
	end
	
	ret = intro( args ) .. '<span style="font-weight: normal">(' .. string.sub( ret, 1, -4 ) .. ')</span>'
	
	return ret
end
 
return p