Kontent qismiga oʻtish

Modul:zh-usex

Vikilug‘atdan olingan

Bu modul uchun Modul:zh-usex/doc nomli hujjat sahifasini yaratishingiz mumkin

local m_zh = require("Module:zh")
local m_zh_data = require("Module:zh/data")
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local sub = mw.ustring.sub

local export = {}

local variety_list = {
	["MSC"] = { "[[w:Standard Chinese|MSC]]", "cmn", "Pinyin" },
	
	["CL"] = { "[[w:Classical Chinese|Classical Chinese]]", "cmn", "Pinyin" },
	
	["C"] = { "[[w:Cantonese|Cantonese]]", "yue", "Jyutping" },
		["C-GZ"] = { "[[w:Cantonese|Guangzhou Cantonese]]", "yue", "Jyutping" },
		["C-LIT"] = { "[[w:Cantonese|Literary Cantonese]]", "yue", "Jyutping" },
		
	["MN"] = { "[[w:Min Nan|Min Nan]]", "nan", "Pe̍h-ōe-jī" },
		["TW"] = { "[[w:Taiwanese|Taiwanese]]", "nan", "Pe̍h-ōe-jī" },
		
	["W"] = { "[[w:Wu Chinese|Wu]]", "wuu", "IPA" },
		["SH"] = { "[[w:Shanghainese|Shanghainese]]", "wuu", "IPA" },
}

local punctuation = {
	[","] = ",",   ["。"] = ".",   ["、"] = ",", 
	["?"] = "?",   ["!"] = "!",
	
	["“"] = "‘",    ["”"] = "’",
	["‘"] = "‘",    ["’"] = "’", 
	["《"] = "‘",   ["》"] = "’", 
	["『"] = "‘",   ["』"] = "’", 
	["「"] = "‘",   ["」"] = "’", 
	
	["("] = "(",   [")"] = ")",
	[";"] = ";",   [":"] = ":",
	["|"] = "|",    ["—"] = "-", 
	["·"] = " ",    ["…"] = "...",  
}

local ref_list = {
	['Analects']   =  { "CL",  "The ''[[w:Analects|Analects]] of Confucius'', circa 475 – 221 BCE" },
	['Hanfeizi']   =  { "CL",  "''[[w:Han Feizi (book)|Han Feizi]]'', circa 2nd century BCE" },
	['Hanshu']     =  { "CL",  "The ''[[w:Book of Han|Book of Han]]'', circa 1st century CE" },
	['Liji']       =  { "CL",  "The ''[[w:Book of Rites|Book of Rites]]'', circa 4th – 2nd century BCE" },
	['Mengzi']     =  { "CL",  "''[[w:Mencius (book)|Mengzi]] (Mencius)'', circa 4th century BCE" }, 
	['Mozi']       =  { "CL",  "''[[w:Mozi|Mozi]] ([[w:zh:墨子 (书)|book]])'', circa 4th century BCE" }, 
	['Shangshu']   =  { "CL",  "The ''[[w:Book of Documents|Book of Documents]]'', circa 4th – 3rd century BCE" },
	['Shiji']      =  { "CL",  "The ''[[w:Records of the Grand Historian|Records of the Grand Historian]]'', by [[w:Sima Qian|Sima Qian]], circa 91 BCE" },
	['Shijing']    =  { "CL",  "The ''[[w:Classic of Poetry|Classic of Poetry]]'', circa 11th – 7th centuries BCE" },
	['Shujing']    =  { "CL",  "The ''[[w:Book of Documents|Book of Documents]]'', circa 7th – 4th centuries BCE" },
	['Shuowen']    =  { "CL",  "''[[w:Shuowen Jiezi|Shuowen Jiezi]]'', circa 2nd century CE" },
	['Houhanshu']  =  { "CL",  "The ''[[w:Book of the Later Han|Book of the Later Han]]'', circa 5th century CE" },
	['Yijing']     =  { "CL",  "''[[w:I Ching|I Ching]]'', 3rd – 2nd millennia BCE" },
	['Zhuangzi']   =  { "CL",  "''[[w:Zhuangzi (book)|Zhuangzi]]'', circa 3rd – 2nd centuries BCE" },
}

local pron_correction = {
	["cmn"] = {
		["吧"] = "ba",
		["的"] = "de", ["都"] = "dōu", 
		["個"] = "ge", ["給"] = "gěi", 
		["還"] = "hái", 
		["將"] = "jiāng",
		["了"] = "le", 
		["沒"] = "méi", 
		["漂"] = "piào", 
		["什"] = "shén", 
		["為"] = "wèi",
		["要"] = "yào", 
	},
	["yue"] = {
		["若"] = "joek6",
		["來"] = "loi4",
	},
	["nan"] = {},
	["wuu"] = {},
}

