Module:Sandbox/Erutuon/author citation

This is an old revision of this page, as edited by Erutuon (talk | contribs) at 18:07, 4 July 2019 (stop loops). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local function advance_pos_if_starts_with(str, pattern, pos)
	local i, j = mw.ustring.find(str, pattern, pos)
	if i == pos then
		return j + 1
	else
		return pos
	end
end

local function advance_by_prefixes(str, prefixes, pos)
	for _, prefix in ipairs(prefixes) do
		pos = advance_pos_if_starts_with(str, prefix, pos)
	end
	
	return pos
end

-- [[d:Property:P428#P1793]]
-- ('t )?(d')?(de )?(la )?(van (der )?)?(Ma?c)?(De)?(Di)?\p{Lu}?C?['\p{Ll}]*([-'. ]*(van )?(y )?(d[ae][nr]?[- ])?(Ma?c)?[\p{Lu}bht]?C?['\p{Ll}]*)*\.? ?f?\.?
function p.is_author_citation(str, i, j)
	local pos = i or 1
	pos = advance_by_prefixes({
		"'t ", "d'", "de ", "la ", "van der ", "van ", "Ma?c", "De", "Di", "%l?C?['%l]*",
	}, pos)
	
	repeat
		local orig_pos = pos
		pos = advance_by_prefixes({
			"Ma?c", "[%lbht]?C?", "['%l]*", "[-'. ]*", "d[ae][nr]?[- ]", "van ", "y "
		}, pos)
	until orig_pos == pos
	
	pos = advance_pos_if_starts_with(str, "%.? ?f?%.?")
	
	return pos == (j or #str)
end

return p