模組:CGroupCheck
外观
local export = {}
local bit32 = require('bit32')
function variant_string_to_number(variant)
if variant == 'zh-hans' then
return 1
elseif variant == 'zh-hant' then
return 2
elseif variant == 'zh-cn' then
return 4
elseif variant == 'zh-hk' then
return 8
elseif variant == 'zh-mo' then
return 16
elseif variant == 'zh-my' then
return 32
elseif variant == 'zh-sg' then
return 64
elseif variant == 'zh-tw' then
return 128
else
error('轉換組中存在未知變體:' .. variant)
end
end
function validate(cgroup_content)
local matchers = {}
for index, content in ipairs(cgroup_content) do
if content.type == 'item' then
local variant_string = string.gmatch(content.rule, 'zh%-[hcmst][ankoygw]n?[st]?')
local variant_number = 0
for variant in variant_string do
variant_number = bit32.bor(variant_number, variant_string_to_number(variant))
end
for rules in string.gmatch(content.rule, '([^;]+)') do
if mw.text.trim(rules) ~= '' then
local split = mw.text.split(rules, '=>')[1]
local rule = mw.text.trim(split:match(":(.*)") or split)
matchers[rule] = matchers[rule] or {}
matchers[rule][index] = variant_number
end
end
end
end
local conflicted_rules = {}
for rule, indices in pairs(matchers) do
local count = 0
local conflict
for index, variant in pairs(indices) do
count = count + 1
conflict = bit32.band((conflict or variant), variant)
end
if count > 1 and conflict > 0 then
conflicted_rules[rule] = indices
end
end
return conflicted_rules
end
function export.main()
local wikitext = ''
local module_name = mw.title.getCurrentTitle().prefixedText:gsub("/doc", "")
local cgroup_content = mw.loadData(module_name).content
local conflicted_rules = validate(cgroup_content)
for rule, indices in pairs(conflicted_rules) do
wikitext = wikitext .. '; -{' .. rule .. '}-\n'
for index, _ in pairs(indices) do
content = cgroup_content[index]
wikitext = wikitext .. ': '
if content.original then
wikitext = wikitext .. '原文:' .. content.original .. ';'
end
wikitext = wikitext .. '-{D|' .. content.rule .. '}-当前显示为:-{|' .. content.rule .. '}-\n'
end
end
if wikitext ~= '' then
wikitext = '以下轉換規則可能存在衝突或重複,請核查:\n' .. wikitext
else
wikitext = '轉換規則中未偵測到衝突或重複。'
end
return wikitext
end
return export