跳转到内容

模組:Vgname/sandbox

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

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

require('Module:No globals')

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

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

local function refSplitter(content)
	local pattern = '\127\'"`UNIQ%-%-[Rr][Ee][Ff]%-%x+%-QINU`"\'\127' -- [[:en:WP:UNIQ]]
	
	local s, _ = content:find(pattern)
	if s then
		return content:sub(1, s-1), content:sub(s)
	end
	
	return content, ''
end

local function bold(args, content)
	local isBold = true
	
	if yesno(args.bold) == false then
		isBold = false
	end
	
	if isBold then
		content = '<b class="vgname-bold">' .. content .. '</b>'
	end
	
	return content
end

local function italicAndLang(args, langIndex, content)
	local langcode = lang[langIndex].langcode
	local italicOrSpan = 'i'
	local dir = ''
		
	if yesno(args.italic) == false then
		italicOrSpan = 'span'
	elseif lang[langIndex].italic ~= true then
		italicOrSpan = 'span'
	end
	
	if lang[langIndex].rtl == true then
		dir = 'dir="rtl"'
	end
	
	return string.format('<%s lang="%s" %s>-{%s}-</%s>', italicOrSpan, langcode, dir, content, italicOrSpan)
end

local function main(args)
	local title, refs
	
	title, refs = refSplitter(args[1] or mw.title.getCurrentTitle())
	title = bold(args, title)
	
	if args.bracket == 'q' then
		title = '「' .. title .. '」'
	elseif yesno(args.bracket) == false then
		-- Nothing to do.
	else
		title = '《' .. title .. '》'
	end
	
	return title .. refs
end

local function original(args)
	--[[
	1. 找到需要标示的原文名
	2. 分割为原文名、拉丁转写来源、参考资料三部分
	2. 为前两部分标记不转换标签、语言标识、斜体
	4. 标记语言label
	--]]
	local orig, origEtymology, refs, refs2
	local langIndex

	for i, v in ipairs(lang) do
		if args[v.langcode] then 
			langIndex = i
			orig = args[v.langcode]
			break
		end
	end
	
	if langIndex == nil then return end
	
	if orig:find(',') then
		local s = orig:find(',')
		orig, origEtymology = orig:sub(1, s-1), orig:sub(s+3) -- One Chinese comma takes three bytes.
	else
		origEtymology = ''
	end
	
	orig, refs = refSplitter(orig)
	origEtymology, refs2 = refSplitter(origEtymology)
		
	orig = italicAndLang(args, langIndex, orig)
	if origEtymology ~= '' then
		origEtymology = ',' .. italicAndLang(args, langIndex, origEtymology)
	end
	
	return orig  .. refs .. origEtymology .. refs2
	
end

local function english(args)
	local tab = {}
	
	if args.na then
		table.push(tab, italicAndLang(args, 'na', args.na))
	end
	if args.eu then
		table.push(tab, italicAndLang(args, 'eu', args.eu))
	end
	if #tab > 0 then
		return table.concat(tab, ',')
	end
	
	if args.en then
		return italicAndLang(args, 'en', args.en)
	end
	
end	
	
local p = {}

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

function p._vgname(args)

	return main(args) .. original(args) .. english(args)
end

return p