Module:Glossary

    From Marovi AI
    Revision as of 23:37, 26 April 2026 by DeployBot (talk | contribs) ([deploy-bot] Deploy Glossary Lua module (v1.3.0))
    (diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

    Documentation for this module may be created at Module:Glossary/doc

    local p = {}
    local data = mw.loadData("Module:Glossary/data")
    
    local function normalize(term)
        if not term then return "" end
        return mw.ustring.lower(mw.text.trim(term))
    end
    
    local function findEntry(term)
        local key = normalize(term)
        if data[key] then
            return data[key], key
        end
        for entryKey, entry in pairs(data) do
            if entry.aliases then
                for _, alias in ipairs(entry.aliases) do
                    if normalize(alias) == key then
                        return entry, entryKey
                    end
                end
            end
        end
        return nil, nil
    end
    
    local function detectLanguage(frame)
        local title = mw.title.getCurrentTitle()
        local subpage = title.subpageText
        if subpage == "es" or subpage == "zh" or subpage == "fr" or subpage == "pt" then
            return subpage
        end
        return "en"
    end
    
    local function getDefinition(entry, lang)
        if lang ~= "en" and entry[lang] then
            return entry[lang]
        end
        return entry.short
    end
    
    function p.term(frame)
        local args = frame:getParent().args
        local termKey = args[1] or ""
        local displayText = args[2] or termKey
    
        local entry, _ = findEntry(termKey)
    
        if not entry then
            return '<span class="marovi-term">' .. displayText .. '</span>'
        end
    
        local lang = detectLanguage(frame)
        local definition = getDefinition(entry, lang)
        local articleLink = entry.article or ""
    
        local attrs = {
            'class="marovi-term"',
            'data-term="' .. mw.text.nowiki(termKey) .. '"',
            'data-definition="' .. mw.text.nowiki(definition) .. '"',
        }
        if articleLink ~= "" then
            table.insert(attrs, 'data-article="' .. mw.text.nowiki(articleLink) .. '"')
        end
    
        return '<span ' .. table.concat(attrs, ' ') .. '>' .. displayText .. '</span>'
    end
    
    function p.definition(frame)
        local args = frame:getParent().args
        local termKey = args[1] or ""
        local lang = args["lang"] or detectLanguage(frame)
    
        local entry, _ = findEntry(termKey)
        if not entry then
            return ""
        end
    
        return getDefinition(entry, lang)
    end
    
    return p