Module:also/auto

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

This module is experimental.
The details of its operation have not yet been fully decided upon. Do not deploy widely until the module is finished.

local p = {}

local PAGENAME = mw.title.getCurrentTitle().text
local m_unicode = require("Module:Unicode data")
local m_data = require("Module:also/processed data") -- this contains only existing pages

function get_hashkey(word)
	buffer = mw.ustring.toNFKD(mw.ustring.lower(word))
	for k, v in pairs(m_data.customkeys) do
		buffer = mw.ustring.gsub(buffer, k, v)
	end
	hashkey = {}
	for cp in mw.ustring.gcodepoint(buffer) do
		table.insert(hashkey, is_valid(cp) and mw.ustring.char(cp) or "")
	end
	return table.concat(hashkey)
end

function is_valid(cp)
	category = m_unicode.lookup_category(cp)
	return (category:match("^[LN].$") or category == "Mc" or category == "Sc") -- can't rely on built-in %w class
end

function p.main(frame)
	local hashkey = get_hashkey(PAGENAME)
	local data = m_data.lookups[hashkey] or {}
	for i, v in ipairs(frame:getParent().args) do -- if it has manual params
		table.insert(data, v)
	end

	if #data == 0 then
		error("This term does not exist in 'Module:also/processed data' and no parameter is given.")
	end

	local new_frame = frame:newChild{title = frame:getTitle(), args = data}
	function new_frame:getParent()
		return self
	end
	-- FIXME: move this to [[Module:also/templates]] and integrate it properly.
	return require('Module:also/templates').also_t(new_frame)
end

return p