توضیحات پودمان[ایجاد] [پاکسازی]
require('strict')

local p = {}
local yesno = require("Module:Yesno")

-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
	["واقعه ۲۸ فوریه"] = true,
	["گاه‌شماری چینی"] = true,
	["مرکز لیپو (هنگ کنگ)"] = true,
	["جمهوری چین"] = true,
	["جمهوری چین در بازی‌های المپیک تابستانی ۱۹۲۴"] = true,
	["تایوان"] = true,
	["تایوان (جزیره)"] = true,
	["استان تایوان تایپه"] = true,
	["وی بویانگ"] = true,
}

-- the labels for each part 
local labels = {
	["c"] = "چینی",
	["s"] = "چینی ساده‌شده",
	["t"] = "چینی سنتی",
	["p"] = "پین‌یین",
	["tp"] = "تونگ‌یونگ پین‌یین",
	["w"] = "وید جایلز",
	["j"] = "جیوت‌پینگ",
	["cy"] = "ییل کانتونی",
	["sl"] = "سیدنی لائو",
	["poj"] = "پهوزی",
	["zhu"] = "ژویین فوهائو",
	["l"] = "ت.ت.",
	["tr"] = "ت.",
}

-- article titles for wikilinks for each part
local wlinks = {
	["c"] = "زبان چینی",
	["s"] = "نویسه‌های چینی ساده‌شده",
	["t"] = "نویسه‌های چینی سنتی",
	["p"] = "پین‌یین",
	["tp"] = "تونگ‌یونگ پین‌یین",
	["w"] = "وید جایلز",
	["j"] = "جیوت‌پینگ",
	["cy"] = "لاتین‌نویسی ییل برای کانتونی",
	["sl"] = "لاتین‌نویسی سیدنی لائو",
	["poj"] = "پهوزی",
	["zhu"] = "بوپوموفو",
	["l"] = "ترجمه تحت‌اللفظی",
	["tr"] = "ترجمه",
}

-- for those parts which are to be treated as languages their ISO code
local ISOlang = {
	["c"] = "zh",
	["t"] = "zh-Hant",
	["s"] = "zh-Hans",
	["p"] = "zh-Latn-pinyin",
	["tp"] = "zh-Latn",
	["w"] = "zh-Latn-wadegile",
	["j"] = "yue-Latn-jyutping",
	["cy"] = "yue-Latn",
	["sl"] = "yue-Latn",
	["poj"] = "nan-Latn",
	["zhu"] = "zh-Bopo",
}

local italic = {
	["p"] = true,
	["tp"] = true,
	["w"] = true,
	["j"] = true,
	["cy"] = true,
	["sl"] = true,
	["poj"] = true,
}

local superscript = {
	["w"] = true,
	["sl"] = true,
}
-- Categories for different kinds of Chinese text
local cats = {
	["c"] = "[[رده:مقاله‌های حاوی متن به زبان چینی]]",
	["s"] = "[[رده:مقاله‌های حاوی متن به زبان چینی ساده‌شده]]",
	["t"] = "[[رده:مقاله‌های حاوی متن به زبان چینی سنتی]]",
}

function p.Zh(frame)
	-- load arguments module to simplify handling of args
	local getArgs = require('Module:Arguments').getArgs
	
	local args = getArgs(frame)
	
	-- Localized args
	if args['اول'] then args['first'] = args['اول'] end
	if args['برچسب'] then args['labels'] = args['برچسب'] end
	if args['بزرگی جمله'] then args['scase'] = args['بزرگی جمله'] end
	if args['بوپوموفو'] then args['zhu'] = args['بوپوموفو'] end
	if args['پهوزی'] then args['poj'] = args['پهوزی'] end
	if args['پین‌یین'] then args['p'] = args['پین‌یین'] end
	if args['پیوند'] then args['links'] = args['پیوند'] end
	if args['تت'] then args['l'] = args['تت'] end
	if args['تونگ‌یونگ پین‌یین'] then args['tp'] = args['تونگ‌یونگ پین‌یین'] end
	if args['جیوت‌پینگ'] then args['j'] = args['جیوت‌پینگ'] end
	if args['چینی'] then args['c'] = args['چینی'] end
	if args['ساده‌شده'] then args['s'] = args['ساده‌شده'] end
	if args['سنتی'] then args['t'] = args['سنتی'] end
	if args['سیدنی لائو'] then args['sl'] = args['سیدنی لائو'] end
	if args['وید جایلز'] then args['w'] = args['وید جایلز'] end
	if args['ییل کانتونی'] then args['cy'] = args['ییل کانتونی'] end
	
	return p._Zh(args)
