模組: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 number_to_variants(num)
local variants = {
[1] = 'zh-hans',
[2] = 'zh-hant',
[4] = 'zh-cn',
[8] = 'zh-hk',
[16] = 'zh-mo',
[32] = 'zh-my',
[64] = 'zh-sg',
[128] = 'zh-tw',
}
local result = ''
for value, variant in pairs(variants) do
if bit32.band(num, value) ~= 0 then
result = result .. (result ~= '' and '、' or '') .. '-{N|' .. variant .. '}-'
end
end
return result
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 flag, full_rules in string.gmatch('-{H|' .. content.rule .. '}-', '%-{(.-)|(.-)}%-') do -- 从 -{flag|full_rules}- 提取 flag 和 full_rules
if flag == '-' then
matchers[full_rules] = nil
else
for rules in string.gmatch(full_rules, '([^;]+)') do
local matcher = rules:match("=>") and rules:match('^(.-)=>') or rules:match('^.-:(.+)') -- 按 ";" 分割后,有 "=>" 就取前面的,没有就取第一个 ":" 后面的
if matcher then
matcher_trimmed = mw.text.trim(matcher)
matchers[matcher_trimmed] = matchers[matcher_trimmed] or {}
matchers[matcher_trimmed][index] = variant_number
end
end
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 .. '}-'
conflicted_variants = 255
for _, variant in pairs(indices) do
conflicted_variants = bit32.band(conflicted_variants, variant)
end
wikitext = wikitext .. '<span style="float: right"><small>(' .. number_to_variants(conflicted_variants) .. ')</small></span>\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