]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateToc.php
Implement "firstlevelstyle" parameter
[SourceForge/phpwiki.git] / lib / plugin / CreateToc.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreateToc.php,v 1.39 2008-08-19 18:19:01 vargenau Exp $');
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:  Automatically link to headers
26  *
27  * Usage:   
28  *  <?plugin CreateToc headers=!!!,!! with_toclink||=1 
29  *                     jshide||=1 ?>
30  * @author:  Reini Urban, Marc-Etienne Vargenau
31  *
32  * Known problems: 
33  * - MacIE will not work with jshide.
34  * - it will crash with old markup and Apache2 (?)
35  * - Certain corner-edges will not work with TOC_FULL_SYNTAX. 
36  *   I believe I fixed all of them now, but who knows?
37  * - bug #969495 "existing labels not honored" seems to be fixed.
38  */
39
40 if (!defined('TOC_FULL_SYNTAX'))
41     define('TOC_FULL_SYNTAX', 1);
42
43 class WikiPlugin_CreateToc
44 extends WikiPlugin
45 {
46     function getName() {
47         return _("CreateToc");
48     }
49
50     function getDescription() {
51         return _("Create a Table of Contents and automatically link to headers");
52     }
53
54     function getVersion() {
55         return preg_replace("/[Revision: $]/", '',
56                             "\$Revision: 1.39 $");
57     }
58
59     function getDefaultArguments() {
60         return array( 'pagename'  => '[pagename]', // TOC of another page here?
61                       // or headers=1,2,3 is also possible.
62                       'headers'   => "!!!,!!,!",   // "!!!"=>h1, "!!"=>h2, "!"=>h3
63                       'noheader'  => 0,            // omit "Table of Contents" header
64                       'notoc'     => 0,            // do not display TOC, only number headers
65                       'position'  => 'right',      // or left
66                       'with_toclink' => 0,         // link back to TOC
67                       'jshide'    => 0,            // collapsed TOC as DHTML button
68                       'extracollapse' => 1,        // provide an entry +/- link to collapse
69                       'liststyle' => 'dl',         // 'dl' or 'ul' or 'ol'
70                       'indentstr' => '&nbsp;&nbsp;',
71                       'with_counter' => 0,
72                       'firstlevelstyle' => 'number' // 'number', 'letter' or 'roman'
73                       );
74     }
75     // Initialisation of toc counter
76     function _initTocCounter() {
77         $counter = array(1=>0, 2=>0, 3=>0);
78         return $counter;
79     }
80
81     // Update toc counter with a new title
82     function _tocCounter(&$counter, $level) {
83         $counter[$level]++;
84         $level--;
85         for($i = $level; $i > 0; $i--) {
86             $counter[$level] = 0;
87         }
88     }
89
90     function _roman_counter($number) {
91
92         $n = intval($number);
93         $result = '';
94
95         $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
96                         'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
97
98         foreach ($lookup as $roman => $value) {
99             $matches = intval($n / $value);
100             $result .= str_repeat($roman, $matches);
101             $n = $n % $value;
102         }
103         return $result;
104     }
105
106     function _letter_counter($number) {
107         if ($number <= 26) {
108             return chr(ord("A") + $number - 1);
109         } else {
110             return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
111         }
112     }
113
114     // Get string corresponding to the current title
115     function _getCounter(&$counter, $level, $firstlevelstyle) {
116         if ($firstlevelstyle == 'roman') {
117             $str= $this->_roman_counter($counter[3]);
118         } else if ($firstlevelstyle == 'letter') {
119             $str= $this->_letter_counter($counter[3]);
120         } else {
121             $str=$counter[3];
122         }
123         for($i = 2; $i > 0; $i--) {
124             if($counter[$i] != 0)
125                 $str .= '.'.$counter[$i];
126         }
127         return $str;
128     }
129
130     function preg_quote ($heading) {
131         return str_replace(array("/",".","?","*"),
132                            array('\/','\.','\?','\*'), $heading);
133     }
134     
135     // Get HTML header corresponding to current level (level is set of !)
136     function _getHeader($level) {
137         $count = substr_count($level,'!');
138         switch ($count) {
139             case 1: $h = "h4"; break;
140             case 2: $h = "h3"; break;
141             case 3: $h = "h2"; break;
142         }
143         return $h;
144     }
145
146     function _quote($heading) {
147         if (TOC_FULL_SYNTAX ) {
148             $theading = TransformInline($heading);
149             if ($theading)
150                 return preg_quote($theading->asXML(), "/");
151             else 
152                 return XmlContent::_quote(preg_quote($heading, "/"));
153         } else {
154             return XmlContent::_quote(preg_quote($heading, "/"));
155         }
156     }
157     
158     /*
159      * @param $hstart id (in $content) of heading start
160      * @param $hend   id (in $content) of heading end
161      */
162     function searchHeader ($content, $start_index, $heading, 
163                            $level, &$hstart, &$hend, $basepage=false) {
164         $hstart = 0;
165         $hend = 0;
166         $h = $this->_getHeader($level);
167         $qheading = $this->_quote($heading);
168         for ($j=$start_index; $j < count($content); $j++) {
169             if (is_string($content[$j])) {
170                 if (preg_match("/<$h>$qheading<\/$h>/", 
171                                $content[$j]))
172                     return $j;
173             }
174             elseif (isa($content[$j], 'cached_link'))
175             {
176                 if (method_exists($content[$j],'asXML')) {
177                     $content[$j]->_basepage = $basepage;
178                     $content[$j] = $content[$j]->asXML();
179                 } else
180                     $content[$j] = $content[$j]->asString();
181                 // shortcut for single wikiword or link headers
182                 if ($content[$j] == $heading
183                     and substr($content[$j-1],-4,4) == "<$h>" 
184                     and substr($content[$j+1],0,5) == "</$h>") 
185                 {
186                     $hstart = $j-1;
187                     $hend = $j+1;
188                     return $j; // single wikiword
189                 } 
190                 elseif (TOC_FULL_SYNTAX) {
191                     //DONE: To allow "!! WikiWord link" or !! http://anylink/
192                     // Check against joined content (after cached_plugininvocation).
193                     // The first link is the anchor then.
194                     if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j-1])) {
195                         $hstart = $j-1;                     
196                         $joined = '';
197                         for ($k=max($j-1,$start_index);$k<count($content);$k++) {
198                             if (is_string($content[$k]))
199                                 $joined .= $content[$k];
200                             elseif (method_exists($content[$k],'asXML'))
201                                 $joined .= $content[$k]->asXML();
202                             else
203                                 $joined .= $content[$k]->asString();
204                             if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
205                                 $hend=$k;
206                                 return $k;
207                             }
208                         }
209                     }
210                 }
211             }
212         }
213         trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
214         return 0;
215     }
216
217     /** prevent from duplicate anchors,
218      *  beautify spaces: " " => "_" and not "x20."
219      */
220     function _nextAnchor($s) {
221         static $anchors = array();
222
223         $s = str_replace(' ','_',$s);
224         $i = 1;
225         $anchor = $s;
226         while (!empty($anchors[$anchor])) {
227             $anchor = sprintf("%s_%d",$s,$i++);
228         }
229         $anchors[$anchor] = $i;
230         return $anchor;
231     }
232     
233     // Feature request: proper nesting; multiple levels (e.g. 1,3)
234     function extractHeaders (&$content, &$markup, $backlink=0, 
235                              $counter=0, $levels=false, $firstlevelstyle='number', $basepage='')
236     {
237         if (!$levels) $levels = array(1,2);
238         $tocCounter = $this->_initTocCounter();        
239         reset($levels);
240         sort($levels);
241         $headers = array();
242         $j = 0;
243         for ($i=0; $i<count($content); $i++) {
244             foreach ($levels as $level) {
245                 if ($level < 1 or $level > 3) continue;
246                 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',
247                                $content[$i], $match)) 
248                 {
249                     $this->_tocCounter($tocCounter, $level);                    
250                     if (!strstr($content[$i],'#[')) {
251                         $s = trim($match[2]);
252                         $anchor = $this->_nextAnchor($s);
253                         $manchor = MangleXmlIdentifier($anchor);
254                         $texts = $s;
255                         if($counter) {
256                             $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
257                         }
258                         $headers[] = array('text' => $texts, 
259                                            'anchor' => $anchor, 
260                                            'level' => $level);
261                         // Change original wikitext, but that is useless art...
262                         $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
263                         // And now change the to be printed markup (XmlTree):
264                         // Search <hn>$s</hn> line in markup
265                         /* Url for backlink */
266                         $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
267                         $j = $this->searchHeader($markup->_content, $j, $s, 
268                                                  $match[1], $hstart, $hend, 
269                                                  $markup->_basepage);
270                         if ($j and isset($markup->_content[$j])) {
271                             $x = $markup->_content[$j];
272                             $qheading = $this->_quote($s);
273                             if ($counter)
274                                  $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
275                             if (($hstart === 0) && is_string($markup->_content[$j])) {
276                                 if ($backlink) {
277                                     if ($counter)
278                                         $anchorString = "<a href=\"$url\" name=\"$manchor\">$counterString</a> - \$2";
279                                     else
280                                         $anchorString = "<a href=\"$url\" name=\"$manchor\">\$2</a>";
281                                 } else {
282                                     $anchorString = "<a name=\"$manchor\"></a>";
283                                     if ($counter)
284                                         $anchorString .= "$counterString - ";
285                                 }
286                                 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
287                                                       "\$1$anchorString\$2\$3",$x,1)) {
288                                     if ($backlink) {
289                                         $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
290                                                           "\$1$anchorString\$3",
291                                                           $markup->_content[$j],1);
292                                     }
293                                     $markup->_content[$j] = $x;
294                                 }
295                             } else {
296                                 $x = $markup->_content[$hstart];
297                                 $h = $this->_getHeader($match[1]);
298
299                                 if ($backlink) {
300                                     if ($counter)
301                                         $anchorString = "\$1<a href=\"$url\" name=\"$manchor\">$counterString</a> - ";
302                                     else {
303                                         /* Not possible to make a backlink on a
304                                          * title with a WikiWord */
305                                         $anchorString = "\$1<a name=\"$manchor\"></a>";
306                                     }
307                                 }
308                                 else {
309                                     $anchorString = "\$1<a name=\"$manchor\"></a>";
310                                     if ($counter)
311                                         $anchorString .= "$counterString - ";
312                                 }
313                                 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/", 
314                                                   $anchorString, $x, 1);
315                                 if ($backlink) {
316                                     $x =  preg_replace("/(<$h>)(?!.*<\/$h>)/", 
317                                                       $anchorString,
318                                                       $markup->_content[$hstart],1);
319                                 }
320                                 $markup->_content[$hstart] = $x;
321                             }
322                         }
323                     }
324                 }
325             }
326         }
327         return $headers;
328     }
329                 
330     function run($dbi, $argstr, &$request, $basepage) {
331         global $WikiTheme;
332         extract($this->getArgs($argstr, $request));
333         if ($pagename) {
334             // Expand relative page names.
335             $page = new WikiPageName($pagename, $basepage);
336             $pagename = $page->name;
337         }
338         if (!$pagename) {
339             return $this->error(_("no page specified"));
340         }
341         if ($jshide and isBrowserIE() and browserDetect("Mac")) {
342             //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
343             $jshide = 0;
344         }
345         if (($notoc) or ($liststyle == 'ol')) {
346             $with_counter = 1;
347         }
348         $page = $dbi->getPage($pagename);
349         $current = $page->getCurrentRevision();
350         //FIXME: I suspect this only to crash with Apache2
351         if (!$current->get('markup') or $current->get('markup') < 2) {
352             if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
353                 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
354                 return '';
355             }
356         }
357         $content = $current->getContent();
358         $html = HTML::div(array('class' => 'toc', 'id'=>'toc'));
359         if ($notoc) {
360             $html->setAttr('style','display:none;');
361         }
362         if ($liststyle == 'dl')
363             $list = HTML::dl(array('id'=>'toclist','class' => 'toc'));
364         elseif ($liststyle == 'ul')
365             $list = HTML::ul(array('id'=>'toclist','class' => 'toc'));
366         elseif ($liststyle == 'ol')
367             $list = HTML::ol(array('id'=>'toclist','class' => 'toc'));
368         if (!strstr($headers,",")) {
369             $headers = array($headers); 
370         } else {
371             $headers = explode(",",$headers);
372         }
373         $levels = array();
374         foreach ($headers as $h) {
375             //replace !!! with level 1, ...
376             if (strstr($h,"!")) {
377                 $hcount = substr_count($h,'!');
378                 $level = min(max(1, $hcount),3);
379                 $levels[] = $level;
380             } else {
381                 $level = min(max(1, (int) $h), 3);
382                 $levels[] = $level;
383             }
384         }
385         if (TOC_FULL_SYNTAX)
386             require_once("lib/InlineParser.php");
387         if ($headers = $this->extractHeaders($content, $dbi->_markup, 
388                                              $with_toclink, $with_counter, 
389                                              $levels, $firstlevelstyle, $basepage))
390         {
391             foreach ($headers as $h) {
392                 // proper heading indent
393                 $level = $h['level'];
394                 $indent = 3 - $level;
395                 $link = new WikiPageName($pagename,$page,$h['anchor']);
396                 $li = WikiLink($link,'known',$h['text']);
397                 if ($liststyle == 'dl')
398                     $list->pushContent(HTML::dt(HTML::raw
399                         (str_repeat($indentstr,$indent)),$li));
400                 else
401                     $list->pushContent(HTML::li(HTML::raw
402                         (str_repeat($indentstr,$indent)),$li));
403             }
404         }
405         $list->setAttr('style','display:'.($jshide?'none;':'block;'));
406         $open = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowOpen.png");
407         $close = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowClosed.png");
408         $html->pushContent(Javascript("
409 function toggletoc(a) {
410   var toc=document.getElementById('toclist')
411   //toctoggle=document.getElementById('toctoggle')
412   var open='".$open."'
413   var close='".$close."'
414   if (toc.style.display=='none') {
415     toc.style.display='block'
416     a.title='"._("Click to hide the TOC")."'
417     a.src = open
418   } else {
419     toc.style.display='none';
420     a.title='"._("Click to display")."'
421     a.src = close
422   }
423 }"));
424         if ($extracollapse)
425             $toclink = HTML(_("Table of Contents"),
426                             " ",
427                             HTML::a(array('name'=>'TOC')),
428                             HTML::img(array(
429                                             'id'=>'toctoggle',
430                                             'class'=>'wikiaction',
431                                             'title'=>_("Click to display to TOC"),
432                                             'onclick'=>"toggletoc(this)",
433                                             'height' => 15,
434                                             'width' => 15,
435                                             'border' => 0,
436                                             'alt' => 'toctoggle',
437                                             'src' => $jshide ? $close : $open )));
438         else
439             $toclink = HTML::a(array('name'=>'TOC',
440                                      'class'=>'wikiaction',
441                                      'title'=>_("Click to display"),
442                                      'onclick'=>"toggletoc(this)"),
443                                _("Table of Contents"),
444                                HTML::span(array('style'=>'display:none',
445                                                 'id'=>'toctoggle')," "));
446         $html->pushContent(HTML::h4($toclink));
447         $html->pushContent($list);
448         return $html;
449     }
450 };
451
452 // $Log: not supported by cvs2svn $
453 // Revision 1.38  2008/08/19 18:15:28  vargenau
454 // Implement "notoc" parameter
455 //
456 // Revision 1.37  2008/05/04 08:37:42  vargenau
457 // Add alt attribute
458 //
459 // Revision 1.36  2007/07/19 12:41:25  labbenes
460 // Correct TOC numbering. It should start from '1' not from '1.1'.
461 //
462 // Revision 1.35  2007/02/17 14:17:48  rurban
463 // declare vars for IE6
464 //
465 // Revision 1.34  2007/01/28 22:47:06  rurban
466 // fix # back link
467 //
468 // Revision 1.33  2007/01/28 22:37:04  rurban
469 // beautify +/- collapse icon
470 //
471 // Revision 1.32  2007/01/20 11:25:30  rurban
472 // remove align
473 //
474 // Revision 1.31  2007/01/09 12:35:05  rurban
475 // Change align to position. Add extracollapse. js now always active, jshide just denotes the initial state.
476 //
477 // Revision 1.30  2006/12/22 17:49:38  rurban
478 // fix quoting
479 //
480 // Revision 1.29  2006/04/15 12:26:54  rurban
481 // need basepage for subpages like /Remove (within CreateTOC)
482 //
483 // Revision 1.28  2005/10/12 06:15:25  rurban
484 // just aesthetics
485 //
486 // Revision 1.27  2005/10/10 19:50:45  rurban
487 // fix the missing formatting problems, add with_counter arg by ?? (20050106), Thanks to ManuelVacelet for the testcase
488 //
489 // Revision 1.26  2004/09/20 14:07:16  rurban
490 // fix Constant toc_full_syntax already defined warning
491 //
492 // Revision 1.25  2004/07/08 20:30:07  rurban
493 // plugin->run consistency: request as reference, added basepage.
494 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
495 //
496 // Revision 1.24  2004/06/28 13:27:03  rurban
497 // CreateToc disabled for old markup and Apache2 only
498 //
499 // Revision 1.23  2004/06/28 13:13:58  rurban
500 // CreateToc disabled for old markup
501 //
502 // Revision 1.22  2004/06/15 14:56:37  rurban
503 // more allow_call_time_pass_reference false fixes
504 //
505 // Revision 1.21  2004/06/13 09:45:23  rurban
506 // display bug workaround for MacIE browsers, jshide: 0
507 //
508 // Revision 1.20  2004/05/11 13:57:46  rurban
509 // enable TOC_FULL_SYNTAX per default
510 // don't <a name>$header</a> to disable css formatting for such anchors
511 //   => <a name></a>$header
512 //
513 // Revision 1.19  2004/05/08 16:59:27  rurban
514 // requires optional TOC_FULL_SYNTAX constnat to enable full link and
515 // wikiword syntax in headers.
516 //
517 // Revision 1.18  2004/04/29 21:55:15  rurban
518 // fixed TOC backlinks with USE_PATH_INFO false
519 //   with_toclink=1, sf.net bug #940682
520 //
521 // Revision 1.17  2004/04/26 19:43:03  rurban
522 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
523 //
524 // Revision 1.16  2004/04/26 14:46:14  rurban
525 // better comments
526 //
527 // Revision 1.14  2004/04/21 04:29:50  rurban
528 // write WikiURL consistently (not WikiUrl)
529 //
530 // Revision 1.12  2004/03/22 14:13:53  rurban
531 // fixed links to equal named headers
532 //
533 // Revision 1.11  2004/03/15 09:52:59  rurban
534 // jshide button: dynamic titles
535 //
536 // Revision 1.10  2004/03/14 20:30:21  rurban
537 // jshide button
538 //
539 // Revision 1.9  2004/03/09 19:24:20  rurban
540 // custom indentstr
541 // h2 toc header
542 //
543 // Revision 1.8  2004/03/09 19:05:12  rurban
544 // new liststyle arg. default: dl (no bullets)
545 //
546 // Revision 1.7  2004/03/09 11:51:54  rurban
547 // support jshide=1: DHTML button hide/unhide TOC
548 //
549 // Revision 1.6  2004/03/09 10:25:37  rurban
550 // slightly better formatted TOC indentation
551 //
552 // Revision 1.5  2004/03/09 08:57:10  rurban
553 // convert space to "_" instead of "x20." in anchors
554 // proper heading indent
555 // handle duplicate headers
556 // allow multiple headers like "!!!,!!" or "1,2"
557 //
558 // Revision 1.4  2004/03/02 18:21:29  rurban
559 // typo: ref=>href
560 //
561 // Revision 1.1  2004/03/01 18:10:28  rurban
562 // first version, without links, anchors and jscript folding
563 //
564 //
565
566 // For emacs users
567 // Local Variables:
568 // mode: php
569 // tab-width: 8
570 // c-basic-offset: 4
571 // c-hanging-comment-ender-p: nil
572 // indent-tabs-mode: nil
573 // End:
574 ?>