Modul:senseno

Daripada Wiktionary

Pendokumenan untuk modul ini boleh diciptakan di Modul:senseno/doc

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

local export = {}

function escape_pattern(text)
    return text:gsub("([^%w])", "%%%1")
end

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 = content:find("%{%{ *senseid *%| *" .. escape_pattern(code) .. " *%| *" .. escape_pattern(id) .. " *%}%}")
	
	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