Actions

Module

format: Difference between revisions

From SUALEX

No edit summary
Undo revision 1348 by Jawad (talk)
Tag: Undo
 
(5 intermediate revisions by the same user not shown)
Line 7: Line 7:
local format = {}
local format = {}


-- make a plain term link
function format.make_term_link(item, homonym, label)
function format.make_term_link(item, homonym, label)
     if not item or item == "" then return "" end
     if not item or item == "" then return "" end
Line 20: Line 19:
end
end


-- unified sup renderer
function format.sup(text, is_uncertainty)
function format.sup(text, is_uncertainty)
     local basic = string.format(parameters.homonym_sup_tag or "<sup>%s</sup>", text)
     local basic = string.format(parameters.homonym_sup_tag or "<sup>%s</sup>", text)
Line 30: Line 28:
end
end


-- render homonym as sup (immediately after the term link)
function format.render_homonym_sup(homonym_text)
function format.render_homonym_sup(homonym_text)
     if homonym_text then
     if homonym_text then
Line 38: Line 35:
end
end


-- Render a term.
-- trailing_sup: item-level sup (string like "?") — will be rendered AFTER the gloss block.
-- gloss_pref: "en" (default) | "es" | "all" | "no"
function format.render_term(lang_code, item, homonym, display_form, _unused_old_param, trailing_sup, gloss_pref)
function format.render_term(lang_code, item, homonym, display_form, _unused_old_param, trailing_sup, gloss_pref)
     local entry = lexicon.get_entry_by_term(lang_code, item, homonym)
     local entry = lexicon.get_entry_by_term(lang_code, item, homonym)


    -- build display label (do not inject trailing_sup into it)
     local display = nil
     local display = nil
     if display_form and display_form ~= "" then
     if display_form and display_form ~= "" then
Line 56: Line 49:
     local homonym_html = format.render_homonym_sup(homonym)
     local homonym_html = format.render_homonym_sup(homonym)


    -- Gloss selection logic (default: en)
     gloss_pref = gloss_pref or "en"
     gloss_pref = gloss_pref or "en"
     local gloss_text = ""
     local gloss_text = ""
Line 89: Line 81:
     local trailing_sup_html = ""
     local trailing_sup_html = ""
     if trailing_sup and trailing_sup ~= "" then
     if trailing_sup and trailing_sup ~= "" then
        -- item-level sup is visual only; render after gloss block
         trailing_sup_html = format.sup(trailing_sup, true)
         trailing_sup_html = format.sup(trailing_sup, true)
     end
     end


    -- homonym_html should appear immediately after link; trailing_sup_html appears after gloss block
     return '<span class="mention-term">' .. link .. homonym_html .. '</span>' .. gloss_block .. trailing_sup_html
     return '<span class="value-term">' .. link .. homonym_html .. '</span>' .. gloss_block .. trailing_sup_html
end
end


Line 122: Line 112:
end
end


function format.language_name(code)
function format.language_name(lang)
     local canon = languages.get_canonical_name(code)
     if not lang then return "" end
     return "[[" .. canon .. " lexicon|" .. canon .. "]]"
 
    if type(lang) == "string" then
        lang = languages.get_name_and_display(lang)
        if not lang then return "" end
    end
 
     return "[[" .. lang.canonical .. " lexicon|" .. lang.display .. "]]"
end
end


return format
return format

Latest revision as of 00:38, 31 January 2026

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

-- Module:format
local utilities  = require("Module:utilities")
local languages  = require("Module:languages")
local parameters = require("Module:parameters")
local lexicon    = require("Module:lexicon")

local format = {}

function format.make_term_link(item, homonym, label)
    if not item or item == "" then return "" end

    local target = item .. (homonym and "#" .. homonym or "")

    if not label or label == "" or label == item then
        return "[[" .. target .. "|" .. item .. "]]"
    end

    return "[[" .. target .. "|" .. label .. "]]"
end

