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 concat = table.concat
local insert = table.insert
local u = mw.ustring.char
local ugsub = mw.ustring.gsub
local usub = mw.ustring.sub

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

local nikud_regex = "[" .. u(0x5B0) .. "-" .. u(0x5BC) .. u(0x5BF) .. u(0x5C1) .. u(0x5C2) .. u(0x5C7) .. "]"

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

local function fix_nikud_subber(x)
	local ret = {}
	-- Shin dots (U+05C1 & U+05C2).
	for s in x:gmatch("\215[\129\130]") do
		insert(ret, s)
	end
	-- Dagesh (U+05BC & U+05BF).
	for d in x:gmatch("\214[\188\191]") do
		insert(ret, d)
	end
	-- Vowels (U+05B0-U+05BB & U+05C7).
	for v in x:gmatch("[\214\215][\176-\187\135]") do -- any false-positives won't be picked up by the original gsub
		insert(ret, v)
	end
	return concat(ret)
end

function export.fix_nikud(x)
    return ugsub(x, nikud_regex .. nikud_regex .. "+", fix_nikud_subber)
end

function export.end_with_makaf(str)
	if str and #str > 0 and usub(str, -1) ~= "־" then
		str = str .. "־"
	end
	return str
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
	end
	return export.remove_nikud(formdwv), formdwv, nil
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.")
	elseif formdwv then
		return "[[" .. form .. "|" .. (formwv or form) .. " / " .. formdwv .. "]]"
	end
	return "[[" .. form .. "|" .. (formwv or form) .. "]]"
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.")
	elseif formdwv then
		return "[[" .. form .. "|" .. (export.end_with_makaf(formwv) or form) .. " / " .. export.end_with_makaf(formdwv) .. "]]"
	end
	return "[[" .. form .. "|" .. export.end_with_makaf(formwv or form) .. "]]"
end

return export