Module:cmn-pron/sandbox

Hello, you have come here looking for the meaning of the word Module:cmn-pron/sandbox. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:cmn-pron/sandbox, but we will also tell you about its etymology, its characteristics and you will know how to say Module:cmn-pron/sandbox in singular and plural. Everything you need to know about the word Module:cmn-pron/sandbox you have here. The definition of the word Module:cmn-pron/sandbox will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:cmn-pron/sandbox, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.


local export = {}

local find = mw.ustring.find
local gsub = mw.ustring.gsub
local len = mw.ustring.len
local match = mw.ustring.match
local sub = mw.ustring.sub
local split = mw.text.split
local gsplit = mw.text.gsplit

local m_data = mw.loadData("Module:cmn-pron/data")
local m_zh = require("Module:zh")
local _m_zh_data = nil
local hom_data = nil

local function hom_data_part(pinyin)
	local x = mw.ustring.toNFD(pinyin):sub(1,1)
	if "a" <= x and x <= "g" then
		return 1
	elseif "h" <= x and x <= "m" then
		return 2
	elseif "n" <= x and x <= "w" then
		return 3
	end
	return 4
end

local function get_hom_data(py)
	if hom_data == nil then
		hom_data = mw.loadData("Module:zh/data/cmn-hom/" .. hom_data_part(py))
	end
	return hom_data
end

-- if not empty
local function ine(var)
	if var == "" then
		return nil
	else
		return var
	end
end

local breve, hacek, circumflex = mw.ustring.char(0x306), mw.ustring.char(0x30C), mw.ustring.char(0x302)
local decompose = mw.ustring.toNFD
local function breve_error(text)
	if type(text) ~= "string" then
		return
	end
	text = decompose(text)
	if text:find(breve) then
		error('The pinyin text "' .. text .. '" contains a breve. Replace it with "' .. text:gsub(breve, hacek) .. '".', 2)
	end
	if find(text, '') or find(text, '') then
		error('The pinyin text "' .. text .. '" contains an uncommon shorthand. Replace it with "' .. gsub(text, '', { = 'zh',  = 'ch',  = 'sh',  = 'ng',  = 'Zh',  = 'Ch',  = 'Sh',  = 'h'}) .. '".', 2)
	end
end

local function m_zh_data()
	if _m_zh_data == nil then _m_zh_data = mw.loadData("Module:zh/data/cmn-tag") end;
	return _m_zh_data;
end

function export.py_detone(f)
	local text = type(f) == 'table' and f.args or f
	return mw.ustring.toNFC(gsub(mw.ustring.toNFD(text), m_data.tones, ''))
end

function export.py_transf(f)
	local text = type(f) == 'table' and f.args or f
	return export.py_detone(text) .. export.tone_determ(text)
end

function export.tone_determ(f)
	local text = type(f) == 'table' and f.args or f
	text = mw.ustring.toNFD(text)
	return m_data.py_tone or '5'
end

function export.py_transform(text, detone, not_spaced)
	if type(text) == 'table' then text, detone, not_spaced = text.args, text.args, text.args end
	if find(text, '​') then
		error("Pinyin contains the hidden character: ​ (U+200B). Please remove that character from the text.")
	end
	detone = ine(detone)
	not_spaced = ine(not_spaced)
	text = gsub(gsub(mw.ustring.toNFD(text), mw.ustring.toNFD('ê'), 'ê'), mw.ustring.toNFD('ü'), 'ü')
	if find(mw.ustring.lower(text), '' .. m_data.tones .. '?' .. m_data.tones .. '') and not not_spaced then
		error(("Missing apostrophe before null-initial syllable - should be \"%s\" instead."):format(gsub(text, '(' .. m_data.tones .. '?)(' .. m_data.tones .. ')', "%1'%2"))) end
	original_text = text
	text = gsub(text,'()()(' .. m_data.tones .. ')', '%1%3%2')
	text = gsub(text,'()(' .. m_data.tones .. ')()', '%1%3%2')
	if text ~= original_text then
		error("Incorrect diacritic placement in Pinyin - should be \"".. text .. "\" instead.") end
	text = mw.ustring.lower(text)
	if not mw.ustring.find(text, m_data.tones) and text:find('') then
		return gsub(text, '(%d)(%l)', '%1 %2')
	end
	text = gsub(text, "#", " #")
	if find(text, '') then
		text = gsub(text, '()$', { = ' yī',  = ' bù'})
		text = gsub(text, '()', ' %1 ')
		text = gsub(text, '()', ' %1 ')
		text = gsub(text, ' +', ' ')
		text = gsub(text, '^ ', '')
		text = gsub(text, ' $', '')
		text = gsub(text, '%. %. %.', '...')
	end
	text = gsub(text, "", ' ')
	text = gsub(text, '(' .. m_data.tones .. '?n?g?r?)(h?)', '%1 %2')
	text = gsub(text, ' ()$', '%1')
	text = gsub(text, ' () ', '%1 ')
	if detone then
		text = mw.ustring.gsub(text, m_data.tones, m_data.py_tone)
		text = gsub(text, '()(*)', '%2%1')
		text = mw.ustring.gsub(text, '() ', '%15 ')
		text = mw.ustring.gsub(text, '()$', '%15')
	end
	if not_spaced then
		text = gsub(text, ' ', '')
	end
	return mw.ustring.toNFC(text)
end

