模块:Nodeclasses

来自决策链云智库

可在模块:Nodeclasses/doc创建此模块的帮助文档

local mArguments --initialize lazily
local checkType = require('libraryUtil').checkType
local p = {}

--- Return matched nodeclasses from string
--- @param string s Match string
--- @return table Nodeclasses
local function matchNodeclasses( s )
    local data = mw.loadJsonData( 'Module:Nodeclasses/data.json' )
    for i, nodeclasses in ipairs( data ) do
        for j, value in pairs( nodeclasses ) do
            if string.match( string.lower( value ),  '^' .. string.lower( s ) .. '$' ) then
                return nodeclasses
            end
        end
    end
    return nil
end

function p.nodeclasses( frame )
    mArguments = require( 'Module:Arguments' )
    local args = mArguments.getArgs( frame )
    local s = args[1]
    -- Default to name key
    local type = args['type'] or 'name'
    if not s then
        return '<span class="error">No text specified.</span>'
    end
    return p._nodeclasses( s, type )
end

function p._nodeclasses( s, type )
    -- Return nil for Lua
    if s == nil then return end
    -- Return table for Lua
    if type == nil then
        type = 'table'
    end

    local nodeclasses = matchNodeclasses( s )
    -- Used for other Lua modules
    if type == 'table' then
        return nodeclasses
        -- Return error message
    elseif nodeclasses == nil or nodeclasses[type] == nil then
        return '<span class="error">No ' .. type .. ' found for ' .. s .. '.</span>'
        -- Return wiki page name
    elseif type == 'page' then
        return nodeclasses['page'] or nodeclasses['name']
        -- Return string from matched nodeclasses
    else
        return nodeclasses[type]
    end
end

return p