Module:head: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local export = {}
local export = {}


function export.show(frame)
local m_links = require("Module:links")
local function make_head(head)
local m_gender_and_number = require("Module:gender and number")
if not string.find(head, " ") then
local m_formatting = require("Module:formatting")
return head
 
end
local function format_headword(data)
local head = data.term
if string.find(head, " ") then
local pieces = {}
local pieces = {}
for piece in string.gmatch(head, "%S+") do
for piece in string.gmatch(head, "%S+") do
table.insert(pieces, "[[" .. piece .. "]]")
table.insert(pieces, "[[" .. piece .. "]]")
end
end
return table.concat(pieces, " ")
head = table.concat(pieces, " ")
end
local cats = "[[Category:" .. data.language.name .. " lemmas]]"
cats = cats .. "[[Category:" .. data.language.name .. " " .. data.pos .. "s]]"
return m_formatting.wrap_face(head, data.language, "head"), cats
end
 
local function format_genders(data)
if (not data.genders) or (type(data.genders) ~= "table") then return nil end
return m_gender_and_number.format_genders(data.language, data.pos, data.genders)
end
 
local function format_inflections(data)
if not (data.forms or data.labels) then return nil end
local out = {}
for _, inflection in pairs(data.inflections) do
if not inflection.label then error("headword inflection must have a label") end
local i_forms = {}
local i = 1
while inflection[i] do
table.insert(i_forms, m_links.make_link({term = inflection[i], language = data.language}, "bold"))
i = i + 1
end
if #i_forms > 0 then
table.insert(out, "''" .. inflection.label .. "'' " .. table.concat(i_forms, ", "))
end
end
end
if #out < 1 then return nil end
local params = {
return "(" .. table.concat(out, ", ") .. ")"
[1] = {required = "true"},
end
[2] = {required = "true"},
 
["head"] = {default = make_head(mw.title.getCurrentTitle().text)},
function export.full_head(data)
["nocat"] = {type = "boolean"}
local out = {}
}
local cat = ""
local args = require("Module:parameters").process(frame:getParent().args, params)
local head_out, head_cat = format_headword(data)
local language = require("Module:languages").get_by_code(args[1])
if head_out then table.insert(out, head_out) end
return "'''" .. args["head"] .. "'''" .. ((args["nocat"] and "") or "[[Category:" .. language.name .. " " .. args[2] .. "s]][[Category:" .. language.name .. " lemmas]]")
if head_cat and head_cat ~= "" then cat = cat .. head_cat end
local genders_out, genders_cat = format_genders(data)
if genders then table.insert(out, genders) end
if genders_cat and genders_cat ~= "" then cat = cat .. genders_cat end
local inflections = format_inflections(data)
if inflections then table.insert(out, inflections) end
return table.concat(out, " ") .. table.concat(cat, " ")
end
end


return export
return export

Revision as of 16:09, 6 August 2023

Documentation for this module may be created at Module:head/documentation

local export = {}

local m_links = require("Module:links")
local m_gender_and_number = require("Module:gender and number")
local m_formatting = require("Module:formatting")

local function format_headword(data)
	local head = data.term
	if string.find(head, " ") then
		local pieces = {}
		for piece in string.gmatch(head, "%S+") do
			table.insert(pieces, "[[" .. piece .. "]]")
		end
		head = table.concat(pieces, " ")
	end
	local cats = "[[Category:" .. data.language.name .. " lemmas]]"
	cats = cats .. "[[Category:" .. data.language.name .. " " .. data.pos .. "s]]"
	return m_formatting.wrap_face(head, data.language, "head"), cats
end

local function format_genders(data)
	if (not data.genders) or (type(data.genders) ~= "table") then return nil end
	return m_gender_and_number.format_genders(data.language, data.pos, data.genders)
end

local function format_inflections(data)
	if not (data.forms or data.labels) then return nil end
	local out = {}
	for _, inflection in pairs(data.inflections) do
		if not inflection.label then error("headword inflection must have a label") end
		local i_forms = {}
		local i = 1
		while inflection[i] do
			table.insert(i_forms, m_links.make_link({term = inflection[i], language = data.language}, "bold"))
			i = i + 1
		end
		if #i_forms > 0 then
			table.insert(out, "''" .. inflection.label .. "'' " .. table.concat(i_forms, ", "))
		end
	end
	if #out < 1 then return nil end
	return "(" .. table.concat(out, ", ") .. ")"
end

function export.full_head(data)
	local out = {}
	local cat = ""
	local head_out, head_cat = format_headword(data)
	if head_out then table.insert(out, head_out) end
	if head_cat and head_cat ~= "" then cat = cat .. head_cat end
	local genders_out, genders_cat = format_genders(data)
	if genders then table.insert(out, genders) end
	if genders_cat and genders_cat ~= "" then cat = cat .. genders_cat end
	local inflections = format_inflections(data)
	if inflections then table.insert(out, inflections) end
	return table.concat(out, " ") .. table.concat(cat, " ")
end

return export