function export.py_ipa(text)
	local ipa_initial = m_data.py_ipa_initial
	local ipa_initial_tl = m_data.py_ipa_initial_tl
	local ipa_final = m_data.py_ipa_final
	local ipa_null = m_data.py_ipa_null
	local ipa_tl_ts = m_data.py_ipa_tl_ts
	local ipa_third_t_ts = m_data.py_ipa_third_t_ts
	local ipa_t_values = m_data.py_ipa_t_values

	local tone = {}
	local tone_cat = {}
	text = gsub(export.py_transform(text), '', '')
	text = gsub(text, ' +', ' ')
	local p = split(text, " ")
	
	for i = 1, #p do
		tone_cat = export.tone_determ(p)
		p = gsub(p, '.?', m_data.py_detone)

		if p == '一' then
			tone_cat = (export.tone_determ(p) == '4' or p == 'ge') and '1-2' or '1-4'
			p = 'yi'
		elseif p == '不' then
			tone_cat = (export.tone_determ(p) == '4') and '4-2' or '4'
			p = 'bu'
		end
	end
	
	tone_cat.length = #tone_cat
	
	local function get_initial_and_final_IPA(a, b, c)
		return a ..
		(ipa_initial or error(("Unrecognised initial: \"%s\""):format(b))) .. 
		(ipa_final or error(("Unrecognised final: \"%s\". Are you missing an apostrophe before the null-initial syllable, or using an invalid Pinyin final?"):format(c)))
	end

	for i, item in ipairs(p) do
		if ipa_null then item = 'ˀ' .. item end
		item = gsub(item, '()u', '%1ü')

		if item == 'ng' then
			item = ipa_final
		else
			item = gsub(item, '^(#?ˀ?)(?h?)(.+)$', 
				get_initial_and_final_IPA)
		end
		
		item = gsub(item, '(ʈ?͡?ʰ?)ir', '%1ʐ̩ɻ')
		item = gsub(item, '(ʈ?͡?ʰ?)i', '%1ʐ̩')
		item = gsub(item, '(t?͡?sʰ?)ir', '%1z̩ɻ')
		item = gsub(item, '(t?͡?sʰ?)i', '%1z̩')
		item = gsub(item, 'ʐʐ̩', 'ʐ̩')
		item = gsub(item, 'ˀwɔ', 'ˀɔ')
		
		local curr_tone_cat, next_tone_cat = tone_cat, tone_cat

		if curr_tone_cat == '5' then
			item = gsub(item, '^()()', function(a, b) return ipa_initial_tl .. b end)
			item = gsub(item, '^(͡)()', function(a, b) return ipa_initial_tl .. b end)
			item = gsub(item, 'ɤ$', 'ə')
			tone = ipa_tl_ts] or ""

		elseif curr_tone_cat == '3' then
			if p and match(p, "#") then next_tone_cat = "#3" end
			if i == tone_cat.length then
				if i == 1 then tone = '²¹⁴' else tone = '²¹⁴⁻²¹⁽⁴⁾' end
			else
				tone = ipa_third_t_ts
			end

		elseif curr_tone_cat == '4' and next_tone_cat == '4' then
			tone = '⁵¹⁻⁵³'

		elseif curr_tone_cat == '4' and next_tone_cat == '1-4' then
			tone = '⁵¹⁻⁵³'

		elseif curr_tone_cat == '1-4' and next_tone_cat == '4' then
			tone = '⁵⁵⁻⁵³'

		else
			tone = ipa_t_values
		end
		p = item .. tone
		p = gsub(p, "#", "")
	end
	return table.concat(p, " ")
end

function export.py_number_to_mark(text)
	local priority = { "a", "o", "e", "ê", "i", "u", "ü" }
	local toneMark = {  = "̄",  = "́",  = "̌",  = "̀",  = "",  = "",  = "" }
	
	local mark = toneMark?$")]
	local toneChars = ""
	text = gsub(text, "?$", "")
	
	for _, letter in ipairs(priority) do
		text = gsub(text, letter, letter .. mark)
		if find(text, toneChars) then break end
	end
	return mw.ustring.toNFC(gsub(text, "i("..toneChars..")u", "iu%1"))
end

function export.py_zhuyin(text)
	local zhuyin_initial = m_data.py_zhuyin_initial
	local zhuyin_final = m_data.py_zhuyin_final
	local zhuyin_er = m_data.py_zhuyin_er
	local zhuyin_tone = m_data.py_zhuyin_tone

	if type(text) == 'table' then
		if text.args == '' then
			text = mw.title.getCurrentTitle().text
		else
			text = text.args
		end
	end
	breve_error(text)
	text = gsub(text, "#", "")
	text = export.py_transform(text, true)
	text = gsub(text, '()u', '%1ü')
	text = gsub(text, '(h?)i', '%1')
	text = gsub(text, '()i', '%1')
	
	local function add_tone(syllable, tone)
		if tone == '5' then
			return zhuyin_tone .. syllable
		else
			return syllable .. zhuyin_tone
		end
	end
	local function fun1(a, b) return add_tone((({ = 'ㄫ',  = 'ㄏㄇ'}) or a), b) end
	local function fun2(number) return add_tone('ㄏㄫ', number) end
	local function fun3(number) return add_tone('ㄦ', number) end
	local function fun4(a, b, c, d) return add_tone(zhuyin_initial .. zhuyin_final, d) .. zhuyin_er end
	
	local word = split(text, " ", true)
	for i, syllable in ipairs(word) do
		if find(syllable, '^$') then
			syllable = gsub(syllable, '^()()$', fun1)
		elseif find(syllable, '^hng$') then
			syllable = gsub(syllable, '^hng()$', fun2)
		elseif find(syllable, '^er$') then
			syllable = gsub(syllable, '^er()$', fun3)
		else
			syllable = gsub(syllable, '^(?h?)(????)(r?)()$', 
				fun4)
		end
		if find(syllable, '') then
			error(("Zhuyin conversion unsuccessful: \"%s\". Are you using a valid Pinyin syllable? Is the text using a breve letter instead of a caron one?"):format(syllable))
		end
		word = syllable
	end
	text = gsub(table.concat(word, " "), ' , ', ', ')
	return text
