# HG changeset patch # User Dan Fuhry # Date 1290155470 18000 # Node ID 63539b03fa5f9e1fa4d00ccc803c5f040399ad7b # Parent 030635a70d614260af8791771623d19d11f53286 Rewrote TOC HTML tree generation. It actually generates valid, working HTML now. Also added heading name conflict resolution diff -r 030635a70d61 -r 63539b03fa5f Wikulator.php --- a/Wikulator.php Thu May 13 09:15:42 2010 -0400 +++ b/Wikulator.php Fri Nov 19 03:31:10 2010 -0500 @@ -30,6 +30,8 @@ function mediafier_draw_toc(&$text) { + static $heading_names = array(); + if ( strstr($text, '__NOTOC__') ) return true; @@ -48,46 +50,69 @@ $prev = 0; $levels = 0; $treenum = array(); - $toc = ''; + $toc = "\n"; foreach ( $heading_map as $i => $head ) { - if ( $head > $prev ) - { - $treenum[] = 0; - $levels++; - $toc .= '
'; - } - else if ( $head < $prev ) - { - if ( $levels > 1 ) - { - $toc .= '
'; - $levels--; - unset($treenum[count($treenum)-1]); - } - } + // $head = level of the heading (1-6) + + if ( $head > $prev ) + { + // deeper heading than the previous; indent + $toc .= "\n
\n "; + $levels++; + $treenum[] = 0; + } + else if ( $head < $prev ) + { + // shallower heading than the previous; go up by one + $toc .= "
\n "; + $levels--; + array_pop($treenum); + } + else + { + // same as previous; terminate it + $toc .= "\n "; + } + $treenum = array_values($treenum); if ( isset($treenum[count($treenum)-1]) ) $treenum[count($treenum)-1]++; - if ( $i > 0 ) - $toc .= ''; + if ( version_compare(enano_version(), '1.1.7', '>=') ) { $tocid = sanitize_page_id(trim($matches[2][$i])); $tocid = str_replace(array('[', ']'), '', $tocid); + + // conflict avoidance + if ( isset($heading_names[$tocid]) ) + { + $id = 2; + while ( isset($heading_names["{$tocid}{$id}"]) ) + $id++; + + $tocid .= $id; + } + $heading_names[$tocid] = true; } else { $tocid = "$i"; } - $toc .= '
' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . ''; - $prev = $head; + + $toc .= '
' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . ""; + + $prev = $head; } - while ( $levels > 0 ) + // and at the end of the loop... + $toc .= "
\n"; + while ( $levels > 1 ) { - $toc .= ''; - $levels--; + $toc .= "\n"; + $levels--; } + $toc .= "\n"; + $toc_body = "
Contents [hide]
$toc