Jump to content

Module:Zh

From Simple English Wikipedia, the free encyclopedia
Revision as of 00:32, 2 May 2014 by JohnBlackburne (talk | changes) (escape colons to fix description list bug)

Documentation for this module may be created at Module:Zh/doc

local p = {}
 
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
	["228 Incident"] = true,
	["Bao'an County"] = true,
	["Chinese calendar"] = true,
	["Lippo Centre, Hong Kong"] = true,
	["Republic of China"] = true,
	["Republic of China at the 1924 Summer Olympics"] = true,
	["Taiwan"] = true,
	["Taiwan (island)"] = true,
	["Taiwan Province"] = true,
	["Wei Boyang"] = true,
}

-- the labels for each part 
local labels = {
	["c"] = "Chinese",
	["s"] = "simplified Chinese",
	["t"] = "traditional Chinese",
	["p"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Cantonese Yale",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Zhuyin Fuhao",
	["l"] = "literally",
}

-- article titles for wikilinks for each part
local wlinks = {
	["c"] = "Chinese language",
	["s"] = "simplified Chinese characters",
	["t"] = "traditional Chinese characters",
	["p"] = "pinyin",
	["tp"] = "Tongyong Pinyin",
	["w"] = "Wade–Giles",
	["j"] = "Jyutping",
	["cy"] = "Yale romanization of Cantonese",
	["poj"] = "Pe̍h-ōe-jī",
	["zhu"] = "Bopomofo",
}

-- for those parts which are to be treated as languages their ISO code
local ISOlang = {
	["c"] = "zh",
	["t"] = "zh-Hant",
	["s"] = "zh-Hans",
}
-- Categories for different kinds of Chinese text
local cats = {
	["c"] = "[[Category:Articles containing Chinese-language text]]",
	["s"] = "[[Category:Articles containing simplified Chinese-language text]]",
	["t"] = "[[Category:Articles containing traditional Chinese-language text]]",
}

function p.Zh(frame)
	-- load arguments module to simplify handling of args
	local getArgs = require('Module:Arguments').getArgs
	
	local args = getArgs(frame)
	return p._Zh(args)
end
	
function p._Zh(args)
	local uselinks = not (args["links"] == "no") -- whether to add links
	local uselabels = not (args["labels"] == "no") -- whether to have labels
 
	local t1 -- whether traditional Chinese characters go first
	if (args["first"]) then
		t1 = args["first"] == "t"
	else
		local title = mw.title.getCurrentTitle()
		t1 = t1st[title.text] == true
	end
	-- based on setting/preference specify order
	local orderlist
	if (t1) then
		orderlist = {"c", "t", "s", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
	else
		orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
	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"] = "Hanyu Pinyin"
	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

	-- 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
			local label = ""
			if (uselabels) then
				label = labels[part]
				if (uselinks and part ~= "l") then
					label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
				end
				label = label .. ": "
			end
			-- build value
			local val = args[part]
			if (cats[part]) then
				-- if has associated category add it
				val = cats[part] .. val
			end
			if (ISOlang[part]) then
				-- add span for language if needed
				params = {["lang"] = ISOlang[part], ["xml:lang"] = ISOlang[part]}
				val = mw.text.tag({name="span",attrs=params, content=val})
			elseif (part == "p") then
				-- italicise pinyin
				val = "''" .. val .. "''"
			elseif (part == "l") then
				-- put literals in quotes
				val = '"' .. val .. '"'
			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 an unnamed first parameter
		if (args[1]) then
			-- if there is treat it as Chinese
			-- default to show links and labels as no options given
			body = args[1]
			body = cats["c"] .. body
			params = {["lang"] = ISOlang["c"], ["xml:lang"] = ISOlang["c"]}
			body = mw.text.tag({name="span",attrs=params, content=body})
			return "[[" .. wlinks["c"] .. "|" .. labels["c"] .. "]]: " .. body
		end
		return ""
	end
end

return p