2 rcs_id('$Id: CreateToc.php,v 1.25 2004-07-08 20:30:07 rurban Exp $');
4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * CreateToc: Automatically link to headers
27 * <?plugin CreateToc headers=!!!,!! with_toclink||=1
29 * @author: Reini Urban
32 * - MacIE will not work with jshide.
33 * - it will crash with old markup!
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 define('TOC_FULL_SYNTAX',1);
41 class WikiPlugin_CreateToc
45 return _("CreateToc");
48 function getDescription() {
49 return _("Automatically link headers at the top");
52 function getVersion() {
53 return preg_replace("/[Revision: $]/", '',
54 "\$Revision: 1.25 $");
57 function getDefaultArguments() {
58 return array( 'pagename' => '[pagename]', // not sure yet. TOC of another page here?
59 // or headers=1,2,3 is also possible.
60 'headers' => "!!!,!!,!", // "!!!" => h1, "!!" => h2, "!" => h3
61 'noheader' => 0, // omit <h1>Table of Contents</h1>
63 'with_toclink' => 0, // link back to TOC
64 'jshide' => 0, // collapsed TOC as DHTML button
65 'liststyle' => 'dl', // or 'ul' or 'ol'
66 'indentstr' => ' ',
70 function preg_quote ($heading) {
71 return str_replace(array("/",".","?","*"),
72 array('\/','\.','\?','\*'), $heading);
75 function searchHeader ($content, $start_index, $heading, $level) {
76 $count = substr_count($level,'!');
78 case 1: $h = "h4"; break;
79 case 2: $h = "h3"; break;
80 case 3: $h = "h2"; break;
82 if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX) {
83 $theading = TransformInline($heading);
84 $qheading = preg_quote($theading->asString());
86 $qheading = preg_quote($heading);
88 for ($j=$start_index; $j < count($content); $j++) {
89 if (is_string($content[$j])) {
90 if (preg_match("/<$h>$qheading<\/$h>/",$content[$j])) {
93 } elseif (isa($content[$j],'cached_wikilink')) {
94 // shortcut for single wikiword headers
95 $content[$j] = $content[$j]->asString();
96 if ($content[$j] == $heading and
97 substr($content[$j-1],-4,4) == "<$h>" and
98 substr($content[$j+1],0,5) == "</$h>") {
99 return $j; // single wikiword
100 } elseif (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX) {
101 //DONE: To allow "!! WikiWord link"
102 // Split heading into WikiWords and check against
103 // joined content (after cached_plugininvocation).
104 // The first wikilink is the anchor then.
106 for ($k=max($j-1,$start_index); $k < count($content); $k++) {
107 $joined .= is_string($content[$k]) ? $content[$k]
108 : $content[$k]->asString();
110 if (preg_match("/<$h>$qheading<\/$h>/",$joined))
115 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
119 /** prevent from duplicate anchors,
120 * beautify spaces: " " => "_" and not "x20."
122 function _nextAnchor($s) {
123 static $anchors = array();
125 $s = str_replace(' ','_',$s);
128 while (!empty($anchors[$anchor])) {
129 $anchor = sprintf("%s_%d",$s,$i++);
131 $anchors[$anchor] = $i;
135 // Feature request: proper nesting; multiple levels (e.g. 1,3)
136 function extractHeaders (&$content, &$markup, $backlink=0, $levels=false, $basepage='') {
137 if (!$levels) $levels = array(1,2);
142 for ($i=0; $i<count($content); $i++) {
143 foreach ($levels as $level) {
144 if ($level < 1 or $level > 3) continue;
145 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',$content[$i],$match)) {
146 if (!strstr($content[$i],'#[')) {
147 $s = trim($match[2]);
148 $anchor = $this->_nextAnchor($s);
149 $manchor = MangleXmlIdentifier($anchor);
150 $headers[] = array('text' => $s, 'anchor' => $anchor, 'level' => $level);
151 // Change original wikitext, but that is useless art...
152 $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
153 // And now change the to be printed markup (XmlTree):
154 // Search <hn>$s</hn> line in markup
155 //Fixme: Find non-singleworded wikilink'ed headers also.
156 $j = $this->searchHeader($markup->_content, $j, $s, $match[1]);
157 if ($j and isset($markup->_content[$j])) {
158 $x = $markup->_content[$j];
159 if (is_string($markup->_content[$j])) {
160 $heading = preg_quote($s);
161 if ($x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
162 "\$1<a name=\"$manchor\"></a>\$2\$3",$x,1)) {
164 $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
165 $x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
166 "\$1<a href=\"$url\" name=\"$manchor\">\$2</a>\$3",
167 $markup->_content[$j],1);
169 $markup->_content[$j] = $x;
172 elseif (isa($markup->_content[$j],'cached_wikilink')) {
173 $label = isset($x->_label) ? $x->_label : false;
174 $markup->_content[$j] = HTML::a(array('name'=>$manchor),WikiLink($x->_page, 'auto', $label));
184 function run($dbi, $argstr, &$request, $basepage) {
185 extract($this->getArgs($argstr, $request));
187 // Expand relative page names.
188 $page = new WikiPageName($pagename, $basepage);
189 $pagename = $page->name;
192 return $this->error(_("no page specified"));
194 if ($jshide and isBrowserIE() and browserDetect("Mac")) {
195 //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
198 $page = $dbi->getPage($pagename);
199 $current = $page->getCurrentRevision();
200 //FIXME: I suspect this only to crash with Apache2
201 if (!$current->get('markup') or $current->get('markup') < 2) {
202 if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
203 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
207 $content = $current->getContent();
208 $html = HTML::div(array('class' => 'toc','align' => $align));
209 if ($liststyle == 'dl')
210 $list = HTML::dl(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
211 elseif ($liststyle == 'ul')
212 $list = HTML::ul(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
213 elseif ($liststyle == 'ol')
214 $list = HTML::ol(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
215 if (!strstr($headers,",")) {
216 $headers = array($headers);
218 $headers = explode(",",$headers);
221 foreach ($headers as $h) {
222 //replace !!! with level 1, ...
223 if (strstr($h,"!")) {
224 $hcount = substr_count($h,'!');
225 $level = min(max(1, $hcount),3);
228 $level = min(max(1, (int) $h), 3);
232 if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX)
233 require_once("lib/InlineParser.php");
234 if ($headers = $this->extractHeaders($content, $dbi->_markup,
235 $with_toclink, $levels, $basepage))
237 foreach ($headers as $h) {
238 // proper heading indent
239 $level = $h['level'];
240 $indent = 3 - $level;
241 $link = new WikiPageName($pagename,$page,$h['anchor']);
242 $li = WikiLink($link,'known',$h['text']);
243 if ($liststyle == 'dl')
244 $list->pushContent(HTML::dt(HTML::raw(str_repeat($indentstr,$indent)),$li));
246 $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
250 $list->setAttr('style','display:none;');
251 $html->pushContent(Javascript("
252 function toggletoc(a) {
253 toc=document.getElementById('toclist');
254 if (toc.style.display=='none') {
255 toc.style.display='block';
256 a.title='"._("Click to hide the TOC")."';
258 toc.style.display='none';
259 a.title='"._("Click to display")."';
262 $html->pushContent(HTML::h4(HTML::a(array('name'=>'TOC',
263 'class'=>'wikiaction',
264 'title'=>_("Click to display"),
265 'onclick'=>"toggletoc(this)"),
266 _("Table Of Contents"))));
269 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
271 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
273 $html->pushContent($list);
278 // $Log: not supported by cvs2svn $
279 // Revision 1.24 2004/06/28 13:27:03 rurban
280 // CreateToc disabled for old markup and Apache2 only
282 // Revision 1.23 2004/06/28 13:13:58 rurban
283 // CreateToc disabled for old markup
285 // Revision 1.22 2004/06/15 14:56:37 rurban
286 // more allow_call_time_pass_reference false fixes
288 // Revision 1.21 2004/06/13 09:45:23 rurban
289 // display bug workaround for MacIE browsers, jshide: 0
291 // Revision 1.20 2004/05/11 13:57:46 rurban
292 // enable TOC_FULL_SYNTAX per default
293 // don't <a name>$header</a> to disable css formatting for such anchors
294 // => <a name></a>$header
296 // Revision 1.19 2004/05/08 16:59:27 rurban
297 // requires optional TOC_FULL_SYNTAX constnat to enable full link and
298 // wikiword syntax in headers.
300 // Revision 1.18 2004/04/29 21:55:15 rurban
301 // fixed TOC backlinks with USE_PATH_INFO false
302 // with_toclink=1, sf.net bug #940682
304 // Revision 1.17 2004/04/26 19:43:03 rurban
305 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
307 // Revision 1.16 2004/04/26 14:46:14 rurban
310 // Revision 1.14 2004/04/21 04:29:50 rurban
311 // write WikiURL consistently (not WikiUrl)
313 // Revision 1.12 2004/03/22 14:13:53 rurban
314 // fixed links to equal named headers
316 // Revision 1.11 2004/03/15 09:52:59 rurban
317 // jshide button: dynamic titles
319 // Revision 1.10 2004/03/14 20:30:21 rurban
322 // Revision 1.9 2004/03/09 19:24:20 rurban
326 // Revision 1.8 2004/03/09 19:05:12 rurban
327 // new liststyle arg. default: dl (no bullets)
329 // Revision 1.7 2004/03/09 11:51:54 rurban
330 // support jshide=1: DHTML button hide/unhide TOC
332 // Revision 1.6 2004/03/09 10:25:37 rurban
333 // slightly better formatted TOC indentation
335 // Revision 1.5 2004/03/09 08:57:10 rurban
336 // convert space to "_" instead of "x20." in anchors
337 // proper heading indent
338 // handle duplicate headers
339 // allow multiple headers like "!!!,!!" or "1,2"
341 // Revision 1.4 2004/03/02 18:21:29 rurban
344 // Revision 1.1 2004/03/01 18:10:28 rurban
345 // first version, without links, anchors and jscript folding
354 // c-hanging-comment-ender-p: nil
355 // indent-tabs-mode: nil