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 * - it will crash with old markup and Apache2 (?)
33 * - Certain corner-edges will not work with TOC_FULL_SYNTAX.
34 * I believe I fixed all of them now, but who knows?
35 * - bug #969495 "existing labels not honored" seems to be fixed.
38 if (!defined('TOC_FULL_SYNTAX'))
39 define('TOC_FULL_SYNTAX', 1);
41 class WikiPlugin_CreateToc
44 function getDescription()
46 return _("Create a Table of Contents and automatically link to headers.");
49 function getDefaultArguments()
51 return array('extracollapse' => 1, // provide an entry +/- link to collapse
52 'firstlevelstyle' => 'number', // 'number', 'letter' or 'roman'
53 'headers' => "1,2,3,4,5", // "!!!"=>h2, "!!"=>h3, "!"=>h4
54 // "1"=>h2, "2"=>h3, "3"=>h4, "4"=>h5, "5"=>h6
55 'indentstr' => ' ',
56 'jshide' => 0, // collapsed TOC as DHTML button
57 'liststyle' => 'dl', // 'dl' or 'ul' or 'ol'
58 'noheader' => 0, // omit "Table of Contents" header
59 'notoc' => 0, // do not display TOC, only number headers
60 'pagename' => '[pagename]', // TOC of another page here?
61 'position' => 'full', // full, right or left
64 'with_toclink' => 0, // link back to TOC
69 // Initialisation of toc counter
70 private function initTocCounter()
72 $counter = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
76 // Update toc counter with a new title
77 private function tocCounter(&$counter, $level)
80 for ($i = $level + 1; $i <= 5; $i++) {
85 private function roman_counter($number)
91 $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
92 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
94 foreach ($lookup as $roman => $value) {
95 $matches = intval($n / $value);
96 $result .= str_repeat($roman, $matches);
102 private function letter_counter($number)
105 return chr(ord("A") + $number - 1);
107 return chr(ord("A") + ($number / 26) - 1) . chr(ord("A") + ($number % 26));
111 // Get string corresponding to the current title
112 private function getCounter(&$counter, $firstlevelstyle)
114 if ($firstlevelstyle == 'roman') {
115 $str = $this->roman_counter($counter[1]);
116 } elseif ($firstlevelstyle == 'letter') {
117 $str = $this->letter_counter($counter[1]);
121 for ($i = 2; $i <= 5; $i++) {
122 if ($counter[$i] != 0)
123 $str .= '.' . $counter[$i];
128 // Get HTML header corresponding to current level (level is set of ! or =)
129 private function getHeader($level)
132 $count = substr_count($level, '!');
141 $count = substr_count($level, '=');
157 private function quote($heading)
159 if (TOC_FULL_SYNTAX) {
160 $theading = TransformInline($heading);
162 return preg_quote($theading->asXML(), "/");
164 return XmlContent::_quote(preg_quote($heading, "/"));
166 return XmlContent::_quote(preg_quote($heading, "/"));
171 * @param $hstart id (in $content) of heading start
172 * @param $hend id (in $content) of heading end
174 private function searchHeader($content, $start_index, $heading,
175 $level, &$hstart, &$hend, $basepage = false)
179 $h = $this->getHeader($level);
180 $qheading = $this->quote($heading);
181 for ($j = $start_index; $j < count($content); $j++) {
182 if (is_string($content[$j])) {
183 if (preg_match("/<$h>$qheading<\/$h>/",
187 } elseif (isa($content[$j], 'cached_link')) {
188 if (method_exists($content[$j], 'asXML')) {
189 $content[$j]->_basepage = $basepage;
190 $content[$j] = $content[$j]->asXML();
192 $content[$j] = $content[$j]->asString();
193 // shortcut for single wikiword or link headers
194 if ($content[$j] == $heading
195 and substr($content[$j - 1], -4, 4) == "<$h>"
196 and substr($content[$j + 1], 0, 5) == "</$h>"
200 return $j; // single wikiword
201 } elseif (TOC_FULL_SYNTAX) {
202 //DONE: To allow "!! WikiWord link" or !! http://anylink/
203 // Check against joined content (after cached_plugininvocation).
204 // The first link is the anchor then.
205 if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j - 1])) {
208 for ($k = max($j - 1, $start_index); $k < count($content); $k++) {
209 if (is_string($content[$k]))
210 $joined .= $content[$k];
211 elseif (method_exists($content[$k], 'asXML'))
212 $joined .= $content[$k]->asXML(); else
213 $joined .= $content[$k]->asString();
214 if (preg_match("/<$h>$qheading<\/$h>/", $joined)) {
223 // Do not trigger error, it happens e.g. for "<<CreateToc pagename=AnotherPage>>"
224 // trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
229 /** prevent from duplicate anchors,
230 * beautify spaces: " " => "_" and not "x20."
232 private function nextAnchor($s)
234 static $anchors = array();
236 $s = str_replace(' ', '_', $s);
239 while (!empty($anchors[$anchor])) {
240 $anchor = sprintf("%s_%d", $s, $i++);
242 $anchors[$anchor] = $i;
246 // We have to find headers in both:
247 // - classic Phpwiki syntax (lines starting with "!", "!!" or "!!!")
248 // - Wikicreole syntax (lines starting with "==", "===", etc.)
249 // We must omit lines starting with "!" if inside a Mediawiki table
250 // (they represent a table header)
251 // Feature request: proper nesting; multiple levels (e.g. 1,3)
252 private function extractHeaders(&$content, &$markup, $backlink = 0,
253 $counter = 0, $levels = false, $firstlevelstyle = 'number', $basepage = '')
255 if (!$levels) $levels = array(1, 2);
256 $tocCounter = $this->initTocCounter();
261 $insidetable = false;
262 $insideverbatim = false;
263 for ($i = 0; $i < count($content); $i++) {
264 if (preg_match('/^\s*{\|/', $content[$i])) {
267 } elseif (preg_match('/^\s*{{{/', $content[$i])
268 || preg_match('/^\s*<pre>/', $content[$i])
269 || preg_match('/^\s*<verbatim>/', $content[$i])
271 $insideverbatim = true;
273 } elseif (preg_match('/^\s*\|}/', $content[$i])) {
274 $insidetable = false;
276 } elseif (preg_match('/^\s*}}}/', $content[$i])
277 || preg_match('/^\s*<\/pre>/', $content[$i])
278 || preg_match('/^\s*<\/verbatim>/', $content[$i])
280 $insideverbatim = false;
283 if (($insidetable) || ($insideverbatim)) {
286 foreach ($levels as $level) {
287 if ($level < 1 or $level > 5) continue;
288 $phpwikiclassiclevel = 4 - $level;
289 $wikicreolelevel = $level + 1;
290 $trim = trim($content[$i]);
292 if ((((strpos($trim, '=') === 0))
293 && (preg_match('/^\s*(={' . $wikicreolelevel . ',' . $wikicreolelevel . '})([^=].*)$/', $content[$i], $match)))
294 or (((strpos($trim, '!') === 0))
295 && ((preg_match('/^\s*(!{' . $phpwikiclassiclevel . ',' . $phpwikiclassiclevel . '})([^!].*)$/', $content[$i], $match))))
297 $this->tocCounter($tocCounter, $level);
298 if (!strstr($content[$i], '#[')) {
299 $s = trim($match[2]);
300 // If it is Wikicreole syntax, remove '='s at the end
301 if (string_starts_with($match[1], "=")) {
305 $anchor = $this->nextAnchor($s);
306 $manchor = MangleXmlIdentifier($anchor);
309 $texts = $this->getCounter($tocCounter, $firstlevelstyle) . ' ' . $s;
311 $headers[] = array('text' => $texts,
314 // Change original wikitext, but that is useless art...
315 $content[$i] = $match[1] . " #[|$manchor][$s|#TOC]";
316 // And now change the to be printed markup (XmlTree):
317 // Search <hn>$s</hn> line in markup
318 /* Url for backlink */
319 $url = WikiURL(new WikiPageName($basepage, false, "TOC"));
321 $j = $this->searchHeader($markup->_content, $j, $s,
322 $match[1], $hstart, $hend,
324 if ($j and isset($markup->_content[$j])) {
325 $x = $markup->_content[$j];
326 $qheading = $this->quote($s);
328 $counterString = $this->getCounter($tocCounter, $firstlevelstyle);
329 if (($hstart === 0) && is_string($markup->_content[$j])) {
332 $anchorString = "<a href=\"$url\" id=\"$manchor\">$counterString</a> - \$2";
334 $anchorString = "<a href=\"$url\" id=\"$manchor\">\$2</a>";
336 $anchorString = "<a id=\"$manchor\"></a>";
338 $anchorString .= "$counterString - ";
340 if ($x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
341 "\$1$anchorString\$2\$3", $x, 1)
344 $x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
345 "\$1$anchorString\$3",
346 $markup->_content[$j], 1);
348 $markup->_content[$j] = $x;
351 $x = $markup->_content[$hstart];
352 $h = $this->getHeader($match[1]);
356 $anchorString = "\$1<a href=\"$url\" id=\"$manchor\">$counterString</a> - ";
358 /* Not possible to make a backlink on a
359 * title with a WikiWord */
360 $anchorString = "\$1<a id=\"$manchor\"></a>";
363 $anchorString = "\$1<a id=\"$manchor\"></a>";
365 $anchorString .= "$counterString - ";
367 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
368 $anchorString, $x, 1);
370 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
372 $markup->_content[$hstart], 1);
374 $markup->_content[$hstart] = $x;
384 function run($dbi, $argstr, &$request, $basepage)
387 extract($this->getArgs($argstr, $request));
389 // Expand relative page names.
390 $page = new WikiPageName($pagename, $basepage);
391 $pagename = $page->name;
394 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'pagename'));
396 if (($notoc) or ($liststyle == 'ol')) {
399 if ($firstlevelstyle and ($firstlevelstyle != 'number')
400 and ($firstlevelstyle != 'letter')
401 and ($firstlevelstyle != 'roman')
403 return $this->error(_("Error: firstlevelstyle must be 'number', 'letter' or 'roman'"));
406 // Check if page exists.
407 if (!($dbi->isWikiPage($pagename))) {
408 return $this->error(sprintf(_("Page ā%sā does not exist."), $pagename));
411 // Check if user is allowed to get the page.
412 if (!mayAccessPage('view', $pagename)) {
413 return $this->error(sprintf(_("Illegal access to page %s: no read access"),
417 $page = $dbi->getPage($pagename);
420 if (!is_whole_number($version) or !($version > 0)) {
421 return $this->error(_("Error: version must be a positive integer."));
423 $r = $page->getRevision($version);
424 if ((!$r) || ($r->hasDefaultContents())) {
425 return $this->error(sprintf(_("%s: no such revision %d."),
426 $pagename, $version));
429 $r = $page->getCurrentRevision();
432 $current = $page->getCurrentRevision();
433 //FIXME: I suspect this only to crash with Apache2
434 if (!$current->get('markup') or $current->get('markup') < 2) {
435 if (in_array(php_sapi_name(), array('apache2handler', 'apache2filter'))) {
436 return $this->error(_("CreateToc disabled for old markup."));
440 $content = $r->getContent();
442 $html = HTML::div(array('class' => 'toc', 'id' => GenerateId("toc")));
444 $html->setAttr('style', 'display:none;');
446 if (($position == "left") or ($position == "right")) {
447 $html->setAttr('style', 'float:' . $position . '; width:' . $width . ';');
449 $toclistid = GenerateId("toclist");
450 $list = HTML::div(array('id' => $toclistid, 'class' => 'toclist'));
451 if (!strstr($headers, ",")) {
452 $headers = array($headers);
454 $headers = explode(",", $headers);
457 foreach ($headers as $h) {
458 //replace !!! with level 1, ...
459 if (strstr($h, "!")) {
460 $hcount = substr_count($h, '!');
461 $level = min(max(1, $hcount), 3);
464 $level = min(max(1, (int)$h), 5);
469 require_once 'lib/InlineParser.php';
470 if ($headers = $this->extractHeaders($content, $dbi->_markup,
471 $with_toclink, $with_counter,
472 $levels, $firstlevelstyle, $basepage)
474 foreach ($headers as $h) {
475 // proper heading indent
476 $level = $h['level'];
477 $indent = $level - 1;
478 $link = new WikiPageName($pagename, $page, $h['anchor']);
479 $li = WikiLink($link, 'known', $h['text']);
480 // Hack to suppress pagename before #
481 // $li->_attr["href"] = strstr($li->_attr["href"], '#');
482 $list->pushContent(HTML::p(HTML::raw
483 (str_repeat($indentstr, $indent)), $li));
486 $list->setAttr('style', 'display:' . ($jshide ? 'none;' : 'block;'));
487 $open = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowOpen.png");
488 $close = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowClosed.png");
491 $toctoggleid = GenerateId("toctoggle");
493 $toclink = HTML(_("Table of Contents"),
495 HTML::a(array('id' => 'TOC')),
497 'id' => $toctoggleid,
498 'class' => 'wikiaction',
499 'title' => _("Click to display to TOC"),
500 'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')",
501 'alt' => 'toctoggle',
502 'src' => $jshide ? $close : $open)));
504 $toclink = HTML::a(array('id' => 'TOC',
505 'class' => 'wikiaction',
506 'title' => _("Click to display"),
507 'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')"),
508 _("Table of Contents"),
509 HTML::span(array('style' => 'display:none',
510 'id' => $toctoggleid), " "));
511 $html->pushContent(HTML::p(array('class' => 'toctitle'), $toclink));
513 $html->pushContent($list);
514 if (count($headers) == 0) {
515 // Do not display an empty TOC
516 $html->setAttr('style', 'display:none;');
526 // c-hanging-comment-ender-p: nil
527 // indent-tabs-mode: nil