Saltar al contento

Modulo:List

De Wikipedia, le encyclopedia libere
Info
Info
Documentation del modulo
Pro le documentation, per favor consulta le subpagina correspondente sur Wikipedia in anglese.

Le modification de iste modulo es limitate al usatores autoconfirmate

-- copiate e traducite desde [[:en:Special:Permalink/1224650061]]

-- Iste modulo produce varie typos de lista. Al momento es supportate le listas
-- a punctos, sin punctos, horizontal, ordinate e horizontal ordinate.

-- ATTENTION: Non simplemente reimplacia iste modulo per un nove version de enwiki;
-- conserva le adaptationes local marcate per ‘-- ADAPTATION_IA’.

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 and 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 = 'Patrono:Lista horizontal/stilo.css' } -- ADAPTATION_IA
		}
	elseif listType == 'unbulleted' then
		table.insert(data.classes, 'lista_simple') -- ADAPTATION_IA
		data.templatestyles = mw.getCurrentFrame():extensionTag{
			name = 'templatestyles', args = { src = 'Patrono:Lista simple/stilo.css' } -- ADAPTATION_IA
		}
	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 _, 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 catena 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 _, 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 _, 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 -- 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(
			"prime argumento passate a ‘makeList’ incorrecte (‘%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, {
			frameOnly = ((frame and frame.args and frame.args.frameonly or '') ~= ''),
			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 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