Jump to content

Module:Pt-noun

Frae Wikipedia, the free beuk o knawledge
-- FIXME: Needs to be converted to use the standard [[Module:headword]] module

local m_headword = require("Module:headword")
local m_gen = {
	codes = {
		["?"] = {type = "other", display = '<abbr title="gender incomplete">?</abbr>'},
		["m"] = {type = "gender", display = '<abbr title="masculine gender">m</abbr>'},
		["f"] = {type = "gender", display = '<abbr title="feminine gender">f</abbr>'},
		["p"] = {type = "number", display = '<abbr title="plural number">pl</abbr>'},
	}
}

function splitter(s, c)
	if (s == nil or c == nil) then
		return nil
	end
	local t = {}
	local cpos = mw.ustring.find(s, c)
	while (cpos ~= nil) do
		table.insert(t, mw.ustring.sub(s, 1, cpos-1))
		s = mw.ustring.sub(s, cpos+1)
		cpos = mw.ustring.find(s, c)
	end
	table.insert(t, s)
	return t
end

function m_gen.format_list(list)
	local s = ""
	list = splitter(list[1], "-")
	
	for n, g in ipairs(list) do
		if (n > 1) then
			s = s .. " "
		end
		s = s .. m_gen.codes[g].display
		
	end
	s = "<span class=\"gender\">" .. s .. "</span>"
	return s
end
local m_plural = require("Module:pt-plural")
local lang = require("Module:languages").getByCode("pt")


local export = {}
local cats = {}
local PAGENAME, NAMESPACE



function export.show(frame)
    local args = frame:getParent().args
    PAGENAME = mw.title.getCurrentTitle().text
    NAMESPACE = mw.title.getCurrentTitle().nsText

    -- for compatibility with old pt-noun
    if (args[2] == "s") then args[2] = PAGENAME .. "s"
    elseif (args[2] == "es") then args[2] = PAGENAME .. "es" end

    local is_plural = get_is_plural(args[1], args["g2"])

    local items = {}
    table.insert(items, get_countability(args, is_plural))
    table.insert(items, get_lemma_plurals(args, is_plural))
    table.insert(items, get_feminine(args["f"], args["qual_f"], is_plural))
    table.insert(items, get_feminine_plural(args["f"], args["fpl"], args["qual_fpl"], args[2] == "-", is_plural))
    table.insert(items, get_feminine(args["f2"], args["qual_f2"], is_plural))
    table.insert(items, get_feminine_plural(args["f2"], args["fpl2"], args["qual_fpl2"], args[2] == "-", is_plural))


    return
        get_headword(args["head"]) ..
        get_genders(args[1], args["g2"], args["qual_g1"], args["qual_g2"]) ..
        get_inflection(items) ..
        get_categories()
end






-- Returns the headword. If the pagename contains spaces or hyphens, its
-- constituents are wikified.
function get_headword(head)
    if (head == nil) then
        head = PAGENAME
        local has_head_links = false

        if head:find(" ", nil, false) then
            head = mw.text.split(head, " ", true)
            head = table.concat(head, "]] [[")
            has_head_links = true
        end

        if (head:find("-", nil, false)) then
            head = mw.text.split(head, "-", true)
            head = table.concat(head, "]]-[[")
            has_head_links = true
        end

        if (has_head_links == true) then
            head = "[[" .. head .. "]]"
        end
    end
	return m_headword.full_headword({lang = lang, pos_category = "nouns", heads = {head}, categories = {}})
end




-- Returns the text containing the gender information.
-- If no gender is provided, or if the gender is '?', the entry is added to 
--     [[Category:Portuguese nouns lacking gender]] and a request is returned.
-- If two genders are provided, the entry is added to [[Category:Portuguese
--     nouns with varying gender]].
-- If two genders are provided, but there are no qualifier for either, the
--     string (in variation) is added after the second, per [[WT:T:APT]].
function get_genders(g1, g2, g1q, g2q)

    if (g1 == "" or g1 == "?" or g1 == nil) then
        table.insert(cats, "terms with incomplete gender")
        return " " .. please_add("gender")
    end
    
    if (g1 == "morf") then
        g1 = "m"
        g2 = "f"
    elseif (g1 == "mf") then
        g1 = "m-f"
    end

    if (g2 == "mf") then
        g2 = "m-f"
    end

    local text = " " .. m_gen.format_list({g1}) .. qualifier(g1q)
    if (g2 ~= "" and g2 ~= nil) then
        text = text .. " or " .. m_gen.format_list({g2}) .. qualifier(g2q)
        table.insert(cats, "nouns with varying gender")
        if (g2q == nil and g1q == nil) then
            text = text .. qualifier("in variation")
        end
    end

    return text
end


-- Returns a boolean indicating whether the noun is a plurale tantum.
-- If true, it also adds the entry to [[Category:Portuguese pluralia tantum]].
function get_is_plural(g1, g2)
    g1 = g1 or ""
    g2 = g2 or ""
    if (mw.ustring.find(g1, "p") ~= nil or mw.ustring.find(g2, "p") ~= nil) then
        table.insert(cats, "pluralia tantum")
        return true
    end
    return false
