4 Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5 Copyright 2008 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 * <?plugin 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 getVersion() {
54 return preg_replace("/[Revision: $]/", '',
58 function getDefaultArguments() {
59 return array( 'pagename' => '[pagename]', // TOC of another page here?
60 'headers' => "1,2,3,4,5", // "!!!"=>h2, "!!"=>h3, "!"=>h4
61 // "1"=>h2, "2"=>h3, "3"=>h4, "4"=>h5, "5"=>h6
62 'noheader' => 0, // omit "Table of Contents" header
63 'notoc' => 0, // do not display TOC, only number headers
64 'position' => 'right', // or left
65 'with_toclink' => 0, // link back to TOC
66 'jshide' => 0, // collapsed TOC as DHTML button
67 'extracollapse' => 1, // provide an entry +/- link to collapse
68 'liststyle' => 'dl', // 'dl' or 'ul' or 'ol'
69 'indentstr' => ' ',
71 'firstlevelstyle' => 'number' // 'number', 'letter' or 'roman'
74 // Initialisation of toc counter
75 function _initTocCounter() {
76 $counter = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0);
80 // Update toc counter with a new title
81 function _tocCounter(&$counter, $level) {
83 for($i = $level+1; $i <= 5; $i++) {
88 function _roman_counter($number) {
93 $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
94 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
96 foreach ($lookup as $roman => $value) {
97 $matches = intval($n / $value);
98 $result .= str_repeat($roman, $matches);
104 function _letter_counter($number) {
106 return chr(ord("A") + $number - 1);
108 return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
112 // Get string corresponding to the current title
113 function _getCounter(&$counter, $level, $firstlevelstyle) {
114 if ($firstlevelstyle == 'roman') {
115 $str= $this->_roman_counter($counter[1]);
116 } else if ($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 function preg_quote ($heading) {
129 return str_replace(array("/",".","?","*"),
130 array('\/','\.','\?','\*'), $heading);
133 // Get HTML header corresponding to current level (level is set of ! or =)
134 function _getHeader($level) {
136 $count = substr_count($level,'!');
142 $count = substr_count($level,'=');
153 function _quote($heading) {
154 if (TOC_FULL_SYNTAX ) {
155 $theading = TransformInline($heading);
157 return preg_quote($theading->asXML(), "/");
159 return XmlContent::_quote(preg_quote($heading, "/"));
161 return XmlContent::_quote(preg_quote($heading, "/"));
166 * @param $hstart id (in $content) of heading start
167 * @param $hend id (in $content) of heading end
169 function searchHeader ($content, $start_index, $heading,
170 $level, &$hstart, &$hend, $basepage=false) {
173 $h = $this->_getHeader($level);
174 $qheading = $this->_quote($heading);
175 for ($j=$start_index; $j < count($content); $j++) {
176 if (is_string($content[$j])) {
177 if (preg_match("/<$h>$qheading<\/$h>/",
181 elseif (isa($content[$j], 'cached_link'))
183 if (method_exists($content[$j],'asXML')) {
184 $content[$j]->_basepage = $basepage;
185 $content[$j] = $content[$j]->asXML();
187 $content[$j] = $content[$j]->asString();
188 // shortcut for single wikiword or link headers
189 if ($content[$j] == $heading
190 and substr($content[$j-1],-4,4) == "<$h>"
191 and substr($content[$j+1],0,5) == "</$h>")
195 return $j; // single wikiword
197 elseif (TOC_FULL_SYNTAX) {
198 //DONE: To allow "!! WikiWord link" or !! http://anylink/
199 // Check against joined content (after cached_plugininvocation).
200 // The first link is the anchor then.
201 if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j-1])) {
204 for ($k=max($j-1,$start_index);$k<count($content);$k++) {
205 if (is_string($content[$k]))
206 $joined .= $content[$k];
207 elseif (method_exists($content[$k],'asXML'))
208 $joined .= $content[$k]->asXML();
210 $joined .= $content[$k]->asString();
211 if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
220 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
224 /** prevent from duplicate anchors,
225 * beautify spaces: " " => "_" and not "x20."
227 function _nextAnchor($s) {
228 static $anchors = array();
230 $s = str_replace(' ','_',$s);
233 while (!empty($anchors[$anchor])) {
234 $anchor = sprintf("%s_%d",$s,$i++);
236 $anchors[$anchor] = $i;
240 // Feature request: proper nesting; multiple levels (e.g. 1,3)
241 function extractHeaders (&$content, &$markup, $backlink=0,
242 $counter=0, $levels=false, $firstlevelstyle='number', $basepage='')
244 if (!$levels) $levels = array(1,2);
245 $tocCounter = $this->_initTocCounter();
250 for ($i=0; $i<count($content); $i++) {
251 foreach ($levels as $level) {
252 if ($level < 1 or $level > 5) continue;
253 $phpwikiclassiclevel = 4 -$level;
254 $wikicreolelevel = $level + 1;
255 if ($phpwikiclassiclevel < 1 or $phpwikiclassiclevel > 3) continue;
256 if ((preg_match('/^\s*(!{'.$phpwikiclassiclevel.','.$phpwikiclassiclevel.'})([^!].*)$/', $content[$i], $match))
257 or (preg_match('/^\s*(={'.$wikicreolelevel.','.$wikicreolelevel.'})([^=].*)$/', $content[$i], $match)) )
259 $this->_tocCounter($tocCounter, $level);
260 if (!strstr($content[$i],'#[')) {
261 $s = trim($match[2]);
262 $anchor = $this->_nextAnchor($s);
263 $manchor = MangleXmlIdentifier($anchor);
266 $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
268 $headers[] = array('text' => $texts,
271 // Change original wikitext, but that is useless art...
272 $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
273 // And now change the to be printed markup (XmlTree):
274 // Search <hn>$s</hn> line in markup
275 /* Url for backlink */
276 $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
277 $j = $this->searchHeader($markup->_content, $j, $s,
278 $match[1], $hstart, $hend,
280 if ($j and isset($markup->_content[$j])) {
281 $x = $markup->_content[$j];
282 $qheading = $this->_quote($s);
284 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
285 if (($hstart === 0) && is_string($markup->_content[$j])) {
288 $anchorString = "<a href=\"$url\" name=\"$manchor\">$counterString</a> - \$2";
290 $anchorString = "<a href=\"$url\" name=\"$manchor\">\$2</a>";
292 $anchorString = "<a name=\"$manchor\"></a>";
294 $anchorString .= "$counterString - ";
296 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
297 "\$1$anchorString\$2\$3",$x,1)) {
299 $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
300 "\$1$anchorString\$3",
301 $markup->_content[$j],1);
303 $markup->_content[$j] = $x;
306 $x = $markup->_content[$hstart];
307 $h = $this->_getHeader($match[1]);
311 $anchorString = "\$1<a href=\"$url\" name=\"$manchor\">$counterString</a> - ";
313 /* Not possible to make a backlink on a
314 * title with a WikiWord */
315 $anchorString = "\$1<a name=\"$manchor\"></a>";
319 $anchorString = "\$1<a name=\"$manchor\"></a>";
321 $anchorString .= "$counterString - ";
323 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
324 $anchorString, $x, 1);
326 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
328 $markup->_content[$hstart],1);
330 $markup->_content[$hstart] = $x;
340 function run($dbi, $argstr, &$request, $basepage) {
342 extract($this->getArgs($argstr, $request));
344 // Expand relative page names.
345 $page = new WikiPageName($pagename, $basepage);
346 $pagename = $page->name;
349 return $this->error(_("no page specified"));
351 if ($jshide and isBrowserIE() and browserDetect("Mac")) {
352 //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
355 if (($notoc) or ($liststyle == 'ol')) {
358 $page = $dbi->getPage($pagename);
359 $current = $page->getCurrentRevision();
360 //FIXME: I suspect this only to crash with Apache2
361 if (!$current->get('markup') or $current->get('markup') < 2) {
362 if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
363 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
367 $content = $current->getContent();
368 $html = HTML::div(array('class' => 'toc', 'id'=>'toc'));
370 $html->setAttr('style','display:none;');
372 if ($liststyle == 'dl')
373 $list = HTML::dl(array('id'=>'toclist','class' => 'toc'));
374 elseif ($liststyle == 'ul')
375 $list = HTML::ul(array('id'=>'toclist','class' => 'toc'));
376 elseif ($liststyle == 'ol')
377 $list = HTML::ol(array('id'=>'toclist','class' => 'toc'));
378 if (!strstr($headers,",")) {
379 $headers = array($headers);
381 $headers = explode(",",$headers);
384 foreach ($headers as $h) {
385 //replace !!! with level 1, ...
386 if (strstr($h,"!")) {
387 $hcount = substr_count($h,'!');
388 $level = min(max(1, $hcount),3);
391 $level = min(max(1, (int) $h), 5);
396 require_once("lib/InlineParser.php");
397 if ($headers = $this->extractHeaders($content, $dbi->_markup,
398 $with_toclink, $with_counter,
399 $levels, $firstlevelstyle, $basepage))
401 foreach ($headers as $h) {
402 // proper heading indent
403 $level = $h['level'];
404 $indent = $level - 1;
405 $link = new WikiPageName($pagename,$page,$h['anchor']);
406 $li = WikiLink($link,'known',$h['text']);
407 if ($liststyle == 'dl')
408 $list->pushContent(HTML::dt(HTML::raw
409 (str_repeat($indentstr,$indent)),$li));
411 $list->pushContent(HTML::li(HTML::raw
412 (str_repeat($indentstr,$indent)),$li));
415 $list->setAttr('style','display:'.($jshide?'none;':'block;'));
416 $open = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowOpen.png");
417 $close = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowClosed.png");
418 $html->pushContent(Javascript("
419 function toggletoc(a) {
420 var toc=document.getElementById('toclist')
421 //toctoggle=document.getElementById('toctoggle')
423 var close='".$close."'
424 if (toc.style.display=='none') {
425 toc.style.display='block'
426 a.title='"._("Click to hide the TOC")."'
429 toc.style.display='none';
430 a.title='"._("Click to display")."'
437 $toclink = HTML(_("Table of Contents"),
439 HTML::a(array('name'=>'TOC')),
442 'class'=>'wikiaction',
443 'title'=>_("Click to display to TOC"),
444 'onclick'=>"toggletoc(this)",
446 'alt' => 'toctoggle',
447 'src' => $jshide ? $close : $open )));
449 $toclink = HTML::a(array('name'=>'TOC',
450 'class'=>'wikiaction',
451 'title'=>_("Click to display"),
452 'onclick'=>"toggletoc(this)"),
453 _("Table of Contents"),
454 HTML::span(array('style'=>'display:none',
455 'id'=>'toctoggle')," "));
456 $html->pushContent(HTML::p(array('id'=>'toctitle'), $toclink));
458 $html->pushContent($list);
459 if (count($headers) == 0) {
460 // Do not display an empty TOC
461 $html->setAttr('style','display:none;');
472 // c-hanging-comment-ender-p: nil
473 // indent-tabs-mode: nil