Модуль:Вложенный список

Материал из Викитеки — свободной библиотеки
Документация Документация

Данный модуль реализует шаблоны {{Вложенный список}}. Он был создан, чтобы сделать единую точку управления всеми тремя шаблонами, из которой в дальнейшем можно будет потенциально изменить код шаблона на более доступный и семантичный.

Код вызова модуля: {{#invoke:Вложенный список|main}}.

Категории

--
-- Implements {{Вложенный список}} in one module
-- Allows us to potentially move towards a more accessible markup for the template when the time is right
-- Right now, it breaks the lists {{Вложенный список}} or {{NL2}} or {{NL3}} are in
--
local getArgs = require('Module:Arguments').getArgs

local errorCat = 'Википедия:Страницы с ошибками шаблона Вложенный список'

local noTitle = 'Нет названия страницы.'
local noContent = 'Ошибка при включении страницы «[[%s]]».'

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	
	local title = args[1]
	local displayedTitle = args[2] and '|' .. args[2] or ''
	local appendedText = args[3] and ' ' .. args[3] or ''
	
	if title == nil or title == '' then
		return '<div class="error">' .. noTitle .. '</div>'
	end
	
	local intro = string.format('<span class="dabhide">[[%s%s]]</span>%s:', title, displayedTitle, appendedText)
	-- frame:expandTemplate is used because mw.title:getContent() does not handle redirects
	local sublistExists, sublist = pcall(function ()
		return frame:expandTemplate{ title = ':' .. title }
	end)
	
	-- The page does not exist or returns empty
	if sublistExists ~= true or mw.text.trim(sublist) == '' then
		return intro .. '<div class="error">' .. string.format(noContent, title) .. '</div>'
			.. string.format('[[Category:%s]]', errorCat)
	end
	
	-- Replace list markers with HTML list openers
	sublist = mw.ustring.gsub(sublist, '^%*+', '<li>', 1)
	sublist = mw.ustring.gsub(sublist, '\n%*+', '<li>')
	
	-- Remove all line breaks to avoid breaking the list
	sublist = mw.ustring.gsub(mw.text.trim(sublist), '\n', '')
	
	-- Wrap the sublist
	sublist = '<ul>' .. sublist .. '</ul>'
	
	return intro .. sublist
end

return p