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