跳转到内容

模組:Vgname/sandbox

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

这是本页的一个历史版本,由Lopullinen留言 | 贡献2021年7月16日 (五) 16:37编辑。这可能和当前版本存在着巨大的差异。

require('Module:No globals')

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local lc = require('Module:WikitextLC')
local regions = mw.loadData('Module:Vgname/languages')

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

-- 中文變种列表
local variants = {
	{'cn'; simp = '中国大陆', trad = '中國大陸'; script = 'hans' },
	{'hk'; simp = '香港', trad = '香港'; script = 'hant' },
	{'mo'; simp = '澳门', trad = '澳門'; script = 'hant' },
	{'my'; simp = '马来西亚', trad = '馬來西亞'; script = 'hans' },
	{'sg'; simp = '新加坡', trad = '新加坡'; script = 'hans' },
	{'tw'; simp = '台湾', trad = '臺灣'; script = 'hant' },
}

-- 將尾隨<ref>標籤的字串分割爲兩部分
local function splitRef(str)
	local refPattern = '\127\'"`UNIQ%-%-[Rr][Ee][Ff]%-%x+%-QINU`"\'\127' -- [[:w:en:WP:UNIQ]]
	local text, ref = '', ''
	str = str or ''
	
	local s, _ = str:find(refPattern)
	if s then
		text, ref = str:sub(1, s-1), str:sub(s)
	else
		text = str
	end

	return text, ref
end

-- 中文加粗
local function boldText(args, str)
	local isBold = true
	str = str or ''

	if yesno(args.bold) == false then
		isBold = false
	end

	if isBold == true then
		str = "'''" .. str .. "'''"
	end
	
	return str
end

-- 標題文字套書名號或引號
local function bracketText(args, str, prefix, suffix)
	local bracketType = (args.bracketType == nil) and "" or args.bracketType
	
	if prefix and suffix then
		-- nothing to do
	elseif mw.ustring.lower(bracketType) == 'q' then -- 'q'爲了引號(quotation marks)
		prefix, suffix = '「', '」'
	elseif mw.ustring.lower(bracketType) == 's' then -- 's'爲了單書名號(single book title marks)
		prefix, suffix = '〈', '〉'
	elseif yesno(bracketType) == false then
		prefix, suffix = '', ''
	else
		prefix, suffix = "《", "》"
	end
	
	return prefix .. str .. suffix
end

-- 判定外语标签模式
local function getLanguageLabelMode(args)
	local mode = 'same'
	
	if yesno(args.diff) == true then
		mode = 'diff'
	elseif args.na or args.eu or args.au then
		mode = 'diff'
	end
	
	return mode
end

-- 外文斜体
local function italicText(args, str, languageUsesItalic)
	local isItalic = false
	str = str or ''
	
	if yesno(args.italic) == false then
		isItalic = false
	elseif yesno(languageUsesItalic) == true then
		isItalic = true
	end
	
	if isItalic == true then
		str = "''" .. str .. "''"
	end
	
	return str
end

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

local function title(args)
	local title, ref = splitRef(args[1])
	
	title = boldText(args, title)
	title = bracketText(args, title, args["bracket-left"], args["bracket-right"])
	
	return title .. ref
end

local function original(args)
	local returnStr = ""
	local title, ref, titleLatin, refLatin = '', '', '', ''
	local language
	
	-- 检查原文名字段
	for _, v in ipairs(regions) do
		if args[v.langcode] then
			language = v
			break
		end
	end
	
	-- 若没有检测到原文名参数,则函数返回空字串
	if language then
		local t = mw.text.split(args[language.langcode], ',')
		title, ref = splitRef(t[1])
		titleLatin, refLatin = splitRef(t[2])
	else
		return ''
	end
	
	-- 加入原文名标签
	if yesno(args.label) == false then
		-- nothing to do
	else
		returnStr = language[getLanguageLabelMode(args)] .. ':'
	end
	
	-- 加入原文名及脚注
	title = mw.ustring.format('<span lang="%s"%s>-{%s}-</span>',
				language.langcode,
				(language.dir) == 'rtl' and ' dir="rtl"' or '',
				italicText(args, title, language.italic)
			)
	returnStr = returnStr .. title .. ref
	
	-- 加入原文名的拉丁字母写法及脚注(多用于日韩文游戏)
	if titleLatin ~= '' then
		titleLatin = mw.ustring.format('<span lang="%s">-{%s}-</span>', language.langcode, titleLatin)
		returnStr = returnStr .. ',' .. titleLatin .. refLatin
	end
	
	-- 告一段落
	return returnStr
end

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

local p = {}

function p.vgname(frame)
	local args = getArgs(frame)
	return p._vgname(args)
end

function p._vgname(args)
	local args = args
	return original(args)
end

return p