模块:ExampleTable:修订间差异
无编辑摘要 |
无编辑摘要 |
||
| 第21行: | 第21行: | ||
end | end | ||
end | end | ||
-- 如果是嵌套调用,确保input保持原始状态 | |||
if frame:getParent() then | |||
input = args[1] or "" | |||
end | |||
-- 清理输出中的多余空格和换行 | |||
output = output:gsub("\n+", "\n"):gsub("^%s*(.-)%s*$", "%1") | |||
local tableHtml = mw.html.create('table') | local tableHtml = mw.html.create('table') | ||
2025年10月16日 (四) 09:08的版本
此模块的文档可以在模块:ExampleTable/doc创建
local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
local args = getArgs(frame)
local input = args[1] or ""
local output = args[2]
-- 检测input中是否包含nowiki标签
local has_nowiki = input:match("</?nowiki>")
if not output then
if has_nowiki then
-- 有nowiki标签:移除nowiki标签并渲染内容,同时去除多余空格
local nowiki_content = input:gsub("</?nowiki>", ""):gsub("^%s*(.-)%s*$", "%1")
output = frame:preprocess(nowiki_content)
else
-- 没有nowiki标签:input已经被预处理,所以output也用同样的值
output = input
end
end
-- 如果是嵌套调用,确保input保持原始状态
if frame:getParent() then
input = args[1] or ""
end
-- 清理输出中的多余空格和换行
output = output:gsub("\n+", "\n"):gsub("^%s*(.-)%s*$", "%1")
local tableHtml = mw.html.create('table')
:addClass('wikitable')
:tag('tr')
:tag('th')
:css('width', '50%')
:wikitext('输入')
:done()
:tag('th')
:css('width', '50%')
:wikitext('输出')
:done()
:done()
:tag('tr')
:tag('td')
:css({
['vertical-align'] = 'middle',
width = '50%'
})
:tag('pre')
:css('white-space', 'pre-wrap')
:wikitext(input)
:done()
:done()
:tag('td')
:css({
['vertical-align'] = 'middle',
width = '50%'
})
:wikitext(output)
:done()
:done()
return tostring(tableHtml)
end
return p