Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Lexeme

From Wikipedia, the free encyclopedia

local p = {}

local function anchor(id)
    return string.format('<span class="anchor" id="%s"></span>', id)
end

-- Returns the lemmas of the entity concatenated with '/'.
local function getLemma(entity) -- simple for simple templates like {{Q|}}}
    if not entity then
        return 'invalid-id'
    end

    local lemmas = ''
    local function valuesSortedByLanguage(t)
        local values = {}
        for k, v in pairs(t) do
            values[#values + 1] = v
        end
        table.sort(values,
            function(a, b)
                return (a.language < b.language)
            end)
        local i = 0
        return function()
            i = i + 1
            if values[i] then
                return values[i]
            end
        end
    end
    if entity.lemmas then
        for v in valuesSortedByLanguage(entity.lemmas) do
            if lemmas ~= '' then
                lemmas = lemmas .. '/' .. '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
            else
                lemmas = '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
            end
        end
    end
    return lemmas
end

local function lemma_value(id)
    -- return mw.getCurrentFrame():expandTemplate { title = "lemma", args = { id } }
    return getLemma(mw.wikibase.getEntity(id))
end

local function getSenses(lexemeId, Sid, senLang)
    local gloss_text = ''
    local entity = mw.wikibase.getEntity(lexemeId)
    for _, sense in pairs(entity:getSenses()) do
        local lexemeId, subId = mw.wikibase.lexeme.splitLexemeId(sense.id)
        if lexemeId == lexemeId and subId == Sid then
            -- "glosses": { "en": { "language": "en", "value": "human between the stages of birth and puberty" } }
            -- glosses = sense:getGlosses()
            if senLang then
                gloss_text = sense.glosses[senLang].value
            else
                for j, gloss in pairs(sense.glosses) do
                    gloss_text = gloss.value
                    break
                end
            end
            break
        end
    end
    return gloss_text
end

local function getFormRepFromEntity(entity)
    local function valuesSortedByLanguage(t)
        local values = {}
        for k, v in pairs(t) do
            values[#values + 1] = v
        end
        table.sort(values,
            function(a, b)
                return (a[2] < b[2])
            end)
        local i = 0
        return function()
            i = i + 1
            if values[i] then
                return values[i]
            end
        end
    end

    local lemmas = ''
    for v in valuesSortedByLanguage(entity:getRepresentations()) do
        if lemmas ~= '' then
            lemmas = lemmas .. '/' .. v[1]
        else
            lemmas = v[1]
        end
    end
    return lemmas
end

local function getLinkAndDisplay(id, showSen, senLang)
    local lexemeId, subId = mw.wikibase.lexeme.splitLexemeId(id)
    if not subId then
        -- only Lexeme
        return "d:Lexeme:" .. id, lemma_value(id)
    end
    local link = "d:Lexeme:" .. lexemeId .. "#" .. subId
    local display
    if subId:sub(1, 1) == "F" then
        -- display = frame:callParserFunction { name = "#invoke", args = { "Lexeme", "getFormRep", id } }
        display = getFormRepFromEntity(mw.wikibase.getEntity(id))
    elseif subId:sub(1, 1) == "S" and showSen then
        display = getSenses(lexemeId, subId, senLang)
    else
        display = lemma_value(lexemeId)
    end
    return link, display
end

local function getDisplay2(id, show_l, short)
    if show_l == "no" then return nil end
    if short == "1" then
        local _, subId = mw.wikibase.lexeme.splitLexemeId(id)
        if subId then
            return subId --  part F or S
        end
    end
    return id
end

function p._main(args, frame)
    local id = mw.text.trim(args[1] or "")
    if id == "" then return "" end

    -- if id start with numbers
    if id:sub(1, 1):match("%d") then
        id = "L" .. id
    end
    local showSen = args["s"]
    local senLang = args["lang"]
    local link, display = getLinkAndDisplay(id, showSen, senLang)
    local display2 = getDisplay2(id, args["show_l"], args["short"])

    local anchorValue = (args["anchor"] and args["anchor"] ~= "") and anchor("Lexeme:" .. id) or ""
    local fullDisplay = display .. (display2 and "&#32;<small>(" .. display2 .. ")</small>" or "")

    return string.format('%s[[%s|%s]]', anchorValue, link, fullDisplay)
end

function p.main(frame)
    return p._main(frame:getParent().args, frame)
end

function p.test(frame)
    return p._main(frame.args, frame)
end

function p.getSenses(frame)
    return getSenses(frame.args[1], frame.args[2])
end

return p