Jump to content

Module:amf-new

From Wiktionary, the free dictionary


export = {}

local m_str_utils = require("Module:string utilities")

local upper = m_str_utils.upper
local sub = m_str_utils.sub

function export.new(frame)
	local parent_args = frame:getParent().args
	
	local params = {
		[1] = true, -- part of speech 1
		[2] = { required = true }, -- definition 1
		[3] = true, -- part of speech 2
		[4] = true, -- definition 2
		[5] = true, -- part of speech 3
		[6] = true, -- definition 3
		["e"] = { alias_of = "etym" },
		["etym"] = true,
		["page"] = true,
		["pages"] = true,
		["cat"] = true, -- categories without namespace prefix separated by ;
		["pic"] = true, -- include caption; e.g. input [[File:blabla]];caption
		["passive"] = true, -- affix with ;<part of speech number>
		["causative"] = true, -- same here
		["alt"] = true, -- alt forms separated by ;
		["suppressref"] = { type = "boolean", default = false }, -- suppress automatic addition of Petrollino, 2016
	}
	
	local args = require("Module:parameters").process(parent_args, params)
	local results = "==Hamer-Banna==\n"
	
	local function processCat(text)
		categories = {}
		text = text:gsub("amf:([^;]+)([;$]?)", "{{c|amf|%1}}%2")
			:gsub("Hamer%-Banna ([^;]+)([;$]?)", "{{cln|amf|%1}}%2")
		for i in text:gmatch("[^;]+") do
			table.insert(categories, i)
		end
		return categories
	end
	
	local function genTitle(text)
		local pos_title = {
			["n"] = "Noun", ["pn"] = "Proper noun", ["propn"] = "Proper noun", ["pron"] = "Pronoun",
			["v"] = "Verb", ["a"] = "Adjective", ["adj"] = "Adjective", ["adv"] = "Adverb",
			["prep"] = "Preposition", ["conj"] = "Conjunction",
			["part"] = "Particle", ["suf"] = "Suffix",
			["prov"] = "Proverb", ["id"] = "Idiom", ["ph"] = "Phrase", ["ideo"] = "Ideophone", ["intj"] = "Interjection",
			["interj"] = "Interjection", ["num"] = "Numeral", ["deter"] = "Determiner",
			["nf"] = "Noun", ["vf"] = "Verb",
		};
		return pos_title[text] or upper(sub(text, 1, 1)) .. sub(text, 2, -1)
	end
	
	local function genHead(text)
		local pos_head = {
			[""] = "noun", ["n"] = "noun", ["pn"] = "proper noun", ["propn"] = "proper noun", ["v"] = "verb", ["a"] = "adj",
			["conj"] = "con", ["part"] = "particle", ["pron"] = "pronoun",
			["prov"] = "proverb", ["id"] = "idiom", ["ideo"] = "ideophone", ["ph"] = "phrase", 
			["intj"] = "interj", ["deter"] = "det", ["nf"] = "noun form",
			["vf"] = "verb form",
		};
		return pos_head[text] or text
	end
	
	if args["pic"] then
		pic = {}
		for i in args["pic"]:gmatch("[^;]+") do
			table.insert(pic, i)
		end
		pic[1] = "\n" .. pic[1]:gsub("%]%]", "%|thumb%|right%]%]")
		if pic[2] then pic[1] = "" .. pic[1]:gsub("%]%]", "%|" .. pic[2] .. "%]%]") end
		results = results .. pic[1]
	end
	
	if args["alt"] then
		args["alt"] = args["alt"]:gsub("([^;]+);?", "* {{alt|amf|%1}}\n")
		results = results .. "===Alternative forms===\n" .. args["alt"]:gsub(";", "\n")
	end
	
	if args["etym"] then results = results .. "\n===Etymology===\n"
		.. args["etym"] .. "\n" end
	
	results = results .. "\n===Pronunciation===\n* {{amf-IPA}}\n\n===" 
		.. genTitle(args[1]) .. "===\n{{head|amf|" .. genHead(args[1]) ..
		"}}\n\n# " .. args[2]
	
	for i=1,4,1 do
		if args[i+2] and (i+2) % 2 ~= 0 then
			results = results .. "\n\n===" .. genTitle(args[i+2])
				.. "===\n{{head|amf|" .. genHead(args[i+2]) .. "}}\n"
		elseif args[i+2] and (i+2) % 2 == 0 then
			results = results .. "\n# " .. args[i+2]
		end
	end
	
	if args["passive"] then
		pas = {}
		for i in args["passive"]:gmatch("[^;]+") do
			table.insert(pas, i)
		end
		if pas[2] == "1" then
			results = results:gsub("(===" .. genTitle(args[1]) .. "===\n{{head|amf|.+)}}",
				"%1|passive|" .. pas[1] .. "}}")
		elseif pas[2] == "2" or "3" then
			results = results:gsub("(===" .. genTitle(args[3]) .. "===\n{{head|amf|.+)}}",
				"%1|passive|" .. pas[1] .. "}}")
		elseif pas[2] == "3" or "5" then
			results = results:gsub("(===" .. genTitle(args[5]) .. "===\n{{head|amf|.+)}}",
				"%1|passive|" .. pas[1] .. "}}")
		end
	end
	
	if args["causative"] then
		cau = {}
		for i in args["causative"]:gmatch("[^;]+") do
			table.insert(cau, i)
		end
		if pas[2] == "1" then
			results = results:gsub("(===" .. genTitle(args[1]) .. "===\n{{head|amf|.+)}}",
				"%1|causative|" .. cau[1] .. "}}")
		elseif pas[2] == "2" or "3" then
			results = results:gsub("(===" .. genTitle(args[3]) .. "===\n{{head|amf|.+)}}",
				"%1|causative|" .. cau[1] .. "}}")
		elseif pas[2] == "3" or "5" then
			results = results:gsub("(===" .. genTitle(args[5]) .. "===\n{{head|amf|.+)}}",
				"%1|causative|" .. cau[1] .. "}}")
		end
	end
	
	if args["suppressref"] ~= true then
		results = results .. "\n\n===References===\n* {{R:amf:Petrollino:2016}}"
	end
	
	if args["page"] then
		results = results:gsub("2016%}%}", "2016|page=" .. args["page"] .. "}}")
	end
	
	if args["pages"] then
		results = results:gsub("2016%}%}", "2016|pages=" .. args["pages"] .. "}}")
	end
	
	if args["cat"] then
		results = results .. "\n"
		for _,cat in ipairs(processCat(args["cat"])) do
			results = results .. "\n" .. cat
		end
	end
	
	return results
end

return export