Revision 22368 of "Модул:headword" on mkwiktionary

local languages = mw.loadData("Module:languages")

local export = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
    local args = frame:getParent().args
    SUBPAGENAME = mw.title.getCurrentTitle().subpageText
    NAMESPACE = mw.title.getCurrentTitle().nsText
    local ret = ""
    local categories = {}
    
    -- Language code
    local lang = args[1] or (NAMESPACE == "Template" and "und")  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 sc = args["sc"] or ""; if sc == "" then sc = langinfo.scripts[1] end
    
    -- Headword
    local head = args["head"] or ""
    
    if head == "" then
        if NAMESPACE == "Appendix" and langinfo.type ~= "appendix-constructed" then
            head = "*" .. SUBPAGENAME
        else
            head = SUBPAGENAME
        end
    end
    
    -- Call the script code template
    ret = ret .. frame:expandTemplate{ title = sc, args = { head, lang = lang, face = "head" } }
    
    -- 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
    
    -- Transliteration
    local tr = args["tr"] or ""
    
    if tr ~= "" then
        ret = ret .. " (" .. tr .. ")"
    end
    
    -- Gender and number
    -- Iterate over all gn parameters (g2, g3 and so on) until one is empty
    local genders = {}
    local g = args["g"] or ""
    local i = 2
    
    while g ~= "" do
        if mw.ustring.find(g, "?", nil, true) then
            table.insert(categories, langinfo.names[1] .. " terms with incomplete gender")
        end
        
        table.insert(genders, g)
        g = args["g" .. i] or ""
        i = i + 1
    end
    
    if genders then
        local gen = require("Module:gender and number")
        ret = ret .. " " .. gen.format_list(genders)
    end
    
    -- Gather inflected forms
    local forms = {}
    
    local i = 3
    local label = args[i] or ""
    local term = args[i+1] or ""
    
    while label ~= "" do
        local form = "''" .. label .. "''"
        
        if term ~= "" then
            form = form .. " " .. frame:expandTemplate{ title = "head/link", args = { term, lang = lang, sc = sc } }
        end
        
        if label == "or" then
            forms[#forms] = forms[#forms] .. " " .. form
        else
            table.insert(forms, form)
        end
        
        i = i + 2
        label = args[i] or ""
        term = args[i+1] or ""
    end
    
    -- Display the forms
    if #forms > 0 then
        ret = ret .. " (" .. table.concat(forms, ", ") .. ")"
    end
    
    -- Add the categories with the appropriate sort key
    if NAMESPACE == "" or NAMESPACE == "Appendix" then
        local sort_key = args["sort"]; if sort_key == "" then sort_key = SUBPAGENAME end
        
        -- Generate a default sort key
        -- Remove initial - from suffixes
        if mw.ustring.find(SUBPAGENAME, "^[-־]") then
            sort_key = sort_key or mw.ustring.gsub(SUBPAGENAME, "^[-־]", "")
        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