Jump to content

Module:phi-placelist

From Wiktionary, the free dictionary
-- https://stackoverflow.com/a/12674376/14021404
local function getKeysOfTable(table)
	
	local keyset={}
	local n=0
	
	for k,v in pairs(table) do
		n=n+1
		keyset[n]=k
	end

	return keyset;
end

-----------------


-- Main function
local function getTableFromData( args )
	
	local inputtedDivisions = {} -- save here the inputted divisions, in order of less specific to more specific
	
	local level = 0
	while ( level < 6 ) do
		
		argLevel = args[level]
		if argLevel ~= nil then  -- get all positional arguments from 0 to 6
		
			inputtedDivisions[level] = argLevel
		
		end
		level = level + 1
	end
	
	-- get the division from data modules
	local list = {} 
	local _region = ""
	for i, v in ipairs( inputtedDivisions ) do
	
		if (i == 1 and #inputtedDivisions == 1) then  -- if the first and only the first one, get from region data module
			list = require("Module:phi-placelist/data/" .. v)
		elseif (i == 1) then --if the first one, it is the region, store it
			_region = v
		elseif (i == 2) then --if the second one, get from data module
			list = require("Module:phi-placelist/data/" .. _region .. "/" .. v)
		else 
			list = list[v]
		end
	
	end
	
	-- error check so that the last inputted parameter is one division up
	if type(list) ~= "table" then
		error(inputtedDivisions[#inputtedDivisions] .. " has no divisions already. Just remove that part.")
	end
	
	-- get the keys from the data module & prepare for template call
	local divisionName = list["divisions"]
	list["divisions"] = nil
	
	local excludesText = nil
	if list["excludes"] ~= nil then
		excludesText =  list["excludes"]
	end
	
	
	local finalList = getKeysOfTable(list)
	
	-- I am mixed on whether to autosort the list. For now, sure.
	table.sort(finalList)

    return finalList, inputtedDivisions, divisionName, excludesText
	
end

local a = {}


-- for use in the definition lines
function a.linksInDefinition( frame )

	local args = frame:getParent().args
	
	local templateToCall = args["type"]
	if templateToCall == nil then
		templateToCall = "coordinate terms"
	end
	

    -- get table
	local finalList, inputtedDivisions, divisionName, excludesText = getTableFromData(args)


	-- prepare parameters for the {{coordinate terms}} (or others) template call
	table.insert(finalList, 1, "en") -- the language parameter
	finalList["lb"] = divisionName .. " of " .. inputtedDivisions[#inputtedDivisions] -- the label parameter
	
	if excludesText ~= nil then
		finalList["lb"] = finalList["lb"] .. " <small>(excluding " .. excludesText .. ")</small>"
	end
	
	return frame:expandTemplate{ title = templateToCall , args = finalList }
	
end


-- for use in `See also` headings
function a.linksInSeeAlso( frame )
	
	local args = frame:getParent().args
	
    -- get table
	local finalList, inputtedDivisions, divisionName, excludesText = getTableFromData(args)
	
	-- prepare parameters for the {{col3}} template call
	table.insert(finalList, 1, "en") -- the language parameter
	
	local label = divisionName .. " of " .. inputtedDivisions[#inputtedDivisions] -- label
	
	
	if excludesText ~= nil then
		label = label .. " <small>(excluding " .. excludesText .. ")</small>"
	end
	
	-- no need to use {{q}} to reduce lua usage
	return "(''" .. label .. "''): " .. frame:expandTemplate{ title = "col3-u" , args = finalList }
	
end


return a;