Module:Road data/strings/doc: Difference between revisions

Content deleted Content added
m source → syntaxhighlight
Undid revision 1271553142 by Ahecht (talk)
 
(22 intermediate revisions by 6 users not shown)
Line 1:
This module is used to store type data for use with several road templates, including {{tl|jct}} and {{tl|routelist row}}.
Background information and a summary of the syntax are listed below.
<includeonly>{{#ifeq:{{FULLPAGENAME}}|Module:Road data/strings|<p>{{Clickable button|Special:Search/-intitle:"/doc" -intitle:"/sandbox" -intitle:"/testcases" -intitle:"/styles.css" prefix:Module:Road data/strings/|View regional modules}}</p>|==Exported types==
 
This module exports the following types:
{{Module exports|exclude=^%s}}}}</includeonly><noinclude><br><br>[BUTTON OR EXPORTS ARE ADDED HERE]</noinclude>
==Syntax==
===Hierarchy and fields===
Line 6 ⟶ 9:
 
<syntaxhighlight lang="lua">
place.type = {
shield = "",
link name = "",
abbr link = ""},
abbr = ""
}
</syntaxhighlight>
 
The threefour main fields in a type table are <code>shield</code>, <code>linkname</code>, and <code>abbr</code>. Generally, <code>shield</code> determines the shield that is displayed, if any; <code>link</code> specifies the target of the link generated by a including module, if any; and <code>abbr</code>. determines the displayed abbreviation. Currently, these are the three types used by all countries. By convention, they are always specified, using an empty string <code>"",</code> if there is no value.
*<code>shield</code> determines the shield that is displayed, if any
*<code>name</code> specifies the name of the route displayed by an infobox
*<code>link</code> specifies the target of a link generated, if any
*<code>abbr</code> determines the displayed abbreviation.
 
Other common fields in road data tables
Two additional fields are used in the United States. <code>banner</code> stores the name of the banner file, while <code>width</code> stores a code representing the width of the shield. Unlike the three main fields, these are generally omitted entirely when unused.
*<code>shieldmain</code> is used when a different shield is desired at the top of an infobox, such as for county roads.
 
Once a type is defined, it can be referred to later in the code. A common idiom used for bannered types is defining the shield to be the shield field of the base type (from [[Module:Road data/strings/USA/MO]]):
<syntaxhighlight lang="lua">
MOUSA.USCR = {
shield = "USCR %route% jct.svg",
link shieldmain = "U.S.[county||%county% Route|]County %route% in Missouri.svg",
name = "County Road abbr = "US&nbsp;%route%",
link = "",
width = "expand"}
abbr = "CR %route%"
}
</syntaxhighlight>
 
*<code>shieldlist</code> is used when a different shield is desired in lists that utilize the {{tl|Routelist row}} series of templates.
MO["US-Alt"] = {shield = MO.US.shield,
 
link = "U.S. Route %route% Alternate ([dab||%dab%, |]Missouri)",
*<code>base</code> can be used for aliasing different types that have a similar base structure, such as U.S. Highway special routes.
abbr = "US&nbsp;%route% Alt.",
 
banner = "Alternate plate.svg",
*<code>banner</code> stores the name of the special route plate, such as <code>[[:File:Business plate.svg|Business plate.svg]]</code>. Can be omitted when unused.
width = "expand"}
*<code>width</code> stores a code representing the width of the shield. It is most often helpful when used with <code>banner</code>. Can be omitted entirely when unused; common values are <code>square</code> and <code>expand</code>.
 
*<code>section</code> stores the specific section number for those routes that are defined by law.
 
