Mô đun:fa-headword

Từ điển mở Wiktionary
local lang = require("Module:languages").getByCode("fa")

local export = {}
local pos_functions = {}

local u = mw.ustring.char

local A = u(0x064E) -- fatḥa
local AN = u(0x064B) -- fatḥatān (fatḥa tanwīn)
local U = u(0x064F) -- ḍamma
local I = u(0x0650) -- kasra
local SK = u(0x0652) -- sukūn = no vowel
local SH = u(0x0651) -- šadda = gemination of consonants

-----------------------
-- Utility functions --
-----------------------

-- If Not Empty
local function ine(arg)
    if arg == "" then
        return nil
    else
        return arg
    end
end

-- version of mw.ustring.gsub() that discards all but the first return value
function rsub(term, foo, bar)
    local retval = mw.ustring.gsub(term, foo, bar)
    return retval
end

local rfind = mw.ustring.find

-- Tracking functions

local trackfn = require("Module:debug").track

function track(page)
    trackfn("fa-headword/" .. page)
    return true
end

function remove_links(text)
    text = rsub(text, "%[%[[^|%]]*|", "")
    text = rsub(text, "%[%[", "")
    text = rsub(text, "%]%]", "")
    return text
end

local function make_unused_key_tracker(t)
    local unused_keys = require"Module:table".listToSet(
                            require"Module:table".keysToList(t))
    local mt = {
        __index = function(self, key)
            if key ~= nil then unused_keys[key] = nil end
            return t[key]
        end,
        __newindex = function(self, key, value) t[key] = value end
    }
    local proxy_table = setmetatable({}, mt)
    return proxy_table, unused_keys
end

function export.ZWNJ(word)
    if mw.ustring.match(word,
                        "[بپتثجچحخسشصضطظعغفقکگلمنهی]",
                        -1) then
        return "\226\128\140" -- U+200C ZERO WIDTH NON-JOINER
    end
    return "" -- empty string
end

-- The main entry point.
function export.show(frame)

    local PAGENAME = mw.title.getCurrentTitle().text

    local poscat = frame.args[1] or error(
                       "Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

    local params = {
        [1] = {list = "head", allow_holes = true, default = ""},
        ["head"] = {default = ""},
        ["tr"] = {list = true, allow_holes = true}
    }

    local args, unused_keys = make_unused_key_tracker(frame:getParent().args)

    -- Gather parameters
    local data = {
        lang = lang,
        pos_category = poscat,
        categories = {},
        heads = {},
        translits = {},
        inflections = {}
    }

	local saw_head = false
	local head = ine(args[1]) or ine(args["head"])
	if head then
		saw_head = true
	else
		head = PAGENAME
	end
	local translit = ine(args["tr"])
	local i = 1

	while head do
		table.insert(data.heads, head)
		data.translits[#data.heads] = translit

		i = i + 1
		head = ine(args["head" .. i])
		if head then
			saw_head = true
		end
		translit = ine(args["tr" .. i])
	end
	data.no_redundant_head_cat = not saw_head

    if pos_functions[poscat] then pos_functions[poscat].func(args, data) end

    local unused_key_list = require"Module:table".keysToList(unused_keys)
    if #unused_key_list > 0 then
        local unused_key_string = require "Module:array"(unused_key_list):map(
                                      function(key)
                return "|" .. key .. "=" .. args[key]
            end):concat("\n")
        error("Unused arguments: " .. unused_key_string)
    end

    return require("Module:headword").full_headword(data)
end

-- Get a list of inflections. See handle_infl() for meaning of ARGS, ARGPREF
local function getargs(args, argpref)
    -- Gather parameters
    local forms = {}

    local form = ine(args[argpref])

    local translit = ine(args[argpref .. "tr"])
    local i = 1

    while form do

        table.insert(forms, {term = form, translit = translit})

        i = i + 1
        form = ine(args[argpref .. i])
        translit = ine(args[argpref .. i .. "tr"])
    end

    return forms
end

local function handle_infl(args, data, argpref, label)
    local newinfls = getargs(args, argpref)
    newinfls.label = label

    if #newinfls > 0 then table.insert(data.inflections, newinfls) end
end

local function handle_all_infl(args, data, argpref, label, nobase)
    if not nobase and argpref ~= "" then
        handle_infl(args, data, argpref, label)
    end
end

pos_functions["Động từ"] = {
    func = function(args, data)
        data.pos_category = "Động từ"
        handle_all_infl(args, data, "prstem", "thân hiện tại")
    end
}

pos_functions["Tính từ"] = {
    func = function(args, data)
        data.pos_category = "Tính từ"
        if ine(args["c"]) and args["c"] == "+" then
            local word = data.heads[1]
            local word_tr = args["tr"]

            table.insert(data.inflections, {
                label = "so sánh hơn",
                accel = {form = "so sánh hơn", translit = word_tr .. "-tar"},
                {
                    term = word .. export.ZWNJ(word) .. "ت" .. A .. "ر",
                    translit = word_tr .. "-tar"
                }
            })

            table.insert(data.inflections, {
                label = "so sánh nhất",
                accel = {form = "so sánh nhất", translit = word_tr .. "-tarin"},
                {
                    term = word .. export.ZWNJ(word) .. "ت" .. A .. "رین",
                    translit = word_tr .. "-tarin"
                }
            })
        end
    end
}
return export