Title: Module:List - freem URL Source: https://freemwiki.com/index.php?oldid=579573&title=Module%3AList Markdown Content: Revision as of 16:45, 19 May 2024 by [en>Pppery](https://freemwiki.com/wiki/User:Pppery "en:User:Pppery") _Documentation for this module may be created at [Module:List/doc](https://freemwiki.com/index.php?title=Module:List/doc&action=edit&redlink=1 "Module:List/doc (page does not exist)")_ [](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-1)local libUtil = require('libraryUtil')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-2)local checkType = libUtil.checkType[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-3)local mTableTools = require(['Module:TableTools'](https://freemwiki.com/wiki/Module:TableTools))[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-4)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-5)local p = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-6)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-7)local listTypes = {[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-8)['bulleted'] = true,[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-9)['unbulleted'] = true,[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-10)['horizontal'] = true,[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-11)['ordered'] = true,[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-12)['horizontal_ordered'] = true[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-13)}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-14)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-15)function p.makeListData(listType, args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-16)-- Constructs a data table to be passed to p.renderList.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-17)local data = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-18)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-19)-- Classes and TemplateStyles[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-20)data.classes = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-21)data.templatestyles = ''[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-22)if listType == 'horizontal' or listType == 'horizontal_ordered' then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-23)table.insert(data.classes, 'hlist')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-24)data.templatestyles = mw.getCurrentFrame():extensionTag{[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-25)name = 'templatestyles', args = { src = 'Hlist/styles.css' }[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-26)}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-27)elseif listType == 'unbulleted' then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-28)table.insert(data.classes, 'plainlist')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-29)data.templatestyles = mw.getCurrentFrame():extensionTag{[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-30)name = 'templatestyles', args = { src = 'Plainlist/styles.css' }[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-31)}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-32)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-33)table.insert(data.classes, args.class)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-34)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-35)-- Main div style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-36)data.style = args.style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-37)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-38)-- Indent for horizontal lists[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-39)if listType == 'horizontal' or listType == 'horizontal_ordered' then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-40)local indent = tonumber(args.indent)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-41)indent = indent and indent * 1.6 or 0[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-42)if indent > 0 then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-43)data.marginLeft = indent .. 'em'[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-44)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-45)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-46)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-47)-- List style types for ordered lists[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-48)-- This could be "1, 2, 3", "a, b, c", or a number of others. The list style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-49)-- type is either set by the "type" attribute or the "list-style-type" CSS[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-50)-- property.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-51)if listType == 'ordered' or listType == 'horizontal_ordered' then [](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-52)data.listStyleType = args.list_style_type or args['list-style-type'][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-53)data.type = args['type'][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-54)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-55)-- Detect invalid type attributes and attempt to convert them to[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-56)-- list-style-type CSS properties.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-57)if data.type [](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-58)and not data.listStyleType[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-59)and not tostring(data.type):find('^%s*[1AaIi]%s*$')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-60)then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-61)data.listStyleType = data.type[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-62)data.type = nil[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-63)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-64)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-65)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-66)-- List tag type[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-67)if listType == 'ordered' or listType == 'horizontal_ordered' then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-68)data.listTag = 'ol'[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-69)else[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-70)data.listTag = 'ul'[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-71)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-72)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-73)-- Start number for ordered lists[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-74)data.start = args.start[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-75)if listType == 'horizontal_ordered' then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-76)-- Apply fix to get start numbers working with horizontal ordered lists.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-77)local startNum = tonumber(data.start)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-78)if startNum then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-79)data.counterReset = 'listitem ' .. tostring(startNum - 1)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-80)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-81)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-82)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-83)-- List style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-84) -- ul_style and ol_style are included for backwards compatibility. No[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-85) -- distinction is made for ordered or unordered lists.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-86)data.listStyle = args.list_style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-87)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-88)-- List items[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-89)-- li_style is included for backwards compatibility. item_style was included[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-90)-- to be easier to understand for non-coders.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-91)data.itemStyle = args.item_style or args.li_style[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-92)data.items = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-93)for _, num in ipairs(mTableTools.numKeys(args)) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-94)local item = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-95)item.content = args[num][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-96)item.style = args['item' .. tostring(num) .. '_style'][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-97)or args['item_style' .. tostring(num)][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-98)item.value = args['item' .. tostring(num) .. '_value'][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-99)or args['item_value' .. tostring(num)][](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-100)table.insert(data.items, item)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-101)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-102)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-103)return data[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-104)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-105)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-106)function p.renderList(data)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-107)-- Renders the list HTML.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-108)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-109)-- Return the blank string if there are no list items.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-110)if type(data.items) ~= 'table' or #data.items < 1 then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-111)return ''[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-112)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-113)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-114)-- Render the main div tag.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-115)local root = mw.html.create('div')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-116)for _, class in ipairs(data.classes or {}) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-117)root:addClass(class)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-118)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-119)root:css{['margin-left'] = data.marginLeft}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-120)if data.style then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-121)root:cssText(data.style)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-122)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-123)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-124)-- Render the list tag.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-125)local list = root:tag(data.listTag or 'ul')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-126)list[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-127):attr{start = data.start, type = data.type}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-128):css{[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-129)['counter-reset'] = data.counterReset,[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-130)['list-style-type'] = data.listStyleType[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-131)}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-132)if data.listStyle then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-133)list:cssText(data.listStyle)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-134)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-135)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-136)-- Render the list items[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-137)for _, t in ipairs(data.items or {}) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-138)local item = list:tag('li')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-139)if data.itemStyle then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-140)item:cssText(data.itemStyle)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-141)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-142)if t.style then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-143)item:cssText(t.style)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-144)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-145)item[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-146):attr{value = t.value}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-147):wikitext(t.content)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-148)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-149)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-150)return data.templatestyles .. tostring(root)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-151)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-152)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-153)function p.renderTrackingCategories(args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-154)local isDeprecated = false -- Tracks deprecated parameters.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-155)for k, v in pairs(args) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-156)k = tostring(k)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-157)if k:find('^item_style%d+$') or k:find('^item_value%d+$') then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-158)isDeprecated = true[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-159)break[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-160)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-161)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-162)local ret = ''[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-163)if isDeprecated then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-164)ret = ret .. '[[Category:List templates with deprecated parameters]]'[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-165)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-166)return ret[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-167)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-168)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-169)function p.makeList(listType, args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-170)if not listType or not listTypes[listType] then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-171)error(string.format([](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-172)"bad argument #1 to 'makeList' ('%s' is not a valid list type)",[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-173)tostring(listType)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-174)), 2)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-175)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-176)checkType('makeList', 2, args, 'table')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-177)local data = p.makeListData(listType, args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-178)local list = p.renderList(data)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-179)local trackingCategories = p.renderTrackingCategories(args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-180)return list .. trackingCategories[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-181)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-182)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-183)for listType in pairs(listTypes) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-184)p[listType] = function (frame)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-185)local mArguments = require(['Module:Arguments'](https://freemwiki.com/wiki/Module:Arguments))[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-186)local origArgs = mArguments.getArgs(frame, {[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-187)frameOnly = ((frame and frame.args and frame.args.frameonly or '') ~= ''),[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-188)valueFunc = function (key, value)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-189)if not value or not mw.ustring.find(value, '%S') then return nil end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-190)if mw.ustring.find(value, '^%s*[%*#;:]') then[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-191)return value[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-192)else[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-193)return value:match('^%s*(.-)%s*$')[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-194)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-195)return nil[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-196)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-197)})[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-198)-- Copy all the arguments to a new table, for faster indexing.[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-199)local args = {}[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-200)for k, v in pairs(origArgs) do[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-201)args[k] = v[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-202)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-203)return p.makeList(listType, args)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-204)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-205)end[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-206)[](https://freemwiki.com/index.php?oldid=579573&title=Module%3AList#L-207)return p