Jump to content

Module:he-common

From Wiktionary, the free dictionary

This module holds some common used functions for Hebrew, that are needed by other modules.


local export = {}
local lang = require("Module:languages").getByCode("he")

-- A nil-safe, Hebrew-specific diacritic remover.
function export.remove_nikud(formwv)
	return formwv and (lang:makeEntryName(formwv))
end

local vowels = "ְֱֲֳִֵֶַָֹֻ"
local dagesh = "ּֿ"
local shin_dots = "ׁׂ"
local vowels_regex = "[" .. vowels .. "]"
local dagesh_regex = "[" .. dagesh .. "]"
local shin_dots_regex = "[" .. shin_dots .. "]"
local nikud_regex = "[" .. shin_dots .. dagesh .. vowels .. "]"

local function fix_nikud_subber(x)
    local s = ""
    local d = ""
    local v = ""
    local subber = function(y)
        if mw.ustring.match(y, shin_dots_regex) then
            s = s .. y
        elseif mw.ustring.match(y, dagesh_regex) then
            d = d .. y
        else
            v = v .. y
        end
    end
    mw.ustring.gsub(x, ".", subber)
    return s .. d .. v
end

function export.fix_nikud(x)
    return mw.ustring.gsub(x, nikud_regex .. "+", fix_nikud_subber)
end

function export.end_with_makaf(string)
	if string == "" or string == nil then return string end
	if mw.ustring.sub(string, -1) ~= "־" then
		string = string .. "־"
	end
	return string
end

-- This should only be used by modules that auto-generate words, otherwise use gen_link
function export.process_wv_triad(form, formwv, formdwv)
	if form then
		return form, formwv, formdwv
	elseif formwv then
		return export.remove_nikud(formwv), formwv, formdwv
	else
		return export.remove_nikud(formdwv), formdwv, nil
	end
end

function export.gen_link(form, formwv, formdwv)
	form, formwv, formdwv = export.process_wv_triad(form, formwv, formdwv)
	if not form then
		error("Can't make a link out of nothing.")
	end
	if formdwv then
		return "[[" .. form .. "|" .. (formwv or form) .. " / " .. formdwv .. "]]"
	else
		return "[[" .. form .. "|" .. (formwv or form) .. "]]"
	end
end

-- Like gen_link for construct forms appends a makaf if one doesn't already exist
function export.gen_link_ending_with_makaf(form, formwv, formdwv)
	form, formwv, formdwv = export.process_wv_triad(form, formwv, formdwv)
	if not form then
		error("Can't make a link out of nothing.")
	end
	if formdwv then
		return "[[" .. form .. "|" .. (export.end_with_makaf(formwv) or form) .. " / " .. export.end_with_makaf(formdwv) .. "]]"
	else
		return "[[" .. form .. "|" .. export.end_with_makaf(formwv or form) .. "]]"
	end
end

return export