Module:senseno

From Wiktionary, the free dictionary
Jump to navigation Jump to search


-- Based on [[Module:descendants tree]]

local export = {}

local pattern_escape = require("Module:string utilities").pattern_escape

local function list_index(wikitext, t_start)
	local text = wikitext:sub(1, t_start)
	local _, section_start = text:find(".*\n==")
	text = text:sub(section_start)

	local lines = mw.ustring.gmatch(text, "\n#[#*:]*")
	local senseno = {}
	line = lines()
	while line ~= nil do
		if line:find(":") or line:find("%*") then
			-- skip
		else
			line = line:match("#+")
			local indentation = #line
			if indentation > #senseno then
				table.insert(senseno, 1)
			else
				senseno = {unpack(senseno, 1, indentation)}
				senseno[#senseno] = senseno[#senseno] + 1
			end
		end

		line = lines()
	end

	return senseno
end

local function find_senseid(title, code, id)
	local content = mw.title.new(title):getContent()
	local t_start = ""
	
	t_start = content:find("%{%{ *senseid *%| *" .. pattern_escape(code) .. " *%| *" .. pattern_escape(id) .. " *%}%}")
	
	if t_start == nil then
		t_start = content:find("%{%{ *sid *%| *" .. pattern_escape(code) .. " *%| *" .. pattern_escape(id) .. " *%}%}")
	end
	
	if t_start == nil then
		error("Could not find the correct senseid template in the entry [["
		.. title .. "]] (with language " .. code .. " and id '" .. id .. "'); this could be because you are in preview mode and the senseid template has not yet been saved to the page")
	end
	
	return table.concat(list_index(content, t_start), ".")
end

function export.main(frame)
    local args = frame.args
    local code = args[1]
    local id = args[2]
    local title = args[3]
    title = args["title"]

	return find_senseid(title, code, id)
end

return export