Module:User:ZxxZxxZ/headword

This is a private module sandbox of ZxxZxxZ, for his own experimentation. Items in this module may be added and removed at ZxxZxxZ's discretion; do not rely on this module's stability.


local languages = mw.loadData("Module:languages")
local utilities = require("Module:useful stuff")
local links = require("Module:links")

local export = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.main(frame)
    local args = frame:getParent().args
    PAGENAME = mw.title.getCurrentTitle().text
    NAMESPACE = mw.title.getCurrentTitle().nsText
    local ret = ""
    
    -- Language code
    local lang = args[1] or error("Language code has not been specified. Please pass parameter 1 to the module invocation.")
    local langinfo = languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    
    local alt = args["head"]; if alt == "" then alt = nil end
    
    local translit = args["tr"]; if translit == "" then translit = nil end
    if not translit then
        translit = utilities.translit(lang, alt or PAGENAME)
    end
 
    local g1 = args["g"]
    if not g1 or g1 == "" then
        g1 = args["g1"]
        if g1 == "" then g1 = nil end
    end
    local g2 = args["g2"]; if g2 == "" then g2 = nil end
    local g3 = args["g3"]; if g3 == "" then g3 = nil end
 
    local script = args["sc"]; if script == "" then script = nil end 
    if not script then
        script = languages[lang].scripts[1]
    end
    
    local class = "headword"
    if script then
        class = class .. " " .. script
    end

    ret = utilities.annotate(head or PAGENAME, lang, "strong", class, translit, nil, {g1, g2, g3}, frame)

    for i = 3, 22, 2 do
        if args[i] then
            if i ~= 3 then
                if args[i] ~= "or" then
                    ret = ret .. ","
                end
                
                ret = ret .. " "
            end
            
            ret = ret .. "''" .. args[i] .. "''"
            
            if args[i+1] then
                ret = ret .. " " .. links.annotated_link(args[i+1], nil, lang, "strong", script)
            end
        end
    end
    
    -- Categories
    if NAMESPACE == "" then
        local categories = {}
        
        local pos = args[2]; if pos == "" then pos = nil end
        if 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
        
        local cat2 = args["cat2"]; if cat2 == "" then cat2 = nil end
        if cat2 then
            table.insert(categories, langinfo.names[1] .. " " .. cat2)
        end
        
        local cat3 = args["cat3"]; if cat3 == "" then cat3 = nil end
        if cat3 then
            table.insert(categories, langinfo.names[1] .. " " .. cat3)
        end
        
        -- Add the categories with the appropriate sort key
        local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
        for key, cat in ipairs(categories) do
            ret = ret .. "[[Category:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
        end
    end
    
    return ret
end

return export