• Модуль: wiki
  • Путь к файлу: ~/bitrix/modules/wiki/classes/general/wiki_parser.php
  • Класс: CWikiParser
  • Вызов: CWikiParser::processToc
function processToc($text)
{
	$matches = array();
	$sToc = '';
	if (preg_match_all('/d)(?.*)>(?.*)>/isU'.BX_UTF_PCRE_MODIFIER, $text, $matches, PREG_SET_ORDER))
	{
		if (count($matches) > 4)
		{
			$iCurrentToc = 1;
			// work level TOC
			$iCurrentTocLevel = 0;
			// previous user defined level of TOC
			$iRealPrevItemTocLevel = 1;
			// previous working level of TOC
			$iPrevItemTocLevel = 0;
			// current user defined the level of TOC
			$iRealItemTocLevel = 1;

			$bfirst = true;
			$aNumToc = array();
			foreach ($matches as $_m =>  $arMatch)
			{
				$iRealItemTocLevel = (int)$arMatch['level'];
				$sItemToc = trim($arMatch['innerHtml']);
				// normalize levels
				if ($bfirst && $iRealPrevItemTocLevel < $iRealItemTocLevel)
					$iItemTocLevel = 1;
				else if ($iCurrentTocLevel == 1 && $iRealItemTocLevel < $iRealPrevItemTocLevel)
					$iItemTocLevel = $iCurrentTocLevel;
				else if ($iRealItemTocLevel > $iRealPrevItemTocLevel)
					$iItemTocLevel = $iCurrentTocLevel + 1;
				else if ($iRealItemTocLevel < $iRealPrevItemTocLevel)
				{
					$_delta = $iRealPrevItemTocLevel - $iRealItemTocLevel;
					$iItemTocLevel = $iCurrentTocLevel - $_delta;
					if ($iItemTocLevel < 1)
						$iItemTocLevel = 1;
				}
				else
					$iItemTocLevel = $iCurrentTocLevel;

				// create a numbering of TOC
				$iCurrentNumTocLevel = $bfirst ? 1 : $iItemTocLevel;
				$aNumToc[$iCurrentNumTocLevel] =  !isset($aNumToc[$iCurrentNumTocLevel]) ? 1 : $aNumToc[$iCurrentNumTocLevel] + 1;
				if ($iItemTocLevel < $iPrevItemTocLevel)
				{
					for ($i = $iItemTocLevel + 1; $i <= $iPrevItemTocLevel; $i++)
						unset($aNumToc[$i]);
				}

				// build a TOC
				if ($iItemTocLevel > $iCurrentTocLevel || empty($sToc))
				{
					$iCurrentTocLevel++;
					$sToc .= '
    '; } else if ($iItemTocLevel < $iCurrentTocLevel) { if ($iItemTocLevel <= 0) $iItemTocLevel = 1; if ($iCurrentTocLevel > 1) { for ($i = 0; $i < ($iCurrentTocLevel - $iItemTocLevel); $i++) $sToc .= '
'; } else $sToc .= ''; if ($iCurrentTocLevel > 1) $iCurrentTocLevel = $iItemTocLevel; } $iRealPrevItemTocLevel = $iRealItemTocLevel; $iPrevItemTocLevel = $iItemTocLevel; $bfirst = false; $sNumToc = implode('.', $aNumToc); $sItemTocId = str_replace(array('%', '+', '.F2', '..'), array('.', '.', '_', '.'), rawurlencode($sItemToc.$sNumToc)); $sToc .= '
  • '.$sNumToc.' '.strip_tags($sItemToc).'
  • '; $matches[$_m]['innerHtml'] = $sItemToc; $matches[$_m]['tocId'] = $sItemTocId; } for ($i = $iCurrentTocLevel; $i > 0; $i--) $sToc .= ''; $bfirst = true; foreach ($matches as $arMatch) { $sReplase = ''.$arMatch['innerHtml'].''; if ($bfirst) $sReplase = $sToc.'
    '.$sReplase; // so as not to replace all of the same titles $text = preg_replace('/'.preg_quote($arMatch[0], '/').'/'.BX_UTF_PCRE_MODIFIER, $sReplase, $text, 1); $bfirst = false; } } } return $text; }