https://www.carakasamhitaonline.com/mediawiki-1.32.1/index.php?title=Module:List&feed=atom&action=historyModule:List - Revision history2024-03-28T18:13:14ZRevision history for this page on the wikiMediaWiki 1.35.2https://www.carakasamhitaonline.com/mediawiki-1.32.1/index.php?title=Module:List&diff=18386&oldid=prevPallavmishra: 1 revision imported2017-08-13T11:35:06Z<p>1 revision imported</p>
<p><b>New page</b></p><div>-- This module outputs different kinds of lists. At the moment, bulleted,<br />
-- unbulleted, horizontal, ordered, and horizontal ordered lists are supported.<br />
<br />
local libUtil = require('libraryUtil')<br />
local checkType = libUtil.checkType<br />
local mTableTools = require('Module:TableTools')<br />
<br />
local p = {}<br />
<br />
local listTypes = {<br />
['bulleted'] = true,<br />
['unbulleted'] = true,<br />
['horizontal'] = true,<br />
['ordered'] = true,<br />
['horizontal_ordered'] = true<br />
}<br />
<br />
function p.makeListData(listType, args)<br />
-- Constructs a data table to be passed to p.renderList.<br />
local data = {}<br />
<br />
-- Classes<br />
data.classes = {}<br />
if listType == 'horizontal' or listType == 'horizontal_ordered' then<br />
table.insert(data.classes, 'hlist hlist-separated')<br />
elseif listType == 'unbulleted' then<br />
table.insert(data.classes, 'plainlist')<br />
end<br />
table.insert(data.classes, args.class)<br />
<br />
-- Main div style<br />
data.style = args.style<br />
<br />
-- Indent for horizontal lists<br />
if listType == 'horizontal' or listType == 'horizontal_ordered' then<br />
local indent = tonumber(args.indent)<br />
indent = indent and indent * 1.6 or 0<br />
if indent > 0 then<br />
data.marginLeft = indent .. 'em'<br />
end<br />
end<br />
<br />
-- List style types for ordered lists<br />
-- This could be "1, 2, 3", "a, b, c", or a number of others. The list style<br />
-- type is either set by the "type" attribute or the "list-style-type" CSS<br />
-- property.<br />
if listType == 'ordered' or listType == 'horizontal_ordered' then <br />
data.listStyleType = args.list_style_type or args['list-style-type']<br />
data.type = args['type']<br />
<br />
-- Detect invalid type attributes and attempt to convert them to<br />
-- list-style-type CSS properties.<br />
if data.type <br />
and not data.listStyleType<br />
and not tostring(data.type):find('^%s*[1AaIi]%s*$')<br />
then<br />
data.listStyleType = data.type<br />
data.type = nil<br />
end<br />
end<br />
<br />
-- List tag type<br />
if listType == 'ordered' or listType == 'horizontal_ordered' then<br />
data.listTag = 'ol'<br />
else<br />
data.listTag = 'ul'<br />
end<br />
<br />
-- Start number for ordered lists<br />
data.start = args.start<br />
if listType == 'horizontal_ordered' then<br />
-- Apply fix to get start numbers working with horizontal ordered lists.<br />
local startNum = tonumber(data.start)<br />
if startNum then<br />
data.counterReset = 'listitem ' .. tostring(startNum - 1)<br />
end<br />
end<br />
<br />
-- List style<br />
-- ul_style and ol_style are included for backwards compatibility. No<br />
-- distinction is made for ordered or unordered lists.<br />
data.listStyle = args.list_style<br />
<br />
-- List items<br />
-- li_style is included for backwards compatibility. item_style was included<br />
-- to be easier to understand for non-coders.<br />
data.itemStyle = args.item_style or args.li_style<br />
data.items = {}<br />
for i, num in ipairs(mTableTools.numKeys(args)) do<br />
local item = {}<br />
item.content = args[num]<br />
item.style = args['item' .. tostring(num) .. '_style']<br />
or args['item_style' .. tostring(num)]<br />
item.value = args['item' .. tostring(num) .. '_value']<br />
or args['item_value' .. tostring(num)]<br />
table.insert(data.items, item)<br />
end<br />
<br />
return data<br />
end<br />
<br />
function p.renderList(data)<br />
-- Renders the list HTML.<br />
<br />
-- Return the blank string if there are no list items.<br />
if type(data.items) ~= 'table' or #data.items < 1 then<br />
return ''<br />
end<br />
<br />
-- Render the main div tag.<br />
local root = mw.html.create('div')<br />
for i, class in ipairs(data.classes or {}) do<br />
root:addClass(class)<br />
end<br />
root:css{['margin-left'] = data.marginLeft}<br />
if data.style then<br />
root:cssText(data.style)<br />
end<br />
<br />
-- Render the list tag.<br />
local list = root:tag(data.listTag or 'ul')<br />
list<br />
:attr{start = data.start, type = data.type}<br />
:css{<br />
['counter-reset'] = data.counterReset,<br />
['list-style-type'] = data.listStyleType<br />
}<br />
if data.listStyle then<br />
list:cssText(data.listStyle)<br />
end<br />
<br />
-- Render the list items<br />
for i, t in ipairs(data.items or {}) do<br />
local item = list:tag('li')<br />
if data.itemStyle then<br />
item:cssText(data.itemStyle)<br />
end<br />
if t.style then<br />
item:cssText(t.style)<br />
end<br />
item<br />
:attr{value = t.value}<br />
:wikitext(t.content)<br />
end<br />
<br />
return tostring(root)<br />
end<br />
<br />
function p.renderTrackingCategories(args)<br />
local isDeprecated = false -- Tracks deprecated parameters.<br />
for k, v in pairs(args) do<br />
k = tostring(k)<br />
if k:find('^item_style%d+$') or k:find('^item_value%d+$') then<br />
isDeprecated = true<br />
break<br />
end<br />
end<br />
local ret = ''<br />
if isDeprecated then<br />
ret = ret .. '[[Category:List templates with deprecated parameters]]'<br />
end<br />
return ret<br />
end<br />
<br />
function p.makeList(listType, args)<br />
if not listType or not listTypes[listType] then<br />
error(string.format(<br />
"bad argument #1 to 'makeList' ('%s' is not a valid list type)",<br />
tostring(listType)<br />
), 2)<br />
end<br />
checkType('makeList', 2, args, 'table')<br />
local data = p.makeListData(listType, args)<br />
local list = p.renderList(data)<br />
local trackingCategories = p.renderTrackingCategories(args)<br />
return list .. trackingCategories<br />
end<br />
<br />
for listType in pairs(listTypes) do<br />
p[listType] = function (frame)<br />
local mArguments = require('Module:Arguments')<br />
local origArgs = mArguments.getArgs(frame)<br />
-- Copy all the arguments to a new table, for faster indexing.<br />
local args = {}<br />
for k, v in pairs(origArgs) do<br />
args[k] = v<br />
end<br />
return p.makeList(listType, args)<br />
end<br />
end<br />
<br />
return p</div>Pallavmishra