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