10,916
editsAd placeholder
Module:ParseList: Difference between revisions
Jump to navigation
Jump to search
no edit summary
Tag: Undo |
No edit summary |
||
Line 2: | Line 2: | ||
local p = {} | local p = {} | ||
-- Config options, | -- Config options, may be overwritten in the main function | ||
local | local listDelimiters = "," | ||
local | local makeElementsLinks = false | ||
local | local pluralizeElements = false | ||
local | local finalJoiningWord = "and" | ||
local function | function p.main(frame) | ||
local | local args = getArgs(frame, { frameOnly = true }) | ||
for | return p._main(args) | ||
table.insert( | end | ||
function p._main(args) | |||
listDelimiters = args['delimiter'] or listDelimiters | |||
makeElementsLinks = args['makeLinks'] or makeElementsLinks | |||
pluralizeElements = args['pluralize'] or pluralizeElements | |||
finalJoiningWord = args['joiningWord'] or finalJoiningWord | |||
local type = args['type'] or 'sentence' | |||
local converter = converters[type] | |||
local elements = {} | |||
for str in splitListString(args[1]) do | |||
table.insert(elements, processElement(str)) | |||
end | |||
return converter(elements) | |||
end | |||
local function splitListString(listString) | |||
local strings = {} | |||
for str in listString:gmatch("[^" .. listDelimiters .. "]+") do | |||
table.insert(strings, str:match("^%s*(.-)%s*$")) | |||
end | end | ||
return | return strings | ||
end | end | ||
Line 23: | Line 43: | ||
} | } | ||
local function | local function processElement(str) | ||
if | if pluralizeElements then | ||
local plural = specialPlurals[str] or str .. "s" | local plural = specialPlurals[str] or str .. "s" | ||
if | if makeElementsLinks then | ||
return "[[" .. str .. "|" .. plural .. "]]" | return "[[" .. str .. "|" .. plural .. "]]" | ||
else | else | ||
Line 32: | Line 52: | ||
end | end | ||
else | else | ||
if | if makeElementsLinks then | ||
return "[[" .. str .. "]]" | return "[[" .. str .. "]]" | ||
else | else | ||
Line 40: | Line 60: | ||
end | end | ||
local | 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 | |||
result = result .. | end | ||
end | |||
result = result .. | return result | ||
end, | |||
htmlList = function (elements) | |||
local result = "" | |||
for str in elements do | |||
result = result .. "* " .. str | |||
end | |||
end, | |||
simpleList = function (elements) | |||
local result = "" | |||
local first = true | |||
for str in elements do | |||
if first then | |||
result = str | |||
first = false | |||
else | |||
result = result .. ", " .. str | |||
end | |||
end | end | ||
return result | |||
end, | |||
end | } | ||
return p | return p |