-- This module outputs different kinds of lists. At the moment, bulleted, unbulleted,-- unbulleted, horizontal, ordered, and horizontal ordered lists are supported. local libUtil = require('libraryUtil')local checkType = libUtil.checkTypelocal mTableTools = require('Module:TableTools')
local p = {}
local htmlBuilder listTypes = require{ ['bulleted'] = true, ['unbulleted'] = true, ['horizontal'] = true, ['ordered'] = true, ['horizontal_ordered'] = true} function p.makeListData(listType, args) -- Constructs a data table to be passed to p.renderList. local data = {} -- Classes data.classes = {} if listType == 'horizontal' or listType == 'horizontal_ordered' then table.insert(data.classes, 'Module:HtmlBuilderhlist hlist-separated') elseif listType == 'unbulleted' then table.insert(data.classes, 'plainlist') end table.insert(data.classes, args.class) -- Main div style data.style = args.style -- Indent for horizontal lists if listType == 'horizontal' or listType == 'horizontal_ordered' then local indent = tonumber(args.indent) indent = indent and indent * 1.6 or 0 if indent > 0 then data.marginLeft = indent .. 'em' end end -- List style types for ordered lists -- This could be "1, 2, 3", "a, b, c", or a number of others. The list style -- type is either set by the "type" attribute or the "list-style-type" CSS -- property. if listType == 'ordered' or listType == 'horizontal_ordered' then data.listStyleType = args.list_style_type or args['list-style-type'] data.type = args['type']
local function getListItem(data, -- Detect invalid type attributes and attempt to convert them to -- list-style, itemStyle, itemValue)-type CSS properties. if data.type and not data .listStyleType and not tostring(data.type):find('^%s*[1AaIi]%s*$') then data.listStyleType = data.type data.type = nil return cend
end
local item -- List tag type if listType == 'ordered' or listType == htmlBuilder.create('lihorizontal_ordered') itemthen data.cssText(style)listTag = 'ol' .cssText(itemStyle) else .wikitext(data) if(itemValue) then item.attr(listTag = 'valueul',itemValue)
end
return tostring(item)
end
local function getArgNums(args) -- Returns an array containing the keys of all positional argumentsStart number for ordered lists -- that contain data (i.estart = args. nonstart if listType == 'horizontal_ordered' then -whitespace values)- Apply fix to get start numbers working with horizontal ordered lists. local nums startNum = {} for k, v in pairstonumber(argsdata.start) do if type(k) == 'number' and startNum then k >= 1 and mathdata.floor(k) =counterReset = k and mw.ustring.match(v, '%Slistitem ') then table.insert. tostring(nums, kstartNum - 1)
end
end
-- List style -- ul_style and ol_style are included for backwards compatibility. No -- distinction is made for ordered or unordered lists. data.listStyle = args.list_style -- List items -- li_style is included for backwards compatibility. item_style was included -- to be easier to understand for non-coders. data.itemStyle = args.item_style or args.li_style data.items = {} for i, num in ipairs(mTableTools.numKeys(args)) do local item = {} item.content = args[num] item.style = args['item' .. tostring(num) .. '_style'] or args['item_style' .. tostring(num)] item.value = args['item' .. tostring(num) .. '_value'] or args['item_value' .. tostring(num)] table.sortinsert(numsdata.items, item) end return numsdata
end
function p.makeListrenderList(listType, argsdata) -- This is Renders the main function to be called from other Lua moduleslist HTML. -- First, get Return the blank string if there are no list items. local listItems = {} local argNums = getArgNumsif type(args) for i, num in ipairs(argNumsdata.items) do local item ~= getListItem( args[num], args.item_style or args.li_style, -- li_style is included for backwards compatibility. item_style was included to be easier to understand for non-coders. args['item_styletable' .. tostring(num)] or args['li_style' #data.. tostring(num)], args['item_value' .. tostring(num)] ) table.insert(listItems, item) end if #listItems == 0 items < 1 then
return ''
end
-- Build Render the opening main div tag. local root = htmlBuildermw.html.create('div') if listType == 'horizontal' for i, class in ipairs(data.classes or listType == 'horizontal_ordered' then root.addClass('hlist'{}) elseif listType == 'unbulleted' thendo root.:addClass('plainlist'class)
end
root.addClass(args.class) if listType == :css{['horizontalmargin-left' or listType ] == 'horizontal_ordered' then local indent = tonumber(argsdata.indent)marginLeft} indent = tostring((indent and indent * 1 if data.6) or 0)style then root.css:cssText('margin-left', indent .data. 'em'style)
end
-- If we are outputting an ordered list or an ordered horizontal list, get Render the type and list-style-typetag. -- Horizontal ordered lists arenlocal list = root:tag(data.listTag or 'ul't supported yet, but including them anyway in case support is ever added to the CSS backend.) local listStyleType, typeAttrlist if listType :attr{start =data.start, type = data.type} :css{ ['orderedcounter-reset' or listType ] == 'horizontal_ordered' then data.counterReset, listStyleType = args.list_style_type or args ['list-style-type']= data.listStyleType typeAttr = args['type']} if data.listStyle then list:cssText(data.listStyle)
end
-- Detect if Render the type attribute specified by args.type is using an invalid valuelist items for i, and if so try passing it as a list-style-type CSS propertyt in ipairs(data.items or {}) do if typeAttr and not typeAttr local item = list:matchtag('^%s*[1AaIi]%s*$li') then if not listStyleType data.itemStyle then listStyleType = typeAttr typeAttr = nilitem:cssText(data.itemStyle)
end
if t.style then
item:cssText(t.style)
end
item
:attr{value = t.value}
:wikitext(t.content)
end
root.cssText(args.style) -- Build the list tags and list items. local list = root.tag((listType == 'ordered' or listType == 'horizontal_ordered') and 'ol' or 'ul') list .attr('start', args.start) .attr('type', typeAttr) .css('list-style-type', listStyleType) .cssText(args.list_style or args.ul_style or args.ol_style) -- ul_style and ol_style are included for backwards compatibility. No distinction is made for ordered or unordered lists. .wikitext(table.concat(listItems))
return tostring(root)
end
local function makeWrapperp.renderTrackingCategories(listTypeargs) return function(frame) local origArgs if frame isDeprecated == mwfalse -- Tracks deprecated parameters.getCurrentFrame() then origArgs = frame:getParent().args for k, v in pairs(frame.args) do origArgs k = frame.argstostring(k) break if k:find('^item_style%d+$') or k:find('^item_value%d+$') then end elseisDeprecated = true origArgs = framebreak
end
end local ret = '' if isDeprecated then ret = ret .. '[[Category:List templates with deprecated parameters]]' end return retend function p.makeList(listType, args) if not listType or not listTypes[listType] then error(string.format( "bad argument #1 to 'makeList' ('%s' is not a valid list type)", tostring(listType) ), 2) end checkType('makeList', 2, args, 'table') local data = p.makeListData(listType, args) local list = p.renderList(data) local trackingCategories = p.renderTrackingCategories(args) return list .. trackingCategoriesend for listType in pairs(listTypes) do p[listType] = function (frame) local mArguments = require('Module:Arguments') local origArgs = mArguments.getArgs(frame) -- Copy all the arguments to a new table, for faster indexing.
local args = {}
for k, v in pairs(origArgs) do
if type(k) == 'number' or v ~= '' then args[k] = v end
end
return p.makeList(listType, args)
end
end
local funcNames = {'bulleted', 'unbulleted', 'horizontal', 'ordered', 'horizontal_ordered'}
for _, funcName in ipairs(funcNames) do
p[funcName] = makeWrapper(funcName)
end
return p