Jump to content
Wikipedia The Free Encyclopedia

Module:Road data/parser/hooks

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
Warning This Lua module is used on approximately 20,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.

This module includes hook functions that provide extra functionality to Module:Road data/parser and its associated string modules.

Basics

Each hook is simply a function stored in the p package table exported by this module. Each function accepts two arguments:

  • parameters: The table in the string module that references the hook. In this example, this argument would be the table stored in the shield field of the type table (from Module:Road data/strings/USA/KY):
KY["KY 1966"]={
shield={
hook="split",
split=100,
below="Elongated circle %route%.svg",
above="Circle sign %route%.svg"
},
link=KY.KY.link,
abbr=KY.KY.abbr
}
  • args: The arguments originally passed to the parser.

Hooks may modify the argument table by simply setting a new key as equal to a computed value. Modifying existing values is allowed, but discouraged.

The return value of a hook is an ordinary format string, which may be computed within the function or pulled from the parameters argument. Generally, if the hook does not compute the format string to be returned, the hook should return parameters.default, which should equal a format string.

Hooks

split

This hook determines the format string to be used by the parser based on whether the route number is above or below a particular number.

Parameters:

  • split: The number on which to split.
  • below: The format string to use if the route number is below split.
  • above: The format string to use if the route number is equal to or above split.

splitlen

This hook operates in a similar fashion to split, but tests the length of the route number instead of its value.

Parameters:

  • split: The length on which to split.
  • below: The format string to use if the route number's length is below split.
  • above: The format string to use if the route number's length is equal to or above split.

between

This hook determines the format string to be used by the parser based on whether the route number is between two given numbers.

Parameters:

  • lower: The lower limit of the test (inclusive).
  • upper: The upper limit of the test (exclusive).
  • yes: The format string to use if the route number is between lower and upper.
  • no: The format string to use if the route number is not between lower and upper.

mask

This hook adds an argument to the args table based on the result of applying a supplied mask to a particular argument.

Parameters:

  • base: The argument to be masked.
  • masked: The key used to store the mask result in the args table.
  • mask: The name of the module to be used as a mask, without the "Module:" prefix. The module must return a table which maps a base argument to the value stored in the masked field of args.
  • default: The format string to be processed by the parser. This string may reference the argument stored in args by this hook.

padroute

This hook zero-pads the route number so that the route number has a particular number of digits.

Parameters:

  • paddedLength: The length to which the route number should be zero-padded.
  • default: The format string to be processed by the parser. This string may reference the zero-padded route number as the paddedRoute argument.

pagename

This hook is similar to running an {{[[Template:#ifeq|#ifeq]]}} to match the title of an article to a specified value in order to display certain content. Particularly useful for images with Fair-use rationales.

Parameters:

  • article: The title against which the page name is compared.
  • iftrue: The result if article and the page name match.
  • default: The result if article and the page name do not match. Defaults to '' if not specified.

lowercase

This hook converts the route "number" to lowercase.

Parameters:

  • default: The format string to be processed by the parser. This string may reference the lowercased route number as the lowercase argument.

startswith

This hook determines whether a particular argument starts with any of the given patterns, and returns the value associated with the matching pattern.

Parameters:

  • base: The argument to test.
  • startPatterns: Key-value pairs of starting patterns and the values to return if a match is found.
  • default: The value to return if no match is found.
The above documentation is transcluded from Module:Road data/parser/hooks/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 localp={}

 localparserModuleName="Module:Road data/parser"-- SANDBOX REFERENCE

 functionp.split(parameters,args)
 localroute=tonumber(string.match(args.route,"%d+"))or0
 ifroute<parameters.splitthen
 returnparameters.below
 else
 returnparameters.above
 end
 end

 functionp.splitlen(parameters,args)
 localroute=args.route
 if#route<parameters.splitthen
 returnparameters.below
 else
 returnparameters.above
 end
 end

 functionp.between(parameters,args)
 locallower=parameters.lower
 localupper=parameters.upper
 localroute=tonumber(string.match(args.route,"%d+"))or0
 ifroute<lowerorroute>=upperthen
 returnparameters.no
 else
 returnparameters.yes
 end
 end

 functionp.mask(parameters,args)
 localbaseParam=parameters.base
 localmaskedParam=parameters.masked
 localmaskModule="Module:"..parameters.mask
 localmask=mw.loadData(maskModule)
 args[maskedParam]=mask[args[baseParam]]
 returnparameters.default
 end

 functionp.padroute(parameters,args)
 localroute=mw.ustring.match(args.route,"%d+")
 localpaddedLength=parameters.paddedLength
 args.paddedRoute=mw.ustring.match(args.route,"^%D*")..
 string.format("%0"..tostring(paddedLength).."d",route)..
 mw.ustring.match(args.route,"%D*$")
 returnparameters.default
 end

 functionp.lowercase(parameters,args)
 localroute=args.route
 args.lowercase=string.lower(route)
 returnparameters.default
 end

 --[[
 For the first element (pattern, action) in .actions such that
 args[.base] begins with pattern, return action.
 If no such element exists, return .default (nil if unspecified).
 ]]
 functionp.beginswith(parameters,args)
 localbaseParam=parameters.base
 localactions=parameters.actions
 localarg=args[baseParam]
 forpattern,actioninpairs(actions)do
 ifmw.ustring.sub(arg,1,mw.ustring.len(pattern))==patternthen
 returnaction
 end
 end
 returnparameters.default
 end

 --[[
 For the first element (pattern, action) in .actions such that
 require(Module:Road data/parser).parser(args, .entry, .path, .kind)
 matches pattern as a regular expression, return action.
 If no such element exists, return .default (nil if unspecified).

 .path and .kind are optional.
 ]]
 functionp.match(parameters,args)
 localparserModule=require(parserModuleName)
 localparser=parserModule.parser

 localentry=parameters.entry
 localpath=parameters.path
 localkind=parameters.kind
 localactions=parameters.actions
 localvalue=parser(args,entry,path,kind)
 forpattern,actioninpairs(actions)do
 ifmw.ustring.match(value,pattern)then
 returnaction
 end
 end
 returnparameters.default
 end

 functionp.iso3166Sub(parameters,args)
 localcountry=args.country
 localsubParam=parameters.sub
 localsubCode=args[subParam]

 localiso3166Module=require("Module:ISO 3166")
 args.iso3166Sub=iso3166Module.luaname{country,subCode}
 returnparameters.default
 end

 functionp.pagename(parameters,args)
 localpagename=mw.title.getCurrentTitle().text
 localarticle=parameters.article

 ifarticle==pagenamethen
 returnparameters.iftrue
 else
 returnparameters.defaultor''
 end
 end


 returnp

AltStyle によって変換されたページ (->オリジナル) /