模块:Quote:修订间差异
来自星露谷物语扩展百科
更多操作
m无编辑摘要 |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
-- | -- 缓存全局变量以减少重复调用 | ||
local quoteLast = "" | |||
-- 内部函数:分割引文文本(高度优化版本) | |||
local function splitQuote(text) | local function splitQuote(text) | ||
-- 快速检查:如果文本为空,返回空结果 | -- 快速检查:如果文本为空,返回空结果 | ||
| 第29行: | 第32行: | ||
local afterColon = mw.ustring.sub(text, firstColonPos + 1) | local afterColon = mw.ustring.sub(text, firstColonPos + 1) | ||
-- 处理特例:'''123:'''456 | -- 处理特例:'''123:'''456 格式和星号开头(一次性处理) | ||
local firstChar = mw.ustring.sub(afterColon, 1, 1) | |||
local afterColonStart = mw.ustring.sub(afterColon, 1, 3) | if firstChar == "'" then | ||
local afterColonStart = mw.ustring.sub(afterColon, 1, 3) | |||
if afterColonStart == "'''" then | |||
-- 移除冒号后的三个单引号,在冒号前添加三个单引号 | |||
afterColon = mw.ustring.sub(afterColon, 4) | |||
beforeColon = beforeColon .. "'''" | |||
elseif | end | ||
-- | elseif firstChar == "*" then | ||
-- 处理冒号后文本开头的星号 | |||
afterColon = "<span>*</span>" .. mw.ustring.sub(afterColon, 2) | afterColon = "<span>*</span>" .. mw.ustring.sub(afterColon, 2) | ||
end | end | ||
| 第45行: | 第49行: | ||
end | end | ||
-- 主要的 squote | -- 主要的 squote 函数,处理整个引文模板逻辑(高度优化版本) | ||
function p.squote(frame) | function p.squote(frame) | ||
local text = frame.args[1] or "引文" | local text = frame.args[1] or "引文" | ||
| 第54行: | 第58行: | ||
local quoteBefore, quoteAfter = splitQuote(text) | local quoteBefore, quoteAfter = splitQuote(text) | ||
-- | -- 检查是否需要清除说话人名称(使用缓存的变量,避免重复调用) | ||
local shouldClearBefore = false | local shouldClearBefore = false | ||
if quoteBefore ~= "" and quoteBefore == quoteLast then | if quoteBefore ~= "" and quoteBefore == quoteLast then | ||
-- | -- 检查引文内容是否包含性别标记(优化:使用普通字符串查找) | ||
local | local afterText = quoteAfter | ||
if not (afterText:find("(男)", 1, true) or afterText:find("(女)", 1, true)) then | |||
shouldClearBefore = true | shouldClearBefore = true | ||
end | end | ||
end | end | ||
-- | -- 预计算样式类名(避免重复字符串拼接) | ||
local | local textClass = textStyle == "english" and "squotetextenglish" or "squotetext" | ||
local sourceClass = textStyle == "english" and "quotesourceenglish" or "quotesource" | |||
-- | -- 构建HTML结果(使用字符串拼接,减少函数调用开销) | ||
local | local html = '<table class="quotetable"><tr><td rowspan="2" class="decorativesquote"></td><td class="' .. textClass .. '">' | ||
-- 添加说话人和引文内容 | -- 添加说话人和引文内容 | ||
if quoteBefore ~= "" and not shouldClearBefore then | if quoteBefore ~= "" and not shouldClearBefore then | ||
html = html .. quoteBefore .. ':' | |||
-- | -- 更新缓存的说话人 | ||
quoteLast = quoteBefore | |||
-- 只在必要时设置全局变量(这是最大的性能瓶颈) | |||
frame:callParserFunction('#vardefine', {'quote_last', quoteBefore}) | frame:callParserFunction('#vardefine', {'quote_last', quoteBefore}) | ||
end | end | ||
-- 添加引文内容 | |||
html = html .. '"' | |||
if quoteAfter ~= "" then | if quoteAfter ~= "" then | ||
html = html .. quoteAfter | |||
else | else | ||
html = html .. text -- 如果没有成功分割,使用原文本 | |||
end | end | ||
html = html .. '"</td></tr>' | |||
-- 添加来源行(如果提供了来源) | -- 添加来源行(如果提供了来源) | ||
if source ~= "" then | if source ~= "" then | ||
html = html .. '<tr><td class="' .. sourceClass .. '">— ' .. source .. '</td></tr>' | |||
end | end | ||
html = html .. '</table>' | |||
return | return html | ||
end | end | ||
return p | return p | ||
2025年7月10日 (四) 18:48的版本
local p = {}
-- 缓存全局变量以减少重复调用
local quoteLast = ""
-- 内部函数:分割引文文本(高度优化版本)
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 格式和星号开头(一次性处理)
local firstChar = mw.ustring.sub(afterColon, 1, 1)
if firstChar == "'" then
local afterColonStart = mw.ustring.sub(afterColon, 1, 3)
if afterColonStart == "'''" then
-- 移除冒号后的三个单引号,在冒号前添加三个单引号
afterColon = mw.ustring.sub(afterColon, 4)
beforeColon = beforeColon .. "'''"
end
elseif firstChar == "*" 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 shouldClearBefore = false
if quoteBefore ~= "" and quoteBefore == quoteLast then
-- 检查引文内容是否包含性别标记(优化:使用普通字符串查找)
local afterText = quoteAfter
if not (afterText:find("(男)", 1, true) or afterText:find("(女)", 1, true)) then
shouldClearBefore = true
end
end
-- 预计算样式类名(避免重复字符串拼接)
local textClass = textStyle == "english" and "squotetextenglish" or "squotetext"
local sourceClass = textStyle == "english" and "quotesourceenglish" or "quotesource"
-- 构建HTML结果(使用字符串拼接,减少函数调用开销)
local html = '<table class="quotetable"><tr><td rowspan="2" class="decorativesquote"></td><td class="' .. textClass .. '">'
-- 添加说话人和引文内容
if quoteBefore ~= "" and not shouldClearBefore then
html = html .. quoteBefore .. ':'
-- 更新缓存的说话人
quoteLast = quoteBefore
-- 只在必要时设置全局变量(这是最大的性能瓶颈)
frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
end
-- 添加引文内容
html = html .. '"'
if quoteAfter ~= "" then
html = html .. quoteAfter
else
html = html .. text -- 如果没有成功分割,使用原文本
end
html = html .. '"</td></tr>'
-- 添加来源行(如果提供了来源)
if source ~= "" then
html = html .. '<tr><td class="' .. sourceClass .. '">— ' .. source .. '</td></tr>'
end
html = html .. '</table>'
return html
end
return p