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