Module:ParseList: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 10: Line 10:
local useTemplateOnElements
local useTemplateOnElements
local useTwoArgTemplateOnElements
local useTwoArgTemplateOnElements
local finalJoiningWord
local textDelim
local textLastDelim


local function splitListString(listString)
local function splitListString(listString)
    local strings = {}
local strings = {}
    for str in listString:gmatch("[^" .. listDelimiters .. "]+") do
for str in listString:gmatch("[^" .. listDelimiters .. "]+") do
        table.insert(strings, str:match("^%s*(.-)%s*$"))
table.insert(strings, str:match("^%s*(.-)%s*$"))
    end
end
    return strings
return strings
end
end


Line 166: Line 167:
end
end


-- These functions implement the different output types.  The elements will have
-- These functions implement different output styles.  The elements will have
-- already gone through processElement() at this point, so they only need to be
-- already gone through processElement() at this point, so they only need to be
-- glued together to produce the desired type of listing format.
-- glued together to produce the desired style of listing format.
local converters = {
local converters = {
sentence = function (elements)
text = function (elements)
    local count = #elements
local result = ""
    if count == 0 then
local count = #elements
    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
for i, str in ipairs(elements) do
if i < count then
if i == 1 then
result = result .. str .. ", "
result = str
elseif i < count then
result = result .. textDelim .. str
else
else
result = result .. finalJoiningWord .. " " .. str
result = result .. textLastDelim .. str
end
end
    end
end
    return result
return result
end,
end,
htmlList = function (elements)
htmlList = function (elements)
Line 233: Line 229:
useTemplateOnElements = args['useTemplate']
useTemplateOnElements = args['useTemplate']
useTwoArgTemplateOnElements = args['useTemplate2']
useTwoArgTemplateOnElements = args['useTemplate2']
finalJoiningWord = args['joinWord'] or "and"
textDelim = args['textDelim']
if textDelim then
textLastDelim = args['textLastDelim'] or textDelim
else
textDelim = ', '
textLastDelim = args['textLastDelim'] or ' and '
end
local type = args['type'] or 'sentence'
local style = args['style'] or args['type'] or 'text'
local converter = converters[type]
local converter = converters[style]
local elements = {}
local elements = {}
local listString = args[1]
local listString = args[1]

Navigation menu