format: Difference between revisions
From SUALEX
No edit summary |
Tag: Undo |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 113: | Line 113: | ||
function format.language_name(lang) | function format.language_name(lang) | ||
if not lang then return "" end | |||
if type(lang) == "string" then | if type(lang) == "string" then | ||
lang = languages.get_name_and_display(lang) | lang = languages.get_name_and_display(lang) | ||
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