模組:Zh-pron

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

漢語模塊。參見{{zh-pron}}關於使用。


local export = {}
local m_string_utils = require("Module:string utilities")

local ucodepoint = mw.ustring.codepoint
local find = m_string_utils.find
local gsub = m_string_utils.gsub
local len = m_string_utils.len
local match = m_string_utils.match
local u = mw.ustring.char
local rsplit = mw.text.split
local rgsplit = mw.text.gsplit

local langname = {
	["cdo"] = "閩東語",
	["cjy"] = "晉語",
	["cmn"] = "官話",
	["dng"] = "東干語",
	["gan"] = "贛語",
	["hak"] = "客家語",
	["hsn"] = "湘語",
	["hsn-lou"] = "湘語",
	["hsn-hya"] = "湘語",
	["mnp"] = "閩北語",
	["nan-hbl"] = "泉漳話",
	["nan-luh"] = "雷州話",
	["nan-tws"] = "潮州話",
	["wuu"] = "吳語",
	["yue"] = "粵語",
	["zhx-sic"] = "四川話",
	["zhx-tai"] = "台山話",
}

local langname_abbr = {
	["m"] = "官話",
	["m-s"] = "四川話",
	["dg"] = "東干語",
	["c"] = "粵語",
	["c-t"] = "台山話",
	["g"] = "贛語",
	["h"] = "客家語",
	["j"] = "晉語",
	["mb"] = "閩北語",
	["md"] = "閩東語",
	["mn"] = "泉漳話",
	["mn-t"] = "潮州話",
	["mn-l"] = "雷州話",
	["w"] = "吳語",
	["x"] = "湘語",
	["x-l"] = "婁底話",
	["x-h"] = "衡陽話",
}

local pos_aliases_cat = {
	["n"] = "名詞", ["noun"] = "名詞",
	["pn"] = "專有名詞", ["propn"] = "專有名詞", ["proper"] = "專有名詞", ["proper noun"] = "專有名詞",
	["pron"] = "代詞", ["pronoun"] = "代詞",
	["v"] = "動詞", ["verb"] = "動詞",
	["a"] = "形容詞", ["adj"] = "形容詞", ["adjective"] = "形容詞",
	["adv"] = "副詞", ["adverb"] = "副詞",
	["prep"] = "介詞", ["pre"] = "介詞",
	["postp"] = "後置詞", ["post"] = "後置詞",
	["con"] = "連詞", ["conj"] = "連詞", ["conjunction"] = "連詞",
	["part"] = "助詞", ["particle"] = "助詞",
	["pref"] = "前綴", ["prefix"] = "前綴",
	["suf"] = "後綴", ["suffix"] = "後綴",
	["infix"] = "間綴",
	["prov"] = "諺語", ["proverb"] = "諺語",
	["id"] = "俗語", ["idiom"] = "俗語",
	["ch"] = "成語", ["cy"] = "成語", ["chengyu"] = "成語",
	["4ci"] = "成語",
	["ph"] = "短語", ["phrase"] = "短語",
	["int"] =  "感嘆詞", ["intj"] = "感嘆詞", ["interj"] = "感嘆詞", ["interjection"] = "感嘆詞",
	["cl"] = "量詞", ["cls"] = "量詞", ["classifier"] = "量詞",
	["num"] = "數詞", ["numeral"] = "數詞",
	["abb"] = "縮寫", ["abbreviation"] = "縮寫",
	["det"] = "限定詞", ["deter"] = "限定詞", ["determiner"] = "限定詞",
	["syl"] = "音節", ["syllable"] = "音節",
}

local function make_note(text)
	if find(text, ": ") then	
		text = "\n*" .. gsub(gsub(text, "\n", "。\n*"), "([:;]) ", "%1\n**")
	elseif find(text, "; ") then
		text = "\n*" .. gsub(text, '; ', ";\n*")
	end
	text = gsub(text, '“([^”]+)”', function (a) return '“' .. gsub(a, ";\n%*+", "; ") .. '”' end)
	return "\n<div style=\"border: 1px solid green; padding: 4px; margin: 8px; background: #F7F4ED; font-size: 85%\">'''註解''':" .. text .. "。</div>"
end

local function ipa_format(text)
	local numbers = { ["1"]="¹",["2"]="²",["3"]="³",["4"]="⁴",["5"]="⁵",["0"]="⁰",["-"]="⁻",["/"]="/, /" }
	return gsub(text,"[0-5%-/]",numbers)
end

local function Consolas(text)
	return '<span style="font-family: Consolas, monospace;">' .. text .. "</span>"
end

local function format_IPA(text)
	return '<span class="IPA">' .. text .. "</span>"
end

