Module:ParseList
Doc page: Module:ParseList/doc
This module allows you to process a list of values (separated by a comma by default) and display them in a variety of useful formats.
{{#invoke:ParseList|main|Fighter, Cleric, Wizard}}
Becomes:
- Script error: The function "main" does not exist.
Parameters
The first parameter is the list to be processed. The remaining parameters are optional:
parameter | default | meaning |
---|---|---|
style |
'text '
|
The desired output format, see below for possibilities. |
type |
Deprecated synonym for style .
| |
delimiter |
', '
|
The character that should serve as a delimiter of the input list. This can actually be a string of multiple characters which will all work as a delimiter, but that usage is discouraged as it may cause confusion. |
makePlural |
(empty) | If provided and not blank, means that each element will be turned into a plural word. |
makeSingular |
(empty) | If provided and not blank, means that each element will be turned into singular. |
makeLowercase |
(empty) | If provided and not blank, means that each element will be made all-lowercase. |
makeLinks |
(empty) |
If provided and not blank, means that each element will be made into a link. If any transforms like plural, singular, or lowercase were specified, the original element will be the link destination, and the transformed version the link text. E.g., the list element "Apple" would become |
useTemplate |
(empty) |
If provided, will use the provided template on each element, i.e., turn every This is done after any transforms like plural, singular and lowercase. E.g., the list element "Apple" would become |
useTemplate2 |
(empty) |
If provided, will use the provided template on each element and its transformed form as two template arguments. E.g., the list element "Apple" would become |
textDelim |
', ' |
If Setting this causes the default value for Note that the default value is not just a comma; it's a comma followed by a space character. |
textLastDelim |
(conditional) |
If If If Note: The use of |
The automatic plural/singular transforms are able to recognize certain irregular plurals and handle them correctly, such as "thieves" and "quarterstaves." This is handled partly through a table of explicit special conversions like staff -> staves
, and partly through logical rules like ...f --> ...ves
. If you find a word that produces a wrong result, it should be added to the table of explicit conversions.
The makeLink
, useTemplate
, and useTemplate2
arguments are mutually exclusive; when several are provided, only the first will take effect.
Output styles
The value of the style
parameter can be the following:
value | example output |
---|---|
text
|
Script error: The function "main" does not exist. |
simpleList
|
Script error: The function "main" does not exist. |
htmlList
|
Script error: The function "main" does not exist. |
htmlListNoBullets
|
Script error: The function "main" does not exist. |
htmlListNoBulletsOrMargin
|
Script error: The function "main" does not exist. |
tableList
|
Script error: The function "main" does not exist. |
none
|
Script error: The function "main" does not exist. |
The style none
is useful if each element is being transformed into an HTML element such that no textual delimiters are desired.
Examples
Make Links
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes }}
Result:
- Script error: The function "main" does not exist.
Make plural
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makePlural = yes }}
Result:
- Script error: The function "main" does not exist.
Make Plural Links
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes }}
Result:
- Script error: The function "main" does not exist.
Make Plural Links, end with "or"
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | textLastDelim = <nowiki>, or </nowiki> }}
Result:
- Script error: The function "main" does not exist.
Make Plural Links, turn into HTML list
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | style = htmlList }}
Result:
Script error: The function "main" does not exist.
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- Config options, these may be overwritten in the main function
local delimiter = ","
local makeLinks = false
local pluralize = false
local joinWord = "and"
local function splitString(str)
local values = {}
for value in str:gmatch("[^" .. delimiter .. "]+") do
table.insert(values, value:match("^%s*(.-)%s*$"))
end
return values
end
local specialPlurals = {
Thief = "Thieves",
thief = "thieves",
Quarterstaff = "Quarterstaves",
quarterstaff = "quarterstaves",
}
local function processValue(str)
if pluralize then
local plural = specialPlurals[str] or str .. "s"
if makeLinks then
return "[[" .. str .. "|" .. plural .. "]]"
else
return plural
end
else
if makeLinks then
return "[[" .. str .. "]]"
else
return str
end
end
end
local function processList(listString)
local values = splitString(listString)
local count = #values
if count == 0 then
return ""
elseif count == 1 then
return processValue(values[1])
elseif count == 2 then
local v1 = processValue(values[1])
local v2 = processValue(values[2])
return v1 .. " " .. joinWord .. " " .. v2
end
local result = ""
for i, value in ipairs(values) do
local v = processValue(value, linked)
if i < count then
result = result .. v .. ", "
else
result = result .. joinWord .. " " .. v
end
end
return result
end
function p.toSentence(frame)
local args = getArgs(frame, {
frameOnly = true,
removeBlanks = false,
})
delimiter = args['delimiter'] or delimiter
makeLinks = args['makeLinks'] or makeLinks
pluralize = args['pluralize'] or pluralize
joinWord = args['joinWord'] or joinWord
return processList(frame.args[1])
end
return p