跳转到内容

模組:Conversion rule extractor

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由PexEric留言 | 贡献2025年5月3日 (六) 17:06编辑。这可能和当前版本存在着巨大的差异。

-- Module:Conversion_rule_extractor
-- 主模块:处理调用参数,协调子模块,格式化输出

local p = {}

local Extractor = require('Module:Conversion_rule_extractor/Extractor')
local Matcher = require('Module:Conversion_rule_extractor/Matcher')
local WikitextLC = require('Module:WikitextLC')
local Arguments = require('Module:Arguments')

-- 格式化输出规则列表
local function formatRules(rules, flag)
    flag = flag or 'H' -- 默认 H
    local output = {}

    if flag == 'raw' then
        return table.concat(rules, "\n")
    elseif flag == 'H' or flag == 'T' then -- H是隐藏规则,T是标题规则(虽然这里通常不用T,但保持一致性)
        local func = (flag == 'T' and WikitextLC.title) or WikitextLC.hidden
        for _, rule in ipairs(rules) do
            table.insert(output, func(rule))
        end
        return table.concat(output)
    else -- 其他 flag,例如 A, D 等,直接用作旗标
        local pattern = '-{' .. flag .. '|%s}-'
        for _, rule in ipairs(rules) do
            table.insert(output, string.format(pattern, rule))
        end
        return table.concat(output)
    end
end

-- 获取并过滤内容规则
function p.getRules(frame)
    local args = Arguments.getArgs(frame)
    local fromPage = args.from or args[1]
    local toPage = args.to or mw.title.getCurrentTitle().prefixedText -- 默认为当前页面
    local flag = args.flag or 'H'

    if not fromPage then
        return '<span class="error">错误:未指定规则来源页面(需要 "from" 或匿名参数 1)。</span>'
    end
    
    -- 1. 从 fromPage 提取所有规则
    local allRules = Extractor.getAllRules(fromPage)
    if not allRules then -- Extractor 可能在页面不存在时返回 nil 或空表
         return '' -- 或者返回错误信息?暂时返回空
    end
    
    -- 2. 过滤 contentRules,看哪些能在 toPage 中匹配
    local applicableContentRules = Matcher.filterRules(allRules.contentRules, toPage)
    
    -- 3. 格式化输出匹配到的内容规则
    return formatRules(applicableContentRules, flag)
end

-- 获取标题转换规则
function p.getTitleRule(frame)
    local args = Arguments.getArgs(frame)
    local fromPage = args.from or args[1]
    local flag = args.flag or 'H'
    local typeParam = args.type

    if not fromPage then
        return '<span class="error">错误:未指定规则来源页面(需要 "from" 或匿名参数 1)。</span>'
    end

    local titleObj = mw.title.new(fromPage)
    if not titleObj or not titleObj.exists then
         return '<span class="error">错误:来源页面 "' .. fromPage .. '" 不存在。</span>'
    end

    -- 1. 从 fromPage 提取所有规则
    local allRules = Extractor.getAllRules(fromPage)
    if not allRules then return '' end -- 防御性编程

    local finalTitleRules = {}

    -- 2. 确定最终用于标题的规则
    if allRules.titleRule and allRules.titleRule ~= '' then
        -- a) 如果有显式的 T= 规则,则使用它
        finalTitleRules = { allRules.titleRule }
        -- mw.log('Using explicit Title rule (T=):', allRules.titleRule)
    else
        -- b) 如果没有 T= 规则,则检查内容规则是否匹配页面标题本身
        finalTitleRules = Matcher.filterRulesAgainstTitleText(allRules.contentRules, fromPage)
        -- mw.logObject('Using content rules matched against title text:', finalTitleRules)
    end

    -- 3. 根据 type 和 flag 格式化输出
    if typeParam == 'context' then
        -- 特殊处理:输出实际渲染效果(近似)
        if allRules.titleRule and allRules.titleRule ~= '' then
            -- 有 T 规则,直接用 T 旗标包裹
            return WikitextLC.title(allRules.titleRule)
        else
            -- 没有 T 规则,将匹配到的内容规则用 H 旗标隐藏,并附加原始标题文本
            -- 注意:这只是一个近似,真实渲染由 MediaWiki 完成,这里无法完美模拟
            -- 输出格式模仿 NoteTA-lite:规则在前,文本在后(虽然逻辑上规则应包裹文本)
            -- 这种方式能让JS读取规则,但不一定能在无JS环境下正确显示组合标题
            local hiddenRules = formatRules(finalTitleRules, 'H') -- 使用 H 旗标隐藏
            return hiddenRules .. titleObj.text -- 返回隐藏规则 + 原始标题文字
        end
    else
        -- 常规处理:根据 flag 输出规则本身
        -- 注意:如果使用的是内容规则,格式化时 flag=T 和 flag=H 效果相同(都是隐藏)
        -- 只有显式 T= 规则在 type=context 时会用 WikitextLC.title()
        local outputFlag = (allRules.titleRule and flag == 'T') and 'T' or flag -- 如果是显式T规则且flag=T,用T,否则用传入的flag
         if not allRules.titleRule and outputFlag == 'T' then outputFlag = 'H' end -- 如果是内容规则模拟的标题规则,强制用H格式化,避免误用T

        return formatRules(finalTitleRules, outputFlag)
    end
end

return p