Module:ZhConversion
此模块的文档可以在Module:ZhConversion/doc创建
local p={}
local zhcvt = mw.loadData('Module:ZhConversion/data')
function p.to_hant(str)
local input_str = str
if type(str) == type({"table"}) then
input_str = (str.args or {})[1] or str[1] or ''
elseif type(str) ~= type("string") then
input_str = tostring(str)
end
return p._language_cvt(input_str, zhcvt.to_hant, zhcvt.max_length)
end
function p.to_hans(str)
local input_str = str
if type(str) == type({"table"}) then
input_str = (str.args or {})[1] or str[1] or ''
elseif type(str) ~= type("string") then
input_str = tostring(str)
end
return p._language_cvt(input_str, zhcvt.to_hans, zhcvt.max_length)
end
function p.zh_title(str)
local input_str = str
if type(str) == type({"table"}) then
input_str = (str.args or {})[1] or str[1] or ''
elseif type(str) ~= type("string") then
input_str = tostring(str)
end
local checked_title = input_str
xpcall(function()
local function getTitle(...)
local success, titleObj = pcall(mw.title.new, ...)
if success then return titleObj else return nil end
end
local titleObj = getTitle(input_str)
local ori_code = titleObj:getContent()
if ori_code then checked_title = input_str else
local zh_hant = p.to_hant(input_str)
local titleHantObj = getTitle(zh_hant)
local hant_code = titleHantObj:getContent()
if hant_code then checked_title = zh_hant else
local zh_hans = p.to_hans(input_str)
local titleHansObj = getTitle(zh_hans)
local hans_code = titleHansObj:getContent()
if hans_code then checked_title = zh_hans end
end
end
end,function()end)
return checked_title
end
function p.equals(str1, str2)
local input_str1 = str1
local input_str2 = str2
if type(str1) == type({"table"}) then
input_str1 = (str1.args or {})[1] or str1[1] or ''
input_str2 = (str1.args or {})[2] or str1[2] or ''
elseif type(str1) ~= type("string") then
input_str1 = tostring(str1)
input_str2 = tostring(str2)
end
local result = false
if input_str1 == input_str2 then
result = true
else
local str1_hans = p._language_cvt(input_str1, zhcvt.to_hans, zhcvt.max_length)
local str2_hans = p._language_cvt(input_str2, zhcvt.to_hans, zhcvt.max_length)
if str1_hans == str2_hans or input_str1 == str2_hans or str1_hans == input_str2 then
result = true
else
local str1_hant = p._language_cvt(input_str1, zhcvt.to_hant, zhcvt.max_length)
local str2_hant = p._language_cvt(input_str2, zhcvt.to_hant, zhcvt.max_length)
if str1_hant == str2_hant or input_str1 == str2_hant or str1_hant == input_str2 then
result = true
end
end
end
if str1 == mw.getCurrentFrame() or (type(str1)==type({}) and type(str1.getParent)==type(function()end)) then
return result and '1' or ''
end
return result
end
function p.zh_convert(str)
local input_str = str
if type(str) == type({"table"}) then
input_str = (str.args or {})[1] or str[1] or ''
elseif type(str) ~= type("string") then
input_str = tostring(str)
end
local result = p._language_cvt(input_str, zhcvt.to_hant, zhcvt.max_length)
if result == input_str then
result = p._language_cvt(input_str, zhcvt.to_hans, zhcvt.max_length)
end
return result
end
function p._language_cvt(str, cvt_table, max_length)
local source = {}
for cp in mw.ustring.gcodepoint(str) do
table.insert(source, mw.ustring.char(cp))
end
local result = {}
local strlen = #source
local i=1
while i<=strlen do
local changed = false
local this_char = source[i]
for ji = 1,max_length do
local j = max_length - ji
if i + j <= strlen then
local check_str = table.concat(source, nil, i, i + j)
if cvt_table[check_str] then
table.insert(result, cvt_table[check_str])
i = i + j
changed = true
break
end
end
end
if not changed then table.insert(result, this_char) end
i = i + 1
end
return table.concat(result)
end
return p