2 rcs_id('$Id: CreateToc.php,v 1.15 2004-04-26 14:09:05 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
26 * Usage: <?plugin CreateToc headers=!!!,!! with_toclink||=1 jshide||=1 align=left noheaders=0 ?>
27 * @author: Reini Urban
30 class WikiPlugin_CreateToc
34 return _("CreateToc");
37 function getDescription() {
38 return _("Automatically link headers at the top");
41 function getVersion() {
42 return preg_replace("/[Revision: $]/", '',
43 "\$Revision: 1.15 $");
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>
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' => ' ',
59 function preg_quote ($heading) {
60 return str_replace(array("/",".","?","*"),
61 array('\/','\.','\?','\*'), $heading);
64 function searchHeader ($content, $start_index, $heading, $level) {
65 $count = substr_count($level,'!');
67 case 1: $h = "h4"; break;
68 case 2: $h = "h3"; break;
69 case 3: $h = "h2"; break;
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])) {
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>")
87 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
91 /** prevent from duplicate anchors,
92 * beautify spaces: " " => "_" and not "x20."
94 function _nextAnchor($s) {
95 static $anchors = array();
97 $s = str_replace(' ','_',$s);
99 $anchor = MangleXmlIdentifier($s);
100 while (!empty($anchors[$anchor])) {
101 $anchor = MangleXmlIdentifier(sprintf("%s_%d",$s,$i++));
103 $anchors[$anchor] = $i;
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);
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]) )
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)) {
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;
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));
154 function run($dbi, $argstr, $request, $basepage) {
155 extract($this->getArgs($argstr, $request));
157 // Expand relative page names.
158 $page = new WikiPageName($pagename, $basepage);
159 $pagename = $page->name;
162 return $this->error(_("no page specified"));
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);
177 $headers = explode(",",$headers);
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);
187 $level = min(max(1, (int) $h), 3);
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));
201 $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
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")."';
213 toc.style.display='none';
214 a.title='"._("Click to display")."';
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"))));
224 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
226 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
228 $html->pushContent($list);
233 // $Log: not supported by cvs2svn $
234 // Revision 1.14 2004/04/21 04:29:50 rurban
235 // write WikiURL consistently (not WikiUrl)
237 // Revision 1.12 2004/03/22 14:13:53 rurban
238 // fixed links to equal named headers
240 // Revision 1.11 2004/03/15 09:52:59 rurban
241 // jshide button: dynamic titles
243 // Revision 1.10 2004/03/14 20:30:21 rurban
246 // Revision 1.9 2004/03/09 19:24:20 rurban
250 // Revision 1.8 2004/03/09 19:05:12 rurban
251 // new liststyle arg. default: dl (no bullets)
253 // Revision 1.7 2004/03/09 11:51:54 rurban
254 // support jshide=1: DHTML button hide/unhide TOC
256 // Revision 1.6 2004/03/09 10:25:37 rurban
257 // slightly better formatted TOC indentation
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"
265 // Revision 1.4 2004/03/02 18:21:29 rurban
268 // Revision 1.1 2004/03/01 18:10:28 rurban
269 // first version, without links, anchors and jscript folding
278 // c-hanging-comment-ender-p: nil
279 // indent-tabs-mode: nil