]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateToc.php
jshide button
[SourceForge/phpwiki.git] / lib / plugin / CreateToc.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreateToc.php,v 1.10 2004-03-14 20:30:21 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.10 $");
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         for ($j=$start_index; $j<count($content); $j++) {
72             if (is_string($content[$j])) {
73                 $heading = preg_quote($heading);
74                 if (preg_match("/<$h>$heading<\/$h>/",$content[$j]))
75                     return $j;
76             }
77         }
78         trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
79         return 0;
80     }
81
82     /** prevent from duplicate anchors,
83      *  beautify spaces: " " => "_" and not "x20."
84      */
85     function _nextAnchor($s) {
86         static $anchors = array();
87
88         $s = str_replace(' ','_',$s);
89         $i = 1;
90         $anchor = MangleXmlIdentifier($s);
91         while (!empty($anchors[$anchor])) {
92             $anchor = MangleXmlIdentifier(sprintf("%s_%d",$s,$i++));
93         }
94         $anchors[$anchor] = $i;
95         return $anchor;
96     }
97     
98     // Feature request: proper nesting; multiple levels (e.g. 1,3)
99     function extractHeaders (&$content, &$markup, $backlink=0, $levels=false, $basepage='') {
100         if (!$levels) $levels = array(1,2);
101         reset($levels);
102         sort($levels);
103         $headers = array();
104         $j = 0;
105         for ($i=0; $i<count($content); $i++) {
106             foreach ($levels as $level) {
107                 if ($level < 1 or $level > 3) continue;
108                 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].+)$/',$content[$i],$match)) {
109                     if (!strstr($content[$i],'#[')) {
110                         $s = trim($match[2]);
111                         $anchor = $this->_nextAnchor($s);
112                         $headers[] = array('text' => $s, 'anchor' => $anchor, 'level' => $level);
113                         // change original wikitext, but that is useless art...
114                         $content[$i] = $match[1]." #[|$anchor][$s|#TOC]";
115                         // and now change the to be printed markup (XmlTree):
116                         // search <hn>$s</hn> line in markup
117                         $j = $this->searchHeader($markup->_content, $j, $s, $match[1]);
118                         if (  $j and isset($markup->_content[$j]) and 
119                               is_string($markup->_content[$j])  ) {
120                             $x = $markup->_content[$j];
121                             $heading = preg_quote($s);
122                             if ($x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
123                                                   "\$1<a name=\"$anchor\">\$2</a>\$3",$x)) {
124                                 if ($backlink)
125                                     $x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
126                                                       "\$1<a href=\"$basepage#TOC\" name=\"$anchor\">\$2</a>\$3",
127                                                       $markup->_content[$j]);
128                                 $markup->_content[$j] = $x;
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135         return $headers;
136     }
137                 
138     function run($dbi, $argstr, $request, $basepage) {
139         extract($this->getArgs($argstr, $request));
140         if ($pagename) {
141             // Expand relative page names.
142             $page = new WikiPageName($pagename, $basepage);
143             $pagename = $page->name;
144         }
145         if (!$pagename) {
146             return $this->error(_("no page specified"));
147         }
148         $page = $dbi->getPage($pagename);
149         $current = $page->getCurrentRevision();
150         $content = $current->getContent();
151         $html = HTML::div(array('class' => 'toc','align' => $align));
152         if ($liststyle == 'dl')
153             $list = HTML::dl(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
154         elseif ($liststyle == 'ul')
155             $list = HTML::ul(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
156         elseif ($liststyle == 'ol')
157             $list = HTML::ol(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
158         if (!strstr($headers,",")) {
159             $headers = array($headers); 
160         } else {
161             $headers = explode(",",$headers);
162         }
163         $levels = array();
164         foreach ($headers as $h) {
165             //replace !!! with level 1, ...
166             if (strstr($h,"!")) {
167                 $hcount = substr_count($h,'!');
168                 $level = min(max(1, $hcount),3);
169                 $levels[] = $level;
170             } else {
171                 $level = min(max(1, (int) $h), 3);
172                 $levels[] = $level;
173             }
174         }
175         if ($headers = $this->extractHeaders(&$content, &$dbi->_markup, $with_toclink, $levels, $basepage)) {
176             foreach ($headers as $h) {
177                 // proper heading indent
178                 $level = $h['level'];
179                 $indent = 3 - $level;
180                 $link = new WikiPageName($pagename,$page,$h['anchor']);
181                 $li = WikiLink($link,'known',$h['text']);
182                 if ($liststyle == 'dl')
183                     $list->pushContent(HTML::dt(HTML::raw(str_repeat($indentstr,$indent)),$li));
184                 else
185                     $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
186             }
187         }
188         if ($jshide) {
189             $list->setAttr('style','display:none;');
190             $html->pushContent(Javascript("
191 function toggletoc() {
192   toc=document.getElementById('toclist');
193   if (toc.style.display=='none') {
194     toc.style.display='block';
195   } else {
196     toc.style.display='none';
197   }
198 }"));
199             $html->pushContent(HTML::h4(HTML::a(array('name'=>'TOC',
200                                                       'class'=>'wikiaction',
201                                                       'title'=>_("Click to display"),
202                                                       'onclick'=>"toggletoc()"),
203                                                 _("Table Of Contents"))));
204         } else {
205             if (!$noheader)
206                 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
207             else 
208                 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
209         }
210         $html->pushContent($list);
211         return $html;
212     }
213 };
214
215 // $Log: not supported by cvs2svn $
216 // Revision 1.9  2004/03/09 19:24:20  rurban
217 // custom indentstr
218 // h2 toc header
219 //
220 // Revision 1.8  2004/03/09 19:05:12  rurban
221 // new liststyle arg. default: dl (no bullets)
222 //
223 // Revision 1.7  2004/03/09 11:51:54  rurban
224 // support jshide=1: DHTML button hide/unhide TOC
225 //
226 // Revision 1.6  2004/03/09 10:25:37  rurban
227 // slightly better formatted TOC indentation
228 //
229 // Revision 1.5  2004/03/09 08:57:10  rurban
230 // convert space to "_" instead of "x20." in anchors
231 // proper heading indent
232 // handle duplicate headers
233 // allow multiple headers like "!!!,!!" or "1,2"
234 //
235 // Revision 1.4  2004/03/02 18:21:29  rurban
236 // typo: ref=>href
237 //
238 // Revision 1.1  2004/03/01 18:10:28  rurban
239 // first version, without links, anchors and jscript folding
240 //
241 //
242
243 // For emacs users
244 // Local Variables:
245 // mode: php
246 // tab-width: 8
247 // c-basic-offset: 4
248 // c-hanging-comment-ender-p: nil
249 // indent-tabs-mode: nil
250 // End:
251 ?>