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 19:27, 26 September 2021 (add error category). 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

--DRAWS THE ROUTE MARKER (i.e. "Circle sign 1.svg")

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

	local function shield_size(image_name, orientation)
		local image = 'File:' .. image_name
		local title = mw.title.new(image)

		local width = title.file.width
		local height = title.file.height

	if orientation == nil and height == nil and width == nil then
		return nil
	elseif (orientation and orientation == 'upright') or height > width then
			return shieldSize
		else
			return 'x' .. shieldSize
		end
	end
	
	if shield_size(shield, orientation) == nil then
		return "[[Category:Infobox road temporary tracking category 2|¶ {{PAGENAME}}]] "
	elseif type(shield) == 'table' then
		local res = {}
		local sizes = {}
		for i,v in ipairs(shield) do
			sizes[i] = v
			res[i] = string.format('[[File:%s|%s|alt=%s]]', v, shield_size(v), alt)
		end
    	return table.concat(res, ' ')
	else
    	return string.format('[[File:%s|%s|alt=%s]]', shield, shield_size(shield, orientation), alt)
	end
end

--DRAWS THE AUXILIARY PLATE (i.e. "Business plate.svg")

local function banner(args, bannerSize)
	if args.marker_image then return nil end
	
	local shield = parser(args, 'banner')
	if not shield or shield == '' then return nil end
	
	local alt = parser(args, 'banner')
	
	return string.format('[[File:%s|%s|alt=%s]]', shield, bannerSize, alt)
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})) or nil
	local shield = shield(args, require('Module:Road data/size').size({style = style})) or nil
	local name = name(args) or nil

	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;')
		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;'):node(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.1em;'):node(shield)
		elseif args.country == 'AUS' then
			container:tag('p'):cssText('margin:0.1em;'):node(name)
			container:tag('p'):cssText('margin:0.1em 0 0 0;'):node(shield)
		elseif banner == nil then
			container:tag('p'):cssText('margin:0.1em 0 0.1em;'):node(shield)
			container:tag('p'):cssText('margin:0.1em;'):node(name)
		else
			container:tag('p'):cssText('margin:0.1em 0 0 0;'):wikitext(banner)
			container:tag('p'):cssText('margin:0 0 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