2 rcs_id('$Id: CreateToc.php,v 1.10 2004-03-14 20:30:21 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.10 $");
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 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]))
78 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
82 /** prevent from duplicate anchors,
83 * beautify spaces: " " => "_" and not "x20."
85 function _nextAnchor($s) {
86 static $anchors = array();
88 $s = str_replace(' ','_',$s);
90 $anchor = MangleXmlIdentifier($s);
91 while (!empty($anchors[$anchor])) {
92 $anchor = MangleXmlIdentifier(sprintf("%s_%d",$s,$i++));
94 $anchors[$anchor] = $i;
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);
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)) {
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;
138 function run($dbi, $argstr, $request, $basepage) {
139 extract($this->getArgs($argstr, $request));
141 // Expand relative page names.
142 $page = new WikiPageName($pagename, $basepage);
143 $pagename = $page->name;
146 return $this->error(_("no page specified"));
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);
161 $headers = explode(",",$headers);
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);
171 $level = min(max(1, (int) $h), 3);
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));
185 $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
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';
196 toc.style.display='none';
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"))));
206 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
208 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
210 $html->pushContent($list);
215 // $Log: not supported by cvs2svn $
216 // Revision 1.9 2004/03/09 19:24:20 rurban
220 // Revision 1.8 2004/03/09 19:05:12 rurban
221 // new liststyle arg. default: dl (no bullets)
223 // Revision 1.7 2004/03/09 11:51:54 rurban
224 // support jshide=1: DHTML button hide/unhide TOC
226 // Revision 1.6 2004/03/09 10:25:37 rurban
227 // slightly better formatted TOC indentation
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"
235 // Revision 1.4 2004/03/02 18:21:29 rurban
238 // Revision 1.1 2004/03/01 18:10:28 rurban
239 // first version, without links, anchors and jscript folding
248 // c-hanging-comment-ender-p: nil
249 // indent-tabs-mode: nil