Перайсьці да зьместу

Модуль:Укладзены сьпіс

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Укладзены сьпіс/Дакумэнтацыя

require( 'strict' )
--
-- Implements {{Укладзены сьпіс}} in one module
-- Allows us to potentially move towards a more accessible markup for the template when the time is right
-- Previously, the template broke the lists {{Вложенный список}} or {{NL2}} or {{NL3}} are in
-- Heavily borrows from https://en.wikipedia.org/wiki/Module:Excerpt_slideshow
--
local getArgs = require('Модуль:Аргумэнты').getArgs

local disambigPageName = ' (неадназначнасьць)'
local errorCat = 'Вікіпэдыя:Старонкі з памылкамі ў парамэтрах шаблёнаў'

local noTitle = 'Адсутнічае назва старонкі.'
local noContent = 'Памылка пры ўлучэньні старонкі «[[%s]]».'

local p = {}

--[[
	@param {String} wikitext: Wikitext of just the list (i.e. each line is a list item)
	@param {String} symbol:   Special character used in the wikitext markup for the list, e.g. '*' or '#'
	@param {String} outerTag: Text portion of the tag for each list or sublist, e.g. 'ul' or 'ol'
	@param {String} innerTag: Text portion of the tag for each list item, e.g. 'li'
]]
local wikitextToHtmlList = function( wikitext, symbol, outerTag, innerTag )
	local listParts = {}
	for level, item in mw.ustring.gmatch( '\n' .. wikitext .. '\n', '\n(%' .. symbol .. '+)(.-)%f[\n]' ) do
	    table.insert( listParts, { level=level, item=item } )
	end
	table.insert( listParts, { level='', item='' } )
	
	local htmlList = {}
	for i, this in ipairs( listParts ) do
		local isFirstItem = ( i == 1 )
		local isLastItem = ( i == #listParts )
	    local lastLevel = isFirstItem and '' or listParts[ i - 1 ][ 'level' ]
	    local tags
	    if #lastLevel == #this.level then
	    	tags = '</'..innerTag..'><'..innerTag..'>'
	    elseif #this.level > #lastLevel then
	    	tags = string.rep( '<'..outerTag..'><'..innerTag..'>', #this.level - #lastLevel )
	    elseif isLastItem then
	    	tags = string.rep( '</'..innerTag..'></'..outerTag..'>', #lastLevel )
	    else -- ( #this.level < #lastLevel ) and not last item
	    	tags = string.rep( '</'..innerTag..'></'..outerTag..'>', #lastLevel - #this.level ) .. '</'..innerTag..'><'..innerTag..'>'
	    end
	    table.insert( htmlList, tags .. this.item )
	end
	return table.concat( htmlList )
end


--[[
	@param {String} wikitext: Wikitext excertp containg zero or more lists
	@param {String} symbol:   Special character used in the wikitext markup for the list, e.g. '*' or '#'
	@param {String} outerTag: Text portion of the tag for each list or sublist, e.g. 'ul' or 'ol'
	@param {String} innerTag: Text portion of the tag for each list item, e.g. 'li'
]]
local gsubWikitextLists = function( wikitext, symbol, outerTag, innerTag )
	-- temporarily remove list linebreaks... 
	wikitext = mw.ustring.gsub( wikitext .. '\n', '\n%' .. symbol, '¿¿¿' .. symbol )
	-- ...so we can grab the whole list (and just the list)...
	return mw.ustring.gsub(
		wikitext,
		'¿¿¿%'..symbol..'[^\n]+', 
		function( listWikitext )
			-- ...and then reinstate linebreaks...
			listWikitext = mw.ustring.gsub( listWikitext, '¿¿¿%' .. symbol, '\n' .. symbol )
			-- ...and finally do the conversion
			return wikitextToHtmlList( listWikitext, symbol, outerTag, innerTag )
		end
	)
end

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
	
	-- Render a disambig page name without its bracketed suffix
	if mw.ustring.find( title, disambigPageName, 1, true ) ~= nil then
		displayedTitle = '|' .. mw.ustring.gsub( title, '^%s*(.+)%s+%b()%s*$', '%1' )
	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( '[[Катэгорыя:%s]]', errorCat )
	end
	
	-- Replace list markers with HTML list openers
	sublist = gsubWikitextLists( '\n' .. sublist, '*', 'ul', 'li' )
	
	-- Replace double line breaks to avoid breaking the list
	sublist = mw.ustring.gsub( mw.text.trim( sublist ), '\n\n', '<p>' )
	
	-- Merge adjacent lists
	sublist = mw.ustring.gsub( mw.text.trim( sublist ), '</ul>\n<ul>', '' )
	
	-- Replace remaining
	sublist = mw.ustring.gsub( mw.text.trim( sublist ), '\n', '<br>' )
	
	-- Detect common errors
	if mw.ustring.find( sublist, 'class="error' ) ~= nil
		or mw.ustring.find( sublist, 'id="disambig"' ) ~= nil
		or mw.ustring.find( sublist, 'class="hatnote' ) ~= nil
		or mw.ustring.find( sublist, '%:%*' ) ~= nil
	then
		sublist = sublist .. string.format( '[[Катэгорыя:%s]]', errorCat )
	end
	
	return intro .. sublist
end

return p