end


-- Returns text indicating a special status with regards to countability. These
-- are plurale tantum, uncountable and usually uncountable.
function get_countability(args, is_plural)
    if (is_plural == true) then return "''plurale tantum''" end
    if (args[2] == "-")  then return "''uncountable''" end
    if (args["unc"] ~= nil)  then return "''usually uncountable''" end
    return nil
end


-- Returns the text with the description, link and qualifier of a plural 
-- (i.e. "feminine plural of [[example]] (qualifier)"). If the plural is not
-- present as a parameter, [[Module:pt-plural]] is used to automatically figure
-- it out from the lemma. If that is impossible, a request is returned and the
-- entry is added to [[Category:Portuguese nouns needing inflection]].
function get_primary_plural(lemma, pl, description, class, qualifier)
    if (pl == "" or pl == nil) then
        pl = m_plural.get_plural(lemma)
    end

    if (pl == "" or pl == nil) then
        table.insert(cats, "nouns needing inflection")
        return please_add(description) end
    return merge(description, class, pl, qualifier)
end


-- Returns the text with the desciption (always "or"), link and qualifier of an
-- alternative plural. If none is provided, nil is returned.
function get_secondary_plural(pl, class, qualifier)
    if (pl ~= nil and pl ~= "") then
        return merge(" or", class, pl, qualifier)
    end
    return ""
end

-- Puts together the text of the lemma's primary and two secondary plurals.
function get_lemma_plurals(args, is_plural)
    if (is_plural == true) then
        return nil
    end
    
    local class = "plural-form-of"
    local pl = args[2];
   
    if (pl == nil or pl == "") then
        pl = args["pl"]
    end

    if (pl == "?") then
        table.insert(cats, "nouns needing inflection")
        return please_add("plural")
    elseif (pl ~= "-") then
        local text = get_primary_plural(PAGENAME, pl, "plural", class, args["qual_pl"])
        text = text .. get_secondary_plural(args["pl2"], class, args["qual_pl2"])
        text = text .. get_secondary_plural(args["pl3"], class, args["qual_pl3"])
        return text
    end
    return nil
end


function get_feminine(f, qualifier, is_plural)
    if (f == "" or f == nil) then
        return nil
    end
    
    
    local class = "feminine-form-of gender-f"
    if is_plural then
        class = class .. "pl"
    end
    return merge("feminine", class, f, qualifier)
end


function get_feminine_plural(f, fpl, qualifier, is_unc, is_plural)
    if (is_plural == true or is_unc == true or f == nil or f == "") then
        return nil
    end
    local class = "feminine-form-of gender-fpl"
    return get_primary_plural(f, fpl, "feminine plural", class, qualifier)
end


-- Returns the parenthetical part of the headword line (plurals and feminines).
function get_inflection(items)
    if (table.getn(items) == 0) then return "" end
    local text = " ("
    for c = 1, table.getn(items) do
        if (c > 1) then text = text .. ", " end
        text = text .. items[c]
    end
    return text .. ")"
end


-- Returns the text containing the categories that the entry will be added to.
function get_categories()
    if (NAMESPACE ~= "") then
        return ""
    end
    local text = ""
    for c = 1, table.getn(cats) do
        text = text .. "[[Category:Wt/sco/Portuguese " .. cats[c] .. "]]"
    end
    return text;
end







-- Nerges the form description (e.g. “plural”, “feminine”), word, its class
-- (e.g. “plural-form-of gender-mpl”) and qualifier if any.
-- FIXME: Needs better parameter names.
function merge(f, c, w, q)
    if (w == nil or w == "") then return "" end
    text = ""
    text = text .. "''" .. f .. "'' "
    text = text .. '<span class="form-of ' .. c .. ' lang-pt" lang="pt">'
    text = text .. make_link(w) .. "</span>"
    if (q ~= nil and q ~= "") then text = text .. qualifier(q) end
    return text
end




-- Returns a piece of text boldened and wikified (unless it is the same as the
-- pagename).
-- FIXME: Convert to use [[Module:links]]
function make_link(text)
    if (PAGENAME == text) then return "'''" .. text .. "'''" end
    return  "'''[[" .. text .. "#Portuguese|" .. text .. "]]'''"
end


-- Returns a text with a request for lacking information.
function please_add(text)
    --table.insert(cats, "terms needing attention")
    return "<sup><small><span style='color:#AAAAAA;'>please add " .. text .. "</span></small></sup>"
end

function qualifier(text)
    if (text == nil or text == "") then return "" end
    return '&nbsp;<span class="ib-brac"><span class="qualifier-brac">(</span></span><span class="ib-content"><span class="qualifier-content">' .. text ..
'</span></span><span class="ib-brac"><span class="qualifier-brac">)</span></span>'
end



return export