end

function export.zhuyin_py(text)
	local zhuyin_py_initial = m_data.zhuyin_py_initial
	local zhuyin_py_final = m_data.zhuyin_py_final
	local zhuyin_py_tone = m_data.zhuyin_py_tone

	if type(text) == "table" then text = text.args end
 	local word = split(text, " ", true)
 	
 	local function process_syllable(syllable)
 		syllable = gsub(syllable, '^()(?)$', '%1ㄧ%2')
 		return gsub(syllable, '(?)(?)(?)(ㄦ?)', function(initial, final, tone, erhua)
			initial = zhuyin_py_initial
			final = zhuyin_py_final

			if erhua ~= '' then
				final = final .. 'r'
			end
			if initial == '' then
				final = final:gsub('^()(n?g?)$', function(a, b) return a:gsub('', { = 'yi',  = 'wu'}) .. b end)
				final = final:gsub('^(w?u)()$', 'ue%2')
				final = final:gsub('^iu$', 'iou')
				final = final:gsub('^()', { = 'y',  = 'w'})
				final = final:gsub('^ong', 'weng')
				final = gsub(final, '^ü', 'yu')
			end
			if initial:find('') then
				final = gsub(final, '^ü', 'u')
			end
			local tone = zhuyin_py_tone
			
			if final:find('') then
				final = final:gsub("()", "%1" .. tone)
			elseif final:find('i') then
				final = final:gsub("(i)", "%1" .. tone)
			elseif final:find('') then
				final = final:gsub("()", "%1" .. tone)
			else
				final = gsub(final, "^(?)(.)", "%1" .. "%2" .. tone)
			end

 			return initial .. final
 		end)
 	end
 	
 	for i, syllable in ipairs(word) do
 		word = process_syllable(syllable)
 	end
  return mw.ustring.toNFC(table.concat(word, " "))
end

function export.py_wg(text)
	local py_wg_initial = m_data.py_wg_initial
	local py_wg_final = m_data.py_wg_final
	local py_wg_syl = m_data.py_wg_syl

	if type(text) == 'table' then text = text.args end
	
	local function process_syllable(initial, final, tone)
		for text, replace in pairs(py_wg_final) do
			final = gsub(final, text, replace)
		end
		if (initial == "zh" or initial == "ch") and final == "i" then
			final = "ih"
		end
		local untoned = (py_wg_initial or initial) .. final
		for text, replace in pairs(py_wg_syl) do
			untoned = gsub(untoned, text, replace)
		end
		return untoned .. '<sup>' .. tone .. '</sup>'
	end
	
	local function process_word(word)
		local uppercase
		if mw.ustring.find(word, '^%u') then uppercase = true else uppercase = false end
		local text = gsub(export.py_transform(word, true), '', '')
		text = gsub(gsub(text, ' +', ' '), '', { = 'yi1',  = 'bu4'})
		text = gsub(text, '()u', '%1ü')
		local p = split(text, " ", true)
		for i = 1, #p do
			p = gsub(p, '^(?h?)(.+)()$', process_syllable)
		end
		text = table.concat(p, "-")
		if uppercase then
			text = gsub(text, '^%l', mw.ustring.upper)
		end
		return text
	end

	local words = split(text, " ")
	for i, word in ipairs(words) do
		words = process_word(word)
	end
	return table.concat(words, " ")
end

local function temp_bg(text, bg)
	if bg == 'y' then
		return '<' .. text .. '>'
	end
	return text
end
	
local function make_bg(text, bg)
	if bg == 'y' then
		return '<span style="background-color:#F5DEB3">' .. text .. '</span>'
	else
		return text
	end
end

