Content deleted Content added
Jackmcbarn (talk | contribs) merge changes from sandbox per talk |
Publishing User:Northern Moonlight's implementation of new fields to specify locale-specific character glyphs according to the IETF standard |
||
(34 intermediate revisions by 17 users not shown) | |||
Line 1:
require('
local p = {}
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
Line 26:
["j"] = "Jyutping",
["cy"] = "Cantonese Yale",
["sl"] = "Sidney Lau",
["poj"] = "Pe̍h-ōe-jī",
["tl"] = "Tâi-lô",
["zhu"] = "Zhuyin Fuhao",
["l"] = "
["tr"] = "trans.",
}
Line 41 ⟶ 44:
["j"] = "Jyutping",
["cy"] = "Yale romanization of Cantonese",
["sl"] = "Sidney Lau romanisation",
["poj"] = "Pe̍h-ōe-jī",
["tl"] = "Tâi-uân Lô-má-jī Phing-im Hong-àn",
["zhu"] = "Bopomofo",
["l"] = "Literal translation",
["tr"] = "Translation",
}
Line 50 ⟶ 57:
["t"] = "zh-Hant",
["s"] = "zh-Hans",
["p"] = "zh-Latn
["tp"] = "zh-Latn-tongyong",
["w"] = "zh-Latn-wadegile",
["j"] = "yue-Latn-jyutping",
["cy"] = "yue-Latn",
["
["poj"] = "nan-Latn",
["tl"] = "nan-Latn-tailo",
["zhu"] = "zh-Bopo",
}
Line 65 ⟶ 74:
["j"] = true,
["cy"] = true,
["sl"] = true,
["poj"] = true,
["tl"] = true,
}
local superscript = {
["w"] = true,
["sl"] = true,
}
-- Categories for different kinds of Chinese text
local cats = {
Line 83 ⟶ 100:
function p._Zh(args)
local
local uselabels = args["labels"] ~= "no" -- whether to have labels
local capfirst = args["scase"] ~= nil
local out = nil -- which term to put before the brackets
local usebrackets = 0 -- whether to have bracketed terms
local numargs = 0
local regionalvariant = nil
if args["out"] then
out = args["out"]
usebrackets = 1
end
local t1 = false -- whether traditional Chinese characters go first
local j1 = false -- whether Cantonese Romanisations go first
local poj1 = false -- whether Hokkien Romanisations go first
local testChar
if (args["first"]) then
if (testChar == "t") then
t1 = true
end
if (testChar == "j") then
j1 = true
end
if (testChar == "poj") then
poj1 = 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", "tl", "zhu", "l", "tr"}
if (t1) then
orderlist[2] =
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
if (poj1) then
orderlist[4] = "poj"
orderlist[5] = "tl"
orderlist[6] = "p"
orderlist[7] = "tp"
orderlist[8] = "w"
orderlist[9] = "j"
orderlist[10] = "cy"
orderlist[11] = "sl"
end
-- rename rules. Rules to change parameters and labels based on other parameters
if args["hp"] then
Line 109 ⟶ 171:
-- if also Tongyu pinyin use full name for Hanyu pinyin
labels["p"] = "Hanyu Pinyin"
end
-- specify traditional Chinese variants
if args["t_hk"] then
args["t"] = args["t_hk"]
regionalvariant = "HK"
elseif args["t_tw"] then
args["t"] = args["t_tw"]
regionalvariant = "TW"
end
Line 116 ⟶ 187:
args["s"] = nil
args["t"] = nil
if out == "s" or out == "t" then
out = "c"
end
elseif (not (args["s"] and args["t"])) then
-- use short label if only one of simplified and traditional
Line 121 ⟶ 195:
labels["t"] = labels["c"]
end
if out then
for i, v in ipairs (orderlist) do -- shift `out` to the beginning of the order list
if v == out then
table.remove(orderlist, i)
table.insert(orderlist, 1, v)
break
end
end
end
if (out == "c" and args["s"]) then usebrackets = 2; end
local body = "" -- the output string
Line 130 ⟶ 215:
for i, part in ipairs(orderlist) do
if (args[part]) then
numargs = numargs + 1
-- build label
label = ""
if (uselabels) then
label = labels[part]
if (
label = mw.language.getContentLanguage():ucfirst(label)
capfirst = false
end
if (uselinks and part ~= "l" and part ~= "tr") then
label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
end
label = "<abbr title=\"" .. wlinks[part] .. "\"><small>" .. label .. "</small></abbr>"
else
label = label .. ":"
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
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") then
-- put individual, potentially comma-separated glosses in single quotes
-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
for term in val:gmatch("[^;,]+") do
term = mw.text.trim(term, "%s\"'")
terms = terms .. "'" .. term .. "', "
end
val = string.sub(terms, 1, -3)
elseif (part == "tr") then
-- put translations in double quotes
-- (first strip leading and trailing spaces and quotes, including bold/italic markup)
val = mw.text.trim(val, "%s\"'")
val = """ .. val .. """
end
if (italic[part]) then
-- italicise
val = "
end
if string.match(val, "</?sup>") then val = val.."[[Category:Pages using template Zh with sup tags]]" 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
if numargs == usebrackets then
-- opening bracket after the `out` term
body = body .. label .. val .. " ("
else
body = body .. label .. val .. "; "
end
end
end
if (body > "") then -- check for empty string
if out and numargs > usebrackets then
-- closing bracket after the rest of the terms
body = body .. ")"
end
return body
else --no named parameters; see if there's a first parameter, ignoring its name
if (args[1]) then
Line 173 ⟶ 294:
label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
end
label = label .. "&
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
|