10,992
editsAd placeholder
Module:ParseList: Difference between revisions
Jump to navigation
Jump to search
no edit summary
(Add drow and dragonborn subraces) |
No edit summary |
||
Line 23: | Line 23: | ||
local specialPlurals = { | local specialPlurals = { | ||
-- Weapons | -- Weapons | ||
Staff = "Staves", | Staff = "Staves", | ||
staff = "staves", | staff = "staves", | ||
Line 36: | Line 34: | ||
Drow = "Drow", | Drow = "Drow", | ||
drow = "drow", | drow = "drow", | ||
Dragonborn = "Dragonborn", | Dragonborn = "Dragonborn", | ||
dragonborn = "dragonborn", | dragonborn = "dragonborn", | ||
-- Creature types | -- Creature types | ||
Fey = "Fey", | Fey = "Fey", | ||
Line 69: | Line 43: | ||
} | } | ||
-- Checks if str is equal to or ends in one of the keys in specialPlurals. | |||
-- Returns the pluralized version if so, otherwise nil. | |||
local function findSpecialPlural(str) | |||
for singular, plural in pairs(specialPlurals) do | |||
if str == singular then | |||
return plural | |||
end | |||
local len = #singular | |||
local suffix = str:sub(-len, -1) | |||
if (suffix == singular) then | |||
return str:sub(1, -len - 1) .. plural | |||
end | |||
end | |||
end | |||
-- Checks if str is equal to or ends in one of the values in specialPlurals. | |||
-- Returns the singular version if so, otherwise nil. | |||
local function findSpecialSingular(str) | |||
for singular, plural in pairs(specialPlurals) do | |||
if str == plural then | |||
return singular | |||
end | |||
local len = #plural | |||
local suffix = str:sub(-len, -1) | |||
if suffix == plural then | |||
return str:sub(1, -len - 1) .. singular | |||
end | |||
end | |||
end | |||
-- Checks for a special pluralization first, then implements these rules: | |||
-- ...f -> ...ves | |||
-- ...y -> ...ies | |||
-- ...s -> ...ses | |||
-- ...ch -> ...ches | |||
-- ...sh -> ...shes | |||
-- ... -> ...s | |||
local function makePlural(str) | local function makePlural(str) | ||
local sp = | local sp = findSpecialPlural(str) | ||
if sp then | if sp then | ||
return sp | return sp | ||
end | end | ||
local | local last1 = str:sub(-1) | ||
if | if last1 == "f" then | ||
return str:sub(1, -2) .. "ves" | |||
elseif last1 == "y" then | |||
return str:sub(1, -2) .. "ies" | return str:sub(1, -2) .. "ies" | ||
elseif | elseif last1 == "s" then | ||
return str:sub( | return str .. "es" | ||
end | |||
local last2 = str:sub(-2) | |||
if last2 == "ch" or last2 == "sh" then | |||
return str .. "es" | |||
end | end | ||
return str .. "s" | return str .. "s" | ||
end | end | ||
-- Checks for a special singularization first, then implements these rules: | |||
-- ...ves -> ...f | |||
-- ...ies -> ...y | |||
-- ...ses -> ...s | |||
-- ...ches -> ...ch | |||
-- ...shes -> ...sh | |||
-- ...s -> ... | |||
-- ... -> ... | |||
local function makeSingular(str) | local function makeSingular(str) | ||
local special | local special = findSpecialSingular(str) | ||
if special then | if special then | ||
return special | return special | ||
end | end | ||
if str:sub(- | local last3 = str:sub(-3) | ||
if last3 == "ves" then | |||
return str:sub(1, -4) .. "f" | |||
elseif last3 == "ies" then | |||
return str:sub(1, -4) .. "y" | return str:sub(1, -4) .. "y" | ||
elseif last3 == "ses" then | |||
return str:sub(1, -3) | |||
end | |||
local last4 = str:sub(-4) | |||
if last4 == "ches" or last4 == "shes" then | |||
return str:sub(1, -3) | |||
end | |||
if str:sub(-1) == "s" then | |||
return str:sub(1, -2) | |||
end | end | ||
return str | return str | ||
end | end | ||
-- Applies the singular/plural transform and linkification | |||
local function processElement(str) | local function processElement(str) | ||
local | local original = str | ||
if makeElementsPlural then | if makeElementsPlural then | ||
str = makePlural(str) | |||
elseif makeElementsSingular then | elseif makeElementsSingular then | ||
str = makeSingular(str) | |||
end | end | ||
if makeElementsLinks then | if makeElementsLinks then | ||
return "[[" .. | return "[[" .. original .. "|" .. str .. "]]" | ||
end | end | ||
return str | |||
end | end | ||