Actions

Module

format: Difference between revisions

From SUALEX

Created page with "-- Module:format local utilities = require("Module:utilities") local languages = require("Module:languages") local parameters = require("Module:parameters") local lexicon = require("Module:lexicon") local format = {} -- make a plain term link 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..."
 
No edit summary
Line 14: Line 14:


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



Revision as of 17:54, 29 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 = {}

-- make a plain term link
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

-- unified sup renderer
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

-- render homonym as sup (immediately after the term link)
function format.render_homonym_sup(homonym_text)
    if homonym_text then
        return format.sup(homonym_text, false)
    end
    return ""
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)
    local entry = lexicon.get_entry_by_term(lang_code, item, homonym)

    -- build display label (do not inject trailing_sup into it)
    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 selection logic (default: en)
    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
        -- item-level sup is visual only; render after gloss block
        trailing_sup_html = format.sup(trailing_sup, true)
    end

    -- homonym_html should appear immediately after link; trailing_sup_html appears after gloss block
    return '<span class="value-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(code)
    local canon = languages.get_canonical_name(code)
    return "[[" .. canon .. " lexicon|" .. canon .. "]]"
end

return format