Module:List
Pro le documentation, per favor consulta le subpagina correspondente sur Wikipedia in anglese.
-- Iste modulo produce varie typos de lista. Al momento es supportate le listas
-- a punctos, sin punctos, horizontal, ordinate e horizontal ordinate.
local libUtil = require('libraryUtil')
local checkType = libUtil.checkType
local mTableTools = require('Module:TableTools')
local p = {}
local listTypes = {
['bulleted'] = true,
['unbulleted'] = true,
['horizontal'] = true,
['ordered'] = true,
['horizontal_ordered'] = true
}
function p.makeListData(listType, args)
-- Construe un tabella de datos a passar a p.renderList.
local data = {}
-- Classes
data.classes = {}
if listType == 'horizontal' or listType == 'horizontal_ordered' then
table.insert(data.classes, 'hlist')
elseif listType == 'unbulleted' then
table.insert(data.classes, 'plainlist')
end
table.insert(data.classes, args.class)
-- Stilo div principal
data.style = args.style
-- Indentation pro listas horizontal
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
-- Typos de stilo de lista pro listas ordinate
-- Isto pote esser "1, 2, 3", "a, b, c" o plure alteres. Le typo de stilo de
-- lista es definite per le attributo "type" o per le proprietate CSS
-- "list-style-type".
if listType == 'ordered' or listType == 'horizontal_ordered' then
data.listStyleType = args.list_style_type or args['list-style-type']
data.type = args['type']
-- Detege attributos de typo invalide e essaya de converter los a
-- proprietates CSS list-style-type.
if data.type
and not data.listStyleType
and not tostring(data.type):find('^%s*[1AaIi]%s*$')
then
data.listStyleType = data.type
data.type = nil
end
end
-- Typo de etiquetta de lista
if listType == 'ordered' or listType == 'horizontal_ordered' then
data.listTag = 'ol'
else
data.listTag = 'ul'
end
-- Numero initial pro listas ordinate
data.start = args.start
if listType == 'horizontal_ordered' then
-- Apply fix to get start numbers working with horizontal ordered lists.
local startNum = tonumber(data.start)
if startNum then
data.counterReset = 'listitem ' .. tostring(startNum - 1)
end
end
-- Stilo de lista
-- ul_style e ol_style es includite pro retrocompatibilitate. Non
-- se distingue inter listas ordinate e non ordinate.
data.listStyle = args.list_style
-- Elementos de lista
-- li_style es includite pro retrocompatibilitate. item_style ha essite
-- includite pro esser facile a comprender pro non programmatores.
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.insert(data.items, item)
end
return data
end
function p.renderList(data)
-- Rende le codice HTML pro le lista.
-- Retorna le string vacue si il non ha elementos de lista.
if type(data.items) ~= 'table' or #data.items < 1 then
return ''
end
-- Rende le etiquetta div principal.
local root = mw.html.create('div')
for i, class in ipairs(data.classes or {}) do
root:addClass(class)
end
root:css{['margin-left'] = data.marginLeft}
if data.style then
root:cssText(data.style)
end
-- Rende le etiquetta del lista.
local list = root:tag(data.listTag or 'ul')
list
:attr{start = data.start, type = data.type}
:css{
['counter-reset'] = data.counterReset,
['list-style-type'] = data.listStyleType
}
if data.listStyle then
list:cssText(data.listStyle)
end
-- Rende le elementos del lista.
for i, t in ipairs(data.items or {}) do
local item = list:tag('li')
if data.itemStyle then
item:cssText(data.itemStyle)
end
if t.style then
item:cssText(t.style)
end
item
:attr{value = t.value}
:wikitext(t.content)
end
return tostring(root)
end
function p.renderTrackingCategories(args)
local isDeprecated = false -- Tracia parametros obsolescente.
for k, v in pairs(args) do
k = tostring(k)
if k:find('^item_style%d+$') or k:find('^item_value%d+$') then
isDeprecated = true
break
end
end
local ret = ''
if isDeprecated then
ret = ret .. '[[Categoria:Patronos de lista con parametros obsolescente]]'
end
return ret
end
function p.makeList(listType, args)
if not listType or not listTypes[listType] then
error(string.format(
"bad argument #1 to 'makeList' ('%s' non es un typo de lista valide)",
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 .. trackingCategories
end
for listType in pairs(listTypes) do
p[listType] = function (frame)
local mArguments = require('Module:Arguments')
local origArgs = mArguments.getArgs(frame)
-- Copia tote le parametros a un nove tabella pro indexation plus rapide.
local args = {}
for k, v in pairs(origArgs) do
args[k] = v
end
return p.makeList(listType, args)
end
end
return p