打开/关闭菜单
331
1.7K
131
11.8K
星露谷物语扩展百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Quote:修订间差异

来自星露谷物语扩展百科
Sizau留言 | 贡献
无编辑摘要
Sizau留言 | 贡献
性能优化
第1行: 第1行:
local p = {}
local p = {}


-- 检查文本中是否有且仅有一个中文冒号":",且冒号前面没有出现过中文逗号和中文句号
-- 内部函数:分割引文文本
function p.split(frame)
local function splitQuote(text)
    local text = frame.args[1] or ""
     -- 快速检查:如果文本为空,返回空结果
   
     if not text or text == "" then
     -- 计算中文冒号的数量
        return "", ""
     local colonCount = 0
    for _ in mw.ustring.gmatch(text, "") do
        colonCount = colonCount + 1
     end
     end
   
 
     -- 如果不是只有一个冒号,返回空
     -- 使用更高效的方式:先找第一个冒号,再检查是否有第二个
     if colonCount ~= 1 then
    local firstColonPos = mw.ustring.find(text, ":")
         return ""
     if not firstColonPos then
         return "", text  -- 没有冒号,整个文本作为引文内容
     end
     end
   
 
     -- 找到冒号的位置
     -- 检查是否有第二个冒号(从第一个冒号之后开始查找)
     local colonPos = mw.ustring.find(text, ":")
     local secondColonPos = mw.ustring.find(text, ":", firstColonPos + 1)
      
     if secondColonPos then
     -- 检查冒号前面是否有中文逗号或中文句号
        return "", text  -- 有多个冒号,整个文本作为引文内容
     local beforeColon = mw.ustring.sub(text, 1, colonPos - 1)
    end
 
     -- 现在确定只有一个冒号,检查冒号前面是否有中文逗号或中文句号
     local beforeColon = mw.ustring.sub(text, 1, firstColonPos - 1)
     if mw.ustring.find(beforeColon, "[,。]") then
     if mw.ustring.find(beforeColon, "[,。]") then
         return ""
         return "", text  -- 不符合条件,整个文本作为引文内容
     end
     end
   
 
     -- 分割文本
     -- 分割文本
     local afterColon = mw.ustring.sub(text, colonPos + 1)
     local afterColon = mw.ustring.sub(text, firstColonPos + 1)
   
 
     -- 处理特例:'''123:'''456 格式
     -- 处理特例:'''123:'''456 格式
     -- 检查冒号后是否紧跟着三个单引号
     -- 检查冒号后是否紧跟着三个单引号(一次性检查,避免重复调用sub)
     if mw.ustring.sub(afterColon, 1, 3) == "'''" then
     local afterColonStart = mw.ustring.sub(afterColon, 1, 3)
    if afterColonStart == "'''" then
         -- 移除冒号后的三个单引号
         -- 移除冒号后的三个单引号
         afterColon = mw.ustring.sub(afterColon, 4)
         afterColon = mw.ustring.sub(afterColon, 4)
         -- 在冒号前添加三个单引号
         -- 在冒号前添加三个单引号
         beforeColon = beforeColon .. "'''"
         beforeColon = beforeColon .. "'''"
    elseif mw.ustring.sub(afterColon, 1, 1) == "*" then
        -- 处理冒号后文本开头的星号(合并到上面的条件中,减少字符串操作)
        afterColon = "<span>*</span>" .. mw.ustring.sub(afterColon, 2)
    end
    return beforeColon, afterColon
end
-- 主要的 squote 函数,处理整个引文模板逻辑
function p.squote(frame)
    local text = frame.args[1] or "引文"
    local source = frame.args[2] or ""
    local textStyle = frame.args.text or ""
    -- 分割引文
    local quoteBefore, quoteAfter = splitQuote(text)
    -- 获取上一次的说话人(从全局变量中)
    local quoteLast = frame:callParserFunction('#var', {'quote_last'}) or ""
    -- 检查是否需要清除说话人名称
    local shouldClearBefore = false
    if quoteBefore ~= "" and quoteBefore == quoteLast then
        -- 检查引文内容是否包含性别标记
        local hasGenderMarker = mw.ustring.find(quoteAfter, "(男)") or mw.ustring.find(quoteAfter, "(女)")
        if not hasGenderMarker then
            shouldClearBefore = true
        end
    end
    -- 构建HTML结果
    local result = {}
    table.insert(result, '<table class="quotetable">')
    table.insert(result, '<tr>')
    table.insert(result, '<td rowspan="2" class="decorativesquote">')
    table.insert(result, '</td>')
    -- 构建引文文本单元格
    local textClass = "squotetext"
    if textStyle == "english" then
        textClass = textClass .. "english"
     end
     end
      
     table.insert(result, '<td class="' .. textClass .. '">')
     -- 处理冒号后文本开头的星号
 
     if mw.ustring.sub(afterColon, 1, 1) == "*" then
     -- 添加说话人和引文内容
         afterColon = "<span>*</span>" .. mw.ustring.sub(afterColon, 2)
     if quoteBefore ~= "" and not shouldClearBefore then
         table.insert(result, quoteBefore .. ':')
        -- 设置当前说话人为下次使用
        frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
     end
     end
   
 
     -- 返回结果
     table.insert(result, '"')
     if frame.args.part == "before" then
     if quoteAfter ~= "" then
         return beforeColon
         table.insert(result, quoteAfter)
    elseif frame.args.part == "after" then
        return afterColon
     else
     else
         -- 默认返回两部分,用 frame.args.separator 分隔(默认为制表符)
         table.insert(result, text) -- 如果没有成功分割,使用原文本
         local separator = frame.args.separator or "\t"
    end
         return beforeColon .. separator .. afterColon
    table.insert(result, '"')
 
    table.insert(result, '</td></tr>')
 
    -- 添加来源行(如果提供了来源)
    if source ~= "" then
        table.insert(result, '<tr>')
         local sourceClass = "quotesource"
        if textStyle == "english" then
            sourceClass = sourceClass .. "english"
         end
        table.insert(result, '<td class="' .. sourceClass .. '">&mdash; ' .. source)
        table.insert(result, '</td></tr>')
     end
     end
    table.insert(result, '</table>')
    return table.concat(result)
