Jump to content

Module:Infobox road/route

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fredddie (talk | contribs) at 00:49, 7 March 2021 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {} 

local format = mw.ustring.format
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs	
local parserModule = require "Module:Road data/parser"
local parser = parserModule.parser

local function banner(args, bannerSize)
	local size = bannerSize
	local shield = parser(args, 'banner')
	if not shield or shield == '' then
		return nil end
	local alt = parser(args, 'banner')

		if args.shielderr then
			return nil
		else return string.format("[[File:%s|%s|alt=%s]]", shield, size, alt)
	end
end

local function shield(args, shieldSize)
	if args.marker_image then return args.marker_image end

	local shield = parser(args, 'shieldmain') or parser(args, 'shield')
	local label = parser(args, 'name') or parser(args, 'abbr') or ''
	local alt = label .. " marker"
	local orientation = parser(args, 'orientation')
	
	if not shield or shield == '' then
		return nil end

	if type(shield) == 'table' then
		local res = {}
		local sizes = {}
			for i,v in ipairs(shield) do
				sizes[i] = v
					local function size(args)
					local image = 'File:' .. v
					local title = mw.title.new(image)
		
					local width = title.file.width
					local height = title.file.height
		
					if height > width then
						return shieldSize
					else return "x" .. shieldSize
				end
			end
					local size = size(args)
				res[i] = string.format("[[File:%s|%s|alt=%s]]", v, size, alt)
			end
    	return table.concat(res, " ")
	else
		local function size(args)
		local image = 'File:' .. shield
		local title = mw.title.new(image)
				
		local width = title.file.width
		local height = title.file.height
		
		if orientation == 'upright' then
			return shieldSize
		elseif height > width then
			return shieldSize
		else return "x" .. shieldSize
		end
	end
	local size = size(args)
    	return string.format("[[File:%s|%s|alt=%s]]", shield, size, alt)
	end
end

-- Links/abbreviations
local function name(args)
	local name = args.name or parser(args, 'name') or parser(args, 'abbr')
	return name
end

function p._routeInfo(args)
	
	local style = args.style
	local banner = banner(args, require("Module:Road data/size").size({style = style}))
	local shield = shield(args, require("Module:Road data/size").size({style = style}))
	local name = name(args)

	if not args.type and not args.route and not args.name and not args.marker_image then
		local container = nil
	else local container = mw.html.create('div'):cssText("text-align:center;")
			container:tag('p'):wikitext('')
		if shield == nil or args.marker_image == 'none' or args.name and not args.marker_image and not args.type and not args.route then 
			container:tag('p'):cssText("margin:0.1em;"):wikitext(name)
		elseif args.marker_image ~= '' and args.name == '' or args.name == nil and not args.type and not args.route then
			container:tag('p'):cssText("margin:0.25em 0.1em;"):wikitext(shield)
		elseif args.country == 'AUS' then
			container:tag('p'):cssText("margin:0.1em;"):wikitext(name)
			container:tag('p'):cssText("margin:0.25em 0.1em;"):wikitext(shield)
		else
			container:tag('p'):cssText("margin:0.25em 0.1em 0 0.1em;"):wikitext(banner)
			container:tag('p'):cssText("margin:0 0.1em 0.25em 0.1em;"):wikitext(shield)
			container:tag('p'):cssText("margin:0.1em;"):wikitext(name)
		end
		return tostring(container)
	end
end

function p.routeInfo(frame)
	local args = getArgs(frame)
	return p._routeInfo(args);
end

return p