*<code>translation</code> specifies the native language name of the route displayed by an infobox
*<code>lang</code> is the [[List of ISO 639-2 codes|ISO 639-2]] code of the native language; this code is used by {{tl|Lang}} and [[Template:Lang#Language-specific templates|Lang-<i>xx</i> templates]].
 
<syntaxhighlight lang="lua">
PER.RN = {
shield = "PE-%route% route sign.svg",
name = "National Route %route%",
link = "Peru Highway %route%",
abbr = "PE-%route%",
translation = "Ruta nacional %route%",
lang = "es-pe"
}
</syntaxhighlight>
 
Once a type is defined, it can be referenced later in the code. As seen here, it is common to define all parameters for main types like <code>US</code> and then to use aliases for subtypes such as <code>US-Alt</code>.
Another common idiom is defining a type alias by setting an entry in the root table equal to a previously defined type (from [[Module:Road data/strings/HKG]]):
 
<syntaxhighlight lang="lua">
HKGMO.RouteUS = {
shield = "HKUS Route%route%.svg",
link base = "U.S. Route %route% (Hong Kong)",
abbr link = "U.S. Route&nbsp; %route% in Missouri"},
abbr = "US&nbsp;%route%",
width = "expand"
HKG.route = HKG.Route
}
 
MO["US-Alt"] = {
shield = MO.US.shield,
link = MO.US.base .. " Alternate ([dab||%dab%, |]Missouri)",
abbr = MO.US.abbr .. " Alt.",
banner = "Alternate plate.svg",
width = "expand"
}
</syntaxhighlight>
 
Line 64 ⟶ 105:
The second special string is in the form of <code>[arg|equals|then|else]</code>. This functions as a rudimentary if-then-else statement. The parser tests the value of <code>arg</code> to see if it is equal to the value specified in <code>equals</code>. <code>equals</code> may be empty, in which case the parser tests the existence of the <code>arg</code> argument. If the result of the test is true, the statement is replaced with the value of the <code>then</code> block. Otherwise, it is replaced with the value of the <code>else</code> block.
 
The two statements may be combined. The parser will parse the if-then-else statement first, and then perform the argument inclusion. This combination is commonly used with bannered routes in the United States, where the <code>dab</code> argument is tested and the link disambiguation is adjusted accordingly, as follows (from [[Module:Road data/strings/USA/AL]]):
<syntaxhighlight lang="lua">
AL["US-Bus"] = {
shield = "US %route%.svg",
link = "U.S. Route %route% Business ([dab||%dab%, |]Alabama)",
abbr = "US-%route% Bus.",
banner = "Business plate.svg",
width = "expand"}
}
</syntaxhighlight>
 
Line 80 ⟶ 123:
The most straightforward functionality provided by nested tables is switching. In its most basic form, the table consists of a series of key-value pairs, with the keys being route numbers and the values being the format strings used by those routes. Usually, the format string returned does not need parsing, but the option is there. A <code>default</code> entry should be provided to handle any route numbers not explicitly stated. The following is a representative example of route-based switching (from [[Module:Road data/strings/USA/AR]]):
<syntaxhighlight lang="lua">
AR.AR = {
shield = {
default = "Arkansas %route%.svg",
["917"] = "Arkansas 917-1.svg",
["980"] = "Arkansas 980(Airport).svg"
},
link = "Arkansas Highway %route% [dab||(%dab%)|]",
abbr = "Hwy.&nbsp;%route%",
width = "expand"
}
</syntaxhighlight>
 
Line 92 ⟶ 139:
Switching on other arguments is also allowed. The name of the argument to be used for switching is stated in the <code>arg</code> field of the table. Nesting switches on different arguments is also allowed. A good example that uses both forms of switching can be found in [[Module:Road data/strings/CAN/ON|Ontario]]:
<syntaxhighlight lang="lua">
local regionalShields = {
arg = "county",
["Essex"] = "Essex County Road %route%.png",
["York"] = "York Regional Road %route%.svg",
["Durham"] = "Durham Regional Road %route%.svg",
["Niagara"] = "Niagara Regional Road %route%.svg",
["Simcoe"] = {
["52"] = "Simcoe county road 52.png",
default = "Simcoe County Road %route%.JPG"}}
}
}
</syntaxhighlight>
 
Line 106 ⟶ 157:
Another use for tables is existence testing. If a table has the <code>ifexists</code> field set to <code>true</code>, the parser will perform existence testing on the result of parsing the <code>default</code> field. If the test fails, the result of parsing the <code>otherwise</code> field is returned. Existence testing may be chained by using a second ifexists table as the value of the first table's <code>otherwise</code> field, and so on. Here's an example of nested existence testing (from [[Module:Road data/strings/GBR]]):
<syntaxhighlight lang="lua">
GBR.B = {
shield = {
ifexists = true,
default = "UK road B%route%.svg",
otherwise = {ifexists = true,
ifexists = true,
default = "UK road B%route%.png"}},
default = "UK road B%route%.png"
link = "",
}
abbr = "B%route%"}
},
link = "",
abbr = "B%route%"
}
</syntaxhighlight>
 