end
end


return p
return p

2025年7月10日 (四) 18:36的版本

[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local p = {}

-- 内部函数:分割引文文本
local function splitQuote(text)
    -- 快速检查:如果文本为空,返回空结果
    if not text or text == "" then
        return "", ""
    end

    -- 使用更高效的方式:先找第一个冒号,再检查是否有第二个
    local firstColonPos = mw.ustring.find(text, ":")
    if not firstColonPos then
        return "", text  -- 没有冒号,整个文本作为引文内容
    end

    -- 检查是否有第二个冒号(从第一个冒号之后开始查找)
    local secondColonPos = mw.ustring.find(text, ":", firstColonPos + 1)
    if secondColonPos then
        return "", text  -- 有多个冒号,整个文本作为引文内容
    end

    -- 现在确定只有一个冒号,检查冒号前面是否有中文逗号或中文句号
    local beforeColon = mw.ustring.sub(text, 1, firstColonPos - 1)
    if mw.ustring.find(beforeColon, "[,。]") then
        return "", text  -- 不符合条件,整个文本作为引文内容
    end

    -- 分割文本
    local afterColon = mw.ustring.sub(text, firstColonPos + 1)

    -- 处理特例:'''123:'''456 格式
    -- 检查冒号后是否紧跟着三个单引号(一次性检查,避免重复调用sub)
    local afterColonStart = mw.ustring.sub(afterColon, 1, 3)
    if afterColonStart == "'''" then
        -- 移除冒号后的三个单引号
        afterColon = mw.ustring.sub(afterColon, 4)
        -- 在冒号前添加三个单引号
        beforeColon = beforeColon .. "'''"
    elseif mw.ustring.sub(afterColon, 1, 1) == "*" then
        -- 处理冒号后文本开头的星号(合并到上面的条件中,减少字符串操作)
        afterColon = "<span>*</span>" .. mw.ustring.sub(afterColon, 2)
    end

    return beforeColon, afterColon
end

-- 主要的 squote 函数,处理整个引文模板逻辑
function p.squote(frame)
    local text = frame.args[1] or "引文"
    local source = frame.args[2] or ""
    local textStyle = frame.args.text or ""

    -- 分割引文
    local quoteBefore, quoteAfter = splitQuote(text)

    -- 获取上一次的说话人(从全局变量中)
    local quoteLast = frame:callParserFunction('#var', {'quote_last'}) or ""

    -- 检查是否需要清除说话人名称
    local shouldClearBefore = false
    if quoteBefore ~= "" and quoteBefore == quoteLast then
        -- 检查引文内容是否包含性别标记
        local hasGenderMarker = mw.ustring.find(quoteAfter, "(男)") or mw.ustring.find(quoteAfter, "(女)")
        if not hasGenderMarker then
            shouldClearBefore = true
        end
    end

    -- 构建HTML结果
    local result = {}
    table.insert(result, '<table class="quotetable">')
    table.insert(result, '<tr>')
    table.insert(result, '<td rowspan="2" class="decorativesquote">')
    table.insert(result, '</td>')

    -- 构建引文文本单元格
    local textClass = "squotetext"
    if textStyle == "english" then
        textClass = textClass .. "english"
    end
    table.insert(result, '<td class="' .. textClass .. '">')

    -- 添加说话人和引文内容
    if quoteBefore ~= "" and not shouldClearBefore then
        table.insert(result, quoteBefore .. ':')
        -- 设置当前说话人为下次使用
        frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
    end

    table.insert(result, '"')
    if quoteAfter ~= "" then
        table.insert(result, quoteAfter)
    else
        table.insert(result, text) -- 如果没有成功分割,使用原文本
    end
    table.insert(result, '"')

    table.insert(result, '</td></tr>')

    -- 添加来源行(如果提供了来源)
    if source ~= "" then
        table.insert(result, '<tr>')
        local sourceClass = "quotesource"
        if textStyle == "english" then
            sourceClass = sourceClass .. "english"
        end
        table.insert(result, '<td class="' .. sourceClass .. '">&mdash; ' .. source)
        table.insert(result, '</td></tr>')
    end

    table.insert(result, '</table>')

    return table.concat(result)
end

return p