Module:ParseList: Difference between revisions

Jump to navigation Jump to search
(Undo revision 20568 by Taylan (talk))
Tag: Undo
No edit summary
Line 2: Line 2:
local p = {}
local p = {}


-- Config options, these may be overwritten in the main function
-- Config options, may be overwritten in the main function
local delimiter = ","
local listDelimiters = ","
local makeLinks = false
local makeElementsLinks = false
local pluralize = false
local pluralizeElements = false
local joinWord = "and"
local finalJoiningWord = "and"


local function splitString(str)
function p.main(frame)
     local values = {}
local args = getArgs(frame, { frameOnly = true })
     for value in str:gmatch("[^" .. delimiter .. "]+") do
return p._main(args)
         table.insert(values, value:match("^%s*(.-)%s*$"))
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 values
     return strings
end
end


Line 23: Line 43:
}
}


local function processValue(str)
local function processElement(str)
if pluralize then
if pluralizeElements then
local plural = specialPlurals[str] or str .. "s"
local plural = specialPlurals[str] or str .. "s"
if makeLinks then
if makeElementsLinks then
return "[[" .. str .. "|" .. plural .. "]]"
return "[[" .. str .. "|" .. plural .. "]]"
else
else
Line 32: Line 52:
end
end
else
else
if makeLinks then
if makeElementsLinks then
return "[[" .. str .. "]]"
return "[[" .. str .. "]]"
else
else
Line 40: Line 60:
end
end


local function processList(listString)
local converters = {
local values = splitString(listString)
sentence = function (elements)
    local count = #values
    local count = #elements
    if count == 0 then
    if count == 0 then
    return ""
    return ""
    elseif count == 1 then
    elseif count == 1 then
    return processValue(values[1])
    return elements[1]
    elseif count == 2 then
    elseif count == 2 then
    local v1 = processValue(values[1])
return elements[1] .. " " .. finalJoiningWord .. " " .. elements[2]
    local v2 = processValue(values[2])
    end
return v1 .. " " .. joinWord .. " " .. v2
    local result = ""
    end
for i, str in ipairs(elements) do
    local result = ""
if i < count then
for i, value in ipairs(values) do
result = result .. str .. ", "
local v = processValue(value, linked)
else
if i < count then
result = result .. finalJoiningWord .. " " .. str
result = result .. v .. ", "
end
else
    end
result = result .. joinWord .. " " .. v
    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
    end
return result
    return result
end,
end
}
 
function p.toSentence(frame)
local args = getArgs(frame, { frameOnly = true })
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
return p