function export.py_gwoyeu(text, original_text)
	local initials = m_data.py_gwoyeu_initials
	local finals = m_data.py_gwoyeu_finals

	if type(text) == 'table' then text = text.args end
	if text:find('^%s') or text:find('%s$') then error('invalid spacing') end
	local words = split(text, " ")
	local count = 0
	for i, word in ipairs(words) do
		local uppercase
		if mw.ustring.find(word, '^%u') then uppercase = true else uppercase = false end
		word = export.py_transform(word, true, true)
		word = gsub(word, "()", "%1 ")
		word = gsub(word, " $", "")
		word = gsub(word, '()', ' %1 ')
		word = gsub(word, ' +', ' ')
		word = gsub(word, ' $', '')
		word = gsub(word, '^ ', '')
		local syllables = split(word, " ")
		for j, syllable in ipairs(syllables) do
			count = count + 1
			if not find(syllable, '^+$') then
				local current = sub(mw.title.getCurrentTitle().text, count, count)
				if find(current, '^$') then
					local exceptions = { = 'i',  = 'chi',  = 'ba',  = 'bu'}
					syllables = exceptions
				else
					local initial, final, tone = '', '', ''
					syllable = gsub(syllable, '()u', '%1ü')
					syllable = gsub(syllable, '^(h?)i(r?)$', '%1ɨ%2')
					if mw.ustring.find(syllable, '(?h?)(??g?r?)()') then
						syllable = gsub(syllable, '(?h?)(??g?r?)()', function(a, b, c)
							initial = initials or error('Unrecognised initial:' .. a); final = finals or error('Unrecognised final:' .. b); tone = c
							return (initial .. final .. tone) end)
					elseif not find(mw.title.getCurrentTitle().text, "") then
						error('Unrecognised syllable:' .. syllable)
					end
					local original = initial..final..tone
					if initial:find('^$') then
						final = initial .. final
						initial = ''
					end
					if initial .. final == "e'l" then
						final = "el"
					end
					final = gsub(final, '()%1', '%1')
					local len = len(initial) + len(final)
					local detone = initial..final
					local replace = detone
					local fullstop = false
					if tone == 5 or tone == '5' then
						fullstop = true
						if original_text then
							tone = split(export.py_transform(original_text, true), ' '):match('')
						elseif initial:find('^$') then
							tone = 2
						else tone = 1 end
						if tone == 5 or tone == '5' then
							tone = export.tone_determ(m_zh.py(current))
						end
					end
					if tone == 1 or tone == '1' then
						if initial:find('^$') then
							replace = initial .. 'h' .. sub(detone, 2, len)
						else
							replace = detone
						end
					elseif tone == 2 or tone == '2' then
						if not initial:find('^$') then
							if final:find('^') then
								replace = gsub(detone, '', { = 'y',  = 'w'}, 1)
								replace = gsub(replace, 'l?$', { = 'yi',  = 'wu',  = 'wul',})
							else
								replace = gsub(detone, '(+)', '%1r')
							end
						else
							replace = detone
						end
					elseif tone == 3 or tone == '3' then
						if final:find("^iu?e'l$") then
							detone = detone:gsub("'", '')
						end
						detone = detone:gsub('^', { = 'yi',  = 'wu'})
						if final:find('') and (not final:find('^') or initial..final=="ie") and (not final:find('^') or initial..final=="uo") then
							replace = detone:gsub('', { = 'e',  = 'o'}, 1)
						elseif final:find('') then
							replace = detone:gsub('(?)(\168?)', '%1%2%2', 1) --\195\168 is the utf8 for è
						else
							error('Unrecognised final:'..final)
						end
					elseif tone == 4 or tone == '4' then
						if final:find("^iu?e'l$") then
							detone = detone:gsub("'", '')
						end
						detone = detone:gsub('^', { = 'yi',  = 'wu'})
						if detone:find('l?$') then
							replace = detone:gsub("l?$", { = 'y',  = 'w',  = 'wl'})
						elseif detone:find('$') then
							replace = detone:gsub("l?$", { = 'nn',  = 'q',  = 'll',  = 'ql',  = 'hl'})
						else
							replace = detone .. 'h'
						end
						replace = replace:gsub('yi()', 'y%1')
						replace = replace:gsub('wu()', 'w%1')
					end
					if fullstop then replace = '.' .. replace end
					syllables = syllable:gsub(original, replace)
				end
			end
		end
		words = table.concat(syllables, "")
		if uppercase then
			words = gsub(words, '^%l', mw.ustring.upper)
		end
	end
	return table.concat(words, " ")
end

-- Converts Hanyu Pinyin into Tongyong Pinyin.
function export.py_tongyong(text)
	if type(text) == 'table' then text = text.args end
	
	local ty_tone = {
		 = "",  = "\204\129",  = "\204\140",  = "\204\128",  = "\204\138"
	}
	
	local function num_to_mark(syllable, tone)
		tone = ty_tone
		if tone ~= "" then
			if syllable:find('') then
				syllable = syllable:gsub("()", "%1" .. tone)
			elseif syllable:find('o') then
				syllable = syllable:gsub("(o)", "%1" .. tone)
			elseif syllable:find('') then
				syllable = syllable:gsub("()", "%1" .. tone)
			end
		end
		return syllable
	end
	
	local words = {}
	for word in gsplit(text, " ") do
		local cap = mw.ustring.find(word, "^%u")
		word = export.py_transform(word, true)
		local syllables = {}
		for syllable in gsplit(word, " ") do
			syllable = syllable:gsub("(h?)i", "%1ih")
			syllable = syllable:gsub("ü", "yu")
			syllable = syllable:gsub("()u", "%1yu")
			syllable = syllable:gsub("iu", "iou")
			syllable = syllable:gsub("ui", "uei")
			syllable = syllable:gsub("()eng", "%1ong")
			syllable = syllable:gsub("wen", "wun")
			syllable = syllable:gsub("iong", "yong")
			syllable = syllable:gsub("^zh", "jh")
			syllable = syllable:gsub("^q", "c")
			syllable = syllable:gsub("^x", "s")
			syllable = #syllables ~= 0 and syllable:gsub("^()", "'%1") or syllable
			syllable = syllable:gsub("^(+)()$", num_to_mark)
			
			table.insert(syllables, syllable)
		end
		word = table.concat(syllables, "")
		word = cap and word:gsub("^.", string.upper) or word
		table.insert(words, word)
	end
	
	return mw.ustring.toNFC(table.concat(words, " "))
end