function export.make(frame)
	local parent_args = frame:getParent().args
	local params = {
		pagename = {},
		namespace = {},
		dial = {},
		mc = {},
		oc = {},
		cat = {},
		only_cat = {type = "boolean"},
	}
	for abbr, _ in pairs(langname_abbr) do
		params[abbr] = {}
		params[abbr .. "a"] = {list = true}
		params[abbr .. "_note"] = {}
	end
	local args = require("Module:parameters").process(parent_args, params, nil, "zh-pron", "make")

	local title = mw.title.getCurrentTitle()
	local pagename = args.pagename or title.text
	local sortkey = (require("Module:Hani-sortkey").makeSortKey(pagename))
	-- Unicode pattern for single Han character, with non-NFC characters
	-- inserted using mw.ustring.char because they can't be saved in a MediaWiki
	-- page.
	-- https://unicode.org/cldr/utility/regex.jsp?a=%5Cp%7BHani%7D
	local hanzi = "^[⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〻㐀-䶿一-鿼"
		.. u(0xF900) .. "-" .. u(0xFA6D)
		.. u(0xFA70) .. "-" .. u(0xFAD9)
		.. "𠀀-𪛝𪜀-𫜴𫝀-𫠝𫠠-𬺡𬺰-𮯠" .. u(0x2F800)
		.. "-" .. u(0x2FA1D) .. "𰀀-𱍊]$"
	local is_single_hanzi = find(pagename, hanzi) ~= nil
	local namespace = args.namespace or title.nsText
	local m_rom = args["m"]
	local m_s_rom = args["m-s"]
	local dg_rom = args["dg"]
	local c_rom = args["c"]
	local c_t_rom = args["c-t"]
	local g_rom = args["g"]
	local h_rom = args["h"]
	local j_rom = args["j"]
	local mb_rom = args["mb"]
	local md_rom = args["md"]
	local mn_rom = args["mn"]
	local mn_l_rom = args["mn-l"]
	local mn_t_rom = args["mn-t"]
	local w_rom = args["w"]
	local x_rom = args["x"]
	local x_l_rom = args["x-l"]
	local x_h_rom = args["x-h"]
	local m_audio = args["ma"]
	local m_s_audio = args["m-sa"]
	local dg_audio = args["dga"]
	local c_audio = args["ca"]
	local c_t_audio = args["c-ta"]
	local g_audio = args["ga"]
	local h_audio = args["ha"]
	local j_audio = args["ja"]
	local mb_audio = args["mba"]
	local md_audio = args["mda"]
	local mn_audio = args["mna"]
	local mn_l_audio = args["mn-la"]
	local mn_t_audio = args["mn-ta"]
	local w_audio = args["wa"]
	local x_audio = args["xa"]
	local x_l_audio = args["x-la"]
	local x_h_audio = args["x-ha"]
	local dial = args["dial"]
	local mc = args["mc"]
	local oc = args["oc"]
	local only_cat = args["only_cat"]

	-- True if arguments exist for the specified variety, using a language param abbreviation.
	local function has_var(var)
		return args[var] or #args[var .. "a"] > 0
	end

	local function add_audio(text, audios, lang, show)
		-- This function has side effects
		for _, audio in ipairs(audios) do
			if audio == "y" then audio = string.format("%s-%s.ogg", lang, mw.title.getCurrentTitle().baseText) end
			table.insert(text, "\n*" .. (show and "::" or "**") .. " [[File:")
			table.insert(text, audio)
			table.insert(text, "]]")
		end
		if #audios > 0 then
			table.insert(text, "[[Category:有音頻鏈接的")
			table.insert(text, langname[lang])
			table.insert(text, "詞|" .. sortkey .. "]]")
		end
	end
	
	local function add_audio_show(text, audios, lang)
		add_audio(text, audios, lang, "show")
	end

	local text = {} --the pronunciation table
	if not only_cat then
        --The whole table consists of 4 parts
        --"textShow" contains Part 1, 3 and 4
		local textShow = {'<div class="toccolours zhpron" style="max-width:500px; min-width: 20em; font-size:100%; overflow: hidden">'}
        --"textHide" is Part 2
		local textHide = {}
		local function show(txt)
			table.insert(textShow, txt)
		end
		local function hide(txt)
			table.insert(textHide, txt)
		end
		local function hide_note(var)
			local note = args[var .. "_note"]
			if note then hide(make_note(note)) end
		end

		show('<div class="vsSwitcher" data-toggle-category="發音">\n<span class="vsToggleElement" style="float: right;"></span>')
        --Part 1 and 2, "Pronunciation" and "Pronunciation expressed in different romanizations"
		if m_rom or m_s_rom or dg_rom then
			show("\n* [[w:官話|官話]]")
		end
		if has_var("m") then
			local m_args = {}
			local m_pron = require("Module:cmn-pron")
			if m_rom then
				local str_analysis = m_pron.str_analysis
				local other_m_vars = has_var("m-s") or has_var("dg")
				show("\n" .. str_analysis(m_rom, "head", other_m_vars))

				m_args[1] = str_analysis(m_rom, "")
				for i = 2, 5 do
					m_args[i] = str_analysis(m_rom, tostring(i))
				end
				local m_args_params_prefixed_with_1 = {"n", "na", "nb", "nc", "nd"}
				for _, base in ipairs(m_args_params_prefixed_with_1) do
					for i = 1, 5 do
						local param = tostring(i) .. base
						m_args[param] = str_analysis(m_rom, param)
					end
				end
				local m_args_params = {"py", "cap", "tl", "tl2", "tl3", "a", "audio", "er", "ertl", "ertl2", "ertl3",
					"era", "eraudio"}
				for _, base in ipairs(m_args_params) do
					m_args[base] = str_analysis(m_rom, base)
					for i = 2, 5 do
						local param = tostring(i) .. base
						m_args[param] = str_analysis(m_rom, param)
					end
				end
			end
			for i, audio in ipairs(m_audio) do
				local ind = i == 1 and "" or tostring(i)
				m_args[ind .. "a"] = audio
			end
			show(m_pron.make_audio(m_args))
			hide(m_pron.make_args(m_args))
			hide_note("m")
		end
		if has_var("m-s") then
			local m_s_pron = require("Module:zhx-sic-pron")
			local m_s_processed = gsub(gsub(gsub(m_s_rom, "/", " / "), "([%d-])([%a])", "%1 %2"), "([%d-]+)", "<sup>%1</sup>")
			if m_s_rom then
				show("\n*: <small>([[w:成都話|成都話]],[[w:四川話拼音|四川話拼音]])</small>:")
				show(Consolas(m_s_processed))
			end
			add_audio_show(textShow, m_s_audio, "zhx-sic")
			if not has_var("m") then
				hide("\n* [[w:官話|官話]]")
			end
			hide("\n** <small>([[w:成都|成都]])</small>")
			hide("\n*** <small>[[w:四川話拼音|四川話拼音]]</small>:")
			hide(Consolas(m_s_processed))
			local xinwenz = m_s_pron.convert(m_s_rom, "SWZ")
			if xinwenz then
				hide("\n*** <small>[[w:四川话拉丁化新文字|四川話拉丁化新文字]]</small>:")
				hide(Consolas(m_s_pron.convert(m_s_rom, "SWZ")))
			end
			hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:zh:成都话|幫助]])</sup></small>:")
			hide(format_IPA(m_s_pron.convert(m_s_rom, "IPA")))
			hide_note("m-s")
		end
		if has_var("dg") then
			local dg_pron = require("Module:dng-pron")
			local dg_processed = dg_pron.process(dg_rom)
			if dg_rom then
				show("\n*: <small>([[w:東干語|東干語]],[[w:东干语#東干語拼寫法|西里爾字母]]和[[Wiktionary:東干語轉寫|維基詞典轉寫]])</small>:")
				show(Consolas(dg_processed))
			end
			add_audio_show(textShow, dg_audio, "dng")
			if not (has_var("m") or has_var("m-s")) then
				hide("\n* [[w:官話|官話]]")
			end
			hide("\n** <small>([[w:東干語|東干語]])</small>")
			hide("\n*** <small>[[w:东干语#東干語拼寫法|西里爾字母]],[[Wiktionary:東干語轉寫|維基詞典]]</small>:")
			hide(Consolas(dg_processed))
			hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:东干语#語音系統|幫助]])</sup></small>:")
			hide(format_IPA(dg_pron.convert(dg_rom, "IPA")) .. "\n**: <small>(注意:東干語發音目前仍處於實驗階段,可能會不準確。)</small>")
			hide_note("dg")
		end
		if has_var("c") or has_var("c-t") then
			local c_pron = require("Module:yue-pron")
			if c_rom or c_t_rom then
				show("\n* [[w:粵語|粵語]]")
				hide("\n* [[w:粵語|粵語]]")
				if c_rom then
					c_rom = c_rom:gsub("%*","-")
					local c_processed = c_rom:gsub(",([^ ])",", %1")
					if len(pagename) == 1 then
						c_processed = c_processed:gsub("([^, ]+)","[[%1]]")
						c_processed = c_processed:gsub("%[%[%[%[","[[")
						c_processed = c_processed:gsub("%]%]%]%]","]]")
						c_processed = gsub(c_processed, "%[%[([^%]]+)%]%]", function(a)
							return "[[" .. a .. "|" .. gsub(a, "([1-9-]+)", "<sup>%1</sup>") .. "]]" end)
					else
						c_processed = gsub(c_processed, "([1-9-]+)", "<sup>%1</sup>")
					end
					if not c_t_rom then
						show(" <small>(")
					else
						show("\n*: <small>([[w:廣州話|廣州]]–[[w:香港粵語|香港話]],")
					end
					show("[[w:香港語言學學會粵語拼音方案|粵拼]])</small>:" .. Consolas(c_processed))
					add_audio_show(textShow, c_audio, "yue")
					c_rom = c_rom:gsub("[%[%]]","")
					local c_hom = mw.loadData("Module:yue-pron/hom")
					local c_hom_exists = false
					for _,c_first in ipairs(c_pron.jyutping_format(c_rom)) do
						if c_hom[c_first] then
							c_hom_exists = c_first
						end
					end
					hide("\n** <small>([[w:粵語|標準粵語]],[[w:廣州話|廣州]]–[[w:香港粵語|香港話]])</small>")
					if not c_hom_exists then
						hide('<sup><small><abbr title="添加粵語同音詞"><span class="plainlinks">[')
						hide(tostring(mw.uri.fullUrl("Module:yue-pron/hom",{["action"]="edit"})))
						hide(" +]</span></abbr></small></sup>")
					end
					local c_comma = gsub(c_rom,",([^ ])",", %1")
					hide("\n*** <small>[[w:香港語言學學會粵語拼音方案|粵拼]]</small>:")
					hide(Consolas(tostring(gsub(c_comma, "([1-9-]+)", "<sup>%1</sup>"))))
					hide("\n*** <small>[[w:耶魯拼音#粤语耶鲁拼音|耶魯粵拼]]</small>:")
					hide(Consolas(c_pron.jyutping_to_yale(c_rom)))
					hide("\n*** <small>[[w:廣州話拼音方案|廣州話拼音]]</small>:")
					hide(Consolas(tostring(gsub(c_pron.jyutping_to_cantonese_pinyin(c_rom), "([1-9-]+)", "<sup>%1</sup>"))))
					hide("\n*** <small>[[w:廣東拼音方案|廣東拼音]]</small>:")
					hide(Consolas(tostring(gsub(c_pron.jyutping_to_guangdong(c_rom), "([1-9-]+)", "<sup>%1</sup>"))))
					hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:粵語音系|幫助]])</sup></small>:")
					hide(format_IPA("/" .. c_pron.jyutping_to_ipa(c_rom) .. "/"))
					for _,c_first in ipairs(c_pron.jyutping_format(c_rom)) do
						if c_hom_exists == c_first then
							local hom_textHide = {'\n*** <small>同音詞</small>:<table class="wikitable mw-collapsible mw-collapsed" style="width:15em;margin:0;'}
							local hom_text = {}
							table.insert(hom_textHide, 'position:left; text-align:center"><tr><th></th></tr><tr><td><div style="float: right; clear: right;"><sup>')
							table.insert(hom_textHide, '<span class="plainlinks">[')
							table.insert(hom_textHide, tostring(mw.uri.fullUrl("Module:yue-pron/hom",{["action"]="edit"})))
							table.insert(hom_textHide, ' edit]</span></sup></div><div style="visibility:hidden; float:left"><sup><span style="color:#FFF">edit</span></sup></div>')
							local yue = require("Module:languages").getByCode("yue")
							for _,hom in ipairs(c_hom[c_first]) do
								table.insert(hom_text, require("Module:links").full_link( { term = hom, lang = yue, tr = "-" } ))
							end
							table.insert(hom_textHide, table.concat(hom_text, "<br>"))
							table.insert(hom_textHide, "</td></tr></table>")
							hide(table.concat(hom_textHide))
							hide("[[Category:有同音詞的粵語詞]]")
						end
					end
					if not args["c_note"] and c_rom and (find(c_rom, "^[ao]") or find(c_rom, ",[ao]")) and len(pagename) == 1 then
						args["c_note"] = "在部分粵語方言,包括香港粵語中,零聲母" .. format_IPA("/∅-/") .. "常讀作鼻音化的 ''ng''-聲母" .. format_IPA("/ŋ-/")
					end
					hide_note("c")
				end
				if c_t_rom then
					local c_t_processed = c_t_rom:gsub(",([^ ])",", %1")
					c_t_processed = gsub(c_t_processed, "([1-9%*]%-?[1-9%*]?)", "<sup>%1</sup>")
					show((c_rom and "\n*:" or "") .. " <small>([[w:台山話|台山]],[[Wiktionary:漢語發音表記#台山話|維基詞典]])</small>:")
					show(Consolas(c_t_processed))
					add_audio_show(textShow, c_t_audio, "zhx-tai")
					c_t_rom = c_t_rom:gsub("[%[%]]","")
					local c_t_comma = gsub(c_t_rom,",([^ ])",", %1")
					hide("\n** <small>([[w:台山話|台山話]],[[w:台城街道|台城]])</small>")
					hide("\n*** <small>[[Wiktionary:漢語發音表記#台山話|維基詞典]]</small>:")
					hide(Consolas(tostring(gsub(c_t_comma, "([1-9%*]%-?[1-9%*]?)", "<sup>%1</sup>"))))
					hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:台山話|幫助]])</sup></small>:")
					hide(format_IPA(c_pron.hoisanva_to_ipa(c_t_rom)))
					hide_note("c-t")
				end
			end
		end
		if has_var("g") then
			local g_pron = require("Module:gan-pron")
			if g_rom then
				show("\n* [[w:贛語|贛語]] <small>([[Wiktionary:漢語發音表記#贛語|維基詞典]])</small>:")
				show(Consolas(g_pron.rom(g_rom)))
			end
			add_audio_show(textShow, g_audio, "gan")
			hide("\n* [[w:贛語|贛語]]")
			hide("\n** <small>([[w:南昌話|南昌話]])</small>")
			hide("\n*** <small>[[Wiktionary:漢語發音表記#贛語|維基詞典]]</small>:")
			hide(Consolas(g_pron.rom(g_rom)))
			hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:南昌話|幫助]])</sup></small>:")
			hide(format_IPA("/" .. g_pron.ipa(g_rom) .. "/"))
			hide_note("g")
		end
		if h_rom and (find(h_rom, "pfs=.") or find(h_rom, "gd=.")) or #h_audio > 0 then
			local h_pron = require("Module:hak-pron")
			if find(h_rom, "pfs=.") or find(h_rom, "gd=.") then
				show("\n* [[w:客語|客語]]")
				show(h_pron.rom_display(h_rom,"yes"))
			end
			add_audio_show(textShow, h_audio, "hak")
			hide("\n* [[Wiktionary:漢語發音表記#客家語|客家語]]")
			if h_rom then hide(h_pron.rom_display(h_rom,"")) end
			hide_note("h")
		end
		if has_var("j") then
			local j_pron = require("Module:cjy-pron")
			if j_rom then
				show("\n* [[w:晉語|晉語]] <small>([[Wiktionary:漢語發音表記#晉語|維基詞典]])</small>:")
				show(Consolas(j_pron.rom(j_rom)))
			end
			add_audio_show(textShow, j_audio, "cjy")
			hide("\n* [[w:晉語|晉語]]")
			hide("\n** <small>([[w:太原話|太原話]])</sup></small>")
			if j_rom then
				hide("\n*** <small>[[Wiktionary:漢語發音表記#晉語|維基詞典]]</small>:")
				hide(Consolas(j_pron.rom(j_rom)))
			end
			local no_sandhi = false
			local roms = rsplit(j_rom, "/")
			for i = 1, table.getn(roms) do
				if find(roms[i], " [^ ]+ ") then
					no_sandhi = true
					break
				end
			end
			hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] (老派" .. (no_sandhi and ",無連音" or "") .. ")</small>:")
			hide(format_IPA("/" .. j_pron.ipa(j_rom, no_sandhi and "no_sandhi" or "") .. "/"))
			hide_note("j")
		end
		if has_var("mb") then
			local mb_pron = require("Module:mnp-pron")
			if mb_rom then
				show("\n* [[w:閩北語|閩北語]] <small>([[w:建寧羅馬字|建寧羅馬字]])</small>:")
				show(Consolas(mb_pron.rom(mb_rom)))
			end
			add_audio_show(textShow, mb_audio, "mnp")
			hide("\n* [[w:閩北語|閩北語]]")
			hide("\n** <small>([[w:建甌話|建甌話]])</small>")
			if mb_rom then
				hide("\n*** <small>[[w:建寧羅馬字|建寧羅馬字]]</small>:")
				hide(Consolas(mb_pron.rom(mb_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:建甌話|幫助]])</sup></small>:")
				hide(format_IPA(mb_pron.ipa(mb_rom)))
			end
			hide_note("mb")
		end
		if has_var("md") then
			local md_pron = require("Module:cdo-pron")
			if md_rom then
				show("\n* [[w:閩東語|閩東語]] <small>([[Wiktionary:漢語發音表記#閩東語|平話字]])</small>:")
				show(Consolas(md_pron.rom(md_rom)))
			end
			add_audio_show(textShow, md_audio, "cdo")
			hide("\n* [[w:閩東語|閩東語]]")
			hide("\n** <small>([[w:福州話|福州話]])</small>")
			if md_rom then
				hide("\n*** <small>[[Wiktionary:漢語發音表記#閩東語|平話字]]</small>:")
				hide(Consolas(md_pron.rom(md_rom)))
				if not (md_rom and find(md_rom, "([^/]*)-([^/]*)-([^/]*)-([^/]*)-([^/]*)")) then
					hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:福州話|幫助]])</sup></small>:")
					hide(format_IPA("/" .. md_pron.ipa(md_rom) .. "/"))
				else
					hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:福州話|幫助]])</sup> (no sandhi)</small>:")
					hide(format_IPA("/" .. md_pron.ipa(md_rom, "no_sandhi") .. "/"))
				end
			end
			hide_note("md")
		end
		if has_var("mn") or has_var("mn-l") or has_var("mn-t") then
			local mn_pron, mn_l_pron
			if has_var("mn") or has_var("mn-t") then
				mn_pron = require("Module:nan-pron")
			end
			if has_var("mn-l") then
				mn_l_pron = require("Module:nan-pron-Leizhou")
			end
			
			if mn_rom or mn_t_rom or mn_l_rom then
				show("\n* [[w:閩南語|閩南語]]")
				if mn_rom then
					show(((mn_l_rom or mn_t_rom) and "\n*:" or "") .. " <small>([[w:泉漳話|泉漳話]],[[w:白話字|白話字]])</small>:")
					show(Consolas(mn_pron.poj_display(mn_pron.poj_check_invalid(mn_rom))))
					add_audio_show(textShow, mn_audio, "nan-hbl")
				end
				if mn_t_rom then
					show(((mn_rom or mn_l_rom) and "\n*:" or "") .. " <small>([[w:潮州話|潮州話]],[[w:潮州話拼音方案|潮州話拼音]])</small>:")
					show(Consolas(mn_pron.pengim_display(mn_t_rom)))
					add_audio_show(textShow, mn_t_audio, "nan-tws")
				end
				if mn_l_rom then
					show(((mn_rom or mn_t_rom) and "\n*:" or "") .. " <small>([[w:雷州話|雷州話]],雷州話拼音)</small>:")
					show(Consolas(mn_l_pron.rom(mn_l_rom)))
					add_audio_show(textShow, mn_l_audio, "nan-luh")
				end
			end
			hide("\n* [[w:閩南語|閩南語]]")
			if has_var("mn") then
				hide(mn_pron.generate_all(mn_rom))
				hide_note("mn")
			end
			if has_var("mn-t") then
				hide("\n** <small>([[w:潮州話|潮州話]])</small>")
				hide("\n*** <small>[[w:潮州話拼音方案|潮州話拼音]]</small>:")
				hide(Consolas(mn_pron.pengim_display(mn_t_rom)))
				hide("\n*** <small>模仿[[w:白話字|白話字]]</small>:")
				hide(Consolas(mn_pron.pengim_to_pojlike_conv(mn_t_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:潮州話#語音|幫助]])</sup></small>:")
				hide(format_IPA(mn_pron.pengim_to_ipa_conv(mn_t_rom)))
				hide_note("mn-t")
			end
			if has_var("mn-l") then
				hide("\n** <small>([[w:雷州話|雷州話]])</small>")
				hide("\n*** <small>雷州話拼音</small>:")
				hide(Consolas(mn_l_pron.rom(mn_l_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]]</small>:")
				hide(format_IPA(mn_l_pron.ipa(mn_l_rom)))
				hide_note("mn-l")
			end
		end
		if has_var("w") then
			local w_pron = require("Module:wuu-pron")
			if w_rom then
				show("\n* [[w:吳語|吳語]] <small>([[Wiktionary:漢語發音表記#吳語|Wugniu]])</small>")
				show(w_pron.make(w_rom,true))
			end
			add_audio_show(textShow, w_audio, "wuu")
			hide("\n* [[w:吳語|吳語]]")
			hide(w_pron.make(w_rom,false))
			-- add_audio(textHide, w_audio, "wuu")
			hide_note("w")
		end
		if has_var("x") or has_var("x-l") or has_var("x-h") then
			if has_var("x") then
				x_pron = require("Module:hsn-pron")
			end
			if has_var("x-l") then
				x_l_pron = require("Module:hsn-pron-Loudi")
			end
			if has_var("x-h") then
				x_h_pron = require("Module:hsn-pron-Hengyang")
			end
			if x_rom or x_l_rom or x_h_rom then
				show("\n* [[w:湘語|湘語]]")
				if x_rom then
					show(((x_l_rom or x_h_rom) and "\n*:" or "") .. " <small>([[w:長沙話|長沙話]],[[Wiktionary:漢語發音表記#湘語|維基詞典]])</small>:")
					show(Consolas(x_pron.rom(x_rom)))
					add_audio_show(textShow, x_audio, "hsn")
				end
				if x_l_rom then
					show(((x_rom or x_h_rom) and "\n*:" or "") .. " <small>([[w:婁底話|婁底話]],[[Wiktionary:漢語發音表記#婁底話|維基詞典]])</small>:")
					show(Consolas(x_l_pron.rom(x_l_rom)))
					add_audio_show(textShow, x_l_audio, "hsn-lou")
				end
				if x_h_rom then
					show(((x_rom or x_l_rom) and "\n*:" or "") .. " <small>([[w:衡陽話|衡陽話]],[[Wiktionary:漢語發音表記#衡陽話|維基詞典]])</small>:")
					show(Consolas(x_h_pron.rom(x_h_rom)))
					add_audio_show(textShow, x_h_audio, "hsn-hya")
				end
			end
			hide("\n* [[w:湘語|湘語]]")
			if x_rom then
				local x_diff = x_pron.stylediff(x_rom)
				hide("\n** <small>([[w:長沙話|長沙話]])</small>")
				hide("\n*** <small>[[Wiktionary:漢語發音表記#湘語|維基詞典]]</small>:")
				hide(Consolas(x_pron.rom(x_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:長沙話|幫助]])</sup>")
				hide(x_diff and " (老派)" or "")
				hide("</small>:")
				hide(format_IPA("/" .. x_pron.ipa(x_rom) .. "/"))
				if x_diff then
					hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:長沙話|幫助]])</sup> (新派)</small>:")
					hide(format_IPA("/" .. x_pron.ipa(x_rom, "new") .. "/"))
				end
				hide_note("x")
			end
			if x_l_rom then
				hide("\n** <small>([[w:zh:婁底話|婁底話]])</small>")
				hide("\n*** <small>[[Wiktionary:婁底話|維基詞典]]</small>:")
				hide(Consolas(x_l_pron.rom(x_l_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:zh:娄底话|幫助]])</sup>")
				hide("</small>:")
				hide(format_IPA("/" .. x_l_pron.ipa(x_l_rom) .. "/"))
				hide_note("x-l")
			end
			if x_h_rom then
				hide("\n** <small>([[w:衡陽話|衡陽話]])</small>")
				hide("\n*** <small>[[Wiktionary:婁底話|維基詞典]]</small>:")
				hide(Consolas(x_h_pron.rom(x_h_rom)))
				hide("\n*** <small>[[Wiktionary:國際音標|國際音標]] <sup>([[w:衡陽話|幫助]])</sup>")
				hide("</small>:")
				hide(format_IPA("/" .. x_h_pron.ipa(x_h_rom) .. "/"))
				hide_note("x-h")
			end
		end
        -- combine textShow and textHide into text
		text = {
            table.concat(textShow),
            '\n<div class="vsHide">\n----\n',
            table.concat(textHide),
            "</div></div>"
        }

        --Part 3 "Dialectal data"
		if dial ~= "n" and is_single_hanzi then
			local success, m_dial = pcall(mw.loadData, "Module:zh/data/dial-pron/" .. pagename)
			if success then
				local dialPron = {}
				local temporary = {}
				if dial and find(dial, "^[0-9\\,]+$") then
					for element in rgsplit(dial, ",") do
						table.insert(dialPron, m_dial[tonumber(element)])
					end
				else
					for _, element in ipairs(m_dial) do
						table.insert(dialPron, element)
					end
				end
				for _, set in ipairs(dialPron) do
					for _, object in ipairs(set[2]) do
						table.insert(temporary, object)
					end
				end
				local rand = gsub("-" .. table.concat(temporary), "[^A-Za-z0-9]", ucodepoint("%1"))
				table.insert(text,
                    '\n----\n<div class="vsSwitcher" data-toggle-category="pronunciations" style="background-color:#FAFFFA">\n* ' ..
                    '<span style="color:#3366bb">各地讀音</span>' ..
					'<span class="vsToggleElement" style="float:right; padding:0 0; font-size:90%"></span>\n' ..
                    '<div class="vsHide">'
                )

				table.insert(text, '\n{| class="wikitable" ' ..
					'id="' .. rand .. '" style="width:100%; margin:0; ' ..
					'text-align:center; border-collapse: collapse; border-style: hidden;"')

				local locStart = "\n|-\n!"
				local readingStart = table.concat({'\n!style="background:#E8ECFA; width:9em"|',
				'<div style="float: right; clear: right; font-size:60%"><span class="plainlinks">[', tostring(mw.uri.fullUrl("Module:zh/data/dial-pron/" .. pagename, {["action"]="edit"})), ' edit]</span></div><span lang="zh" class="Hani">'})
				local locEnd = '<span class="IPA">'
				local headclr = 'style="background:#E8ECFA"|'
				local mclr = 'style="background:#FAF5F0"|'
				local jclr = 'style="background:#FAF5F0"|'
				local wclr = 'style="background:#F4F0FA"|'
				local huclr = 'style="background:#FAF9F0"|'
				local xclr = 'style="background:#F0F2FA"|'
				local gclr = 'style="background:#F0FAF3"|'
				local haclr = 'style="background:#FAF0F6"|'
				local cclr = 'style="background:#F0F5FA"|'
				local minclr = 'style="background:#F7FAF0"|'
				local clrList = {
					mclr, mclr, mclr, mclr, mclr, mclr, mclr, mclr, mclr, mclr,
					mclr, mclr, mclr, mclr, mclr, mclr, mclr, jclr, jclr, jclr,
					wclr, wclr, wclr, wclr, huclr, huclr, xclr, xclr, gclr, haclr,
					haclr, cclr, cclr, cclr, minclr, minclr, minclr, minclr, minclr
				}
				local locList = {
					table.concat({headclr, "語言\n!", headclr, "地區"}),
					table.concat({"rowspan=17 ", mclr, "[[w:標準漢語|標準漢語]]\n|", mclr, "[[w:北京話|北京]]\n|", mclr}),
					table.concat({mclr, "[[w:哈爾濱話|哈爾濱]]\n|", mclr}),
					table.concat({mclr, "[[w:天津話|天津]]\n|", mclr}),
					table.concat({mclr, "[[w:濟南話|濟南]]\n|", mclr}),
					table.concat({mclr, "[[w:青島話|青島]]\n|", mclr}),
					table.concat({mclr, "[[w:鄭州話|鄭州]]\n|", mclr}),
					table.concat({mclr, "[[w:關中話|西安]]\n|", mclr}),
					table.concat({mclr, "[[w:西寧|西寧]]\n|", mclr}),
					table.concat({mclr, "[[w:銀川|銀川]]\n|", mclr}),
					table.concat({mclr, "[[w:蘭州話|蘭州]]\n|", mclr}),
					table.concat({mclr, "[[w:烏魯木齊話|烏魯木齊]]\n|", mclr}),
					table.concat({mclr, "[[w:武漢話|武漢]]\n|", mclr}),
					table.concat({mclr, "[[w:成都話|成都]]\n|", mclr}),
					table.concat({mclr, "[[w:貴陽話|貴陽]]\n|", mclr}),
					table.concat({mclr, "[[w:昆明話|昆明]]\n|", mclr}),
					table.concat({mclr, "[[w:南京話|南京]]\n|", mclr}),
					table.concat({mclr, "[[w:合肥話|合肥]]\n|", mclr}),
					table.concat({"rowspan=3 ", jclr, "晉語\n!", jclr, "[[w:太原話|太原]]\n|", jclr}),
					table.concat({jclr, "[[w:平遙|平遙]]\n|", jclr}),
					table.concat({jclr, "[[w:張呼片|呼和浩特]]\n|", jclr}),
					table.concat({"rowspan=4 ", wclr, "吳語\n!", wclr, "[[w:上海|上海]]\n|", wclr}),
					table.concat({wclr, "[[w:蘇州話|蘇州]]\n|", wclr}),
					table.concat({wclr, "[[w:杭州話|杭州]]\n|" , wclr}),
					table.concat({wclr, "[[w:溫州話|溫州]]\n|", wclr}),
					table.concat({"rowspan=2 ", huclr, "徽語\n!", huclr, "[[w:歙縣|歙縣]]\n|", huclr}),
					table.concat({huclr, "[[w:屯溪話|屯溪]]\n|", huclr}),
					table.concat({"rowspan=2 ", xclr, "湘語\n!", xclr, "[[w:長沙話|長沙]]\n|", xclr}),
					table.concat({xclr, "[[w:湘潭話|湘潭]]\n|", xclr}),
					table.concat({gclr, "贛語\n!", gclr, "[[w:南昌話|南昌]]\n|", gclr}),
					table.concat({"rowspan=2 ", haclr, "客家語\n!", haclr, "[[w:梅縣話|梅縣]]\n|", haclr}),
					table.concat({haclr, "[[w:桃源縣|桃源]]\n|", haclr}),
					table.concat({"rowspan=3 ", cclr, "粵語\n!", cclr, "[[w:廣州話|廣州]]\n|", cclr}),
					table.concat({cclr, "[[w:南寧|南寧]]\n|", cclr}),
					table.concat({cclr, "[[w:香港粵語|香港]]\n|", cclr}),
					table.concat({"rowspan=5 ", minclr, "閩語\n!", minclr, "[[w:廈門話|廈門]] (泉漳)\n|", minclr}),
					table.concat({minclr, "[[w:福州話|福州]] (閩東)\n|", minclr}),
					table.concat({minclr, "[[w:建甌話|建甌]] (閩北)\n|", minclr}),
					table.concat({minclr, "[[w:汕頭話|汕頭]] (潮州)\n|", minclr}),
					table.concat({minclr, "[[w:海口話|海口]] (海南)\n|", minclr})}

				local function ipa_correct(ipa, location)
					if location == 22 then return (gsub(ipa, "13", "23")) else return ipa end
				end

				local function fmtDial(text, location)
					local fmttedPron = {}
					if text == "" then return "" end
					for pronunciation in rgsplit(text, "|") do
						local ipa = match(pronunciation, "^[^%(%)一-龯㐀-䶵~,]+")
						ipa = gsub(ipa, "([ptk])([0-5])", "%1̚%2")
						local environ = match(pronunciation, "[%(%)一-龯㐀-䶵~,]*$") or false
						table.insert(fmttedPron, "<span class=\"IPA\"><small>/" ..
							tostring(ipa_format(ipa_correct(ipa, location))) .. "/</small></span>" .. (environ
							and " <span class=\"Hani\" lang=\"zh\"><small>"..environ.."</small></span>" or nil))
					end
					return table.concat(fmttedPron, "<br>")
				end

				for locationNo = 1, 40 do
					for readingNo = 1, #dialPron do
						if readingNo == 1 then
							table.insert(text, locStart)
							table.insert(text, locList[locationNo])
						end
						if locationNo == 1 then
							local situation = dialPron[readingNo][1]
							table.insert(text, readingStart)
							table.insert(text, pagename)
							table.insert(text, (pagename ~= situation and " (" ..
								gsub(situation, pagename, "<b>" .. pagename .. "</b>") .. ")" or ""))
							table.insert(text, "</span>")
						else
							table.insert(text, (readingNo == 1 and "" or "\n|" .. clrList[locationNo-1]))
							table.insert(text, locEnd)
							table.insert(text, fmtDial(dialPron[readingNo][2][locationNo-1], locationNo))
							table.insert(text, "</span>")
						end
					end
				end
				table.insert(text, "\n|}</div></div>")
			end
		end

        --Part 4 "Middle Chinese & Old Chinese"
		local mc_preview, oc_preview
		local m_ltc_pron, m_och_pron

		-- !!!
		-- The following function modifies the tables generated by mod:ltc-pron and mod:och-pron, shifting them
		-- from using "mw-collapsible" to using "vsSwitcher", because the former
		-- can not collapse on the mobile site and makes Chinese entries a mess.
		-- It is supposed to be a temporary solution.
		-- !!!
        local function shiftCustomtoggle2Vsswitcher(s)
            local result
            result = (s:gsub(
                '\n%* <div title="expand" class="mw%-customtoggle[^>]+">',
                '\n<div class="vsSwitcher" data-toggle-category="發音">\n* '
            ):gsub(
                '<span style="float:right; border:1px solid #ccc; border%-radius:1px; padding:0 0; font%-size:90%%">▼</span>(.-)</div>\n{| class="wikitable',
                '<span class="vsToggleElement" style="float:right; padding:0 0; font-size:90%%"></span>%1\n<div class="vsHide">\n{| class="wikitable'
            ):gsub(
                '{| class="wikitable mw%-collapsible mw%-collapsed" id="[^"]+"',
                '{| class="wikitable"'
            ):gsub(
                "\n|}$",
                "\n|}</div></div>"
            ))
            return result
        end

		if mc then
			m_ltc_pron = require("Module:ltc-pron")
			mc_preview = m_ltc_pron.retrieve_pron(pagename, false, mc, true)
			if not mc_preview then
				require("Module:debug/track")("zh-pron/Middle Chinese data not found")
				mc = nil
			end
		end
		if oc then
			m_och_pron = require("Module:och-pron")
			oc_preview = m_och_pron.generate_show(pagename, oc)
			if not oc_preview then
				require("Module:debug/track")("zh-pron/Old Chinese data not found")
				oc = nil
			end
		end
		if mc or oc then
			table.insert(text, '\n----\n<div style="background-color:#f7fbff">')
			if mc then
				table.insert(text, shiftCustomtoggle2Vsswitcher(m_ltc_pron.ipa(mc, mc_preview)))
			end
			if oc then
				table.insert(text, shiftCustomtoggle2Vsswitcher(m_och_pron.ipa(oc, oc_preview)))
			end
			table.insert(text, "</div>")
		end
		table.insert(text, "</div>")
		if namespace == ""  then
			if mc then
				table.insert(text, "[[Category:中古漢語詞元|" .. sortkey .. "]]")
                if is_single_hanzi then
                    if mc_preview:find("k̚$") then
                        table.insert(text, "[[Category:中古漢語-k韻尾|" .. sortkey .. "]]")
                    elseif mc_preview:find("t̚$") then
                        table.insert(text, "[[Category:中古漢語-t韻尾|" .. sortkey .. "]]")
                    elseif mc_preview:find("p̚$") then
                        table.insert(text, "[[Category:中古漢語-p韻尾|" .. sortkey .. "]]")
                    end
                end
			end
			if oc then table.insert(text, "[[Category:上古漢語詞元|" .. sortkey .. "]]") end
			if not args["cat"] then
				table.insert(text, "[[Category:zh-pron usage missing POS]]")
			end
		end
	end

	local conv_text = {} --categories
	if namespace == "" then
		local catText = args["cat"] or ""
		local cat_start = "[[Category:"
		local cat_end = "]]"
		if w_rom then
			w_rom = gsub(w_rom, "%d", "")
		end
		
		local function add_cat(cat_table, name, cat)
			table.insert(cat_table, cat_start .. name .. cat .. "|" .. sortkey .. cat_end)
		end

		local cats = rsplit(catText, ",", true)
		if pos_aliases_cat[cats[1]] == "成語" then
			table.insert(cats, 2, "俗語")
		end
		table.insert(cats, 1, "詞元")
		for i = 1, #cats do
			local cat = cats[i]
			if cat == "" then break end
			cat = gsub(cat, "^ +", "")
			if find(cat, ":") then
				local cat_split = rsplit(cat, ":", true)
				local lang_name = langname_abbr[cat_split[1]]
				local category = pos_aliases_cat[cat_split[2]] or cat
				add_cat(conv_text, "漢語", category)
				table.insert(conv_text, cat_start .. lang_name .. category .. "|" .. sortkey .. cat_end)
			else
				cat = pos_aliases_cat[cat] or cat
				add_cat(conv_text, "漢語", cat)
				if m_rom then add_cat(conv_text, "官話", cat) end
				if has_var("m-s") then add_cat(conv_text, "四川話", cat) end
				if has_var("dg") then add_cat(conv_text, "東干語", cat) end
				if c_rom then add_cat(conv_text, "粵語", cat) end
				if c_t_rom then add_cat(conv_text, "台山話", cat) end
				if has_var("g") then add_cat(conv_text, "贛語", cat) end
				if h_rom and (find(h_rom, "pfs=.") or find(h_rom, "gd=.")) or #h_audio > 0 then
					add_cat(conv_text, "客家語", cat)
				end
				if has_var("j") then add_cat(conv_text, "晉語", cat) end
				if has_var("mb") then add_cat(conv_text, "閩北語", cat) end
				if has_var("md") then add_cat(conv_text, "閩東語", cat) end
				if has_var("mn") then add_cat(conv_text, "泉漳話", cat) end
				if has_var("mn-t") then add_cat(conv_text, "潮州話", cat) end
				if has_var("mn-l") then add_cat(conv_text, "雷州話", cat) end
				if has_var("w") then add_cat(conv_text, "吳語", cat) end
				if has_var("x") then add_cat(conv_text, "湘語", cat) end
				if has_var("x-l") then add_cat(conv_text, "湘語", cat) end
				if has_var("x-h") then add_cat(conv_text, "湘語", cat) end
			end
		end
		table.insert(conv_text, cat_start .. "有國際音標的漢語詞|" .. sortkey .. cat_end)
		if is_single_hanzi then
			table.insert(conv_text, cat_start .. "漢字|" .. sortkey .. cat_end)
		end
	end

	local output = table.concat(text) .. table.concat(conv_text)
	if namespace ~= "" then
		output = gsub(output, "%[%[Category:[^%]]+%]%]", "")
	end
	return output
end

return export