Module:User:Victar/term cognates


This is a private module sandbox of Victar, for their own experimentation. Items in this module may be added and removed at Victar's discretion; do not rely on this module's stability.


local export = {}

function export.getCognates(lang, term)
	local page = mw.title.new(require("Module:links").getLinkPage(term, lang))
	local content = page:getContent()
	
	if not content then
		return ""
	end
	
	local _, index = mw.ustring.find(content, "\n==" .. lang:getCanonicalName() .. "==", nil, true)
	if not index then
		_, index = mw.ustring.find(content, "^==" .. require("Module:utilities").pattern_escape(lang:getCanonicalName()) .. "==", nil, false)
	end
	if not index then
		error("Language not found: " .. lang:getCanonicalName() .. ".")
	end
	_, next_lang = mw.ustring.find(content, "\n==[^=\n]+==", index, false)
	_, index = mw.ustring.find(content, "\n(====?)Descendants%1", index, false)
	if (not index) or (next_lang and next_lang < index) then
		error("Cognates not found")
	end
		
	local cognates = mw.ustring.match(content, "^\*\s*\{{\w*\|(\w*\|\w*).*")
	
	cognates = mw.getCurrentFrame():preprocess(cognates)
	
	cognates = mw.text.trim(cognates)
	
	return cognates
	
end

return export