function export.py_format(text, cap, bg, simple, nolink)
	if cap == false then cap = nil end
	if bg == false then bg = 'n' else bg = 'y' end
	if simple == false then simple = nil end
	if nolink == false then nolink = nil end
	text = mw.ustring.toNFD(text)
	local phon = text
	local title = mw.title.getCurrentTitle().text
	local cat = ''
	local spaced = mw.ustring.toNFD(export.py_transform(text))
	local space_count
	spaced, space_count = gsub(spaced, ' ', '@')
	local consec_third_count
	
	for _ = 1, space_count do
		spaced, consec_third_count = gsub(spaced, "(+)̌(*)@(+̌)", function(a, b, c)
			return temp_bg(a..'́'..b, bg)..'@'..c end, 1)
		if consec_third_count > 0 then
			phon = gsub(spaced, '@', '')
		end
	end
	text = gsub(text, "#", "")
	phon = gsub(phon, "#", "")
	
	if title:find('一') and not text:find('一') and not simple then
		cat = cat .. ']'
	end
	
	if text:find('') and not simple then
		text = gsub(text, '$', { = 'yī',  = 'bù'})
		phon = gsub(phon, '$', { = 'yī',  = 'bù'})
		
		if find(text, '一') then
			if find(text, '一*') then
				cat = cat .. ']'
				phon = gsub(phon, '一(*)', function(a) return temp_bg('yì', bg) .. a end)
				text = gsub(text, '一(*)', 'yī%1')
			end
			if find(text, '一*̀') or find(text, '一ge$') or find(text, '一ge') then
				cat = cat .. ']'
				phon = gsub(phon, '一(*̀)', function(a) return temp_bg('yí', bg) .. a end)
				phon = gsub(phon, '一ge', temp_bg('yí', bg) .. 'ge')
				text = gsub(text, '一(*)', 'yī%1')
			end
		end
		if find(text, '不 ??h?*̀') then
			cat = cat .. ']'
			phon = gsub(phon, '不( ??h?*̀)', function(a) return temp_bg('bú', bg) .. a end)
		end
	end
	text = gsub(text, '', { = 'yī',  = 'bù'})
	text = gsub(text, '兒', function() return make_bg('r', bg) end) -- character is deleted
	phon = gsub(phon, '<(+)>', '<span style="background-color:#F5DEB3">%1</span>')
	
	if not simple then
		if cap then
			text = gsub(text, '^%l', string.upper)
			phon = gsub(phon, '^%l', string.upper)
		end
		if not nolink then
			text = ']'
		end
		if '', { = 'yī',  = 'bù'}) .. ']]' ~= text then
			phon = gsub(phon, '', { = 'yī',  = 'bù'})
			text = text .. ' '
		end
		if mw.title.getCurrentTitle().nsText ~= 'Template' and not nolink then
			text = text .. cat
		end
	end
	return mw.ustring.toNFC(text)
end

function export.make_tl(original_text, tl_pos, bg, cap)
	if bg == false then bg = 'n' else bg = 'y' end
	local _, countoriginal = gsub(original_text, " ", " ")
	local spaced = export.py_transform(original_text)
	if sub(spaced, -1, -1) == ' ' then spaced = sub(spaced, 1, -2) end
	local _, count = gsub(spaced, " ", " ")
	local index = {}
	local start, finish
	local pos = 1
	for i = 1, count, 1 do
		if i ~= 1 then pos = (index + 1) end
		index = mw.ustring.find(spaced, ' ', pos)
	end
	if tl_pos == 2 then
		start = index - count + countoriginal + 2
		finish = index - count + countoriginal
	elseif tl_pos == 3 then
		start = index - count + countoriginal + 3
		finish = index - count + countoriginal + 1
	else
		start = count == 0 and 1 or (index - count + countoriginal + 1)
		finish = -1
	end
	local text = (sub(original_text, 1, start-1) .. make_bg(gsub(sub(original_text, start, finish), '.', m_data.py_detone), bg))
	if finish ~= -1 then text = (text .. sub(original_text, finish+1, -1)) end
	if cap == true then text = gsub(text, '^%l', string.upper) end
	return text
end

function export.tag(first, second, third, fourth, fifth)
	local text = "(''"
	local tag = {}
	local tagg = first or "Standard Chinese"
	tag = (second ~= '') and second or "Standard Chinese"
	tag = (third ~= '') and third or nil
	tag = (fourth ~= '') and fourth or nil
	tag = (fifth ~= '') and fifth or nil
	text = text .. ((tagg == '') and table.concat(tag, ", ") or tagg) .. "'')"
	text = gsub(text, 'Standard Chinese', "]")
	text = gsub(text, 'Mainland', "]")
	text = gsub(text, 'Taiwan', "]")
	text = gsub(text, 'Beijing', "]")
	text = gsub(text, 'erhua', "]")
	text = gsub(text, 'Min Nan', "]")
	text = gsub(text, 'shangkouzi', "'']''")
	return text
end

function export.straitdiff(text, pron_ind, tag)
	local conv_text = text
	for i = 1, #text do
		if m_zh_data().MT then conv_text = 'y' end
	end
	if tag == 'tag' then
		conv_text = (conv_text == 'y') and m_zh_data().MT_tag')] or ''
	elseif pron_ind == 1 or pron_ind == 2 or pron_ind == 3 or pron_ind == 4 or pron_ind == 5 then
		local reading = {}
		for a, b in pairs(m_zh_data().MT) do
			reading = b
			if reading then reading = gsub(reading, "^()", "'%1") end
		end
		conv_text = gsub(text, '.', reading)
		text = gsub(text, "^'", "")
		text = gsub(text, " '", " ")
		if conv_text == text and tag == 'exist' then return nil end
	end
	conv_text = gsub(conv_text, "^'", "")
	return conv_text
end