function export.show(frame)
	local args = frame:getParent().args
	local example = args[1] or error("Example unspecified.")
	local translation = args[2] or error("Lacking translation.")
	local reference = args["ref"] or args["r"] or false
	local manual_tr = args["tr"] or false
	local display = args["type"] or args["display_type"] or "plain"
	local phonetic = ""
	local pagename = mw.title.getCurrentTitle().text
	
	local variety = args[3] or (ref_list[reference] and ref_list[reference][1] or false) or "MSC"
	variety_code = variety_list[variety][2]
	
	local link = args["link"] or args["l"] or "yes"
	link = match(link, "n") == nil and not (not match(example, " ") and match(example, "[,。?!]"))
	
	-- automatically boldify pagetitle if it occurs only once in the example
	_, count = gsub(example, pagename, "")
	if count == 1 and not match(example, "%'%'%'") then
		example = gsub(example, pagename, "'''" .. pagename .. "'''")
		example = gsub(example, "%'%'%'%'%'%'", "'''")
	end
	
	-- tidying up the example, making it ready for transcription
	example = gsub(example, "([?!,。、“”…;:‘’|()「」—《》·])", " %1 ")
	example = gsub(example, "^ *", "")
	example = gsub(example, " *$", "")
	example = gsub(example, " +", " ")
	example = gsub(example, "%'%'%'([^%']+)%'%'%'", "<b>%1</b>")
	
	local ruby_start, ruby_mid, ruby_end = "<big><ruby><span class=\"Hani\">", "</span><rp>&nbsp;(</rp><rt><big>", "</big></rt><rp>)</rp></ruby></big>"
	local ruby_words = {}
	local trad_words, simp_words, tr_words = {}, {}, {}
	
	simp_exist = m_zh.ts_determ(example) == "trad" or match(example, "%[")
	
	for word in mw.text.gsplit(example, " ", true) do
		local trad_word, simp_word, tr_word, ruby_word = word, false, false, ""
	
		-- various tricks for linking and display in trad. and simp.
		trad_word = gsub(trad_word, "(.)%[(.)%]", "%1")
		trad_word = gsub(trad_word, "%{[^%}]+%}", "")
		trad_word = gsub(trad_word, "[%^%.]", "")
		trad_word = gsub(trad_word, "\\", "|")
		
		if simp_exist then
			simp_word = gsub(m_zh.ts(word), ".%[(.)%]", "%1")
			simp_word = gsub(simp_word, "%{[^%}]+%}", "")
			simp_word = gsub(simp_word, "[%^%.]", "")
			simp_word = gsub(simp_word, "\\", "|")
		end
		
		-- produce links
		if match(trad_word, "|") or (link and not match(trad_word, "[>@]") and not punctuation[word]) then
			trad_word = "[[" .. trad_word .. "]]"
			trad_word = gsub(trad_word, "%-", "]][[")
			if simp_exist then
				simp_word = "[[" .. simp_word .. "]]"
				simp_word = gsub(simp_word, "%-", "]][[")
			end
		end
		
		trad_word = gsub(trad_word, "@", "")
		simp_word = simp_exist and gsub(simp_word, "@", "")
		
		-- same tricks applied to transcription
		if not manual_tr and variety_code ~= "wuu" then
			if punctuation[word] then
				tr_word = punctuation[word]
			else
				real_word = true
				tr_word = gsub(word, "[@%-]", "")
				tr_word = gsub(tr_word, "%.", " ")
				tr_word = gsub(tr_word, ".+\\", "")
				tr_word = gsub(tr_word, ".%[(.)%]", "%1")
				tr_word = gsub(tr_word, ".</b>%{([^%}]+)%}", "%1</b>")
				tr_word = gsub(tr_word, ".%{([^%}]+)%}", "%1")
				tr_word = gsub(tr_word, ".", pron_correction[variety_code])
				if variety_code == "yue" then
					tr_word = gsub(tr_word, ".", m_zh_data.jyutping)
					tr_word = gsub(tr_word, "([0-9])([^ ])", "%1 %2")
				else
					tr_word = m_zh.py(tr_word)
				end
			end
		end

		if display == "ruby" then
			ruby_word = ruby_start .. trad_word .. (simp_exist and "<br>" .. simp_word or "") .. ruby_mid .. (real_word and tr_word or "") .. ruby_end
			table.insert(ruby_words, ruby_word)
		else
			table.insert(trad_words, trad_word)
			table.insert(simp_words, simp_word or nil)
			table.insert(tr_words, tr_word or nil)
		end
	end
	
	local tag_start = " <span style=\"color:darkgreen\"><small>[</span><span style=\"color:darkgreen\"><small>"
	local tag_end = "</small></span><span style=\"color:darkgreen\">]</small></span>"
	
	if display == "ruby" then
		tag = " <ruby><rb><big>" ..
				tag_start .. variety_list[variety][1] .. 
					(simp_exist
						and ", ''[[Traditional Chinese|trad.]]''↑ + ''[[Simplified Chinese|simp.]]''↓"
						or ", ''[[Traditional Chinese|trad.]]'' and ''[[Simplified Chinese|simp.]]''") .. tag_end .. 
				
				tag_start .. "''rom.'': " .. variety_list[variety][3] .. tag_end ..
					"</rb></big></ruby>"
			
		return table.concat(ruby_words, "") .. tag .. "<dl><dd><i>" .. translation .. "</i></dd></dl>"
		
	else
		trad_text = table.concat(trad_words)
		simp_text = simp_exist and table.concat(simp_words) or false
		phonetic = manual_tr or (#tr_words > 0 and table.concat(tr_words, " ") or false)

		-- overall transcription formatting
		if phonetic then
			phonetic = gsub(phonetic, " ([,%.?!;:’)])", "%1")
				phonetic = gsub(phonetic, "([‘(]) ", "%1")
			phonetic = gsub(phonetic, "[‘’]", "\"")
			phonetic = gsub(phonetic, "%'([^%'])", "%1")
			if variety_code == "yue" then
				phonetic = gsub(phonetic, "([1-9-])", "<sup>%1</sup>")
			end

			-- capitalisation
			if match(example, "[。?!]") then
				phonetic = "^" .. gsub(phonetic, "([%.?!]) ", "%1 ^")
			end
			phonetic = gsub(phonetic, "%^<b>", "<b>^")
			phonetic = gsub(phonetic, "%^+.", mw.ustring.upper)
			phonetic = gsub(phonetic, "%^", "")
				
			if variety_code == "wuu" then
				local wuu_pron = require("Module:wuu-pron") 
				phonetic = "<span class=\"IPA\">[" .. wuu_pron.ipa_conv(phonetic) .. "]</span>"
			else
				phonetic = "<i>" .. phonetic .. "</i>"
			end
		end
	end

	-- indentation, font and identity tags
	if match(example, "[,。?!]") then
		trad_text = "<dd><span lang=\"zh\" class=\"Hani\">" .. trad_text .. "</span>"
		if phonetic then
			phonetic = "<dl><dd>" .. phonetic
			translation = "<dd>" .. translation .. "</dd></dl>"
			tr_tag = tag_start .. variety_list[variety][3] .. tag_end .. "</dd>"
		else
			translation = "<dl><i>" .. translation .. "</i></dl>"
		end
		
		trad_tag = tag_start .. variety_list[variety][1] .. ", <i>[[w:Traditional Chinese|trad.]]" .. 
			(simp_exist and "" or " and [[w:Simplified Chinese|simp.]]") .. "</i>" .. tag_end .. "</dd>"
		
		if simp_exist then
			simp_text = "<dd><span lang=\"zh\" class=\"Hani\">" .. simp_text .. "</span>"
			simp_tag = tag_start .. variety_list[variety][1] .. ", <i>[[w:Simplified Chinese|simp.]]</i>" .. tag_end .. "</dd>"
		end
		
		if reference then
			reference = "<dd><i><small>From:</i> " .. (ref_list[reference] and ref_list[reference][2] or reference) .. "</small></dd>"
		end
		
		return trad_text .. trad_tag .. (simp_text or "") .. (simp_tag or "") .. (reference or "") .. (phonetic and phonetic .. tr_tag or "") .. translation
		
	else
		trad_text = "<span lang=\"zh\" class=\"Hani\">" .. trad_text .. "</span>"
		divider = "&nbsp; ―&nbsp; "
		
		if variety_code ~= "cmn" then
			ts_tag = tag_start .. variety_list[variety][1] .. tag_end
			tr_tag = tag_start .. variety_list[variety][3] .. tag_end
		end
		
		if not phonetic then
			translation = "<i>" .. translation .. "</i>"
		end
		
		if simp_exist then
			simp_text = " / <span lang=\"zh\" class=\"Hani\">" .. simp_text .. "</span>"
		end
		
		return trad_text .. (simp_text or "") .. (ts_tag or "") .. divider .. (phonetic and phonetic .. (tr_tag or "") .. divider or "") .. translation
	end
end

return export