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