- 欢迎来到THBWiki!如果您是第一次来到这里,请点击右上角注册一个帐户
- 有任何意见、建议、求助、反馈都可以在 讨论板 提出
- THBWiki以专业性和准确性为目标,如果你发现了任何确定的错误或疏漏,可在登录后直接进行改正
模块:使用Lua与CSS
跳到导航
跳到搜索
| 本模块依赖以下其他模块: |
本模块可以在模板或模块的源码中,通过字符串模式匹配找出其中使用了哪些其他模块和 CSS,并添加 模板:使用Lua 和 模板:使用CSS。
说明:
- 本模块通过 MediaWiki:Top-notice-ns-10 自动添加到所有模板的 /doc 子页面上方
- 通过 MediaWiki:Scribunto-doc-page-header 自动添加到所有模块的文档页面上方
- 模块文档名是在 MediaWiki:Scribunto-doc-page-name 中定义的,默认也是 /doc 子页面
- 同时,本模块内置于 模板:Documentation 中
- 后者通过 MediaWiki:Bottom-notice-ns-10 自动添加到所有模板页面下方
- 并通过 MediaWiki:Scribunto-doc-page-show 和 MediaWiki:Scribunto-doc-page-does-not-exist 自动添加到所有模块页面上方
- 所以正常情况下不需要直接使用本模块
local list = require('Module:list')
local p = {}
-- 提取 onlyinclude 的内容, 去掉 noinclude 内容和剩余的 includeonly 标签
function p.transclude(text)
if text:find('<onlyinclude>') then
text = list(
text:gmatch('<onlyinclude>(.-)</onlyinclude>')
):join('')
end
text = text
:gsub('<noinclude>.-</noinclude>', '')
:gsub('</?includeonly>', '')
return text
end
function p.get_lua_modules(title, content)
local t = mw.title.new(title)
content = content or t:getContent() or ''
local l
if t.namespace == 828 then
-- 模块
l = list(content:gmatch('require[%s\'"%[=(]+[^:\'"%]\n]+:([^\'"%]\n]+)'))
else
content = p.transclude(content)
l = list(content:gmatch('{{%s*#invoke:%s*([^|]-)%s*|'))
end
l = l:set() -- 去重
return l
end
function p.get_style_sheets(title, content)
local t = mw.title.new(title)
content = content or t:getContent() or ''
local l
if t.namespace == 828 then
-- 模块
l = list(content:gmatch('["\']templatestyles["\'].-src%s*=%s*["\']([^"\']+)["\']'))
else
content = p.transclude(content)
l = list(content:gmatch('<templatestyles%s*src%s*=%s*"([^"]+)"'))
end
l = l:set() -- 去重
l = l:map(function(page)
local obj = mw.title.new(page, '模板') -- 默认是模板
return obj and obj.fullText or page
end)
return l
end
function p.render(frame)
local t = mw.title.getCurrentTitle()
if t.subpageText == 'doc' then
t = t.basePageTitle
end
local content = t:getContent() or ''
local title = t.fullText
local modules = p.get_lua_modules(title, content)
local styles = p.get_style_sheets(title, content)
rawset(modules, 'title', title)
rawset(styles, 'title', title)
local lua = #modules > 0
and frame:expandTemplate{ title = '使用Lua', args = modules }
or ''
local css = #styles > 0
and frame:expandTemplate{ title = '使用CSS', args = styles }
or ''
return lua .. css
end
return p