Wikulator.php
changeset 6 63539b03fa5f
parent 4 a803741a5fc0
equal deleted inserted replaced
5:030635a70d61 6:63539b03fa5f
    28   mediafy_process_references($text);
    28   mediafy_process_references($text);
    29 }
    29 }
    30 
    30 
    31 function mediafier_draw_toc(&$text)
    31 function mediafier_draw_toc(&$text)
    32 {
    32 {
       
    33 	static $heading_names = array();
       
    34 	
    33   if ( strstr($text, '__NOTOC__') )
    35   if ( strstr($text, '__NOTOC__') )
    34     return true;
    36     return true;
    35   
    37   
    36   if ( !preg_match_all('/^\s*([=]{1,6})([^\r\n]+)\\1\s*$/m', $text, $matches) )
    38   if ( !preg_match_all('/^\s*([=]{1,6})([^\r\n]+)\\1\s*$/m', $text, $matches) )
    37     return true;
    39     return true;
    46     return true;
    48     return true;
    47   
    49   
    48   $prev = 0;
    50   $prev = 0;
    49   $levels = 0;
    51   $levels = 0;
    50   $treenum = array();
    52   $treenum = array();
    51   $toc = '';
    53   $toc = "\n";
    52   foreach ( $heading_map as $i => $head )
    54   foreach ( $heading_map as $i => $head )
    53   {
    55   {
    54     if ( $head > $prev )
    56   	// $head = level of the heading (1-6)
    55     {
    57   	  
    56       $treenum[] = 0;
    58   	if ( $head > $prev )
    57       $levels++;
    59   	{
    58       $toc .= '<dl>';
    60   		// deeper heading than the previous; indent
    59     }
    61   		$toc .= "\n    <dl>\n  ";
    60     else if ( $head < $prev )
    62   		$levels++;
    61     {
    63   		$treenum[] = 0;
    62       if ( $levels > 1 )
    64   	}
    63       {
    65   	else if ( $head < $prev )
    64         $toc .= '</dl>';
    66   	{
    65         $levels--;
    67   		// shallower heading than the previous; go up by one
    66         unset($treenum[count($treenum)-1]);
    68   		$toc .= "</dd></dl></dd>\n  ";
    67       }
    69   		$levels--;
    68     }
    70   		array_pop($treenum);
       
    71   	}
       
    72   	else
       
    73   	{
       
    74   		// same as previous; terminate it
       
    75   		$toc .= "</dd>\n  ";
       
    76   	}
       
    77   	
    69     $treenum = array_values($treenum);
    78     $treenum = array_values($treenum);
    70     if ( isset($treenum[count($treenum)-1]) )
    79     if ( isset($treenum[count($treenum)-1]) )
    71       $treenum[count($treenum)-1]++;
    80       $treenum[count($treenum)-1]++;
    72     if ( $i > 0 )
    81   	
    73       $toc .= '</dd>';
       
    74     if ( version_compare(enano_version(), '1.1.7', '>=') )
    82     if ( version_compare(enano_version(), '1.1.7', '>=') )
    75     {
    83     {
    76 		$tocid = sanitize_page_id(trim($matches[2][$i]));
    84 		$tocid = sanitize_page_id(trim($matches[2][$i]));
    77 		$tocid = str_replace(array('[', ']'), '', $tocid);
    85 		$tocid = str_replace(array('[', ']'), '', $tocid);
       
    86 		
       
    87 		// conflict avoidance
       
    88 		if ( isset($heading_names[$tocid]) )
       
    89 		{
       
    90 			$id = 2;
       
    91 			while ( isset($heading_names["{$tocid}{$id}"]) )
       
    92 				$id++;
       
    93 			
       
    94 			$tocid .= $id;
       
    95 		}
       
    96 		$heading_names[$tocid] = true;
    78 	}
    97 	}
    79 	else
    98 	else
    80 	{
    99 	{
    81 		$tocid = "$i";
   100 		$tocid = "$i";
    82 	}
   101 	}
    83 	$toc .= '<dd><a href="#head:' . $tocid . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . '</a>';
   102 	
    84     $prev = $head;
   103 	$toc .= '<dd><a href="#head:' . $tocid . '">' . implode('.', $treenum) . ' ' . htmlspecialchars($matches[2][$i]) . "</a>";
    85   }
   104 	
    86   while ( $levels > 0 )
   105 	$prev = $head;
    87   {
   106   }
    88     $toc .= '</dd></dl>';
   107   // and at the end of the loop...
    89     $levels--;
   108   $toc .= "</dd>\n";
    90   }
   109   while ( $levels > 1 )
       
   110   {
       
   111   	  $toc .= "</dl></dd>\n";
       
   112   	  $levels--;
       
   113   }
       
   114   $toc .= "</dl>\n";
       
   115   
    91   $toc_body = "<nowiki><div class=\"toc mdg-comment\">
   116   $toc_body = "<nowiki><div class=\"toc mdg-comment\">
    92                 <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl>
   117                 <dl><dd><b>Contents</b> <small>[<a href=\"#\" onclick=\"collapseTOC(this); return false;\">hide</a>]</small></dd></dl>
    93                 <div>$toc</div>
   118                 <div>$toc</div>
    94               </div></nowiki>";
   119               </div></nowiki>";
    95     
   120