Модуль:EnumTOC

Материал из Указатель частей и соединений РККА 1941-1945
Перейти к:навигация, поиск

Для документации этого модуля может быть создана страница Модуль:EnumTOC/doc

local p = {}

function p.makeTOC( enum )
	local function printNode( toc, idx, nolink )
		local out = {}
		local lastpart = toc[idx].part
		local child = toc[idx].down
		while child do
			local dsc = toc[child]
			local class = 'enum-toc-' .. dsc.level
			local part = dsc.part
			if part ~= lastpart then
				class = class .. ' enum-toc-part'
				lastpart = part
			end
			if part then
				part = ' (часть ' .. part .. ')'
			end
			local linkA, linkB = '', ''
			if not nolink then
				linkA = '[[РККА:Перечень № ' .. enum .. ( part or '' ) .. '#enum-' .. child .. '|'
				linkB = ']]'
			end
			table.insert( out, '<div class="' .. class .. '">' .. linkA
				.. '<span class=enum-numb>' .. dsc[1] .. '</span> <span class=enum-name>'
				.. dsc[2] .. '</span>' .. linkB .. '</div>' )
			table.insert( out, printNode( toc, child, nolink ) )
			child = dsc.right
		end
		return table.concat( out )
	end
	local toc = require( 'Module:EnumTOC/' .. enum )
	local root = 's' .. string.sub( '0' .. enum, -2, -1 )
	local nolink = string.match( mw.title.getCurrentTitle().text, ' %(оглавление(%))$' )
	if nolink then
		local part = toc[root].part
		if part then
			part = ' (часть ' .. part .. ')'
		end
		if mw.title.new( 'РККА:Перечень № ' .. enum .. ( part or '' ) ).id ~= 0 then
			nolink = nil
		end
	end
	local intro = ''
	if nolink then
		intro = '<p class="lead"><i>Этот перечень еще не включен в указатель. Пока здесь представлено оглавление перечня.</i></p>'
	end
	toc[1] = { part = toc[root].part, down = root }
	return intro .. printNode( toc, 1, nolink )
end

function p.MakeTOC( frame )
	return p.makeTOC( tonumber( frame.args[1] or 1 ) )
end
return p