Module:Sandbox/BrandonXLF/4 explained

-- Sandbox, do not deletelocal p =

local concat = table.concatlocal insert = table.insertlocal format = mw.ustring.formatlocal trim = mw.text.trim

local parserModule = require("Module:Road data/parser")local parser = parserModule.parserlocal util = require("Module:Road data/util")local sizeModule = require("Module:Road data/size/sandbox") -- REMOVE SANDBOX

-- Shieldslocal defaultShieldSize = 24

local function addContextBanner(route, name, suffix, bannerSpec, sizeStyle) local bannerModule = 'Module:Road data/banners/' .. string.upper(route.country) local shieldfield = name .. 'shield' local shield = parser(route, shieldfield) if shield

nil then -- This route type does not define shield. -- Find shield in the default banner table. shield = parser(route, 'shield', name, bannerModule) if shield and shield ~= then if suffix

nil then suffix = parser(route, 'shield', 'suffix', bannerModule) end if suffix and suffix ~= then shield = shield .. " " .. suffix end shield = shield .. ".svg" end end if shield and shield ~= then local shieldSize = sizeModule._size -- Add banner plate. insert(bannerSpec,) endend

local function bannerSpec(banner, bannerSize, bannerSuffix, route, sizeStyle) local banners = if type(banner)

"table" then local bannerSizeIsNotTable = type(bannerSize) ~= "table" for i,filename in ipairs(banner) do local bannersize = bannerSizeIsNotTable and bannerSize or bannerSize[i] or defaultShieldSize insert(banners,) end elseif banner ~= then insert(banners,) end

if route.dir then addContextBanner(route, 'dir', bannerSuffix, banners, sizeStyle) end if route.to then addContextBanner(route, 'to', bannerSuffix, banners, sizeStyle) end

return bannersend

local function shieldSpec(route, shieldStyle, sizeStyle) local shieldSpec =

local shield if route.to then shield = parser(route, 'shieldto') end if not shield and shieldStyle

'main' then shield = parser(route, 'shieldmain') end if not shield and shieldStyle

'list' then shield = parser(route, 'shieldlist') end if not shield then shield = parser(route, 'shield') or end

if shield

then return shieldSpec end

local orientation = parser(route, 'orientation') local shieldsize = sizeModule._size local banner = parser(route, 'banner') or local bannersize = sizeModule._size local bannersuffix = parser(route, 'bannersuffix')

local bannerIsNotTable = type(banner) ~= "table" local bannersizeIsNotTable = type(bannersize) ~= "table" local bannersuffixIsNotTable = type(bannersuffix) ~= "table"

if type(shield)

"table" then for i,filename in ipairs(shield) do local size = shieldsize or shieldsize[i] if size

"" then size = nil end -- banner.all describes banners that apply to all multiple shields. local shieldBanner = bannerIsNotTable and banner or (banner[i] or banner.all or) -- Banner size is default if the corresponding entry -- in bannerSize table is not set. local shieldBannerSize = bannersizeIsNotTable and bannersize or (bannersize[i] or bannersize.all or defaultShieldSize) local shieldBannerSuffix = bannersuffix and (bannersuffixIsNotTable and bannersuffix or bannersuffix[i]) insert(shieldSpec,) end elseif shield ~= then if shieldsize

"" then shieldsize = nil end insert(shieldSpec,) end

return shieldSpecend

local missingShields

local shieldExistsCache =

-- Return up to two booleans.-- The first boolean is false if `shield` does not exist, and true otherwise.-- If the first boolean is true, the second boolean is true if the shield is-- landscape (width >= height), and false otherwise.local function shieldExists(shield) local result = shieldExistsCache[shield] if result

nil then local file = mw.title.new(shield, 'Media').file -- Cache result. local exists = file.exists result = if exists then result[2] = file.width >= file.height end shieldExistsCache[shield] = result end if result[1] then return true, result[2] end insert(missingShields, shield) return falseend

local function render(shieldEntry) local shield = shieldEntry.shield local banners = shieldEntry.banners

local shieldCode = format("", shield[1], shield[2]) if not banners[1] then return shieldCode end

for _,banner in ipairs(banners) do shieldCode = format("
%s", banner[1], banner[2], shieldCode) end return '

' .. shieldCode .. ''end

function p.shield(route, shieldStyle, sizeStyle) missingShields =

local rendered = for _,entry in ipairs(shieldSpec(route, shieldStyle, sizeStyle)) do insert(rendered, render(entry)) end return concat(rendered), missingShieldsend

function p.link(route) local abbr, errMsg = parser(route, 'abbr') if not abbr then route.typeerror = true return util.err(errMsg or format("Invalid type: %s", route.type or "(nil)")) end if route.nolink then return abbr, abbr end

local link = parser(route, 'link') or if link

then return abbr, abbr end

return format("%s", link, abbr), abbrend

return p