Jump to content

Module:User:Morwen/infobox test

From Wikipedia, the free encyclopedia
local p = {}

local infobox = require('Module:Infobox').infobox
local getArgs = require('Module:Arguments').getArgs
local makeList = require('Module:List').makeList
local langx = require('Module:Lang')._langx
local map = require('Module:Location map')

local function wrapWithLink(value)
	return string.format("[[%s]]", value)
end

local function wrapWithPrefix(prefix)
	return function(value)
		return string.format("[[%s %s|%s]]", prefix, value, value)
	end
end

local function makeLink(link, article)
	return string.format("[[%s|%s]]", link, article)
end

local function renderOsGrid(value)
	-- return oscoord._main("main", value[2])
	return tostring(value)
end

local convert = require('Module:Convert')._convert

-- 		table.insert(getGrouping("location").entries, {
-- 			label = "* [[Charing Cross]]",
-- 			data = 
-- end

local function renderDistance(directionProp)
	return function(distance, args) 
		return convert({}, {tostring(distance), "mi", "km"}) .. " " .. args[directionProp]
	end
end

local function waypointField(waypoint, waypointTag)
	if not waypointTag then
		waypointTag = string.lower(waypoint)
	end
	return {
		field =  waypointTag .."_distance_mi",
		label = "• [[" .. waypoint .. "]]",
		render = renderDistance(waypointTag .. "_direction"),
		consumes = waypointTag .. "_direction",
	}
end

local schema = {

	above = {
		render = function(settlement, consumed)
			local morenames = {}

			local above = settlement.official_name
			consumed["official_name"] = true

			local names = {
				gaelic_name = "gd",
				irish_name = "ga",
				scots_name = "sco",
				cornish_name = "kw",
				welsh_name = "cy",
				manx_name = "gv",
			}
		
			for prop, tag in pairs(names) do
				if settlement[prop] then
					table.insert(morenames, langx({tag, settlement[prop]}))
				end
				consumed[prop] = true
			end

			if settlement.other_language_name then
				table.insert(morenames, makeLink(settlement.other_language, settlement.other_language_name))
				consumed["other_language_name"] = true
				consumed["other_language"] = true
			end
			
			if #morenames > 0 then
				morenames["class"] = "ib-uk-place-other-names"
				above = above .. makeList("unbulleted", morenames)
			end

			return above
		end
	},

	groups = {

		{
			group = "pop",
			fields = {
				{
					field = {"population", "population_ref"},
					label = "Population",
				}
			}
		},

		{
			group = "location",
		
			fields = {
				{
					field = "os_grid_reference",
					label = "OS grid reference",
					render = renderOsGrid
				},
				{
					field = "coordinates",
					label = "Coordinates"
				},
				{
					field = "london_distance",
					label = "• [[London]]",
				},
				waypointField("Belfast"),
				waypointField("Cardiff"),
				waypointField("Douglas"),
				waypointField("Dublin"),
				waypointField("Edinburgh"),
				waypointField("London"),
				waypointField("Charing Cross", "charingX"),
				{
					field = "edinburgh_distance",
					label = "• [[Edinburgh]]",
				}
			},
		},

		{
			group = "localgov",
			fields = {
			 	{
					field = "civil_parish",
					label_link = "Civil parish"
			 	},
				{
					field = "unitary_england",
			 		label_link = "Unitary authorities of England",
			 		label = "Unitary authority"
				},
				{
					field = "unitary_scotland",
			 		label_link = "Subdivisions of Scotland",
			 		label = "Council area"
				},
				{
					field = "unitary_wales",
			 		label_link = "Principal areas of Wales",
			 		label = "Principal area"
				},
				{
				 	field = "london_borough",
					label_link = "London borough",
					label = "London borough",
					render = wrapWithPrefix("London Borough of")
				},
				{
					field = "lieutenancy_england",
			 		label_link = "Ceremonial counties of England",
			 		label = "Ceremonial county"
			 	},
				{
					field = "lieutenancy_wales",
			 		label_link = "Preserved county of Wales",
			 		label = "Preserved county"
			 	},
				{
					field = "lieutenancy_scotland",
			 		label_link = "Lieutenancy areas of Scotland",
		 			label = "Lieutenancy area"
				},
				{
					field = "region",
			 		label_link = "Regions of England",
			 		label = "Region",
			 		render = wrapWithLink
				},
				{
					field = "country",
			 		label_link = "Countries of the United Kingdom",
			 		label = "Country",
			 		render = wrapWithLink
				},
				{
			 		label_link = "List of sovereign states",
					label = "Sovereign state",
			 		data = "United Kingdom",
				}
		 	}
		},

		{
			group = "services",
			fields = {
				{
					field = "post_town",
					label_link = "Post town"
				},
				{
					field = "postcode_district",
					consumes = "postcode_area",
					label_link = "Postcodes in the United Kingdom",
					label = "Postcode district",
			 	 	render = function(value, args)
						if args.postcode_area then
		 					return string.format("[[%s postcode area|%s]]", args.postcode_area, value)
						end
					end,
		 		},
				{
					field = "dial_code",
					label_link = "Telephone numbers in the United Kingdom",
					label = "Dialling code"
				},
		 	}	
		},

		{
			group = "parliamentary",
			fields = {
				{
					field = "constituency_westminster",
					label_link = "Constituencies of the Parliament of the United Kingdom",
					label = "UK Parliament",
				},
				{
					field = "constituency_scottish_parliament",
					label_link = "Scottish Parliament constituencies and electoral regions",
					label = "Scottish Parliament"
,				},
				{
					field = "constituency_welsh_assembly",
					label_link = "Senedd constituencies",
					label = "Senedd Cymru – Welsh Parliament"
				}
			}
		},

		{
			fields = {
				{
					field = "website",
					label = "Website"
				}
			}
		}
	}
}

function p._render(settlement)

	local consumed = {}
		
	local args = {name="Test Infobox",
		bodyclass = "ib-uk-place vcard",
		templatestyles = "Infobox UK place/styles.css",
		aboveclass = "fn org nowrap",
		above = schema.above.render(settlement, consumed),
		subheader = settlement.type,
	}

	local nested = {}

	if settlement.static_image_name then
		args["image"] = "[[File:" .. settlement.static_image_name .. "|250px]]"
		args["caption"] = settlement.static_image_caption
		settlement.static_image_name = nil
		settlement.static_image_caption = nil
		settlement.label_position = nil
	end

	for rk, v in pairs(settlement) do
		local text, _ = string.match(rk, "^(.-)(%d+)$")
		local k
		if text then k = text else k = rk end
		if not nested[k] then
			nested[k] = {}
		end
		table.insert(nested[k], v)
	end

	-- for groupkey, group in pairs(schema.groups or {}) do
	-- 	for property, entry in pairs(group) do
	-- 		entry["grouping"] = groupkey
	-- 		schema.properties[property] = entry
	-- 	end
	-- end

	local groupings = {}
	
	local function getGrouping(entry)
		local groupKey = entry.grouping or ""
		if groupings[groupKey] then return groupings[groupKey] end
		groupings[groupKey] = {entries={}}
		return groupings[groupKey]
	end

	local n = 1
	local function addEntry(label, data, class)
		-- if idx == 1 then
		args["rowclass" .. n] = class
		args["label" .. n] = label
		args["data" .. n] = data
		n = n + 1
	end

	-- local function functor(arg)
	-- 	if arg == "defaultscale" then
	-- 		return 1
	-- 	end
	-- 	error("functor called with "..arg)
	-- end

	local frame = {}
	function frame.extensionTag()
		return ""
	end

	addEntry(nil,
			map.main(frame,
				{"United Kingdom",
				 lat = 51.5,
				 long = 0},
			map.getMapParams("United Kingdom", frame)))

	local function makeLabel(entry)
		if entry.label_link then
			if entry.label then
				return string.format("[[%s|%s]]", entry.label_link, entry.label)
			else
				return string.format("[[%s]]", entry.label_link)
			end
		end
		return entry.label
	end

	for i, v in ipairs(schema.groups) do
		local newGroup = true
		for j, entry in pairs(v.fields) do

			local fields = entry.field
			if type(fields) ~= "table" then
				fields = {fields}
			end
			
			for _, field in ipairs(fields) do
				consumed[field] = true
			end

			local consumes = entry.consumes

			if type(consumes) == "string" then
				consumed[consumes] = true
			elseif type(consumes) == "table" then
				for _, field in ipairs(consumes) do
					consumed[field] = true
				end
			end
			
			
			local output = {}
			for l, field in ipairs(fields) do
				for k, c in ipairs(nested[field] or {}) do
				  	if entry.render then
						table.insert(output, entry.render(c, settlement))
					else
						table.insert(output, c)
				  	end
				end
			end

			if #output > 0 then
				output = makeList("unbulleted", output)
			else
				output = ""
			end

			if entry.data then
				output = entry.data
			end
			
			if output then
				local class = "mergedrow"
				if newGroup then 
					class = "mergedtoprow"
					newGroup = false
				end

				addEntry(makeLabel(entry), output, class)
			end
		end
	end

	for k, v in pairs(nested) do
		if not consumed[k] then
			addEntry("MISSING FROM SCHEMA: " .. k, v[1])
		end
	end
		
	return infobox(args)
end

function p.render(frame)
    local args = getArgs(frame, {
        wrappers = 'User:Morwen/infobox test/template'
    })

    return p._render(args)
end

return p