Jump to content

Module:Lang-zh/sandbox

From Wikipedia, the free encyclopedia
require('strict')localp={}-- articles in which traditional Chinese preceeds simplified Chineselocalt1st={["228 Incident"]=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 locallabels={["c"]="Chinese",["s"]="simplified Chinese",["t"]="traditional Chinese",["p"]="pinyin",["tp"]="Tongyong Pinyin",["w"]="Wade–Giles",["j"]="Jyutping",["cy"]="Cantonese Yale",["sl"]="Sidney Lau",["poj"]="Pe̍h-ōe-jī",["tl"]="Tâi-lô",["wgn"]="Wugniu",["zhu"]="Zhuyin Fuhao",["l"]="lit.",["tr"]="trans.",}-- article titles for wikilinks for each partlocalwlinks={["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",["sl"]="Sidney Lau romanisation",["poj"]="Pe̍h-ōe-jī",["tl"]="Tâi-uân Lô-má-jī Phing-im Hong-àn",["wgn"]="Romanization of Wu Chinese",["zhu"]="Bopomofo",["l"]="Literal translation",["tr"]="Translation",}-- for those parts which are to be treated as languages their ISO codelocalISOlang={["c"]="zh",["t"]="zh-Hant",["s"]="zh-Hans",["p"]="zh-Latn",["tp"]="zh-Latn-tongyong",["w"]="zh-Latn-wadegile",["j"]="yue-Latn-jyutping",["cy"]="yue-Latn",["sl"]="yue-Latn",["poj"]="nan-Latn-pehoeji",["tl"]="nan-Latn-tailo",["wgn"]="wuu-Latn",["zhu"]="zh-Bopo",}localitalic={["p"]=true,["tp"]=true,["w"]=true,["j"]=true,["cy"]=true,["sl"]=true,["poj"]=true,["tl"]=true,["wgn"]=true,}localsuperscript={["w"]=true,["sl"]=true,["wgn"]=true,}-- Categories for different kinds of Chinese textlocalcats={["c"]="[[Category:Articles containing Chinese-language text]]",["s"]="[[Category:Articles containing simplified Chinese-language text]]",["t"]="[[Category:Articles containing traditional Chinese-language text]]",}functionp.Zh(frame)-- load arguments module to simplify handling of argslocalgetArgs=require('Module:Arguments').getArgslocalargs=getArgs(frame)returnp._Zh(args)endfunctionp._Zh(args)ifargs["link"]thenargs["links"]=args["link"];endifargs["label"]thenargs["labels"]=args["label"];endlocaluselinks=args["links"]~="no"-- whether to add linkslocaluselabels=args["labels"]~="no"-- whether to have labelslocalcapfirst=args["scase"]~=nillocalout=nil-- which term to put before the bracketslocalusebrackets=0-- whether to have bracketed termslocalnumargs=0localregionalVariant=nilifargs["out"]thenout=args["out"]usebrackets=1endlocalt1=false-- whether traditional Chinese characters go firstlocalj1=false-- whether Cantonese Romanisations go firstlocalpoj1=false-- whether Hokkien Romanisations go firstlocalwgn1=false-- whether Wugniu goes firstlocaltestCharif(args["first"])thenfortestCharinmw.ustring.gmatch(args["first"],"%a+")doif(testChar=="t")thent1=trueendif(testChar=="j")thenj1=trueendif(testChar=="poj")thenpoj1=trueendif(testChar=="wgn")thenwgn1=trueendendendif(t1==false)thenlocaltitle=mw.title.getCurrentTitle()t1=t1st[title.text]==trueend-- based on setting/preference specify orderlocalorderlist={"c","s","t","p","tp","w","j","cy","sl","poj","tl","wgn","zhu","l","tr"}if(t1)thenorderlist[2]="t"orderlist[3]="s"endif(j1)thenorderlist[4]="j"orderlist[5]="cy"orderlist[6]="sl"orderlist[7]="p"orderlist[8]="tp"orderlist[9]="w"endif(poj1)thenorderlist[4]="poj"orderlist[5]="tl"orderlist[6]="p"orderlist[7]="tp"orderlist[8]="w"orderlist[9]="j"orderlist[10]="cy"orderlist[11]="sl"endif(wgn1)thenorderlist[4]="wgn"orderlist[5]="p"orderlist[6]="tp"orderlist[7]="w"orderlist[8]="j"orderlist[9]="cy"orderlist[10]="sl"orderlist[11]="poj"orderlist[12]="tl"end-- rename rules. Rules to change parameters and labels based on other parametersifargs["hp"]then-- hp an alias for p ([hanyu] pinyin)args["p"]=args["hp"]endifargs["tp"]then-- if also Tongyong pinyin use full name for Hanyu pinyinlabels["p"]="Hanyu Pinyin"end-- specify traditional Chinese variantsifargs["t_hk"]thenargs["t"]=args["t_hk"]regionalVariant="HK"elseifargs["t_tw"]thenargs["t"]=args["t_tw"]regionalVariant="TW"endif(args["s"]andargs["s"]==args["t"])then-- Treat simplified + traditional as Chinese if they're the sameargs["c"]=args["s"]args["s"]=nilargs["t"]=nilifout=="s"orout=="t"thenout="c"endelseif(not(args["s"]andargs["t"]))then-- use short label if only one of simplified and traditionallabels["s"]=labels["c"]labels["t"]=labels["c"]endifoutthenfori,vinipairs(orderlist)do-- shift `out` to the beginning of the order listifv==outthentable.remove(orderlist,i)table.insert(orderlist,1,v)breakendendendif(out=="c"andargs["s"])thenusebrackets=2;endlocalbody=""-- the output stringlocalparams-- for creating HTML spanslocallabel-- the label, i.e. the bit preceeding the supplied textlocalval-- the supplied text-- go through all possible fields in loop, adding them to the outputfori,partinipairs(orderlist)doif(args[part])thennumargs=numargs+1-- build labellabel=""if(uselabels)thenlabel=labels[part]if(capfirst)thenlabel=mw.language.getContentLanguage():ucfirst(label)capfirst=falseendif(uselinksandpart~="l"andpart~="tr")thenlabel="[["..wlinks[part].."|"..label.."]]"endif(part=="l"orpart=="tr")thenlabel="<abbr title=\""..wlinks[part].."\"><small>"..label.."</small></abbr>"elselabel=label.."&colon;"endlabel=label.." "end-- build valueval=args[part]if(cats[part])andmw.title.getCurrentTitle().namespace==0then-- if has associated category AND current page in article namespace, add categoryval=cats[part]..valendif(ISOlang[part])then-- add span for language if neededparams={["lang"]=ISOlang[part]..(regionalVariantand"-"..regionalVariantor"")}val=mw.text.tag({name="span",attrs=params,content=val})elseif(part=="l")thenlocalterms=""-- put individual, potentially comma-separated glosses in single quotes-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)forterminval:gmatch("[^;,]+")doterm=mw.text.trim(term,"%s\"'")terms=terms.."&apos;"..term.."&apos;, "endval=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="&quot;"..val.."&quot;"endif(italic[part])then-- italiciseval="<i>"..val.."</i>"endifstring.match(val,"</?sup>")thenval=val.."[[Category:Pages using template Zh with sup tags]]"endif(superscript[part])then-- superscriptval=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 bodyifnumargs==usebracketsthen-- opening bracket after the `out` termbody=body..label..val.." ("elsebody=body..label..val.."; "endendendif(body>"")then-- check for empty stringbody=string.sub(body,1,-3)-- chop off final semicolon and spaceifoutandnumargs>usebracketsthen-- closing bracket after the rest of the termsbody=body.."&rpar;"endreturnbodyelse--no named parameters; see if there's a first parameter, ignoring its nameif(args[1])then-- if there is treat it as Chineselabel=""if(uselabels)thenlabel=labels["c"]if(uselinks)thenlabel="[["..wlinks["c"].."|"..label.."]]"endlabel=label.."&colon; "end-- default to show links and labels as no options givenifmw.title.getCurrentTitle().namespace==0then-- if current page in article namespaceval=cats["c"]..args[1]elseval=args[1]endparams={["lang"]=ISOlang["c"]}val=mw.text.tag({name="span",attrs=params,content=val})returnlabel..valendreturn""endendreturnp
close