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