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
47 return _("CreateToc");
50 function getDescription()
52 return _("Create a Table of Contents and automatically link to headers");
55 function getDefaultArguments()
57 return array('extracollapse' => 1, // provide an entry +/- link to collapse
58 'firstlevelstyle' => 'number', // 'number', 'letter' or 'roman'
59 'headers' => "1,2,3,4,5", // "!!!"=>h2, "!!"=>h3, "!"=>h4
60 // "1"=>h2, "2"=>h3, "3"=>h4, "4"=>h5, "5"=>h6
61 'indentstr' => ' ',
62 'jshide' => 0, // collapsed TOC as DHTML button
63 'liststyle' => 'dl', // 'dl' or 'ul' or 'ol'
64 'noheader' => 0, // omit "Table of Contents" header
65 'notoc' => 0, // do not display TOC, only number headers
66 'pagename' => '[pagename]', // TOC of another page here?
67 'position' => 'full', // full, right or left
70 'with_toclink' => 0, // link back to TOC
75 // Initialisation of toc counter
76 function _initTocCounter()
78 $counter = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
82 // Update toc counter with a new title
83 function _tocCounter(&$counter, $level)
86 for ($i = $level + 1; $i <= 5; $i++) {
91 function _roman_counter($number)
97 $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
98 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
100 foreach ($lookup as $roman => $value) {
101 $matches = intval($n / $value);
102 $result .= str_repeat($roman, $matches);
108 function _letter_counter($number)
111 return chr(ord("A") + $number - 1);
113 return chr(ord("A") + ($number / 26) - 1) . chr(ord("A") + ($number % 26));
117 // Get string corresponding to the current title
118 function _getCounter(&$counter, $level, $firstlevelstyle)
120 if ($firstlevelstyle == 'roman') {
121 $str = $this->_roman_counter($counter[1]);
122 } elseif ($firstlevelstyle == 'letter') {
123 $str = $this->_letter_counter($counter[1]);
127 for ($i = 2; $i <= 5; $i++) {
128 if ($counter[$i] != 0)
129 $str .= '.' . $counter[$i];
134 // Get HTML header corresponding to current level (level is set of ! or =)
135 function _getHeader($level)
138 $count = substr_count($level, '!');
147 $count = substr_count($level, '=');
163 function _quote($heading)
165 if (TOC_FULL_SYNTAX) {
166 $theading = TransformInline($heading);
168 return preg_quote($theading->asXML(), "/");
170 return XmlContent::_quote(preg_quote($heading, "/"));
172 return XmlContent::_quote(preg_quote($heading, "/"));
177 * @param $hstart id (in $content) of heading start
178 * @param $hend id (in $content) of heading end
180 function searchHeader($content, $start_index, $heading,
181 $level, &$hstart, &$hend, $basepage = false)
185 $h = $this->_getHeader($level);
186 $qheading = $this->_quote($heading);
187 for ($j = $start_index; $j < count($content); $j++) {
188 if (is_string($content[$j])) {
189 if (preg_match("/<$h>$qheading<\/$h>/",
193 } elseif (isa($content[$j], 'cached_link')) {
194 if (method_exists($content[$j], 'asXML')) {
195 $content[$j]->_basepage = $basepage;
196 $content[$j] = $content[$j]->asXML();
198 $content[$j] = $content[$j]->asString();
199 // shortcut for single wikiword or link headers
200 if ($content[$j] == $heading
201 and substr($content[$j - 1], -4, 4) == "<$h>"
202 and substr($content[$j + 1], 0, 5) == "</$h>"
206 return $j; // single wikiword
207 } elseif (TOC_FULL_SYNTAX) {
208 //DONE: To allow "!! WikiWord link" or !! http://anylink/
209 // Check against joined content (after cached_plugininvocation).
210 // The first link is the anchor then.
211 if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j - 1])) {
214 for ($k = max($j - 1, $start_index); $k < count($content); $k++) {
215 if (is_string($content[$k]))
216 $joined .= $content[$k];
217 elseif (method_exists($content[$k], 'asXML'))
218 $joined .= $content[$k]->asXML(); else
219 $joined .= $content[$k]->asString();
220 if (preg_match("/<$h>$qheading<\/$h>/", $joined)) {
229 // Do not trigger error, it happens e.g. for "<<CreateToc pagename=AnotherPage>>"
230 // trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
235 /** prevent from duplicate anchors,
236 * beautify spaces: " " => "_" and not "x20."
238 function _nextAnchor($s)
240 static $anchors = array();
242 $s = str_replace(' ', '_', $s);
245 while (!empty($anchors[$anchor])) {
246 $anchor = sprintf("%s_%d", $s, $i++);
248 $anchors[$anchor] = $i;
252 // We have to find headers in both:
253 // - classic Phpwiki syntax (lines starting with "!", "!!" or "!!!")
254 // - Wikicreole syntax (lines starting with "==", "===", etc.)
255 // We must omit lines starting with "!" if inside a Mediawiki table
256 // (they represent a table header)
257 // Feature request: proper nesting; multiple levels (e.g. 1,3)
258 function extractHeaders(&$content, &$markup, $backlink = 0,
259 $counter = 0, $levels = false, $firstlevelstyle = 'number', $basepage = '')
261 if (!$levels) $levels = array(1, 2);
262 $tocCounter = $this->_initTocCounter();
267 $insidetable = false;
268 $insideverbatim = false;
269 for ($i = 0; $i < count($content); $i++) {
270 if (preg_match('/^\s*{\|/', $content[$i])) {
273 } elseif (preg_match('/^\s*{{{/', $content[$i])
274 || preg_match('/^\s*<pre>/', $content[$i])
275 || preg_match('/^\s*<verbatim>/', $content[$i])
277 $insideverbatim = true;
279 } elseif (preg_match('/^\s*\|}/', $content[$i])) {
280 $insidetable = false;
282 } elseif (preg_match('/^\s*}}}/', $content[$i])
283 || preg_match('/^\s*<\/pre>/', $content[$i])
284 || preg_match('/^\s*<\/verbatim>/', $content[$i])
286 $insideverbatim = false;
289 if (($insidetable) || ($insideverbatim)) {
292 foreach ($levels as $level) {
293 if ($level < 1 or $level > 5) continue;
294 $phpwikiclassiclevel = 4 - $level;
295 $wikicreolelevel = $level + 1;
296 $trim = trim($content[$i]);
298 if ((((strpos($trim, '=') === 0))
299 && (preg_match('/^\s*(={' . $wikicreolelevel . ',' . $wikicreolelevel . '})([^=].*)$/', $content[$i], $match)))
300 or (((strpos($trim, '!') === 0))
301 && ((preg_match('/^\s*(!{' . $phpwikiclassiclevel . ',' . $phpwikiclassiclevel . '})([^!].*)$/', $content[$i], $match))))
303 $this->_tocCounter($tocCounter, $level);
304 if (!strstr($content[$i], '#[')) {
305 $s = trim($match[2]);
306 // If it is Wikicreole syntax, remove '='s at the end
307 if (string_starts_with($match[1], "=")) {
311 $anchor = $this->_nextAnchor($s);
312 $manchor = MangleXmlIdentifier($anchor);
315 $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle) . ' ' . $s;
317 $headers[] = array('text' => $texts,
320 // Change original wikitext, but that is useless art...
321 $content[$i] = $match[1] . " #[|$manchor][$s|#TOC]";
322 // And now change the to be printed markup (XmlTree):
323 // Search <hn>$s</hn> line in markup
324 /* Url for backlink */
325 $url = WikiURL(new WikiPageName($basepage, false, "TOC"));
327 $j = $this->searchHeader($markup->_content, $j, $s,
328 $match[1], $hstart, $hend,
330 if ($j and isset($markup->_content[$j])) {
331 $x = $markup->_content[$j];
332 $qheading = $this->_quote($s);
334 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
335 if (($hstart === 0) && is_string($markup->_content[$j])) {
338 $anchorString = "<a href=\"$url\" id=\"$manchor\">$counterString</a> - \$2";
340 $anchorString = "<a href=\"$url\" id=\"$manchor\">\$2</a>";
342 $anchorString = "<a id=\"$manchor\"></a>";
344 $anchorString .= "$counterString - ";
346 if ($x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
347 "\$1$anchorString\$2\$3", $x, 1)
350 $x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
351 "\$1$anchorString\$3",
352 $markup->_content[$j], 1);
354 $markup->_content[$j] = $x;
357 $x = $markup->_content[$hstart];
358 $h = $this->_getHeader($match[1]);
362 $anchorString = "\$1<a href=\"$url\" id=\"$manchor\">$counterString</a> - ";
364 /* Not possible to make a backlink on a
365 * title with a WikiWord */
366 $anchorString = "\$1<a id=\"$manchor\"></a>";
369 $anchorString = "\$1<a id=\"$manchor\"></a>";
371 $anchorString .= "$counterString - ";
373 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
374 $anchorString, $x, 1);
376 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
378 $markup->_content[$hstart], 1);
380 $markup->_content[$hstart] = $x;
390 function run($dbi, $argstr, &$request, $basepage)
393 extract($this->getArgs($argstr, $request));
395 // Expand relative page names.
396 $page = new WikiPageName($pagename, $basepage);
397 $pagename = $page->name;
400 return $this->error(sprintf(_("A required argument '%s' is missing."), 'pagename'));
402 if (isBrowserIE() and browserDetect("Mac")) {
405 if (($notoc) or ($liststyle == 'ol')) {
408 if ($firstlevelstyle and ($firstlevelstyle != 'number')
409 and ($firstlevelstyle != 'letter')
410 and ($firstlevelstyle != 'roman')
412 return $this->error(_("Error: firstlevelstyle must be 'number', 'letter' or 'roman'"));
415 // Check if page exists.
416 if (!($dbi->isWikiPage($pagename))) {
417 return $this->error(sprintf(_("Page '%s' does not exist."), $pagename));
420 // Check if user is allowed to get the page.
421 if (!mayAccessPage('view', $pagename)) {
422 return $this->error(sprintf(_("Illegal access to page %s: no read access"),
426 $page = $dbi->getPage($pagename);
429 if (!is_whole_number($version) or !($version > 0)) {
430 return $this->error(_("Error: version must be a positive integer."));
432 $r = $page->getRevision($version);
433 if ((!$r) || ($r->hasDefaultContents())) {
434 return $this->error(sprintf(_("%s: no such revision %d."),
435 $pagename, $version));
438 $r = $page->getCurrentRevision();
441 $current = $page->getCurrentRevision();
442 //FIXME: I suspect this only to crash with Apache2
443 if (!$current->get('markup') or $current->get('markup') < 2) {
444 if (in_array(php_sapi_name(), array('apache2handler', 'apache2filter'))) {
445 return $this->error(_("CreateToc disabled for old markup."));
449 $content = $r->getContent();
451 $html = HTML::div(array('class' => 'toc', 'id' => GenerateId("toc")));
453 $html->setAttr('style', 'display:none;');
455 if (($position == "left") or ($position == "right")) {
456 $html->setAttr('style', 'float:' . $position . '; width:' . $width . ';');
458 $toclistid = GenerateId("toclist");
459 $list = HTML::div(array('id' => $toclistid, 'class' => 'toclist'));
460 if (!strstr($headers, ",")) {
461 $headers = array($headers);
463 $headers = explode(",", $headers);
466 foreach ($headers as $h) {
467 //replace !!! with level 1, ...
468 if (strstr($h, "!")) {
469 $hcount = substr_count($h, '!');
470 $level = min(max(1, $hcount), 3);
473 $level = min(max(1, (int)$h), 5);
478 require_once 'lib/InlineParser.php';
479 if ($headers = $this->extractHeaders($content, $dbi->_markup,
480 $with_toclink, $with_counter,
481 $levels, $firstlevelstyle, $basepage)
483 foreach ($headers as $h) {
484 // proper heading indent
485 $level = $h['level'];
486 $indent = $level - 1;
487 $link = new WikiPageName($pagename, $page, $h['anchor']);
488 $li = WikiLink($link, 'known', $h['text']);
489 // Hack to suppress pagename before #
490 // $li->_attr["href"] = strstr($li->_attr["href"], '#');
491 $list->pushContent(HTML::p(HTML::raw
492 (str_repeat($indentstr, $indent)), $li));
495 $list->setAttr('style', 'display:' . ($jshide ? 'none;' : 'block;'));
496 $open = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowOpen.png");
497 $close = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowClosed.png");
500 $toctoggleid = GenerateId("toctoggle");
502 $toclink = HTML(_("Table of Contents"),
504 HTML::a(array('id' => 'TOC')),
506 'id' => $toctoggleid,
507 'class' => 'wikiaction',
508 'title' => _("Click to display to TOC"),
509 'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')",
510 'alt' => 'toctoggle',
511 'src' => $jshide ? $close : $open)));
513 $toclink = HTML::a(array('id' => 'TOC',
514 'class' => 'wikiaction',
515 'title' => _("Click to display"),
516 'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')"),
517 _("Table of Contents"),
518 HTML::span(array('style' => 'display:none',
519 'id' => $toctoggleid), " "));
520 $html->pushContent(HTML::p(array('class' => 'toctitle'), $toclink));
522 $html->pushContent($list);
523 if (count($headers) == 0) {
524 // Do not display an empty TOC
525 $html->setAttr('style', 'display:none;');
537 // c-hanging-comment-ender-p: nil
538 // indent-tabs-mode: nil