Módulo:Lista
De Dicionário Brasileiro de Linguagem Jurídica
Ir para navegaçãoIr para pesquisar
A documentação para este módulo pode ser criada em Módulo:Lista/doc
local libUtil = require('libraryUtil')
local checkType = libUtil.checkType
local mTableTools = require('Módulo:TableTools')
local p = {}
local listTypes = {
['bulleted'] = true,
['unbulleted'] = true,
['horizontal'] = true,
['ordered'] = true,
['horizontal_ordered'] = true
}
function p.makeListData(listType, args)
-- Constrói uma tabela de dados para ser passada para p.renderList.
local data = {}
-- Classes e TemplateStyles
data.classes = {}
data.templatestyles = ''
if listType == 'horizontal' or listType == 'horizontal_ordered' then
table.insert(data.classes, 'hlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Hlist/styles.css' }
}
elseif listType == 'unbulleted' then
table.insert(data.classes, 'plainlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Lista simples/styles.css' }
}
end
local argsClass = args.class or args.classe
table.insert(data.classes, argsClass)
-- Estilo div principal
data.style = args.style or args.estilo
-- Indentação para listas horizontais
if listType == 'horizontal' or listType == 'horizontal_ordered' then
local indent
if args.indent then
indent = tonumber(args.indent)
else
indent = tonumber(args.indentar)
end
indent = indent and indent * 1.6 or 0
if indent > 0 then
data.marginLeft = indent .. 'em'
end
end
-- Tipos de estilo de lista para listas ordenadas.
-- Isto pode ser "1, 2, 3", "a, b, c" ou vários outros. O tipo de estilo de
-- lista é definido pelo atributo "type" ou pela propriedade de CSS
-- "list-style-type".
if listType == 'ordered' or listType == 'horizontal_ordered' then
data.listStyleType = args.list_style_type or args['list-style-type'] or args.lista_estilo_tipo or args['lista-estilo-tipo']
data.type = args['type'] or args['tipo']
-- Detecta atributos de tipo inválidos e tenta convertê-los em
-- propriedades de CSS de 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
-- Tipo de tag de lista
if listType == 'ordered' or listType == 'horizontal_ordered' then
data.listTag = 'ol'
else
data.listTag = 'ul'
end
-- Número inicial para listas ordenadas
data.start = args.start or args['início']
if listType == 'horizontal_ordered' then
-- Aplica correção para fazer com que os números iniciais funcionem com
-- listas ordenadas horizontais.
local startNum = tonumber(data.start)
if startNum then
data.counterReset = 'listitem ' .. tostring(startNum - 1)
end
end
-- Estilo de lista
-- ul_style e ol_style estão incluídos para compatibilidade com versões
-- anteriores. Nenhuma distinção é feita para listas que são ordenadas ou
-- que não são ordenadas.
data.listStyle = args.list_style or args.lista_estilo
-- Lista de itens
-- li_style está incluído para compatibilidade com versões anteriores.
-- item_style foi incluído para ser mais fácil de entender para os que não
-- são programadores.
data.itemStyle = args.item_style or args.li_style or args.item_estilo or args.li_estilo
data.items = {}
for _, 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)] or args['item' .. tostring(num) .. '_estilo']
or args['item_estilo' .. tostring(num)]
item.value = args['item' .. tostring(num) .. '_value']
or args['item_value' .. tostring(num)] or args['item' .. tostring(num) .. '_valor']
or args['item_valor' .. tostring(num)]
table.insert(data.items, item)
end
return data
end
function p.renderList(data)
-- Renderiza a lista em HTML.
-- Retorna a sequência (string) em branco se não houver itens na lista.
if type(data.items) ~= 'table' or #data.items < 1 then
return ''
end
-- Renderiza a tag div principal.
local root = mw.html.create('div')
for _, 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
-- Renderiza a tag da 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
-- Renderiza os itens da lista
for _, 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 data.templatestyles .. tostring(root)
end
function p.renderTrackingCategories(args)
local isDeprecated = false -- Rastreia parâmetros obsoletos.
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:!Predefinições de lista com parâmetros obsoletos]]'
end
return ret
end
function p.makeList(listType, args)
if not listType or not listTypes[listType] then
error(string.format(
"argumento #1 inválido para 'makeList' ('%s' não é um tipo de lista válido)",
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('Módulo:Arguments')
local origArgs = mArguments.getArgs(frame, {
valueFunc = function (key, value)
if not value or not mw.ustring.find(value, '%S') then return nil end
if mw.ustring.find(value, '^%s*[%*#;:]') then
return value
else
return value:match('^%s*(.-)%s*$')
end
return nil
end
})
-- Copia todos os argumentos para uma nova tabela, para uma indexação
-- mais rápida.
local args = {}
for k, v in pairs(origArgs) do
args[k] = v
end
return p.makeList(listType, args)
end
end
return p