4 * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2010 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
25 * CreateToc: Create a Table of Contents and automatically link to headers
28 * <<CreateToc arguments>>
29 * @author: Reini Urban, Marc-Etienne Vargenau
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.
39 if (!defined('TOC_FULL_SYNTAX'))
40 define('TOC_FULL_SYNTAX', 1);
42 class WikiPlugin_CreateToc
46 return _("CreateToc");
49 function getDescription() {
50 return _("Create a Table of Contents and automatically link to headers");
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' => ' ',
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
67 'with_toclink' => 0, // link back to TOC
71 // Initialisation of toc counter
72 function _initTocCounter() {
73 $counter = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
77 // Update toc counter with a new title
78 function _tocCounter(&$counter, $level) {
80 for($i = $level+1; $i <= 5; $i++) {
85 function _roman_counter($number) {
90 $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
91 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
93 foreach ($lookup as $roman => $value) {
94 $matches = intval($n / $value);
95 $result .= str_repeat($roman, $matches);
101 function _letter_counter($number) {
103 return chr(ord("A") + $number - 1);
105 return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
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 } else if ($firstlevelstyle == 'letter') {
114 $str= $this->_letter_counter($counter[1]);
118 for($i = 2; $i <= 5; $i++) {
119 if($counter[$i] != 0)
120 $str .= '.'.$counter[$i];
125 // Get HTML header corresponding to current level (level is set of ! or =)
126 function _getHeader($level) {
128 $count = substr_count($level,'!');
134 $count = substr_count($level,'=');
145 function _quote($heading) {
146 if (TOC_FULL_SYNTAX ) {
147 $theading = TransformInline($heading);
149 return preg_quote($theading->asXML(), "/");
151 return XmlContent::_quote(preg_quote($heading, "/"));
153 return XmlContent::_quote(preg_quote($heading, "/"));
158 * @param $hstart id (in $content) of heading start
159 * @param $hend id (in $content) of heading end
161 function searchHeader ($content, $start_index, $heading,
162 $level, &$hstart, &$hend, $basepage=false) {
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>/",
173 elseif (isa($content[$j], 'cached_link'))
175 if (method_exists($content[$j],'asXML')) {
176 $content[$j]->_basepage = $basepage;
177 $content[$j] = $content[$j]->asXML();
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>")
187 return $j; // single wikiword
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])) {
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();
202 $joined .= $content[$k]->asString();
203 if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
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);
218 /** prevent from duplicate anchors,
219 * beautify spaces: " " => "_" and not "x20."
221 function _nextAnchor($s) {
222 static $anchors = array();
224 $s = str_replace(' ','_',$s);
227 while (!empty($anchors[$anchor])) {
228 $anchor = sprintf("%s_%d",$s,$i++);
230 $anchors[$anchor] = $i;
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='')
243 if (!$levels) $levels = array(1,2);
244 $tocCounter = $this->_initTocCounter();
249 $insidetable = false;
250 $insideverbatim = false;
251 for ($i=0; $i<count($content); $i++) {
252 if (preg_match('/^\s*{\|/', $content[$i])) {
255 } else if (preg_match('/^\s*{{{/', $content[$i])
256 || preg_match('/^\s*<pre>/', $content[$i])
257 || preg_match('/^\s*<verbatim>/', $content[$i])) {
258 $insideverbatim = true;
260 } else if (preg_match('/^\s*\|}/', $content[$i])) {
261 $insidetable = false;
263 } else if (preg_match('/^\s*}}}/', $content[$i])
264 || preg_match('/^\s*<\/pre>/', $content[$i])
265 || preg_match('/^\s*<\/verbatim>/', $content[$i])) {
266 $insideverbatim = false;
269 if (($insidetable) || ($insideverbatim)) {
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]);
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], "=")) {
290 $anchor = $this->_nextAnchor($s);
291 $manchor = MangleXmlIdentifier($anchor);
294 $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
296 $headers[] = array('text' => $texts,
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"));
306 $j = $this->searchHeader($markup->_content, $j, $s,
307 $match[1], $hstart, $hend,
309 if ($j and isset($markup->_content[$j])) {
310 $x = $markup->_content[$j];
311 $qheading = $this->_quote($s);
313 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
314 if (($hstart === 0) && is_string($markup->_content[$j])) {
317 $anchorString = "<a href=\"$url\" id=\"$manchor\">$counterString</a> - \$2";
319 $anchorString = "<a href=\"$url\" id=\"$manchor\">\$2</a>";
321 $anchorString = "<a id=\"$manchor\"></a>";
323 $anchorString .= "$counterString - ";
325 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
326 "\$1$anchorString\$2\$3",$x,1)) {
328 $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
329 "\$1$anchorString\$3",
330 $markup->_content[$j],1);
332 $markup->_content[$j] = $x;
335 $x = $markup->_content[$hstart];
336 $h = $this->_getHeader($match[1]);
340 $anchorString = "\$1<a href=\"$url\" id=\"$manchor\">$counterString</a> - ";
342 /* Not possible to make a backlink on a
343 * title with a WikiWord */
344 $anchorString = "\$1<a id=\"$manchor\"></a>";
348 $anchorString = "\$1<a id=\"$manchor\"></a>";
350 $anchorString .= "$counterString - ";
352 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
353 $anchorString, $x, 1);
355 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
357 $markup->_content[$hstart],1);
359 $markup->_content[$hstart] = $x;
369 function run($dbi, $argstr, &$request, $basepage) {
371 extract($this->getArgs($argstr, $request));
373 // Expand relative page names.
374 $page = new WikiPageName($pagename, $basepage);
375 $pagename = $page->name;
378 return $this->error(_("No page specified."));
380 if (isBrowserIE() and browserDetect("Mac")) {
383 if (($notoc) or ($liststyle == 'ol')) {
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'"));
392 // Check if page exists.
393 if (!($dbi->isWikiPage($pagename))) {
394 return $this->error(sprintf(_("Page '%s' does not exist."), $pagename));
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"),
403 $page = $dbi->getPage($pagename);
406 if (!is_whole_number($version) or !($version>0)) {
407 return $this->error(_("Error: version must be a positive integer."));
409 $r = $page->getRevision($version);
410 if ((!$r) || ($r->hasDefaultContents())) {
411 return $this->error(sprintf(_("%s: no such revision %d."),
412 $pagename, $version));
415 $r = $page->getCurrentRevision();
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."));
426 $content = $r->getContent();
428 $html = HTML::div(array('class' => 'toc', 'id'=> GenerateId("toc")));
430 $html->setAttr('style','display:none;');
432 if (($position == "left") or ($position == "right")) {
433 $html->setAttr('style','float:'.$position.'; width:'.$width.';');
435 $toclistid = GenerateId("toclist");
436 $list = HTML::div(array('id'=>$toclistid, 'class'=>'toclist'));
437 if (!strstr($headers,",")) {
438 $headers = array($headers);
440 $headers = explode(",",$headers);
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);
450 $level = min(max(1, (int) $h), 5);
455 require_once("lib/InlineParser.php");
456 if ($headers = $this->extractHeaders($content, $dbi->_markup,
457 $with_toclink, $with_counter,
458 $levels, $firstlevelstyle, $basepage))
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));
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");
477 $toctoggleid = GenerateId("toctoggle");
479 $toclink = HTML(_("Table of Contents"),
481 HTML::a(array('id'=>'TOC')),
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 )));
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));
499 $html->pushContent($list);
500 if (count($headers) == 0) {
501 // Do not display an empty TOC
502 $html->setAttr('style','display:none;');
512 // c-hanging-comment-ender-p: nil
513 // indent-tabs-mode: nil