模組:Zh-dial-syn

維基詞典,自由的多語言詞典


local export = {}
local m_links = require("Module:links")
local zh = require("Module:languages").getByCode("zh")

local variety_list = {
	"Classical", "Formal", "Argot", "Taxonomic", 
	"官話", "粵語", "贛語", "客家語", "徽語", "晉語", "九姓漁民船上話",
	"閩北語", "閩東語", "閩南語", "莆仙語","閩中語", "邵將語", "中山閩語",
	"南部平話", "北部平話", "畲話", "瓦鄉話", "吳語", "湘語"
}

local variety_colour = {
	["Classical"]	= "FAF0F2",
	["Formal"]		= "FAF0F2",
	["Argot"]		= "FAF0F2",
	["Taxonomic"]	= "FAF0F2",
	["官話"]	= "FAF5F0",
	["粵語"]	= "F0F5FA",
	["贛語"]			= "F0FAF3",
	["客家語"]		= "FAF0F6",
	["徽語"]		= "FAF9F0",
	["晉語"]			= "F0F5FA",
	["九姓漁民船上話"]	= "FAF0FE",
	["閩北語"]		= "F7FAF0",
	["閩東語"]	= "F7FAF0",
	["閩南語"]		= "F7FAF0",
	["莆仙語"]	= "F7FAF0",
	["閩中語"]	= "F7FAF0",
	["邵將語"]	= "F7FAF0",
	["中山閩語"]	= "F7FAF0",
	["北部平話"]		= "F0F5FA",
	["南部平話"]		= "F0F5FA",
	["畲話"]		= "FAF0F6",
	["瓦鄉話"]			= "F0FAF6",
	["吳語"]			= "F4F0FA",
	["湘語"]		= "F0F2FA",
}

local special_note = {
	["Classical"] = "[[w:文言文|文言文]]",
	["Formal"] = "書面語 <small>([[w:官話白話文|白話文]])</small>",
	["Argot"] = "黑話",
	["Taxonomic"] = "分類學名稱",
}

function export.main(frame)
	local args = frame:getParent().args
	local pagename = mw.title.getCurrentTitle().text
	local target_page = args[1] or pagename
	local resource_page = "Module:zh/data/dial-syn/" .. target_page
	local variety_data = require("Module:zh/data/dial")
	local m_syndata
	if mw.title.new(resource_page).exists then
		m_syndata = require(resource_page).list
	else
		return frame:expandTemplate{ title = "Template:zh-dial/uncreated", args = { target_page } }
	end
	
	local template = {
		["Classical"]	=	{},
		["Formal"]		=	{},
		["Argot"]		=	{},
		["Taxonomic"]	= 	{},
		["官話"]	=	{},
		["粵語"]	=	{},
		["贛語"]			=	{},
		["客家語"]		=	{},
		["徽語"]		=	{},
		["晉語"]			= 	{},
		["九姓漁民船上話"]	= 	{},
		["閩北語"]		=	{},
		["閩東語"]	= 	{},
		["閩南語"]		=	{},
		["莆仙語"]	=	{},
		["閩中語"]	=	{},
		["邵將語"]	=	{},
		["中山閩語"]	=	{},
		["南部平話"]		=	{},
		["北部平話"]		=	{},
		["畲話"]		=	{},
		["瓦鄉話"]		=	{},
		["吳語"]			=	{},
		["湘語"]		=	{},
	}

	local main_title = mw.ustring.gsub((target_page == pagename and pagename or '[[' .. target_page .. ']]'), "[0-9%-]", "")
	local syn_table_string = [=[
	{| class="wikitable mw-collapsible mw-collapsed" style="margin:0; text-align:center;"
	|-
	! style="background:#FCFFFC; width:40em" colspan=3 | 方言用詞 — <b><span class="Hani" lang="zh">]=] ..
		main_title .. '</span></b>'
	--判斷釋義是否含有拉丁字母
	if not m_syndata["meaning"]:match('[A-Za-z]') then
		syn_table_string = syn_table_string .. ' (“' .. m_syndata["meaning"] .. '”) '
	end
	syn_table_string = syn_table_string .. "[[Template:Zh-dial-map/" .. target_page .. '|<small>&#91;地圖&#93;</small>]]\n' .. [=[
	|-
	! style="background:#E8ECFA" | 語言
	! style="background:#E8ECFA" | 地區
	! style="background:#E8ECFA" | 詞]=] .. [=[
	<div style="float: right; clear: right; font-size:60%"><span class="plainlinks">[]=] ..
		tostring(mw.uri.fullUrl("Module:zh/data/dial-syn/" .. target_page, { ["action"] = "edit" })) ..
	' 編輯]</span></div>'
	local syn_table = { syn_table_string }
	
	m_syndata["meaning"] = nil
	if m_syndata["note"] then
		note = m_syndata["note"]
		m_syndata["note"] = nil
	end
	m_syndata["Formal"] = m_syndata["Formal"][1] ~= "" and m_syndata["Formal"] or { target_page }
	
	for location, synonym_set in pairs(m_syndata) do
		if synonym_set[1] ~= "" then
			local formatted_synonyms = {}
			for i, synonym in ipairs(synonym_set) do
				local synonym_note = mw.text.split(synonym, ":")
				local synonym_etym = mw.text.split(synonym_note[1], "_")
				local syn, etym, note = synonym_etym[1], synonym_etym[2], synonym_note[2]
				local synonym_link = m_links.full_link( {
						term = syn .. (etym and string.format("#詞源%s", etym) or "") .. "//",
						alt = etym and (syn .. string.format("<sub>%s</sub>", etym)) .. "//",
						lang = zh,
						tr = "-"
					} )
				note = note and ' <span style="font-size:60%"><i>' .. note .. '</i></span>' or ""
				table.insert(formatted_synonyms, synonym_link .. note)
			end
			local location_data = variety_data[location] or error("沒有關於" .. location .. "的數據!")
			local location_name = mw.ustring.gsub(location_data.chinese or location, "((.*%))", "<small>%1</small>")
			local location_link = location_data.link or location_name
			if template[location_data.group] ~= nil then
				table.insert(template[location_data.group],
					{ location_data.order, location_name, location_link, formatted_synonyms })
			end
		end
	end
	
	for _, variety in ipairs(variety_list) do
		local colour = variety_colour[variety]
		if #template[variety] > 0 then
			table.sort(template[variety], function(first, second) return first[1] < second[1] end)
			for i, point_data in ipairs(template[variety]) do
				table.insert(syn_table, "\n|-")
				if i == 1 then
					table.insert(syn_table, "\n!rowspan=" .. #template[variety] .. (special_note[variety] and " colspan=2" or "") .. 
					' style="background:#' .. colour .. '"| ' .. (special_note[variety] or variety))
				end
				table.insert(syn_table, ((point_data[2] and not special_note[variety]) and ('\n|style="background:#' .. colour .. '"| ' .. 
					'[[w:' .. point_data[3] .. '|' .. point_data[2] .. ']]') or '') ..
					'\n|style="background:#' .. colour .. '"| ' ..
					table.concat(point_data[4], "、"))
			end
		end
	end

	if note and note ~= "" then
		table.insert(syn_table, '\n|-\n! style="background:#FFF7FB; padding-top:5px; padding-bottom: 5px" | ' ..
			"<small>註解</small>\n| colspan=2|<small><i>" .. note .. "</i></small>")
	end
	
	table.insert(syn_table, '\n|}')
	
	return table.concat(syn_table, "")
end

return export