]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateToc.php
proper msg if rss connection is broken or no items found
[SourceForge/phpwiki.git] / lib / plugin / CreateToc.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreateToc.php,v 1.26 2004-09-20 14:07:16 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
24  * CreateToc:  Automatically link to headers
25  *
26  * Usage:   
27  *  <?plugin CreateToc headers=!!!,!! with_toclink||=1 
28  *                     jshide||=1 ?>
29  * @author:  Reini Urban
30  *
31  * Known problems: 
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.
37  */
38
39 if (!defined('TOC_FULL_SYNTAX'))
40     define('TOC_FULL_SYNTAX',1);
41
42 class WikiPlugin_CreateToc
43 extends WikiPlugin
44 {
45     function getName() {
46         return _("CreateToc");
47     }
48
49     function getDescription() {
50         return _("Automatically link headers at the top");
51     }
52
53     function getVersion() {
54         return preg_replace("/[Revision: $]/", '',
55                             "\$Revision: 1.26 $");
56     }
57
58     function getDefaultArguments() {
59         return array( 'pagename'  => '[pagename]', // not sure yet. TOC of another page here?
60                       // or headers=1,2,3 is also possible.
61                       'headers'   => "!!!,!!,!",   // "!!!" => h1, "!!" => h2, "!" => h3
62                       'noheader'  => 0,            // omit <h1>Table of Contents</h1>
63                       'align'     => 'left',
64                       'with_toclink' => 0,         // link back to TOC
65                       'jshide'    => 0,            // collapsed TOC as DHTML button
66                       'liststyle' => 'dl',         // or 'ul' or 'ol'
67                       'indentstr' => '&nbsp;&nbsp;',
68                       );
69     }
70
71     function preg_quote ($heading) {
72         return str_replace(array("/",".","?","*"),
73                            array('\/','\.','\?','\*'), $heading);
74     }
75     
76     function searchHeader ($content, $start_index, $heading, $level) {
77         $count = substr_count($level,'!');
78         switch ($count) {
79                 case 1: $h = "h4"; break;
80                 case 2: $h = "h3"; break;
81                 case 3: $h = "h2"; break;
82         }
83         if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX) {
84             $theading = TransformInline($heading);
85             $qheading = preg_quote($theading->asString());
86         } else {
87             $qheading = preg_quote($heading);
88         }
89         for ($j=$start_index; $j < count($content); $j++) {
90             if (is_string($content[$j])) {
91                 if (preg_match("/<$h>$qheading<\/$h>/",$content[$j])) {
92                     return $j;
93                 }
94             } elseif (isa($content[$j],'cached_wikilink')) {
95                 // shortcut for single wikiword headers
96                 $content[$j] = $content[$j]->asString();
97                 if ($content[$j] == $heading and 
98                     substr($content[$j-1],-4,4) == "<$h>" and
99                     substr($content[$j+1],0,5) == "</$h>") {
100                     return $j; // single wikiword
101                 } elseif (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX) {
102                     //DONE: To allow "!! WikiWord link"
103                     // Split heading into WikiWords and check against 
104                     // joined content (after cached_plugininvocation).
105                     // The first wikilink is the anchor then.
106                     $joined = '';
107                     for ($k=max($j-1,$start_index); $k < count($content); $k++) {
108                         $joined .= is_string($content[$k]) ? $content[$k] 
109                                                            : $content[$k]->asString();
110                     }
111                     if (preg_match("/<$h>$qheading<\/$h>/",$joined))
112                         return $j;
113                 }
114             }
115         }
116         trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
117         return 0;
118     }
119
120     /** prevent from duplicate anchors,
121      *  beautify spaces: " " => "_" and not "x20."
122      */
123     function _nextAnchor($s) {
124         static $anchors = array();
125
126         $s = str_replace(' ','_',$s);
127         $i = 1;
128         $anchor = $s;
129         while (!empty($anchors[$anchor])) {
130             $anchor = sprintf("%s_%d",$s,$i++);
131         }
132         $anchors[$anchor] = $i;
133         return $anchor;
134     }
135     
136     // Feature request: proper nesting; multiple levels (e.g. 1,3)
137     function extractHeaders (&$content, &$markup, $backlink=0, $levels=false, $basepage='') {
138         if (!$levels) $levels = array(1,2);
139         reset($levels);
140         sort($levels);
141         $headers = array();
142         $j = 0;
143         for ($i=0; $i<count($content); $i++) {
144             foreach ($levels as $level) {
145                 if ($level < 1 or $level > 3) continue;
146                 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',$content[$i],$match)) {
147                     if (!strstr($content[$i],'#[')) {
148                         $s = trim($match[2]);
149                         $anchor = $this->_nextAnchor($s);
150                         $manchor = MangleXmlIdentifier($anchor);
151                         $headers[] = array('text' => $s, 'anchor' => $anchor, 'level' => $level);
152                         // Change original wikitext, but that is useless art...
153                         $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
154                         // And now change the to be printed markup (XmlTree):
155                         // Search <hn>$s</hn> line in markup
156                         //Fixme: Find non-singleworded wikilink'ed headers also.
157                         $j = $this->searchHeader($markup->_content, $j, $s, $match[1]);
158                         if ($j and isset($markup->_content[$j])) {
159                             $x = $markup->_content[$j];
160                             if (is_string($markup->_content[$j])) {
161                                 $heading = preg_quote($s);
162                                 if ($x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
163                                                       "\$1<a name=\"$manchor\"></a>\$2\$3",$x,1)) {
164                                     if ($backlink) {
165                                         $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
166                                         $x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
167                                                           "\$1<a href=\"$url\" name=\"$manchor\">\$2</a>\$3",
168                                                           $markup->_content[$j],1);
169                                     }
170                                     $markup->_content[$j] = $x;
171                                 }
172                             }
173                             elseif (isa($markup->_content[$j],'cached_wikilink')) {
174                                 $label = isset($x->_label) ? $x->_label : false;
175                                 $markup->_content[$j] = HTML::a(array('name'=>$manchor),WikiLink($x->_page, 'auto', $label));
176                             }
177                         }
178                     }
179                 }
180             }
181         }
182         return $headers;
183     }
184                 
185     function run($dbi, $argstr, &$request, $basepage) {
186         extract($this->getArgs($argstr, $request));
187         if ($pagename) {
188             // Expand relative page names.
189             $page = new WikiPageName($pagename, $basepage);
190             $pagename = $page->name;
191         }
192         if (!$pagename) {
193             return $this->error(_("no page specified"));
194         }
195         if ($jshide and isBrowserIE() and browserDetect("Mac")) {
196             //trigger_error(_("jshide set to 0 on Mac IE"), E_USER_NOTICE);
197             $jshide = 0;
198         }
199         $page = $dbi->getPage($pagename);
200         $current = $page->getCurrentRevision();
201         //FIXME: I suspect this only to crash with Apache2
202         if (!$current->get('markup') or $current->get('markup') < 2) {
203             if (in_array(php_sapi_name(),array('apache2handler','apache2filter'))) {
204                 trigger_error(_("CreateToc disabled for old markup"), E_USER_WARNING);
205                 return '';
206             }
207         }
208         $content = $current->getContent();
209         $html = HTML::div(array('class' => 'toc','align' => $align));
210         if ($liststyle == 'dl')
211             $list = HTML::dl(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
212         elseif ($liststyle == 'ul')
213             $list = HTML::ul(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
214         elseif ($liststyle == 'ol')
215             $list = HTML::ol(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
216         if (!strstr($headers,",")) {
217             $headers = array($headers); 
218         } else {
219             $headers = explode(",",$headers);
220         }
221         $levels = array();
222         foreach ($headers as $h) {
223             //replace !!! with level 1, ...
224             if (strstr($h,"!")) {
225                 $hcount = substr_count($h,'!');
226                 $level = min(max(1, $hcount),3);
227                 $levels[] = $level;
228             } else {
229                 $level = min(max(1, (int) $h), 3);
230                 $levels[] = $level;
231             }
232         }
233         if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX)
234             require_once("lib/InlineParser.php");
235         if ($headers = $this->extractHeaders($content, $dbi->_markup, 
236                                              $with_toclink, $levels, $basepage)) 
237         {
238             foreach ($headers as $h) {
239                 // proper heading indent
240                 $level = $h['level'];
241                 $indent = 3 - $level;
242                 $link = new WikiPageName($pagename,$page,$h['anchor']);
243                 $li = WikiLink($link,'known',$h['text']);
244                 if ($liststyle == 'dl')
245                     $list->pushContent(HTML::dt(HTML::raw(str_repeat($indentstr,$indent)),$li));
246                 else
247                     $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
248             }
249         }
250         if ($jshide) {
251             $list->setAttr('style','display:none;');
252             $html->pushContent(Javascript("
253 function toggletoc(a) {
254   toc=document.getElementById('toclist');
255   if (toc.style.display=='none') {
256     toc.style.display='block';
257     a.title='"._("Click to hide the TOC")."';
258   } else {
259     toc.style.display='none';
260     a.title='"._("Click to display")."';
261   }
262 }"));
263             $html->pushContent(HTML::h4(HTML::a(array('name'=>'TOC',
264                                                       'class'=>'wikiaction',
265                                                       'title'=>_("Click to display"),
266                                                       'onclick'=>"toggletoc(this)"),
267                                                 _("Table Of Contents"))));
268         } else {
269             if (!$noheader)
270                 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
271             else 
272                 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
273         }
274         $html->pushContent($list);
275         return $html;
276     }
277 };
278
279 // $Log: not supported by cvs2svn $
280 // Revision 1.25  2004/07/08 20:30:07  rurban
281 // plugin->run consistency: request as reference, added basepage.
282 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
283 //
284 // Revision 1.24  2004/06/28 13:27:03  rurban
285 // CreateToc disabled for old markup and Apache2 only
286 //
287 // Revision 1.23  2004/06/28 13:13:58  rurban
288 // CreateToc disabled for old markup
289 //
290 // Revision 1.22  2004/06/15 14:56:37  rurban
291 // more allow_call_time_pass_reference false fixes
292 //
293 // Revision 1.21  2004/06/13 09:45:23  rurban
294 // display bug workaround for MacIE browsers, jshide: 0
295 //
296 // Revision 1.20  2004/05/11 13:57:46  rurban
297 // enable TOC_FULL_SYNTAX per default
298 // don't <a name>$header</a> to disable css formatting for such anchors
299 //   => <a name></a>$header
300 //
301 // Revision 1.19  2004/05/08 16:59:27  rurban
302 // requires optional TOC_FULL_SYNTAX constnat to enable full link and
303 // wikiword syntax in headers.
304 //
305 // Revision 1.18  2004/04/29 21:55:15  rurban
306 // fixed TOC backlinks with USE_PATH_INFO false
307 //   with_toclink=1, sf.net bug #940682
308 //
309 // Revision 1.17  2004/04/26 19:43:03  rurban
310 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
311 //
312 // Revision 1.16  2004/04/26 14:46:14  rurban
313 // better comments
314 //
315 // Revision 1.14  2004/04/21 04:29:50  rurban
316 // write WikiURL consistently (not WikiUrl)
317 //
318 // Revision 1.12  2004/03/22 14:13:53  rurban
319 // fixed links to equal named headers
320 //
321 // Revision 1.11  2004/03/15 09:52:59  rurban
322 // jshide button: dynamic titles
323 //
324 // Revision 1.10  2004/03/14 20:30:21  rurban
325 // jshide button
326 //
327 // Revision 1.9  2004/03/09 19:24:20  rurban
328 // custom indentstr
329 // h2 toc header
330 //
331 // Revision 1.8  2004/03/09 19:05:12  rurban
332 // new liststyle arg. default: dl (no bullets)
333 //
334 // Revision 1.7  2004/03/09 11:51:54  rurban
335 // support jshide=1: DHTML button hide/unhide TOC
336 //
337 // Revision 1.6  2004/03/09 10:25:37  rurban
338 // slightly better formatted TOC indentation
339 //
340 // Revision 1.5  2004/03/09 08:57:10  rurban
341 // convert space to "_" instead of "x20." in anchors
342 // proper heading indent
343 // handle duplicate headers
344 // allow multiple headers like "!!!,!!" or "1,2"
345 //
346 // Revision 1.4  2004/03/02 18:21:29  rurban
347 // typo: ref=>href
348 //
349 // Revision 1.1  2004/03/01 18:10:28  rurban
350 // first version, without links, anchors and jscript folding
351 //
352 //
353
354 // For emacs users
355 // Local Variables:
356 // mode: php
357 // tab-width: 8
358 // c-basic-offset: 4
359 // c-hanging-comment-ender-p: nil
360 // indent-tabs-mode: nil
361 // End:
362 ?>