]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateToc.php
width was missing in getDefaultArguments
[SourceForge/phpwiki.git] / lib / plugin / CreateToc.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5  Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * CreateToc:  Create a Table of Contents and automatically link to headers
26  *
27  * Usage:   
28  *  <?plugin CreateToc arguments ?>
29  * @author:  Reini Urban, Marc-Etienne Vargenau
30  *
31  * Known problems: 
32  * - MacIE will not work with jshide.
33  * - it will crash with old markup and Apache2 (?)
34  * - Certain corner-edges will not work with TOC_FULL_SYNTAX. 
35  *   I believe I fixed all of them now, but who knows?
36  * - bug #969495 "existing labels not honored" seems to be fixed.
37  */
38
39 if (!defined('TOC_FULL_SYNTAX'))
40     define('TOC_FULL_SYNTAX', 1);
41
42 class WikiPlugin_CreateToc
43 extends WikiPlugin
44 {
45     function getName() {
46         return _("CreateToc");
47     }
48
49     function getDescription() {
50         return _("Create a Table of Contents and automatically link to headers");
51     }
52
53     function getVersion() {
54         return preg_replace("/[Revision: $]/", '',
55                             "\$Revision$");
56     }
57
58     function getDefaultArguments() {
59         return array('extracollapse' => 1,            // provide an entry +/- link to collapse
60                      'firstlevelstyle' => 'number',   // 'number', 'letter' or 'roman'
61                      'headers'   =>  "1,2,3,4,5",     // "!!!"=>h2, "!!"=>h3, "!"=>h4
62                                                       // "1"=>h2, "2"=>h3, "3"=>h4, "4"=>h5, "5"=>h6
63                      'indentstr' => '&nbsp;&nbsp;',
64                      'jshide'    => 0,                // collapsed TOC as DHTML button
65                      'liststyle' => 'dl',             // 'dl' or 'ul' or 'ol'
66                      'noheader'  => 0,                // omit "Table of Contents" header
67                      'notoc'     => 0,                // do not display TOC, only number headers
68                      'pagename'  => '[pagename]',     // TOC of another page here?
69                      'position'  => 'full',           // full, right or left
70                      'width'     => '200px',
71                      'with_counter' => 0,
72                      'with_toclink' => 0,             // link back to TOC
73                     );
74     }
75     // Initialisation of toc counter
76     function _initTocCounter() {
77         $counter = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
78         return $counter;
79     }
80
81     // Update toc counter with a new title
82     function _tocCounter(&$counter, $level) {
83         $counter[$level]++;
84         for($i = $level+1; $i <= 5; $i++) {
85             $counter[$i] = 0;
86         }
87     }
88
89     function _roman_counter($number) {
90
91         $n = intval($number);
92         $result = '';
93
94         $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
95                         'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
96
97         foreach ($lookup as $roman => $value) {
98             $matches = intval($n / $value);
99             $result .= str_repeat($roman, $matches);
100             $n = $n % $value;
101         }
102         return $result;
103     }
104
105     function _letter_counter($number) {
106         if ($number <= 26) {
107             return chr(ord("A") + $number - 1);
108         } else {
109             return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
110         }
111     }
112
113     // Get string corresponding to the current title
114     function _getCounter(&$counter, $level, $firstlevelstyle) {
115         if ($firstlevelstyle == 'roman') {
116             $str= $this->_roman_counter($counter[1]);
117         } else if ($firstlevelstyle == 'letter') {
118             $str= $this->_letter_counter($counter[1]);
119         } else {
120             $str=$counter[1];
121         }
122         for($i = 2; $i <= 5; $i++) {
123             if($counter[$i] != 0)
124                 $str .= '.'.$counter[$i];
125         }
126         return $str;
127     }
128
129     function preg_quote ($heading) {
130         return str_replace(array("/",".","?","*"),
131                            array('\/','\.','\?','\*'), $heading);
132     }
133     
134     // Get HTML header corresponding to current level (level is set of ! or =)
135     function _getHeader($level) {
136
137         $count = substr_count($level,'!');
138         switch ($count) {
139             case 3: return "h2";
140             case 2: return "h3";
141             case 1: return "h4";
142         }
143         $count = substr_count($level,'=');
144         switch ($count) {
145             case 2: return "h2";
146             case 3: return "h3";
147             case 4: return "h4";
148             case 5: return "h5";
149             case 6: return "h6";
150         }
151         return "";
152     }
153
154     function _quote($heading) {
155         if (TOC_FULL_SYNTAX ) {
156             $theading = TransformInline($heading);
157             if ($theading)
158                 return preg_quote($theading->asXML(), "/");
159             else 
160                 return XmlContent::_quote(preg_quote($heading, "/"));
161         } else {
162             return XmlContent::_quote(preg_quote($heading, "/"));
163         }
164     }
165     
166     /*
167      * @param $hstart id (in $content) of heading start
168      * @param $hend   id (in $content) of heading end
169      */
170     function searchHeader ($content, $start_index, $heading, 
171                            $level, &$hstart, &$hend, $basepage=false) {
172         $hstart = 0;
173         $hend = 0;
174         $h = $this->_getHeader($level);
175         $qheading = $this->_quote($heading);
176         for ($j=$start_index; $j < count($content); $j++) {
177             if (is_string($content[$j])) {
178                 if (preg_match("/<$h>$qheading<\/$h>/", 
179                                $content[$j]))
180                     return $j;
181             }
182             elseif (isa($content[$j], 'cached_link'))
183             {
184                 if (method_exists($content[$j],'asXML')) {
185                     $content[$j]->_basepage = $basepage;
186                     $content[$j] = $content[$j]->asXML();
187                 } else
188                     $content[$j] = $content[$j]->asString();
189                 // shortcut for single wikiword or link headers
190                 if ($content[$j] == $heading
191                     and substr($content[$j-1],-4,4) == "<$h>" 
192                     and substr($content[$j+1],0,5) == "</$h>") 
193                 {
194                     $hstart = $j-1;
195                     $hend = $j+1;
196                     return $j; // single wikiword
197                 } 
198                 elseif (TOC_FULL_SYNTAX) {
199                     //DONE: To allow "!! WikiWord link" or !! http://anylink/
200                     // Check against joined content (after cached_plugininvocation).
201                     // The first link is the anchor then.
202                     if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j-1])) {
203                         $hstart = $j-1;                     
204                         $joined = '';
205                         for ($k=max($j-1,$start_index);$k<count($content);$k++) {
206                             if (is_string($content[$k]))
207                                 $joined .= $content[$k];
208                             elseif (method_exists($content[$k],'asXML'))
209                                 $joined .= $content[$k]->asXML();
210                             else
211                                 $joined .= $content[$k]->asString();
212                             if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
213                                 $hend=$k;
214                                 return $k;
215                             }
216                         }
217                     }
218                 }
219             }
220         }
221         trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
222         return 0;
223     }
224
225     /** prevent from duplicate anchors,
226      *  beautify spaces: " " => "_" and not "x20."
227      */
228     function _nextAnchor($s) {
229         static $anchors = array();
230
231         $s = str_replace(' ','_',$s);
232         $i = 1;
233         $anchor = $s;
234         while (!empty($anchors[$anchor])) {
235             $anchor = sprintf("%s_%d",$s,$i++);
236         }
237         $anchors[$anchor] = $i;
238         return $anchor;
239     }
240     
241     // Feature request: proper nesting; multiple levels (e.g. 1,3)
242     function extractHeaders (&$content, &$markup, $backlink=0, 
243                              $counter=0, $levels=false, $firstlevelstyle='number', $basepage='')
244     {
245         if (!$levels) $levels = array(1,2);
246         $tocCounter = $this->_initTocCounter();        
247         reset($levels);
248         sort($levels);
249         $headers = array();
250         $j = 0;
251         for ($i=0; $i<count($content); $i++) {
252             foreach ($levels as $level) {
253                 if ($level < 1 or $level > 5) continue;
254                 $phpwikiclassiclevel = 4 -$level;
255                 $wikicreolelevel = $level + 1;
256                 if ($phpwikiclassiclevel < 1 or $phpwikiclassiclevel > 3) continue;
257                 if ((preg_match('/^\s*(!{'.$phpwikiclassiclevel.','.$phpwikiclassiclevel.'})([^!].*)$/', $content[$i], $match))
258                  or (preg_match('/^\s*(={'.$wikicreolelevel.','.$wikicreolelevel.'})([^=].*)$/', $content[$i], $match)) )
259                 {
260                     $this->_tocCounter($tocCounter, $level);                    
261                     if (!strstr($content[$i],'#[')) {
262                         $s = trim($match[2]);
263                         // If it is Wikicreole syntax, remove '='s at the end
264                         if (string_starts_with($match[1], "=")) {
265                             $s = trim($s, "=");
266                             $s = trim($s);
267                         }
268                         $anchor = $this->_nextAnchor($s);
269                         $manchor = MangleXmlIdentifier($anchor);
270                         $texts = $s;
271                         if($counter) {
272                             $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
273                         }
274                         $headers[] = array('text' => $texts, 
275                                            'anchor' => $anchor, 
276                                            'level' => $level);
277                         // Change original wikitext, but that is useless art...
278                         $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
279                         // And now change the to be printed markup (XmlTree):
280                         // Search <hn>$s</hn> line in markup
281                         /* Url for backlink */
282                         $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
283                         $j = $this->searchHeader($markup->_content, $j, $s, 
284                                                  $match[1], $hstart, $hend, 
285                                                  $markup->_basepage);
286                         if ($j and isset($markup->_content[$j])) {
287                             $x = $markup->_content[$j];
288                             $qheading = $this->_quote($s);
289                             if ($counter)
290                                  $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
291                             if (($hstart === 0) && is_string($markup->_content[$j])) {
292                                 if ($backlink) {
293                                     if ($counter)
294                                         $anchorString = "<a href=\"$url\" name=\"$manchor\">$counterString</a> - \$2";
295                                     else
296                                         $anchorString = "<a href=\"$url\" name=\"$manchor\">\$2</a>";
297                                 } else {
298                                     $anchorString = "<a name=\"$manchor\"></a>";
299                                     if ($counter)
300                                         $anchorString .= "$counterString - ";
301                                 }
302                                 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
303                                                       "\$1$anchorString\$2\$3",$x,1)) {
304                                     if ($backlink) {
305                                         $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
306                                                           "\$1$anchorString\$3",
307                                                           $markup->_content[$j],1);
308                                     }
309                                     $markup->_content[$j] = $x;
310                                 }
311                             } else {
312                                 $x = $markup->_content[$hstart];
313                                 $h = $this->_getHeader($match[1]);
314
315                                 if ($backlink) {
316                                     if ($counter)
317                                         $anchorString = "\$1<a href=\"$url\" name=\"$manchor\">$counterString</a> - ";
318                                     else {
319                                         /* Not possible to make a backlink on a
320                                          * title with a WikiWord */
321                                         $anchorString = "\$1<a name=\"$manchor\"></a>";
322                                     }
323                                 }
324                                 else {
325                                     $anchorString = "\$1<a name=\"$manchor\"></a>";
326                                     if ($counter)
327                                         $anchorString .= "$counterString - ";
328                                 }
329                                 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/", 
330                                                   $anchorString, $x, 1);
331                                 if ($backlink) {
332                                     $x =  preg_replace("/(<$h>)(?!.*<\/$h>)/", 
333                                                       $anchorString,
334                                                       $markup->_content[$hstart],1);
335                                 }
336                                 $markup->_content[$hstart] = $x;
337                             }
338                         }
339                     }
340                 }
341             }
342         }
343         return $headers;
344     }
345                 
346     function run($dbi, $argstr, &$request, $basepage) {
347         global $WikiTheme;
348         extract($this->getArgs($argstr, $request));
349         if ($pagename) {
350             // Expand relative page names.
351             $page = new WikiPageName($pagename, $basepage);
352             $pagename = $page->name;
353         }
354         if (!$pagename) {
355             return $this->error(_("no page specified"));
356         }
357         if ($jshide and isBrowserIE() and browserDetect("Mac")) {
358             //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
359             $jshide = 0;
360         }
361         if (($notoc) or ($liststyle == 'ol')) {
362             $with_counter = 1;
363         }
364         $page = $dbi->getPage($pagename);
365         $current = $page->getCurrentRevision();
366         //FIXME: I suspect this only to crash with Apache2
367         if (!$current->get('markup') or $current->get('markup') < 2) {
368             if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
369                 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
370                 return '';
371             }
372         }
373         $content = $current->getContent();
374         $html = HTML::div(array('class' => 'toc', 'id'=>'toc'));
375         if ($notoc) {
376             $html->setAttr('style','display:none;');
377         }
378         if (($position == "left") or ($position == "right")) {
379             $html->setAttr('style','float:'.$position.'; width:'.$width.';');
380         }
381         $list = HTML::div(array('id'=>'toclist'));
382         if (!strstr($headers,",")) {
383             $headers = array($headers); 
384         } else {
385             $headers = explode(",",$headers);
386         }
387         $levels = array();
388         foreach ($headers as $h) {
389             //replace !!! with level 1, ...
390             if (strstr($h,"!")) {
391                 $hcount = substr_count($h,'!');
392                 $level = min(max(1, $hcount),3);
393                 $levels[] = $level;
394             } else {
395                 $level = min(max(1, (int) $h), 5);
396                 $levels[] = $level;
397             }
398         }
399         if (TOC_FULL_SYNTAX)
400             require_once("lib/InlineParser.php");
401         if ($headers = $this->extractHeaders($content, $dbi->_markup, 
402                                              $with_toclink, $with_counter, 
403                                              $levels, $firstlevelstyle, $basepage))
404         {
405             foreach ($headers as $h) {
406                 // proper heading indent
407                 $level = $h['level'];
408                 $indent = $level - 1;
409                 $link = new WikiPageName($pagename,$page,$h['anchor']);
410                 $li = WikiLink($link,'known',$h['text']);
411                 $list->pushContent(HTML::p(HTML::raw
412                        (str_repeat($indentstr,$indent)),$li));
413             }
414         }
415         $list->setAttr('style','display:'.($jshide?'none;':'block;'));
416         $open = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowOpen.png");
417         $close = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowClosed.png");
418         $html->pushContent(Javascript("
419 function toggletoc(a) {
420   var toc=document.getElementById('toclist')
421   //toctoggle=document.getElementById('toctoggle')
422   var open='".$open."'
423   var close='".$close."'
424   if (toc.style.display=='none') {
425     toc.style.display='block'
426     a.title='"._("Click to hide the TOC")."'
427     a.src = open
428   } else {
429     toc.style.display='none';
430     a.title='"._("Click to display")."'
431     a.src = close
432   }
433 }"));
434       if ($noheader) {
435       } else {
436         if ($extracollapse)
437             $toclink = HTML(_("Table of Contents"),
438                             " ",
439                             HTML::a(array('name'=>'TOC')),
440                             HTML::img(array(
441                                             'id'=>'toctoggle',
442                                             'class'=>'wikiaction',
443                                             'title'=>_("Click to display to TOC"),
444                                             'onclick'=>"toggletoc(this)",
445                                             'border' => 0,
446                                             'alt' => 'toctoggle',
447                                             'src' => $jshide ? $close : $open )));
448         else
449             $toclink = HTML::a(array('name'=>'TOC',
450                                      'class'=>'wikiaction',
451                                      'title'=>_("Click to display"),
452                                      'onclick'=>"toggletoc(this)"),
453                                _("Table of Contents"),
454                                HTML::span(array('style'=>'display:none',
455                                                 'id'=>'toctoggle')," "));
456         $html->pushContent(HTML::p(array('id'=>'toctitle'), $toclink));
457       }
458       $html->pushContent($list);
459       if (count($headers) == 0) {
460           // Do not display an empty TOC
461           $html->setAttr('style','display:none;');
462       }
463       return $html;
464     }
465 };
466
467 // For emacs users
468 // Local Variables:
469 // mode: php
470 // tab-width: 8
471 // c-basic-offset: 4
472 // c-hanging-comment-ender-p: nil
473 // indent-tabs-mode: nil
474 // End:
475 ?>