Modul:inflection-tools

A modult a Modul:inflection-tools/doc lapon tudod dokumentálni

local export = {}

local u = require('Module:utils')

local function trim_stress(str)
	-- remove space after stress (we put it in the code just to make visually readable sources)
	local result, count = mw.ustring.gsub(str, '́ ', '́')
	return result
end

export.stash = {}

function export.clear_stash()
	export.stash = {}
end

function export.add_stash(name, value)
	export.stash[name] = value
end

function export.apply_stash(str)
	for name, value in pairs(export.stash) do
		str = mw.ustring.gsub(str, u.escape(name), value)
	end
	return str
end

function export.replace(str, pattern, replace_to)
	pattern = export.apply_stash(pattern)
	return mw.ustring.gsub(str, trim_stress(pattern), trim_stress(replace_to))
end

function export.extract(str, pattern)
	pattern = export.apply_stash(pattern)
	return mw.ustring.match(str, trim_stress(pattern))
end

function export.check(str, values, checker)
	if type(values) == 'string' then
		values = {values}
	end
	for i, value in pairs(values) do
		value = trim_stress(export.apply_stash(value))
		local ok = 
			   checker == 'equals'           and value == str
			or checker == 'endswith'         and mw.ustring.match(str, value .. "$") ~= nil
			or checker == 'penultimate'      and mw.ustring.match(str, value .. ".$") ~= nil
			or checker == 'contains'         and mw.ustring.match(str, value) ~= nil 
			or checker == 'contains_once'    and table.getn(mw.text.split(str, value)) == 2
			or checker == 'contains_several' and table.getn(mw.text.split(str, value)) > 2
		if ok then
			return true
		end
	end
	return false	
end

function export.equals(str, values)
	return export.check(str, values, 'equals')
end

function export.endswith(str, values)
	return export.check(str, values, 'endswith')
end

function export.penultimate(str, values)
	return export.check(str, values, 'penultimate')
end

function export.contains(str, values)
	return export.check(str, values, 'contains')
end

function export.contains_once(str, values)
	return export.check(str, values, 'contains_once')
end

function export.contains_several(str, values)
	return export.check(str, values, 'contains_several')
end

return export