function export.str_analysis(text, conv_type, other_m_vars)
	if type(text) == 'table' then text, conv_type = text.args, (text.args or "") end
	local MT = m_zh_data().MT
	
	text = gsub(text, '=', '—')
	text = gsub(text, ',', '隔')
	text = gsub(text, '隔 ', ', ')
	if conv_type == 'head' or conv_type == 'link' then
		if find(text, '隔cap—') then
			text = gsub(text, '', { = 'Yī',  = 'Bù'})
		end
		text = gsub(text, '', { = 'yī',  = 'bù'})
	end
	local comp = split(text, '隔', true)
	local reading = {}
	local alternative_reading = {}
	local zhuyin = {}
	--[[
	-- not used
	local param = {
		'1n', '1na', '1nb', '1nc', '1nd', 'py', 'cap', 'tl', 'tl2', 'tl3', 'a', 'audio', 'er', 'ertl', 'ertl2', 'ertl3', 'era', 'eraudio',
		'2n', '2na', '2nb', '2nc', '2nd', '2py', '2cap', '2tl', '2tl2', '2tl3', '2a', '2audio', '2er', '2ertl', '2ertl2', '2ertl3', '2era', '2eraudio',
		'3n', '3na', '3nb', '3nc', '3nd', '3py', '3cap', '3tl', '3tl2', '3tl3', '3a', '3audio', '3er', '3ertl', '3ertl2', '3ertl3', '3era', '3eraudio',
		'4n', '4na', '4nb', '4nc', '4nd', '4py', '4cap', '4tl', '4tl2', '4tl3', '4a', '4audio', '4er', '4ertl', '4ertl2', '4ertl3', '4era', '4eraudio',
		'5n', '5na', '5nb', '5nc', '5nd', '5py', '5cap', '5tl', '5tl2', '5tl3', '5a', '5audio', '5er', '5ertl', '5ertl2', '5ertl3', '5era', '5eraudio'
	}
	--]]
	
	if conv_type == '' then
		return comp
	elseif conv_type == 'head' or conv_type == 'link' then
		for i, item in ipairs(comp) do
			if not find(item, '—') then
				if find(item, '') then
					local M, T, t = {}, {}, {}
					for a, b in pairs(MT) do
						M = b; T = b; t = b;
						M = gsub(M, "^()", "'%1")
						T = gsub(T, "^()", "'%1")
						if t then t = gsub(t, "^()", "'%1") end
					end
					local mandarin = gsub(item, '.', M)
					local taiwan = gsub(item, '.', T)
					mandarin = gsub(mandarin, "^'", "")
					mandarin = gsub(mandarin, " '", " ")
					if conv_type == 'link' then return mandarin end
					taiwan = gsub(taiwan, "^'", "")
					taiwan = gsub(taiwan, " '", " ")
					local tt = gsub(item, '.', t)
					if find(text, 'cap—') then
						mandarin = gsub(mandarin, '^%l', mw.ustring.upper)
						taiwan = gsub(taiwan, '^%l', mw.ustring.upper)
						tt = gsub(tt, '^%l', mw.ustring.upper)
					end
					if tt == item then
						zhuyin = export.py_zhuyin(mandarin, true) .. ', ' .. export.py_zhuyin(taiwan, true)
						reading = mandarin .. ']], [[' .. taiwan
					else
						tt = gsub(tt, "^'", "")
						tt = gsub(tt, " '", " ")
						zhuyin = export.py_zhuyin(mandarin, true) .. ', ' .. export.py_zhuyin(taiwan, true) .. ', ' .. export.py_zhuyin(tt, true)
						reading = mandarin .. ']], ], [[' .. tt
					end
				else
					if conv_type == 'link' then return item end
					zhuyin = export.py_zhuyin(item, true)
					reading = item
					if len(mw.title.getCurrentTitle().text) == 1 and #mw.text.split(export.py_transform(item), " ") == 1 then
						alternative_reading = ") .. "|" .. mw.ustring.gsub(export.py_transf(reading), '()', '<sup>%1</sup>') .. "]]"
					end
				end
				if reading ~= '' then reading = ' .. ']]' end
				reading = gsub(reading, "#", "")
			end
			comp = item
			if conv_type == 'link' then return comp end
		end
		local id = m_zh.ts_determ(mw.title.getCurrentTitle().text)
		local accel
		if id == 'trad' then
			accel = '<span class="form-of pinyin-t-form-of transliteration-' .. m_zh.ts(mw.title.getCurrentTitle().text)
		elseif id == 'simp' then
			accel = '<span class="form-of pinyin-s-form-of transliteration-' .. m_zh.st(mw.title.getCurrentTitle().text)
		elseif id == 'both' then
			accel = '<span class="form-of pinyin-ts-form-of'
		end
		accel = accel .. '" lang="cmn" style="font-family: Consolas, monospace;">'
		local result = other_m_vars and "*: <small>('']'')</small>\n*::" or "*:"
		result = result .. "<small>('']'')</small>: " .. accel .. gsub(table.concat(reading, ", "), ", ,", ",")
		if alternative_reading then
			result = result .. " (" .. table.concat(alternative_reading, ", ") .. ")"
		end
		result = result .. (other_m_vars and "</span>\n*::" or "</span>\n*:")
		result = result .. "<small>('']'')</small>: " .. '<span lang="zh-Bopo" class="Bopo">' .. gsub(table.concat(zhuyin, ", "), ", ,", ",") .. "</span>"
		return result

	elseif conv_type == '2' or conv_type == '3' or conv_type == '4' or conv_type == '5' then
		if not find(text, '隔') or (comp and find(comp, '—')) then
			return ''
		else
			return comp
		end
	else
		for i = 1, #comp, 1 do
			local target = '^' .. conv_type .. '—'
			if find(comp, target) then
				text = gsub(comp, target, '')
				return text
			end
		end
		text = ''
	end
	return text
end

function export.homophones(pinyin)
	local text = ''
	if mw.title.getCurrentTitle().nsText == '' then
		local args = get_hom_data(pinyin).list
		text = '<div style="visibility:hidden; float:left"><sup><span style="color:#FFF">edit</span></sup></div>'
		for i, term in ipairs(args) do
			if i > 1 then
				text = text .. "<br>"
			end
			if mw.title.new(term).exists and term ~= mw.title.getCurrentTitle().text then
				local forms = { term }
				local content = mw.title.new(term):getContent()
				local template = match(content, "{{zh%-forms*}}")
				if template then
					local simp = match(template, "|s=()+")
					if simp then
						table.insert(forms, simp)
					end
					for tradVar in mw.ustring.gmatch(template, "|t=()+") do
						table.insert(forms, tradVar)
					end
					for simpVar in mw.ustring.gmatch(template, "|s=()+") do
						table.insert(forms, simpVar)
					end
					term = table.concat(forms, "/")
				end
			end
			text = text .. mw.getCurrentFrame():expandTemplate{ title = "Template:zh-l", args = { term, tr = "-" } }
		end
		text = text .. ']'
	end
	return text