end
	
function p._Zh(args)
	local uselinks = not (yesno(args["links"]) == false) -- whether to add links
	local uselabels = not (yesno(args["labels"]) == false) -- whether to have labels
	local capfirst = args["scase"] ~= nil
 
	local t1 = false -- whether traditional Chinese characters go first
	local j1 = false -- whether Cantonese Romanisations go first
	local testChar
	if (args["first"]) then
	 	 for testChar in mw.ustring.gmatch(args["first"], "%a+") do
			if (testChar == "t") then
				t1 = true
			 end
			if (testChar == "j") then
				j1 = true
			 end
		end
	end
	if (t1 == false) then
		local title = mw.title.getCurrentTitle()
		t1 = t1st[title.text] == true
	end

	-- based on setting/preference specify order
	local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "sl", "poj", "zhu", "l", "tr"}
	if (t1) then
		orderlist[2] = "t"
		orderlist[3] = "s"
	end
	if (j1) then
		orderlist[4] = "j"
		orderlist[5] = "cy"
		orderlist[6] = "sl"
		orderlist[7] = "p"
		orderlist[8] = "tp"
		orderlist[9] = "w"
	end
	
	-- rename rules. Rules to change parameters and labels based on other parameters
	if args["hp"] then
		-- hp an alias for p ([hanyu] pinyin)
		args["p"] = args["hp"]
	end
	if args["tp"] then
		-- if also Tongyu pinyin use full name for Hanyu pinyin
		labels["p"] = "هانیو پین‌یین"
	end
	
	if (args["s"] and args["s"] == args["t"]) then
		-- Treat simplified + traditional as Chinese if they're the same
		args["c"] = args["s"]
		args["s"] = nil
		args["t"] = nil
	elseif (not (args["s"] and args["t"])) then
		-- use short label if only one of simplified and traditional
		labels["s"] = labels["c"]
		labels["t"] = labels["c"]
	end

	local body = "" -- the output string
	local params -- for creating HTML spans
	local label -- the label, i.e. the bit preceeding the supplied text
	local val -- the supplied text
	
	-- go through all possible fields in loop, adding them to the output
	for i, part in ipairs(orderlist) do
		if (args[part]) then
			-- build label
			label = ""
			if (uselabels) then
				label = labels[part]
				if (capfirst) then
					label = mw.language.getContentLanguage():ucfirst(label)
					capfirst = false
				end
				if (uselinks) then
					label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
				end
				if (labels[part] ~= "ت.ت.") then
					label = label .. ":"
				else
					label = "<small>" .. label .. "</small>"
				end
				label = label .. " "
			end
			-- build value
			val = args[part]
			if (cats[part]) and mw.title.getCurrentTitle().namespace == 0 then
				-- if has associated category AND current page in article namespace, add category
				val = cats[part] .. val
			end
			if (ISOlang[part]) then
				-- add span for language if needed
				params = {["lang"] = ISOlang[part]}
				val = mw.text.tag({name="span",attrs=params, content=val})
			elseif ((part == "l") or (part == "tr")) then
				-- put literals in quotes
				val = "'" .. val .. "'"
			end
			if (italic[part]) then
				-- italicise
				val = "<i>" .. val .. "</i>"
			end
			if mw.ustring.match(val, "</?sup>") then val = val.."[[رده:صفحه‌هایی که از الگوی Lang-zh به‌همراه برچسب‌های بالانویس استفاده می‌کنند]]" end
			if (superscript[part]) then
				-- superscript
				val = val:gsub("(%d)", "<sup>%1</sup>"):gsub("(%d)</sup>%*<sup>(%d)", "%1*%2"):gsub("<sup><sup>([%d%*]+)</sup></sup>", "<sup>%1</sup>")
			end
			-- add both to body
			body = body .. label .. val .. "؛ "
		end
	end
	
	if (body > "") then -- check for empty string
		return string.sub(body, 1, -3) -- chop off final semicolon and space
	else --no named parameters; see if there's a first parameter, ignoring its name
		if (args[1]) then
			-- if there is treat it as Chinese
			label = ""
			if (uselabels) then
				label = labels["c"]
				if (uselinks) then
					label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
				end
				label = label .. "&#58; "
			end
			-- default to show links and labels as no options given
			if mw.title.getCurrentTitle().namespace == 0 then
				-- if current page in article namespace
				val = cats["c"] .. args[1]
			else
				val = args[1]
			end
			params = {["lang"] = ISOlang["c"]}
			val = mw.text.tag({name="span",attrs=params, content=val})
			return label .. val
		end
		return ""
	end
end

return p