Modul:headword
Dokumentasjon for denne modulen kan opprettes på Modul:headword/dok
local m_languages = mw.loadData("Module:languages/alldata")
local export = {}
-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
local m_utilities = require("Module:utilities")
local args = frame:getParent().args
PAGENAME = mw.title.getCurrentTitle().text
SUBPAGENAME = mw.title.getCurrentTitle().subpageText
NAMESPACE = mw.title.getCurrentTitle().nsText
-- Get language and script information
local lang = args[1] or (NAMESPACE == "Mal" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
local sc = args["sc"] or ""; if sc == "" then sc = nil end
-- Gather basic parameters
local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
local pos = args[2]; if pos == "" then pos = nil end
local cat = args["cat"]; if cat == "" then cat = nil end
local cat2 = args["cat2"]; if cat2 == "" then cat2 = nil end
local cat3 = args["cat3"]; if cat3 == "" then cat3 = nil end
local tr = args["tr"]; if tr == "" then tr = nil end
-- Gather headwords
local heads = {}
local head = args["head"]; if head == "" then head = nil end
local i = 2
while head do
table.insert(heads, head)
head = args["head" .. i]; if head == "" then head = nil end
i = i + 1
end
-- Gather gender and number specifications
-- Iterate over all gn parameters (g2, g3 and so on) until one is empty
local genders = {}
local g = args["g"]; if g == "" then g = nil end
local i = 2
while g do
table.insert(genders, g)
g = args["g" .. i]; if g == "" then g = nil end
i = i + 1
end
-- Gather inflected forms
local inflections = {}
local i = 3
local label = args[i] or ""
local term = args[i+1] or ""
local parts = {}
while label ~= "" do
table.insert(parts, label)
if term ~= "" then
table.insert(parts, term)
end
i = i + 2
label = args[i] or ""
term = args[i+1] or ""
-- If the next label is "or" then continue adding to the current parts list.
-- Otherwise, insert the previous one and create a new one.
if label ~= "or" then
table.insert(inflections, parts)
parts = {}
end
end
table.insert(inflections, part)
-- Get/set categories
local categories = {}
if cat then
table.insert(categories, langinfo.names[1] .. " " .. cat)
elseif pos then
-- Make the plural form of the part of speech
if pos:find("x$") then
pos = pos .. "es"
elseif pos ~= "punctuation" or pos ~= "phrasebook" then
pos = pos .. "s"
end
table.insert(categories, langinfo.names[1] .. " " .. pos)
end
if cat2 then
table.insert(categories, langinfo.names[1] .. " " .. cat2)
end
if cat3 then
table.insert(categories, langinfo.names[1] .. " " .. cat3)
end
return
export.full_headword(lang, sc, heads, tr, genders, inflections, categories, sort_key) ..
m_utilities.format_categories(categories, lang, sort_key)
end
function export.full_headword(lang, sc, heads, tr, genders, inflections, categories, sort_key)
local m_links = require("Module:links")
local m_utilities = require("Module:utilities")
local m_scriptutils = require("Module:script utilities")
local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
local scFix = false
-- Try to detect the script if it was not provided
if not sc then
sc, scFix = m_utilities.detect_script(heads[1] or SUBPAGENAME, lang)
end
-- Try to generate a transliteration if necessary
-- Generate it if the script is not Latn or similar, and if no transliteration was provided
if not tr and not ((sc:find("Latn", nil, true)) or sc == "Latinx" or sc == "unicode") then
tr = m_scriptutils.transliterate(heads[1] and m_links.remove_links(heads[1]) or SUBPAGENAME, lang, sc)
end
-- Format and return all the gathered information
return
export.format_headword(heads, lang, sc) ..
export.format_transliteration(tr, lang) ..
export.format_genders(genders, lang) ..
export.format_inflections(inflections, lang, sc) ..
(scFix and "[[Category:Terms using script detection fallback]][[Category:Terms using script detection fallback/" .. lang .. "]]" or "")
end
-- Format a headword
function export.format_headword(heads, lang, sc)
local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
local m_links = require("Module:links")
local m_scriptutils = require("Module:script utilities")
if type(heads) ~= "table" then
heads = {heads}
end
if #heads == 0 then
NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
if NAMESPACE == "Appendix" and langinfo.type ~= "appendix-constructed" then
heads = {"*" .. SUBPAGENAME}
else
heads = {SUBPAGENAME}
end
end
for i, head in ipairs(heads) do
-- Apply processing to the headword, for formatting links and such
if head:find("[[", nil, true) then
head = m_links.language_link(head, nil, lang)
end
-- Add language and script wrapper
head = m_scriptutils.tag_text(head, lang, sc, "head")
heads[i] = head
end
return table.concat(heads, " ''or'' ")
end
-- Format transliteration
function export.format_transliteration(tr, lang)
if tr then
local ret = " (<span lang=\"\">" .. tr .. "</span>)"
if lang and mw.title.new(m_languages[lang].names[1] .. " transliteration", "Wiktionary").exists then
ret = " [[Wiktionary:" .. m_languages[lang].names[1] .. " transliteration|•]]" .. ret
end
return ret
else
return ""
end
end
function export.format_genders(genders, lang)
if #genders > 0 then
local gen = require("Module:gender and number")
return " " .. gen.format_list(genders, lang)
else
return ""
end
end
-- Format the inflections following the headword
function export.format_inflections(inflections, lang, sc)
FULLPAGENAME = FULLPAGENAME or mw.title.getCurrentTitle().prefixedText
if #inflections > 0 then
local m_links = require("Module:links")
local m_scriptutils = require("Module:script utilities")
-- Format each inflection individually
for key, infl in ipairs(inflections) do
-- If this inflection is a table, it contains parts
-- Consisting of alternating label-term pairs. Format them too.
if type(infl) == "table" then
for key2, part in ipairs(infl) do
-- Odd-numbered keys are labels, even-numbered keys are terms
if key2 % 2 == 1 then
part = "''" .. part .. "''"
else
-- Apply processing to the term, for formatting links and such
-- Then add language and script wrapper
part = m_scriptutils.tag_text(m_links.language_link(part, nil, lang, nil, FULLPAGENAME), lang, sc, "bold")
end
infl[key2] = part
end
inflections[key] = table.concat(infl, " ")
end
end
return " (" .. table.concat(inflections, ", ") .. ")"
else
return ""
end
end
return export