end

local function erhua(word, erhua_pos, pagename)
	local title = split(pagename, '')
	local linked_title = ''
	local syllables = split(export.py_transform(word), ' ')
	local count = #syllables
	erhua_pos = find(erhua_pos, '') and split(erhua_pos, ';') or { count }
	for _, pos in ipairs(erhua_pos) do
		pos = tonumber(pos)
		title = title .. '兒'
		syllables = syllables .. 'r'
	end
	local title = table.concat(title)
	if mw.title.new(title).exists then
		linked_title = ' (' .. require("Module:zh/link").link(nil, nil, {title, tr='-'}) .. ')'
	end
	for i, syllable in pairs(syllables) do
		if i ~= 1 and mw.ustring.toNFD(syllable):find('^') then
			syllables = "'" .. syllable
		end
	end
	word = table.concat(syllables, '')
	return (export.tag('', '', 'erhua-ed') .. linked_title), word
end

export.erhua = erhua

function export.make(frame)
	local args = frame:getParent().args
	return export.make_args(args)
end

function export.make_args(args)
	local pagename = mw.title.getCurrentTitle().text
	local text = {}
	local reading = {args or '', args or '', args or '', args or '', args or ''}
	args = ine(args)
	if reading ~= '' then
		local title = export.tag((args or ''), (args or ''), (args or export.straitdiff(args, 1, 'tag')), (args or ''), (args or ''))
		local pinyin = export.straitdiff(reading, 1, '')
		table.insert(text, export.make_table(title, pinyin, (args or ''), (args or ''), (args or ''), (args or ''), (args or ''), (args or args or '')))
		
		if args and args ~= '' then
			title, pinyin = erhua(pinyin, args, pagename)
			table.insert(text, export.make_table(title, pinyin, '', (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
		end
	end
	
	if reading ~= '' or export.straitdiff(reading, 2, 'exist') then
		if args and args ~= '' then tagb = args else tagb = export.straitdiff(args, 2, 'tag') end
		title = export.tag((args or ''), (args or ''), tagb, (args or ''), (args or ''))
		pinyin = (reading ~= '') and reading or export.straitdiff(reading, 2, '')
		table.insert(text, export.make_table(title, pinyin, (args or ''), (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
		table.insert(text, ', 1, '') or args) .. ']]')
		
		if args and args ~= '' then
			title, pinyin = erhua(pinyin, args, pagename)
			table.insert(text, export.make_table(title, pinyin, '', (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
		end
		
		if reading ~= '' or export.straitdiff(reading, 3, 'exist') then
			if args and args ~= '' then tagb = args else tagb = export.straitdiff(args, 3, 'tag') end
			title = export.tag((args or ''), (args or ''), tagb, (args or ''), (args or ''))
			if reading ~= '' then pinyin = reading else pinyin = export.straitdiff(reading, 3, '') end
			table.insert(text, export.make_table(title, pinyin, (args or ''), (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
			
			if args and args ~= '' then
				title, pinyin = erhua(pinyin, args, pagename)
				table.insert(text, export.make_table(title, pinyin, '', (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
			end
			
			if reading ~= '' or export.straitdiff(reading, 4, 'exist') then
				if args and args ~= '' then tagb = args else tagb = export.straitdiff(args, 4, 'tag') end
				title = export.tag((args or ''), (args or ''), tagb, (args or ''), (args or ''))
				if reading ~= '' then pinyin = reading else pinyin = export.straitdiff(reading, 4, '') end
				table.insert(text, export.make_table(title, pinyin, (args or ''), (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
			
				if args and args ~= '' then
					title, pinyin = erhua(pinyin, args, pagename)
					table.insert(text, export.make_table(title, pinyin, '', (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
				end
				if reading ~= '' or export.straitdiff(reading, 5, 'exist') then
					if args and args ~= '' then tagb = args else tagb = export.straitdiff(args, 5, 'tag') end
					title = export.tag((args or ''), (args or ''), tagb, (args or ''), (args or ''))
					if reading ~= '' then pinyin = reading else pinyin = export.straitdiff(reading, 5, '') end
					table.insert(text, export.make_table(title, pinyin, (args or ''), (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
				
					if args and args ~= '' then
						title, pinyin = erhua(pinyin, args, pagename)
						table.insert(text, export.make_table(title, pinyin, '', (args or ''), (args or ''), (args or ''), (args or ''), (args or args or ''), true))
					end
				end
			end
		end
	end
	if (args or '') .. (args or '') .. (args or '') .. (args or '') .. (args or '') .. (args or '') ~= '' then
		table.insert(text, ', 1, '') .. ']]')
	end
	return table.concat(text)
end

function export.make_audio(args)
	local text, reading, pinyin = {}, {}, ""
	local audio = {
		args or args or '',
		args or args or '',
		args or args or '',
		args or args or '',
		args or args or '',
	}
	for i=1,5 do
		reading = args or ''
		if i == 1 then
			pinyin = export.straitdiff(reading, 1, '')
		else
			pinyin = (reading ~= '') and reading or export.straitdiff(reading, i, '')
		end
		pinyin = export.py_format(pinyin, false, false, true)
		add_audio(text, audio, pinyin)
	end
	return table.concat(text)
end

function add_audio(text, audio, pinyin)
	if audio and audio ~= "" then
		if audio == "y" then audio = string.format('zh-%s.ogg', pinyin) end
		table.insert(text, '\n*:: [[File:')
		table.insert(text, audio)
		table.insert(text, ']]')
		table.insert(text, ']')
	end
end

function export.make_table(title, pinyin, py, cap, tl, tl2, tl3, a, novariety)
	py = ine(py);cap = ine(cap);tl = ine(tl);tl2 = ine(tl2);tl3 = ine(tl3);a = ine(a);novariety = ine(novariety)
	local text = {}
	
	local pinyin_simple_fmt = export.py_format(pinyin, false, false, true)
	local pinyin_simple_fmt_nolink = export.py_format(pinyin, false, false, true, true)
	
	if not novariety then
		table.insert(text, '* ]')
	else
		table.insert(text, '<br>')
	end
	table.insert(text, '\n** <small>' .. title .. '</small>')
	local hom_found
	if get_hom_data(pinyin_simple_fmt).list then
		hom_found = true
	else
		hom_found = false
		table.insert(text, '<sup><small><abbr title="Add Mandarin homophones"><span class="plainlinks">="edit"})) .. ' +]</span></abbr></small></sup>')
	end
	table.insert(text, "\n*** <small>'']''</small>: ")
	local id = m_zh.ts_determ(mw.title.getCurrentTitle().text)
	if id == 'trad' then
		table.insert(text, '<span class="form-of pinyin-t-form-of transliteration-')
		table.insert(text, m_zh.ts(mw.title.getCurrentTitle().text))
	elseif id == 'simp' then
		table.insert(text, '<span class="form-of pinyin-s-form-of transliteration-')
		table.insert(text, m_zh.st(mw.title.getCurrentTitle().text))
	else -- both
		table.insert(text, '<span class="form-of pinyin-ts-form-of')
	end
	table.insert(text, '" lang="cmn" style="font-family: Consolas, monospace;">')
	if py then
		table.insert(text, py)
	else
		if cap then
			table.insert(text, export.py_format(pinyin, true, true))
		else
			table.insert(text, export.py_format(pinyin, false, true))
		end
		if tl or tl2 or tl3 then
			table.insert(text, ' → ')
			if tl then tl_pos = 1 elseif tl2 then tl_pos = 2 elseif tl3 then tl_pos = 3 end
			if cap then
				table.insert(text, export.make_tl(export.py_format(pinyin, true, false, true, true), tl_pos, true, true))
			else
				table.insert(text, export.make_tl(pinyin_simple_fmt_nolink, tl_pos, true))
			end
		end
		if tl then table.insert(text, ' <small>(toneless final syllable variant)</small>')
			elseif tl2 or tl3 then table.insert(text, ' <small>(toneless variant)</small>') end
	end
	table.insert(text, "</span>\n*** <small>'']''</small>: ")
	table.insert(text, '<span lang="zh-Bopo" class="Bopo">')
	table.insert(text, export.py_zhuyin(pinyin_simple_fmt, true))
	if tl or tl2 or tl3 then
		table.insert(text, ' → ')
		table.insert(text, export.py_zhuyin(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), true))
	end
	table.insert(text, '</span>')
	if tl then table.insert(text, ' <small>(toneless final syllable variant)</small>')
		elseif tl2 or tl3 then table.insert(text, ' <small>(toneless variant)</small>') end
	table.insert(text, "\n*** <small>'']''</small>: <code>")
	if tl or tl2 or tl3 then
		table.insert(text, export.py_gwoyeu(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), pinyin_simple_fmt))
	else
		table.insert(text, export.py_gwoyeu(pinyin_simple_fmt))
	end
	table.insert(text, '</code>')
	table.insert(text, "\n*** <small>'']''</small>: <code>")
	if tl or tl2 or tl3 then
		table.insert(text, export.py_tongyong(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), pinyin_simple_fmt))
	else
		table.insert(text, export.py_tongyong(pinyin_simple_fmt))
	end
	table.insert(text, '</code>')
	table.insert(text, "\n*** <small>'']''</small>: <code>")
	if tl or tl2 or tl3 then
				table.insert(text, export.py_wg(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false), pinyin_simple_fmt))
	else
				table.insert(text, export.py_wg(pinyin_simple_fmt))
	end
	table.insert(text, '</code>')
	table.insert(text, '\n*** <small>Sinological ] <sup>(])</sup></small>: <span class="IPA">/')
	table.insert(text, export.py_ipa(pinyin))
	if tl or tl2 or tl3 then
		table.insert(text, '/ → /')
		table.insert(text, export.py_ipa(export.make_tl(pinyin_simple_fmt_nolink, tl_pos, false)))
	end
	table.insert(text, '/</span>')
	-- if a then
	-- 	if a == 'y' then a = 'zh-' .. pinyin_simple_fmt .. '.ogg' end
	-- 	table.insert(text, '\n*** <div style="display:inline-block; position:relative; top:0.5em;">[[File:')
	-- 	table.insert(text, a)
	-- 	table.insert(text, ']]</div>]')
	-- end
	if hom_found then
		table.insert(text, "\n*** <small>Homophones</small>: " ..
			'<table class="wikitable" style="width:15em;margin:0; position:left; text-align:center">' ..
			'<tr><th class="mw-customtoggle-cmnhom" style="color:#3366bb"></th></tr>' ..
			'<tr class="mw-collapsible mw-collapsed" id="mw-customcollapsible-cmnhom">' ..
			'<td><sup><div style="float: right; clear: right;"><span class="plainlinks">[')
		table.insert(text, tostring(mw.uri.fullUrl("Module:zh/data/cmn-hom/" .. hom_data_part(pinyin_simple_fmt), {="edit"})))
		table.insert(text, ' edit]</span></div></sup>')
		table.insert(text, export.homophones(mw.ustring.lower(pinyin_simple_fmt)))
		table.insert(text, '</td></tr></table>')
	end
	return table.concat(text)
end

return export