Module:reference information

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

Retrieves information associated with ranges of pages (author name, title) for the reference and quotation templates {{R:sem-pro:FS Militarev}}, {{RQ:Hakluyt Principall Navigations}}, {{R:Textile Terminologies}}, {{R:sem-pro:Weninger-Handbook}}. Data found in Module:reference information/data, functions for individual templates in Module:reference information/functions.


local export = {}

local works = mw.loadData "Module:reference information/data"

local function retrieve_from_ranges(ranges, number)
	for _, range in ipairs(ranges) do
		if number < range[1] then
			return nil
		elseif number <= range[2] then
			return range
		end
	end
end

function export.retrieve_info(work, page, information)
	local data = works[work]
	if not data then
		error("The work " .. work .. " is not found in [[Module:reference information/data]].")
	end
	
	local index = data.indices[information] or error("The type of information " .. information .. " is not recognized.")
	
	local result = retrieve_from_ranges(data, page)
	-- If a function is listed in field "func" of data table, look up that
	-- function in [[Module:reference information/functions]] and use it to generate
	-- the output.
	if data.func then
		local func = require("Module:reference information/functions")[data.func]
			or error("There's no function by the name " .. data.func
				.. " in [[Module:reference information/functions]].")
		return result and result[index]
			and func(result[index], page)
	else
		return result and result[index]
	end
end

local function process(param)
	if param == "" then
		return nil
	else
		return param
	end
end

-- The first parameter in the module invocation is the work, the second is which
-- info to return, and the page or pages parameter from the template instance
-- determines the number to search for in the ranges in [[Module:reference information/data]].
-- {{#invoke:reference information|retrieve|FS_Militaev|first}}
-- {{#invoke:reference information|retrieve|FS_Militaev|last}}
-- Any numbered parameters beginning with the third (optional) specify the
-- parameter or parameters in which the page number is to be found.
function export.retrieve(frame)
	local work = frame.args[1]
	
	if not works[work] then
		error("No data for " .. work .. " in [[Module:reference information/data]].")
	end
	
	local info = process(frame.args[2])
	
	local pargs = frame:getParent().args
	local page
	
	if frame.args[3] then
		-- Parameter names have been supplied in parameters 3 and up of the
		-- module invocation.
		-- Check each in turn until we find one that has a value in the current
		-- template instance.
		local i = 3
		while not page and frame.args[i] do
			local param = process(frame.args[i])
			param = tonumber(param) or param -- "2" -> 2
			page = process(pargs[param])
			i = i + 1
		end
	else
		page = process(pargs.page) or process(pargs.pages)
	end
	
	-- Get first sequence of digits in parameter: a lone number, or the first
	-- number of a range ("110–111" -> 110).
	if page then
		page = tonumber(page:match("%d+"))
	end
	
	if not page then
		return nil
	end
	
	-- Use first number in "page" parameter.
	return export.retrieve_info(work, page, info)
end

return export