• 欢迎来到THBWiki!如果您是第一次来到这里,请点击右上角注册一个帐户
  • 有任何意见、建议、求助、反馈都可以在 讨论板 提出
  • THBWiki以专业性和准确性为目标,如果你发现了任何确定的错误或疏漏,可在登录后直接进行改正

模块:使用Lua与CSS

来自THBWiki
跳到导航 跳到搜索

本模块可以在模板或模块的源码中,通过字符串模式匹配找出其中使用了哪些其他模块和 CSS,并添加 模板:使用Lua模板:使用CSS

说明:

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