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
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
37 * - some constructs might incorrectly be recognized as a header
38 * (e.g. lines starting with "!!!" or "==" inside <verbatim>)
41 if (!defined('TOC_FULL_SYNTAX'))
42 define('TOC_FULL_SYNTAX', 1);
44 class WikiPlugin_CreateToc
48 return _("CreateToc");
51 function getDescription() {
52 return _("Create a Table of Contents and automatically link to headers");
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' => ' ',
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
69 'with_toclink' => 0, // link back to TOC
72 // Initialisation of toc counter
73 function _initTocCounter() {
74 $counter = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
78 // Update toc counter with a new title
79 function _tocCounter(&$counter, $level) {
81 for($i = $level+1; $i <= 5; $i++) {
86 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 function _letter_counter($number) {
104 return chr(ord("A") + $number - 1);
106 return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
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]);
119 for($i = 2; $i <= 5; $i++) {
120 if($counter[$i] != 0)
121 $str .= '.'.$counter[$i];
126 function preg_quote ($heading) {
127 return str_replace(array("/",".","?","*"),
128 array('\/','\.','\?','\*'), $heading);
131 // Get HTML header corresponding to current level (level is set of ! or =)
132 function _getHeader($level) {
134 $count = substr_count($level,'!');
140 $count = substr_count($level,'=');
151 function _quote($heading) {
152 if (TOC_FULL_SYNTAX ) {
153 $theading = TransformInline($heading);
155 return preg_quote($theading->asXML(), "/");
157 return XmlContent::_quote(preg_quote($heading, "/"));
159 return XmlContent::_quote(preg_quote($heading, "/"));
164 * @param $hstart id (in $content) of heading start
165 * @param $hend id (in $content) of heading end
167 function searchHeader ($content, $start_index, $heading,
168 $level, &$hstart, &$hend, $basepage=false) {
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>/",
179 elseif (isa($content[$j], 'cached_link'))
181 if (method_exists($content[$j],'asXML')) {
182 $content[$j]->_basepage = $basepage;
183 $content[$j] = $content[$j]->asXML();
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>")
193 return $j; // single wikiword
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])) {
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();
208 $joined .= $content[$k]->asString();
209 if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
218 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
222 /** prevent from duplicate anchors,
223 * beautify spaces: " " => "_" and not "x20."
225 function _nextAnchor($s) {
226 static $anchors = array();
228 $s = str_replace(' ','_',$s);
231 while (!empty($anchors[$anchor])) {
232 $anchor = sprintf("%s_%d",$s,$i++);
234 $anchors[$anchor] = $i;
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='')
250 if (!$levels) $levels = array(1,2);
251 $tocCounter = $this->_initTocCounter();
256 $insidetable = false;
257 for ($i=0; $i<count($content); $i++) {
258 if (preg_match('/^\s*{\|/', $content[$i])) {
262 if (preg_match('/^\s*\|}/', $content[$i])) {
263 $insidetable = false;
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]);
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], "=")) {
287 $anchor = $this->_nextAnchor($s);
288 $manchor = MangleXmlIdentifier($anchor);
291 $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
293 $headers[] = array('text' => $texts,
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"));
303 $j = $this->searchHeader($markup->_content, $j, $s,
304 $match[1], $hstart, $hend,
306 if ($j and isset($markup->_content[$j])) {
307 $x = $markup->_content[$j];
308 $qheading = $this->_quote($s);
310 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
311 if (($hstart === 0) && is_string($markup->_content[$j])) {
314 $anchorString = "<a href=\"$url\" name=\"$manchor\">$counterString</a> - \$2";
316 $anchorString = "<a href=\"$url\" name=\"$manchor\">\$2</a>";
318 $anchorString = "<a name=\"$manchor\"></a>";
320 $anchorString .= "$counterString - ";
322 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
323 "\$1$anchorString\$2\$3",$x,1)) {
325 $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
326 "\$1$anchorString\$3",
327 $markup->_content[$j],1);
329 $markup->_content[$j] = $x;
332 $x = $markup->_content[$hstart];
333 $h = $this->_getHeader($match[1]);
337 $anchorString = "\$1<a href=\"$url\" name=\"$manchor\">$counterString</a> - ";
339 /* Not possible to make a backlink on a
340 * title with a WikiWord */
341 $anchorString = "\$1<a name=\"$manchor\"></a>";
345 $anchorString = "\$1<a name=\"$manchor\"></a>";
347 $anchorString .= "$counterString - ";
349 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
350 $anchorString, $x, 1);
352 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
354 $markup->_content[$hstart],1);
356 $markup->_content[$hstart] = $x;
366 function run($dbi, $argstr, &$request, $basepage) {
368 extract($this->getArgs($argstr, $request));
370 // Expand relative page names.
371 $page = new WikiPageName($pagename, $basepage);
372 $pagename = $page->name;
375 return $this->error(_("no page specified"));
377 if ($jshide and isBrowserIE() and browserDetect("Mac")) {
378 //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
381 if (($notoc) or ($liststyle == 'ol')) {
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"),
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);
400 $content = $current->getContent();
401 $html = HTML::div(array('class' => 'toc', 'id'=> GenerateId("toc")));
403 $html->setAttr('style','display:none;');
405 if (($position == "left") or ($position == "right")) {
406 $html->setAttr('style','float:'.$position.'; width:'.$width.';');
408 $toclistid = GenerateId("toclist");
409 $list = HTML::div(array('id'=>$toclistid, 'class'=>'toclist'));
410 if (!strstr($headers,",")) {
411 $headers = array($headers);
413 $headers = explode(",",$headers);
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);
423 $level = min(max(1, (int) $h), 5);
428 require_once("lib/InlineParser.php");
429 if ($headers = $this->extractHeaders($content, $dbi->_markup,
430 $with_toclink, $with_counter,
431 $levels, $firstlevelstyle, $basepage))
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));
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");
450 $toctoggleid = GenerateId("toctoggle");
452 $toclink = HTML(_("Table of Contents"),
454 HTML::a(array('name'=>'TOC')),
457 'class'=>'wikiaction',
458 'title'=>_("Click to display to TOC"),
459 'onclick'=>"toggletoc(this, '".$open."', '".$close."', '".$toclistid."')",
461 'alt' => 'toctoggle',
462 'src' => $jshide ? $close : $open )));
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));
473 $html->pushContent($list);
474 if (count($headers) == 0) {
475 // Do not display an empty TOC
476 $html->setAttr('style','display:none;');
487 // c-hanging-comment-ender-p: nil
488 // indent-tabs-mode: nil