soo_toc Page 3 of 3

soo_toc source code

 1 : function soo_toc$atts ) {
 2 : 
 3 :     global $thisarticle;
 4 :     assert_article();
 5 : 
 6 :     extract(lAtts(array(
 7 :         'label'         => '',
 8 :         'labeltag'      => '',
 9 :         'break'         => 'li',
10 :         'wraptag'       => 'ul',
11 :         'class'         => 'toc',
12 :         'level'         => 6,
13 :         'full_url'      => '',
14 :     ), $atts));
15 : 
16 :     $full_url $full_url permlinkurl($thisarticle) : '';
17 : 
18 :     $pattern '/<h(\d)[^>]+?id\s*=\s*"([\w-]+)"[^>]*>(.+?)<\\/h\\1/';
19 :     preg_match_all($pattern$thisarticle['body'], $headingsPREG_SET_ORDER);
20 : 
21 :     $items = array();
22 : 
23 :     foreach ( $headings as $h )
24 :         if ( $h[1] <= $level ) {
25 :             $filtered[] = $h;
26 :             $items[] = '<a href="' $full_url '#' $h[2] . '">' $h[3] . '</a>';
27 :         }
28 : 
29 :     if ( !$items )
30 :         return;
31 : 
32 :     if ( $wraptag != 'ul' && $wraptag != 'ol' )
33 :         return doLabel($label$labeltag) . doWrap($items$wraptag$break$class);
34 : 
35 :     $nest_level 1;
36 :     $lines[] = soo_toc_prepend('<li>' $items[0], t$nest_level);
37 : 
38 :     for ($i 1$i count($items); $i++) {
39 :         $level_change $filtered[$i][1] - $filtered[$i-1][1];
40 :         if ($level_change 0)
41 :             for ($j 1$j <= $level_change$j++) {
42 :                 $lines[] = soo_toc_prepend("<$wraptag>"t$nest_level).
43 :                     ($level_change $j '<li>' '');
44 :                 $nest_level ++;
45 :             }
46 :         elseif ($level_change 0)
47 :             for ($j 1$j <= -$level_change$j++) {
48 :                 $nest_level --;
49 :                 $lines[] = soo_toc_prepend("</$wraptag></li>"t$nest_level);
50 :             }
51 :         $lines[] = soo_toc_prepend('<li>' $items[$i], t$nest_level);
52 :     }
53 : 
54 :     while ($nest_level 1) {
55 :         $nest_level --;
56 :         $lines[] = soo_toc_prepend("</$wraptag></li>"t$nest_level);
57 :     }
58 : 
59 :     for ($i 0$i count($lines)-1$i++)
60 :         if ( preg_match('/<li>/'$lines[$i])
61 :             && !preg_match("/<$wraptag>/"$lines[$i+1]) )
62 :                 $lines[$i] .= '</li>';
63 : 
64 :     if ( !preg_match("/<\/$wraptag>/"$lines[count($lines)-1]) )
65 :         $lines[count($lines)-1] .= '</li>';
66 : 
67 :     $out '<' $wraptag . ( $class ' class="' $class '">' '>' )
68 :         . join(n$lines) . "</$wraptag>" n;
69 : 
70 :     return doLabel($label$labeltag) . $out;
71 : }
72 : 
73 : function soo_toc_prepend($base$add$times) {
74 :     for ( $i 1$i <= $times$i++ )
75 :         $base $add $base;
76 :     return $base;
77 : }