跳转到内容

模組:Vgname

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由風中的刀劍留言 | 贡献2015年9月11日 (五) 16:16编辑。这可能和当前版本存在着巨大的差异。

require('Module:No globals')

local langList = mw.loadData('Module:Vgname/languages')

local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs
local p = {}

local function refSeparator(content)
	local pattern = '\127UNIQ%-%-ref%-%x+%-QINU\127$'
	local k = string.find(content, pattern)
	
	if k then
		return string.sub(content, 1, k - 1), string.sub(content, k)
	end
	
	return content, ''
end

local function bold(args, content)
	local content, refs = refSeparator(content)
	
	if yesno(args.bold) ~= false then
		return string.format('<b>%s</b>%s', content, refs)
	end
	
	return string.format('%s%s', content, refs)
end

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

local function labelWithContent(args, content, index, isDiff, isTrans)
	local label, code, refs, trans, ret
	local v = langList[index]
	
	if yesno(args.label) == false then
		label  = ''
	else
		label = isDiff and v[3] or v[2]
		label = label .. ':'
	end
	
	code = v[1]
	
	content, refs = refSeparator(content)
	
	if isTrans then
		local _, temp
		_, _, temp, trans = string.find(content, "(.+),([%w%p%s]+)")
		content = temp or content
	end
	
	if (yesno(args.italic) ~= false) and (v.italic == true) then
		content = string.format('<i>%s</i>', content)
		trans = trans and string.format('<i>%s</i>', trans)
	end
	
	trans = trans and string.format(',-{<span lang="%s" xml:lang="%s">%s</span>}-', code, code, trans) or ''
	
	return string.format('%s-{<span lang="%s" xml:lang="%s">%s</span>}-%s%s', label, code, code, content, trans, refs)
		
end

local function variantMode(args)
	local cn, hk, tw = args.cn, (args.hk or 'tw'), args.tw
	
	if cn and tw then else return end
		
	if hk == 'tw' or hk == tw then
		return 'hktw'
	end
	
	if hk == 'cn' or hk == cn then
		return 'cnhk'
	end
	
	if tw == 'cn' or tw == cn then
		return 'cntw'
	end
	
	if cn == 'tw' then
		args.cn = args.tw
		return 'cntw'
	end
	
	return 'default'
	
end

local function translationContent(args, content, area, as, multiple)
	local temp, refs
	
	local areaList = {
		['cn'] = '中国大陆',
		['hk'] = '香港',
		['tw'] = '台灣',
		['cnhk'] = '中国大陆和香港',
		['cntw'] = '中国大陆和台灣',
		['hktw'] = '香港和台灣',
		[''] = '',
	}
	
	area = areaList[area]
	content, refs = refSeparator(content)
	
	if content == 'en' then
		temp = '常用英文'
	else
		if multiple then
			content = content .. '、'
			content = string.gsub(content, "(.-) *、", '「' .. bold(args, '%1') .. '」、' )
			content = string.sub(content, 1, -4)
		else
			content = '「' .. bold(args, content) .. '」'
		end
		temp = string.format('%s%s', as, content)
	end
	
	return string.format('-{zh; zh-hans; zh-hant;|%s%s}-%s', area, temp, refs)
end

local function lead(args)
	
	if args.bracket == 'q' then
		return string.format('「%s」', bold(args, args[1]))
	end
	
	if yesno(args.bracket) ~= false then
		return string.format('《%s》', bold(args, args[1]))
	end
	
	return bold(args, args[1])
	
end

local function orig(args, isDiff)

	for i, v in ipairs(langList) do
		if args[v[1]] then
			return labelWithContent(args, args[v[1]], i, isDiff, true)
		end
	end

end

local function english(args, isDiff)
	local ret
	
	if args.na then
		ret = labelWithContent(args, args.na, 'na', isDiff)
	end
	
	if args.eu then
		ret = (ret and (ret .. ',') or '') .. labelWithContent(args, args.eu, 'eu', isDiff)
	end
	
	if (ret == nil) and args.en then
		ret = labelWithContent(args, args.en, 'en', isDiff)
	end
	
	return ret
end

local function variant(args, variantMode)
	local ret = ''
	local variantList = {'', 'hans', 'hant', 'cn', 'hk', 'mo', 'sg', 'tw'}
	local t = { 
			['hktw'] = { {'cn', 'cn'}, {'hktw', 'tw'} },
			['cnhk'] = { {'cnhk', 'cn'}, {'tw', 'tw'} },
			['cntw'] = { {'cntw', 'cn'}, {'hk', 'tw'} },
		 	['default'] = { {'cn', 'cn'}, {'hk', 'tw'}, {'tw', 'tw'} }
		 }
	local modeList = t[variantMode]
		
	for _, v in ipairs(variantList) do
		local temp, flag = ''
				if v == '' then
			flag = 'zh'
		else
			flag = 'zh' .. '-' .. v
		end
		
		if v == 'mo' then v = 'hk' end
		
		for _, w in ipairs(modeList) do
			if v == string.sub(w[1], 1, 2) or v == string.sub(w[1], -2, -1) then
				-- Nothing to do
			else
				temp = temp .. translationContent(args, args[w[2]], w[1], '譯作') .. ','
			end
		end
	
		temp = string.sub(temp, 1, -4)
		
		ret = ret .. string.format('%s:%s; ', flag, temp)
		
	end
	
	ret = string.format('-{ %s}-', ret)
	
	return ret
end

local function aka(args)
	local ret = ''
	
	args.cnhk = args.cnhk or args.hkcn
	args.cntw = args.cntw or args.twcn
	args.hktw = args.hktw or args.twhk
	local akaList = {'', 'cnhk', 'cntw', 'hktw', 'cn', 'hk', 'tw'}
	
	if args.aka then
		ret = ret .. translationContent(args, args.aka, '', '又譯', true) .. ','
	end
	
	for _, v in ipairs(akaList) do
		if args['aka-' .. v] then
			ret = ret .. translationContent(args, args['aka-' .. v], v, '又譯', true) .. ','
		end
	end
	
	if ret == '' then return end
	
	return string.sub(ret, 1, -4)
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	-- Main module code goes here.
	local allNotes
	
	local isDiff = isDiff(args)
	local variantMode = variantMode(args)
	
	local lead = lead(args) 
	local orig = orig(args, isDiff) or nil
	local english = english(args, isDiff) or nil
	local variant = variantMode and variant(args, variantMode) or nil
	local aka = aka(args) or nil
	local note = args[2] or nil
	
	if orig and english then
		allNotes = string.format('%s,%s', orig, english)
	elseif orig or english then
		allNotes = orig or english
	else
		return '<span class="error">本模板仅用于外文游戏。若您正确输入原语言代码但模板报错,请于[[Module:Vgname/languages]]新增语言代码。</span>'
	end
	
	if variant then
		allNotes = allNotes .. ',' .. variant
	end
	
	if aka then
		allNotes = allNotes .. ',' .. aka
	end
	
	if note then
		allNotes = allNotes .. ',' .. note
	end

	return string.format('%s<span style="font-weight: normal">(%s)</span>', lead, allNotes)
end

return p