Module:ParseList: Difference between revisions
(Add dragonborn) |
No edit summary |
||
Line 33: | Line 33: | ||
["Half-Elf"] = "Half-Elves", | ["Half-Elf"] = "Half-Elves", | ||
["half-elf"] = "half-elves", | ["half-elf"] = "half-elves", | ||
["High Half-Elf"] = "High Half-Elves", | |||
["high half-elf"] = "high half-elves", | |||
Dwarf = "Dwarves", | Dwarf = "Dwarves", | ||
dwarf = "dwarves", | dwarf = "dwarves", |
Revision as of 21:39, 24 July 2023
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:
- Fighter, Cleric, and Wizard
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
|
Fighter, Cleric, and Wizard |
simpleList
|
Fighter, Cleric, and Wizard |
htmlList
|
Fighter, Cleric, and Wizard |
htmlListNoBullets
|
Fighter, Cleric, and Wizard |
htmlListNoBulletsOrMargin
|
Fighter, Cleric, and Wizard |
tableList
|
Fighter, Cleric, and Wizard |
none
|
Fighter, Cleric, and Wizard |
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:
Make plural
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makePlural = yes }}
Result:
- Fighters, Clerics, and Wizards
Make Plural Links
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes }}
Result:
Make Plural Links, end with "or"
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | textLastDelim = <nowiki>, or </nowiki> }}
Result:
Make Plural Links, turn into HTML list
{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | style = htmlList }}
Result:
Fighters, Clerics, and Wizards
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- Config options, set in main function
local listDelimiters
local makeElementsLinks
local makeElementsPlural
local makeElementsSingular
local finalJoiningWord
local function splitListString(listString)
local strings = {}
for str in listString:gmatch("[^" .. listDelimiters .. "]+") do
table.insert(strings, str:match("^%s*(.-)%s*$"))
end
return strings
end
-- We don't need to cover most regular English words, as they're very unlikely
-- to ever be used with this module. The kind of stuff you want to add here is
-- gameplay elements like classes, weapons, races, etc. that you may want to
-- pluralize in a listing.
local specialPlurals = {
-- Classes
Thief = "Thieves",
thief = "thieves",
-- Weapons
Quarterstaff = "Quarterstaves",
quarterstaff = "quarterstaves",
-- Races
Elf = "Elves",
elf = "elves",
["Half-Elf"] = "Half-Elves",
["half-elf"] = "half-elves",
["High Half-Elf"] = "High Half-Elves",
["high half-elf"] = "high half-elves",
Dwarf = "Dwarves",
dwarf = "dwarves",
Gith = "Gith",
gith = "gith",
Githyanki = "Githyanki",
githyanki = "githyanki",
Duergar = "Duergar",
duergar = "duergar",
Drow = "Drow",
drow = "drow",
Dragonborn = "Dragonborn",
dragonborn = "dragonborn",
-- Creature types
Fey = "Fey",
fey = "fey",
Undead = "Undead",
undead = "undead",
}
local function makePlural(str)
local sp = specialPlurals[str]
if sp then
return sp
end
if str:sub(-1) == "y" then
return str:sub(1, -2) .. "ies"
end
return str .. "s"
end
local function makeSingular(str)
local special
for k, v in pairs(specialPlurals) do
if v == str then
special = k
break
end
end
if special then
return special
end
if str:sub(-3) == "ies" then
return str:sub(1, -4) .. "y"
end
return str:sub(1, -2)
end
local function processElement(str)
local modified = str
if makeElementsPlural then
modified = makePlural(str)
elseif makeElementsSingular then
modified = makeSingular(str)
end
if makeElementsLinks then
return "[[" .. str .. "|" .. modified .. "]]"
else
return modified
end
end
local converters = {
sentence = function (elements)
local count = #elements
if count == 0 then
return ""
elseif count == 1 then
return elements[1]
elseif count == 2 then
return elements[1] .. " " .. finalJoiningWord .. " " .. elements[2]
end
local result = ""
for i, str in ipairs(elements) do
if i < count then
result = result .. str .. ", "
else
result = result .. finalJoiningWord .. " " .. str
end
end
return result
end,
htmlList = function (elements)
local result = ""
for i, str in ipairs(elements) do
result = result .. "* " .. str .. "\n"
end
return result
end,
simpleList = function (elements)
local result = ""
local first = true
for i, str in ipairs(elements) do
if first then
result = str
first = false
else
result = result .. ", " .. str
end
end
return result
end,
}
function p.main(frame)
local args = getArgs(frame, { frameOnly = true })
return p._main(args)
end
function p._main(args)
listDelimiters = args['delimiter'] or ","
makeElementsLinks = args['makeLinks']
makeElementsPlural = args['makePlural']
makeElementsSingular = args['makeSingular']
finalJoiningWord = args['joinWord'] or "and"
local type = args['type'] or 'sentence'
local converter = converters[type]
local elements = {}
local listString = args[1]
local strings = splitListString(listString)
for i, str in ipairs(strings) do
table.insert(elements, processElement(str))
end
return converter(elements)
end
return p