function format.sup(text, is_uncertainty)
    local basic = string.format(parameters.homonym_sup_tag or "<sup>%s</sup>", text)
    if is_uncertainty then
        return '<span title="uncertain"><b>' .. basic .. '</b></span>'
    else
        return basic
    end
end

function format.render_homonym_sup(homonym_text)
    if homonym_text then
        return format.sup(homonym_text, false)
    end
    return ""
end

function format.render_term(lang_code, item, homonym, display_form, _unused_old_param, trailing_sup, gloss_pref)
    local entry = lexicon.get_entry_by_term(lang_code, item, homonym)

    local display = nil
    if display_form and display_form ~= "" then
        local ok, formatted = pcall(string.format, display_form, "")
        if ok then display = formatted else display = display_form end
    else
    	display = nil
    end

    local link = format.make_term_link(item, homonym, display)
    local homonym_html = format.render_homonym_sup(homonym)

    gloss_pref = gloss_pref or "en"
    local gloss_text = ""
    if gloss_pref ~= "no" and entry then
        if gloss_pref == "en" then
            local en_list = {}
            for _, g in ipairs(entry.gloss_en or {}) do
                table.insert(en_list, format.gloss_text(g.gloss_item, g.sup))
            end
            if #en_list > 0 then gloss_text = utilities.join_strings(en_list, parameters.value_display_separator or "; ") end
        elseif gloss_pref == "es" then
            local es_list = {}
            for _, g in ipairs(entry.gloss_es or {}) do
                table.insert(es_list, format.gloss_text(g.gloss_item, g.sup))
            end
            if #es_list > 0 then gloss_text = utilities.join_strings(es_list, parameters.value_display_separator or "; ") end
        elseif gloss_pref == "all" then
            local parts = {}
            local en_list = {}
            for _, g in ipairs(entry.gloss_en or {}) do table.insert(en_list, format.gloss_text(g.gloss_item, g.sup)) end
            if #en_list > 0 then table.insert(parts, utilities.join_strings(en_list, parameters.value_display_separator or "; ") .. " (English)") end
            local es_list = {}
            for _, g in ipairs(entry.gloss_es or {}) do table.insert(es_list, format.gloss_text(g.gloss_item, g.sup)) end
            if #es_list > 0 then table.insert(parts, utilities.join_strings(es_list, parameters.value_display_separator or "; ") .. " (Spanish)") end
            if #parts > 0 then gloss_text = utilities.join_strings(parts, "; ") end
        end
    end

    local gloss_block = ""
    if gloss_text ~= "" then gloss_block = " '" .. gloss_text .. "'" end

    local trailing_sup_html = ""
    if trailing_sup and trailing_sup ~= "" then
        trailing_sup_html = format.sup(trailing_sup, true)
    end

    return '<span class="mention-term">' .. link .. homonym_html .. '</span>' .. gloss_block .. trailing_sup_html
end

function format.reference(author, year, page, noparens)
    author = author or ""
    year = year or ""
    page = page or ""
    if author ~= "" and year ~= "" then
        local target = "Reference:" .. author .. " " .. year
        local label = author .. " (" .. year .. (page ~= "" and ":" .. page or "") .. ")"
        local ref = "[[" .. target .. "|" .. label .. "]]"
        if noparens then ref = ref:gsub("%((.-)%)", "%1") end
        return ref
    else
        local inline = author .. (year ~= "" and " " .. year or "") .. (page ~= "" and ":" .. page or "")
        return noparens and inline or "(" .. inline .. ")"
    end
end

function format.gloss_text(gloss_item, sup_text)
    local unc = sup_text and format.sup(sup_text, true) or ""
    return (gloss_item or "") .. unc
end

function format.wikitable_from_rows(rows)
    return table.concat(rows, "\n")
end

function format.language_name(lang)
    if not lang then return "" end

    if type(lang) == "string" then
        lang = languages.get_name_and_display(lang)
        if not lang then return "" end
    end

    return "[[" .. lang.canonical .. " lexicon|" .. lang.display .. "]]"
end

return format