2 rcs_id('$Id: CreateToc.php,v 1.39 2008-08-19 18:19:01 vargenau Exp $');
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: Automatically link to headers
28 * <?plugin CreateToc headers=!!!,!! with_toclink||=1
30 * @author: Reini Urban, Marc-Etienne Vargenau
33 * - MacIE will not work with jshide.
34 * - it will crash with old markup and Apache2 (?)
35 * - Certain corner-edges will not work with TOC_FULL_SYNTAX.
36 * I believe I fixed all of them now, but who knows?
37 * - bug #969495 "existing labels not honored" seems to be fixed.
40 if (!defined('TOC_FULL_SYNTAX'))
41 define('TOC_FULL_SYNTAX', 1);
43 class WikiPlugin_CreateToc
47 return _("CreateToc");
50 function getDescription() {
51 return _("Create a Table of Contents and automatically link to headers");
54 function getVersion() {
55 return preg_replace("/[Revision: $]/", '',
56 "\$Revision: 1.39 $");
59 function getDefaultArguments() {
60 return array( 'pagename' => '[pagename]', // TOC of another page here?
61 // or headers=1,2,3 is also possible.
62 'headers' => "!!!,!!,!", // "!!!"=>h1, "!!"=>h2, "!"=>h3
63 'noheader' => 0, // omit "Table of Contents" header
64 'notoc' => 0, // do not display TOC, only number headers
65 'position' => 'right', // or left
66 'with_toclink' => 0, // link back to TOC
67 'jshide' => 0, // collapsed TOC as DHTML button
68 'extracollapse' => 1, // provide an entry +/- link to collapse
69 'liststyle' => 'dl', // 'dl' or 'ul' or 'ol'
70 'indentstr' => ' ',
72 'firstlevelstyle' => 'number' // 'number', 'letter' or 'roman'
75 // Initialisation of toc counter
76 function _initTocCounter() {
77 $counter = array(1=>0, 2=>0, 3=>0);
81 // Update toc counter with a new title
82 function _tocCounter(&$counter, $level) {
85 for($i = $level; $i > 0; $i--) {
90 function _roman_counter($number) {
95 $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
96 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
98 foreach ($lookup as $roman => $value) {
99 $matches = intval($n / $value);
100 $result .= str_repeat($roman, $matches);
106 function _letter_counter($number) {
108 return chr(ord("A") + $number - 1);
110 return chr(ord("A") + ($number/26) - 1) . chr(ord("A") + ($number%26));
114 // Get string corresponding to the current title
115 function _getCounter(&$counter, $level, $firstlevelstyle) {
116 if ($firstlevelstyle == 'roman') {
117 $str= $this->_roman_counter($counter[3]);
118 } else if ($firstlevelstyle == 'letter') {
119 $str= $this->_letter_counter($counter[3]);
123 for($i = 2; $i > 0; $i--) {
124 if($counter[$i] != 0)
125 $str .= '.'.$counter[$i];
130 function preg_quote ($heading) {
131 return str_replace(array("/",".","?","*"),
132 array('\/','\.','\?','\*'), $heading);
135 // Get HTML header corresponding to current level (level is set of !)
136 function _getHeader($level) {
137 $count = substr_count($level,'!');
139 case 1: $h = "h4"; break;
140 case 2: $h = "h3"; break;
141 case 3: $h = "h2"; break;
146 function _quote($heading) {
147 if (TOC_FULL_SYNTAX ) {
148 $theading = TransformInline($heading);
150 return preg_quote($theading->asXML(), "/");
152 return XmlContent::_quote(preg_quote($heading, "/"));
154 return XmlContent::_quote(preg_quote($heading, "/"));
159 * @param $hstart id (in $content) of heading start
160 * @param $hend id (in $content) of heading end
162 function searchHeader ($content, $start_index, $heading,
163 $level, &$hstart, &$hend, $basepage=false) {
166 $h = $this->_getHeader($level);
167 $qheading = $this->_quote($heading);
168 for ($j=$start_index; $j < count($content); $j++) {
169 if (is_string($content[$j])) {
170 if (preg_match("/<$h>$qheading<\/$h>/",
174 elseif (isa($content[$j], 'cached_link'))
176 if (method_exists($content[$j],'asXML')) {
177 $content[$j]->_basepage = $basepage;
178 $content[$j] = $content[$j]->asXML();
180 $content[$j] = $content[$j]->asString();
181 // shortcut for single wikiword or link headers
182 if ($content[$j] == $heading
183 and substr($content[$j-1],-4,4) == "<$h>"
184 and substr($content[$j+1],0,5) == "</$h>")
188 return $j; // single wikiword
190 elseif (TOC_FULL_SYNTAX) {
191 //DONE: To allow "!! WikiWord link" or !! http://anylink/
192 // Check against joined content (after cached_plugininvocation).
193 // The first link is the anchor then.
194 if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j-1])) {
197 for ($k=max($j-1,$start_index);$k<count($content);$k++) {
198 if (is_string($content[$k]))
199 $joined .= $content[$k];
200 elseif (method_exists($content[$k],'asXML'))
201 $joined .= $content[$k]->asXML();
203 $joined .= $content[$k]->asString();
204 if (preg_match("/<$h>$qheading<\/$h>/",$joined)) {
213 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
217 /** prevent from duplicate anchors,
218 * beautify spaces: " " => "_" and not "x20."
220 function _nextAnchor($s) {
221 static $anchors = array();
223 $s = str_replace(' ','_',$s);
226 while (!empty($anchors[$anchor])) {
227 $anchor = sprintf("%s_%d",$s,$i++);
229 $anchors[$anchor] = $i;
233 // Feature request: proper nesting; multiple levels (e.g. 1,3)
234 function extractHeaders (&$content, &$markup, $backlink=0,
235 $counter=0, $levels=false, $firstlevelstyle='number', $basepage='')
237 if (!$levels) $levels = array(1,2);
238 $tocCounter = $this->_initTocCounter();
243 for ($i=0; $i<count($content); $i++) {
244 foreach ($levels as $level) {
245 if ($level < 1 or $level > 3) continue;
246 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',
247 $content[$i], $match))
249 $this->_tocCounter($tocCounter, $level);
250 if (!strstr($content[$i],'#[')) {
251 $s = trim($match[2]);
252 $anchor = $this->_nextAnchor($s);
253 $manchor = MangleXmlIdentifier($anchor);
256 $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle).' '.$s;
258 $headers[] = array('text' => $texts,
261 // Change original wikitext, but that is useless art...
262 $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
263 // And now change the to be printed markup (XmlTree):
264 // Search <hn>$s</hn> line in markup
265 /* Url for backlink */
266 $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
267 $j = $this->searchHeader($markup->_content, $j, $s,
268 $match[1], $hstart, $hend,
270 if ($j and isset($markup->_content[$j])) {
271 $x = $markup->_content[$j];
272 $qheading = $this->_quote($s);
274 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
275 if (($hstart === 0) && is_string($markup->_content[$j])) {
278 $anchorString = "<a href=\"$url\" name=\"$manchor\">$counterString</a> - \$2";
280 $anchorString = "<a href=\"$url\" name=\"$manchor\">\$2</a>";
282 $anchorString = "<a name=\"$manchor\"></a>";
284 $anchorString .= "$counterString - ";
286 if ($x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
287 "\$1$anchorString\$2\$3",$x,1)) {
289 $x = preg_replace('/(<h\d>)('.$qheading.')(<\/h\d>)/',
290 "\$1$anchorString\$3",
291 $markup->_content[$j],1);
293 $markup->_content[$j] = $x;
296 $x = $markup->_content[$hstart];
297 $h = $this->_getHeader($match[1]);
301 $anchorString = "\$1<a href=\"$url\" name=\"$manchor\">$counterString</a> - ";
303 /* Not possible to make a backlink on a
304 * title with a WikiWord */
305 $anchorString = "\$1<a name=\"$manchor\"></a>";
309 $anchorString = "\$1<a name=\"$manchor\"></a>";
311 $anchorString .= "$counterString - ";
313 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
314 $anchorString, $x, 1);
316 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
318 $markup->_content[$hstart],1);
320 $markup->_content[$hstart] = $x;
330 function run($dbi, $argstr, &$request, $basepage) {
332 extract($this->getArgs($argstr, $request));
334 // Expand relative page names.
335 $page = new WikiPageName($pagename, $basepage);
336 $pagename = $page->name;
339 return $this->error(_("no page specified"));
341 if ($jshide and isBrowserIE() and browserDetect("Mac")) {
342 //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
345 if (($notoc) or ($liststyle == 'ol')) {
348 $page = $dbi->getPage($pagename);
349 $current = $page->getCurrentRevision();
350 //FIXME: I suspect this only to crash with Apache2
351 if (!$current->get('markup') or $current->get('markup') < 2) {
352 if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
353 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
357 $content = $current->getContent();
358 $html = HTML::div(array('class' => 'toc', 'id'=>'toc'));
360 $html->setAttr('style','display:none;');
362 if ($liststyle == 'dl')
363 $list = HTML::dl(array('id'=>'toclist','class' => 'toc'));
364 elseif ($liststyle == 'ul')
365 $list = HTML::ul(array('id'=>'toclist','class' => 'toc'));
366 elseif ($liststyle == 'ol')
367 $list = HTML::ol(array('id'=>'toclist','class' => 'toc'));
368 if (!strstr($headers,",")) {
369 $headers = array($headers);
371 $headers = explode(",",$headers);
374 foreach ($headers as $h) {
375 //replace !!! with level 1, ...
376 if (strstr($h,"!")) {
377 $hcount = substr_count($h,'!');
378 $level = min(max(1, $hcount),3);
381 $level = min(max(1, (int) $h), 3);
386 require_once("lib/InlineParser.php");
387 if ($headers = $this->extractHeaders($content, $dbi->_markup,
388 $with_toclink, $with_counter,
389 $levels, $firstlevelstyle, $basepage))
391 foreach ($headers as $h) {
392 // proper heading indent
393 $level = $h['level'];
394 $indent = 3 - $level;
395 $link = new WikiPageName($pagename,$page,$h['anchor']);
396 $li = WikiLink($link,'known',$h['text']);
397 if ($liststyle == 'dl')
398 $list->pushContent(HTML::dt(HTML::raw
399 (str_repeat($indentstr,$indent)),$li));
401 $list->pushContent(HTML::li(HTML::raw
402 (str_repeat($indentstr,$indent)),$li));
405 $list->setAttr('style','display:'.($jshide?'none;':'block;'));
406 $open = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowOpen.png");
407 $close = DATA_PATH.'/'.$WikiTheme->_findFile("images/folderArrowClosed.png");
408 $html->pushContent(Javascript("
409 function toggletoc(a) {
410 var toc=document.getElementById('toclist')
411 //toctoggle=document.getElementById('toctoggle')
413 var close='".$close."'
414 if (toc.style.display=='none') {
415 toc.style.display='block'
416 a.title='"._("Click to hide the TOC")."'
419 toc.style.display='none';
420 a.title='"._("Click to display")."'
425 $toclink = HTML(_("Table of Contents"),
427 HTML::a(array('name'=>'TOC')),
430 'class'=>'wikiaction',
431 'title'=>_("Click to display to TOC"),
432 'onclick'=>"toggletoc(this)",
436 'alt' => 'toctoggle',
437 'src' => $jshide ? $close : $open )));
439 $toclink = HTML::a(array('name'=>'TOC',
440 'class'=>'wikiaction',
441 'title'=>_("Click to display"),
442 'onclick'=>"toggletoc(this)"),
443 _("Table of Contents"),
444 HTML::span(array('style'=>'display:none',
445 'id'=>'toctoggle')," "));
446 $html->pushContent(HTML::h4($toclink));
447 $html->pushContent($list);
452 // $Log: not supported by cvs2svn $
453 // Revision 1.38 2008/08/19 18:15:28 vargenau
454 // Implement "notoc" parameter
456 // Revision 1.37 2008/05/04 08:37:42 vargenau
459 // Revision 1.36 2007/07/19 12:41:25 labbenes
460 // Correct TOC numbering. It should start from '1' not from '1.1'.
462 // Revision 1.35 2007/02/17 14:17:48 rurban
463 // declare vars for IE6
465 // Revision 1.34 2007/01/28 22:47:06 rurban
468 // Revision 1.33 2007/01/28 22:37:04 rurban
469 // beautify +/- collapse icon
471 // Revision 1.32 2007/01/20 11:25:30 rurban
474 // Revision 1.31 2007/01/09 12:35:05 rurban
475 // Change align to position. Add extracollapse. js now always active, jshide just denotes the initial state.
477 // Revision 1.30 2006/12/22 17:49:38 rurban
480 // Revision 1.29 2006/04/15 12:26:54 rurban
481 // need basepage for subpages like /Remove (within CreateTOC)
483 // Revision 1.28 2005/10/12 06:15:25 rurban
486 // Revision 1.27 2005/10/10 19:50:45 rurban
487 // fix the missing formatting problems, add with_counter arg by ?? (20050106), Thanks to ManuelVacelet for the testcase
489 // Revision 1.26 2004/09/20 14:07:16 rurban
490 // fix Constant toc_full_syntax already defined warning
492 // Revision 1.25 2004/07/08 20:30:07 rurban
493 // plugin->run consistency: request as reference, added basepage.
494 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
496 // Revision 1.24 2004/06/28 13:27:03 rurban
497 // CreateToc disabled for old markup and Apache2 only
499 // Revision 1.23 2004/06/28 13:13:58 rurban
500 // CreateToc disabled for old markup
502 // Revision 1.22 2004/06/15 14:56:37 rurban
503 // more allow_call_time_pass_reference false fixes
505 // Revision 1.21 2004/06/13 09:45:23 rurban
506 // display bug workaround for MacIE browsers, jshide: 0
508 // Revision 1.20 2004/05/11 13:57:46 rurban
509 // enable TOC_FULL_SYNTAX per default
510 // don't <a name>$header</a> to disable css formatting for such anchors
511 // => <a name></a>$header
513 // Revision 1.19 2004/05/08 16:59:27 rurban
514 // requires optional TOC_FULL_SYNTAX constnat to enable full link and
515 // wikiword syntax in headers.
517 // Revision 1.18 2004/04/29 21:55:15 rurban
518 // fixed TOC backlinks with USE_PATH_INFO false
519 // with_toclink=1, sf.net bug #940682
521 // Revision 1.17 2004/04/26 19:43:03 rurban
522 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
524 // Revision 1.16 2004/04/26 14:46:14 rurban
527 // Revision 1.14 2004/04/21 04:29:50 rurban
528 // write WikiURL consistently (not WikiUrl)
530 // Revision 1.12 2004/03/22 14:13:53 rurban
531 // fixed links to equal named headers
533 // Revision 1.11 2004/03/15 09:52:59 rurban
534 // jshide button: dynamic titles
536 // Revision 1.10 2004/03/14 20:30:21 rurban
539 // Revision 1.9 2004/03/09 19:24:20 rurban
543 // Revision 1.8 2004/03/09 19:05:12 rurban
544 // new liststyle arg. default: dl (no bullets)
546 // Revision 1.7 2004/03/09 11:51:54 rurban
547 // support jshide=1: DHTML button hide/unhide TOC
549 // Revision 1.6 2004/03/09 10:25:37 rurban
550 // slightly better formatted TOC indentation
552 // Revision 1.5 2004/03/09 08:57:10 rurban
553 // convert space to "_" instead of "x20." in anchors
554 // proper heading indent
555 // handle duplicate headers
556 // allow multiple headers like "!!!,!!" or "1,2"
558 // Revision 1.4 2004/03/02 18:21:29 rurban
561 // Revision 1.1 2004/03/01 18:10:28 rurban
562 // first version, without links, anchors and jscript folding
571 // c-hanging-comment-ender-p: nil
572 // indent-tabs-mode: nil