. This file code is inspired by MediaWiki ideas. */ function tpl_function_toc_anchor($level, $title) { $anchor = preg_replace('!\s+!', '_', trim($title)); $anchor = preg_replace('![^a-z0-9_-]+!i', '', $anchor); return 'h'.$level.'-'.$anchor; } function tpl_function_toc_unindent($level) { return ' ' . str_repeat(' ', $level>0 ? $level : 0); } function tpl_function_toc_line($level, $anchor, $title) { return '
  • '.htmlspecialchars(trim($title)).''; } function tpl_function_toc($params, $tpl, $content=false) { if ($content === false && empty($params['page'])) return nk_plugin_error("{toc} a besoin soit du paramètre page soit d'être utilisé dans une page."); if ($content === false) { global $nk; $content = $nk->getPage($params['page']); } preg_match_all('!(.+)!Usi', $content, $match, PREG_SET_ORDER); if (count($match) < 2) return ''; $level = 0; $toc_level = 0; $prev_level = 0; $prev_toc_level = 0; $sub_level_count = array(); $level_count = array(); $html = '

    Sommaire

    '; foreach ($match as $m) { if ($toc_level) { $prev_level = $level; $prev_toc_level = $toc_level; } $level = (int)$m[1]; $current_title = $m[2]; $current_anchor = tpl_function_toc_anchor($level, $current_title); $content = str_replace($m[0], ''.$current_title.'', $content); if ($level > $prev_level) { $toc_level++; $sub_level_count[$toc_level] = 0; $prev_toc_level = $toc_level; $html .= '
      '; } elseif ($level < $prev_level && $toc_level > 1) { if ($toc_level == 2 && $level <= $level_count[1]) { $toc_level = 1; } else { for ($i = $toc_level; $i > 0; $i--) { if ($level_count[$i] == $level) { $toc_level = $i; break; } elseif ($level_count[$i] < $level) { $toc_level = $i + 1; break; } } } $html .= tpl_function_toc_unindent($prev_toc_level - $toc_level); $prev_toc_level = $toc_level; } else { $html .= ' '; } $level_count[$toc_level] = $level; $sub_level_count[$toc_level]++; $html .= tpl_function_toc_line($toc_level, $current_anchor, $current_title); } if ($prev_toc_level > 0) { $html .= tpl_function_toc_unindent($prev_toc_level - 1); } $html .= '
    '; return $html; } ?>