Module:gmy-R

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


local export = {}

local data = require("Module:gmy-R/data")
local tag_text = require("Module:script utilities").tag_text
local get_lang = require("Module:languages").getByCode

function export.show(frame)

	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = { },
		["h"] = { list = true, allow_holes = true },
		[2] = { list = true, allow_holes = true, alias_of = "h" },
		["p"] = { list = true, allow_holes = true },
		["s"] = { list = true, allow_holes = true },
		["§"] = { list = true, allow_holes = true, alias_of = "s" },
	})

	local source_index = args[1]
	local source = data.sources[source_index]
	if source == nil then
		source_index = data.aliases[source_index]
		if source_index == nil then
			error("The source " .. args[1] .. " is not in the [[MOD:gmy-R/data|database]].")
		end
		source = data.sources[source_index]
	end

	local str =
		"[[Wiktionary:About Mycenaean Greek/Bibliography#"
		.. source_index
		.. "|"
		.. source_index:gsub("^(.+)%:(%d+)$", "%1 (%2)")
		.. "]]"

	local entries = args.h
	local pages = args.p
	local sections = args.s

	local function pluralise(number, singular, plural)
		return (number:find("–") or number:find("f%.$") or number:find(", ")) and plural or singular
	end

	for i = 1, math.max(entries.maxindex, pages.maxindex, sections.maxindex) do
		if pages[i] == nil and entries[i] == nil and sections[i] == nil then
			error("No entry or page or section is given for the index " .. i .. ", but a higher index is defined.")
		end
		str = str .. ", "
		if sections[i] then
			str = str .. pluralise(sections[i], "§", "§§") .. sections[i]
		end
		if pages[i] then
			if sections[i] then str = str .. " " end
			local page_text = pluralise(pages[i], "page ", "pages ") .. pages[i]
			if source.page_url then
				page_text = "[" .. source.page_url(tonumber(pages[i]:match("^%d+"))) .. " " .. page_text .. "]"
			end
			str = str .. page_text
		end
		if entries[i] then
			if pages[i] or sections[i] then str = str .. ": " end
			local entry_text = entries[i]
			if source.entry_lang then
				entry_text = tag_text(entry_text, get_lang(source.entry_lang))
			end
			str = str .. "“" .. entry_text .. "”"
		end
	end

	return str

end

return export