Line 119 ⟶ 176:
Generally speaking, a hook is called by setting the <code>hook</code> field in a table as equal to the name of a hook. Hooks receive two arguments, both tables: <code>parameters</code>, which is the table in the definition; and <code>args</code>, which is simply the table of arguments normally passed to the parser. The hook returns a string, which is then parsed as usual. A powerful feature of hooks is that they can add arbitrary values to the argument table, which may be referenced in the string returned by the hook. Generally, the format string returned by the hook is specified in some form by the <code>default</code> field of the table, though there are exceptions. Here is an example of a hook (from [[Module:Road data/strings/MEX]]):
<syntaxhighlight lang="lua">
MEX.SH = {
shield = {
ifexists = true,
arg = "state",
SON = "HIGHWAYSON %route%.jpg",
NLE = "Nuevo Leon State Highway %route%.PNG",
default = ""},
},
link = {hook = "mask",
link = {
mask = "Road data/masks/MEX",
hook = "mask",
base = "state",
mask = "Road data/masks/MEX",
masked = "fullstate",
base = "state",
default = "%fullstate% State Highway %route%"},
masked = "fullstate",
abbr = "SH&nbsp;%route%"}
default = "%fullstate% State Highway %route%"
},
abbr = "SH&nbsp;%route%"
}
</syntaxhighlight>
 
Line 137 ⟶ 200:
Functionality exists to display multiple shields for one route, which is used to display tolled and free shields for routes where they differ. This is done by supplying a table with two values, which are listed without indices. The parser is called twice by the calling module, and it returns one shield per call. An example may be found in [[Module:Road data/strings/USA/TX|Texas]]:
<syntaxhighlight lang="lua">
TX.Both = {
shield = {"Texas %route%.svg", "Toll Texas %route% new.svg"},
link = "Texas State Highway %route%",
abbr = "SH&nbsp;%route%",
width = 40}
}
</syntaxhighlight>
 
Line 158 ⟶ 223:
There are two ways to define a type as an alias. If the type is defined within the module, simply set the new type as equal to the type being aliased, as shown above (from [[Module:Road data/strings/HKG]]):
<syntaxhighlight lang="lua">
HKG.Route = {
shield = "HK Route%route%.svg",
link = "Route %route% (Hong Kong)",
abbr = "Route&nbsp;%route%"
}
 
HKG.route = HKG.Route
</syntaxhighlight>
Line 171 ⟶ 238:
 
This code sets the <code>NY</code> type as a link to the <code>NY</code> type in [[Module:Road data/strings/USA/NY]]. The parser will import that module and process the type as if the original module had declared it itself. The alias declaration may not add or override any data in the type table it points to.
 
===Inheriting types===
It is possible to predefine several types for a ___location by inheriting them from another module. In this example, the module for Albania inherits all of the specified types from the Europe module.
 
<syntaxhighlight lang="lua">
-- Albania
local ALB = {}
 
local util = require("Module:Road data/util")
util.addAll(ALB, require("Module:Road data/strings/EUR"))
</syntaxhighlight>
 
{{n.b.}} Only one module may be inherited at this time. <!-- if they don't intersect it might not be true -->
 
==Advanced uses==
It is possible to create multiple types based on a specified pattern using <code>ipairs</code>. In this example from [[Module:Road data/strings/USA/WA]], the <code>US 1926</code>, <code>US 1948</code>, and <code>US 1961</code> types are all created from the same code. At the bottom that is an override for <code>US 1961</code>'s <code>shieldmain</code>.
<syntaxhighlight lang="lua">
for _,year in ipairs({"1926", "1948", "1961"}) do
WA["US " .. year] = {
shield = format("US %%route%% (%s).svg", year),
shieldmain = format("US %%route%% Washington %s.svg", year),
base = WA.US.base,
name = WA.US.name,
link = WA.US.link,
abbr = WA.US.abbr,
width = "square",
}
end
 
WA["US 1961"].shieldmain = "US %route% (1961).svg"
</syntaxhighlight>
 
Similarly, subtypes can be created in the same manner. This example creates 9 subtypes each for <code>WA</code> and <code>SR</code>. The <code>aux</code> is inherited from [[Module:Road data/strings/USA]]. That, in turn, modifies <code>auxType</code> and <code>spec</code> accordingly.
 
<syntaxhighlight lang="lua">
for _,type in ipairs({'WA', 'SR'}) do
for _,auxType in ipairs({"Alt", "Bus", "Byp", "Conn", "Opt", "Scenic", "Spur", "Temp", "Truck"}) do
local spec = WA[" aux "][auxType]
WA[type .. "-" .. auxType] = {
shield = WA[type].shield,
shieldmain = WA[type].shieldmain,
name = WA[type].name .. " " .. spec.name,
link = WA[type].link .. " " .. spec.name .. suffix,
abbr = WA[type].abbr .. " " .. spec.abbrsuffix,
banner = spec.bannerprefix .. " plate.svg",
aux = spec.aux,
width = WA[type].width
}
end
end
</syntaxhighlight>
 
==Style==
There are a few style guidelines that should be followed:
# Align table fields using spacestabs. All tables should be aligned so that fields line up with each other, as shown in the above examples.
# Do not place a table's closing brace on a separate line.
# Each table field should be on its own line.
# Add spaces to either side of an assignment operator (equals sign).