2 rcs_id('$Id: CreateToc.php,v 1.18 2004-04-29 21:55:15 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
27 * <?plugin CreateToc headers=!!!,!! with_toclink||=1
29 * @author: Reini Urban
32 class WikiPlugin_CreateToc
36 return _("CreateToc");
39 function getDescription() {
40 return _("Automatically link headers at the top");
43 function getVersion() {
44 return preg_replace("/[Revision: $]/", '',
45 "\$Revision: 1.18 $");
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>
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' => ' ',
61 function preg_quote ($heading) {
62 return str_replace(array("/",".","?","*"),
63 array('\/','\.','\?','\*'), $heading);
66 function searchHeader ($content, $start_index, $heading, $level) {
67 $count = substr_count($level,'!');
69 case 1: $h = "h4"; break;
70 case 2: $h = "h3"; break;
71 case 3: $h = "h2"; break;
73 $theading = TransformInline($heading);
74 $qheading = preg_quote($theading->asString());
76 for ($j=$start_index; $j < count($content); $j++) {
77 if (is_string($content[$j])) {
78 if (preg_match("/<$h>$qheading<\/$h>/",$content[$j])) {
81 } elseif (isa($content[$j],'cached_wikilink')) {
82 // shortcut for single wikiword headers
83 $content[$j] = $content[$j]->asString();
84 if ($content[$j] == $heading and
85 substr($content[$j-1],-4,4) == "<$h>" and
86 substr($content[$j+1],0,5) == "</$h>") {
87 return $j; // single wikiword
89 //DONE: To allow "!! WikiWord link"
90 // Split heading into WikiWords and check against
91 // joined content (after cached_plugininvocation).
92 // The first wikilink is the anchor then.
94 for ($k=max($j-1,$start_index); $k < count($content); $k++) {
95 $joined .= is_string($content[$k]) ? $content[$k]
96 : $content[$k]->asString();
98 if (preg_match("/<$h>$qheading<\/$h>/",$joined))
103 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
107 /** prevent from duplicate anchors,
108 * beautify spaces: " " => "_" and not "x20."
110 function _nextAnchor($s) {
111 static $anchors = array();
113 $s = str_replace(' ','_',$s);
116 while (!empty($anchors[$anchor])) {
117 $anchor = sprintf("%s_%d",$s,$i++);
119 $anchors[$anchor] = $i;
123 // Feature request: proper nesting; multiple levels (e.g. 1,3)
124 function extractHeaders (&$content, &$markup, $backlink=0, $levels=false, $basepage='') {
125 if (!$levels) $levels = array(1,2);
130 for ($i=0; $i<count($content); $i++) {
131 foreach ($levels as $level) {
132 if ($level < 1 or $level > 3) continue;
133 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',$content[$i],$match)) {
134 if (!strstr($content[$i],'#[')) {
135 $s = trim($match[2]);
136 $anchor = $this->_nextAnchor($s);
137 $manchor = MangleXmlIdentifier($anchor);
138 $headers[] = array('text' => $s, 'anchor' => $anchor, 'level' => $level);
139 // Change original wikitext, but that is useless art...
140 $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
141 // And now change the to be printed markup (XmlTree):
142 // Search <hn>$s</hn> line in markup
143 //Fixme: Find non-singleworded wikilink'ed headers also.
144 $j = $this->searchHeader($markup->_content, $j, $s, $match[1]);
145 if ($j and isset($markup->_content[$j])) {
146 $x = $markup->_content[$j];
147 if (is_string($markup->_content[$j])) {
148 $x = $markup->_content[$j];
149 $heading = preg_quote($s);
150 if ($x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
151 "\$1<a name=\"$manchor\">\$2</a>\$3",$x,1)) {
153 $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
154 $x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
155 "\$1<a href=\"$url\" name=\"$manchor\">\$2</a>\$3",
156 $markup->_content[$j],1);
158 $markup->_content[$j] = $x;
161 elseif (isa($markup->_content[$j],'cached_wikilink')) {
162 $label = isset($x->_label) ? $x->_label : false;
163 $markup->_content[$j] = HTML::a(array('name'=>$manchor),WikiLink($x->_page, 'auto', $label));
173 function run($dbi, $argstr, $request, $basepage) {
174 extract($this->getArgs($argstr, $request));
176 // Expand relative page names.
177 $page = new WikiPageName($pagename, $basepage);
178 $pagename = $page->name;
181 return $this->error(_("no page specified"));
183 $page = $dbi->getPage($pagename);
184 $current = $page->getCurrentRevision();
185 $content = $current->getContent();
186 $html = HTML::div(array('class' => 'toc','align' => $align));
187 if ($liststyle == 'dl')
188 $list = HTML::dl(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
189 elseif ($liststyle == 'ul')
190 $list = HTML::ul(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
191 elseif ($liststyle == 'ol')
192 $list = HTML::ol(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
193 if (!strstr($headers,",")) {
194 $headers = array($headers);
196 $headers = explode(",",$headers);
199 foreach ($headers as $h) {
200 //replace !!! with level 1, ...
201 if (strstr($h,"!")) {
202 $hcount = substr_count($h,'!');
203 $level = min(max(1, $hcount),3);
206 $level = min(max(1, (int) $h), 3);
210 require_once("lib/InlineParser.php");
211 if ($headers = $this->extractHeaders(&$content, &$dbi->_markup, $with_toclink, $levels, $basepage)) {
212 foreach ($headers as $h) {
213 // proper heading indent
214 $level = $h['level'];
215 $indent = 3 - $level;
216 $link = new WikiPageName($pagename,$page,$h['anchor']);
217 $li = WikiLink($link,'known',$h['text']);
218 if ($liststyle == 'dl')
219 $list->pushContent(HTML::dt(HTML::raw(str_repeat($indentstr,$indent)),$li));
221 $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
225 $list->setAttr('style','display:none;');
226 $html->pushContent(Javascript("
227 function toggletoc(a) {
228 toc=document.getElementById('toclist');
229 if (toc.style.display=='none') {
230 toc.style.display='block';
231 a.title='"._("Click to hide the TOC")."';
233 toc.style.display='none';
234 a.title='"._("Click to display")."';
237 $html->pushContent(HTML::h4(HTML::a(array('name'=>'TOC',
238 'class'=>'wikiaction',
239 'title'=>_("Click to display"),
240 'onclick'=>"toggletoc(this)"),
241 _("Table Of Contents"))));
244 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
246 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
248 $html->pushContent($list);
253 // $Log: not supported by cvs2svn $
254 // Revision 1.17 2004/04/26 19:43:03 rurban
255 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
257 // Revision 1.16 2004/04/26 14:46:14 rurban
260 // Revision 1.14 2004/04/21 04:29:50 rurban
261 // write WikiURL consistently (not WikiUrl)
263 // Revision 1.12 2004/03/22 14:13:53 rurban
264 // fixed links to equal named headers
266 // Revision 1.11 2004/03/15 09:52:59 rurban
267 // jshide button: dynamic titles
269 // Revision 1.10 2004/03/14 20:30:21 rurban
272 // Revision 1.9 2004/03/09 19:24:20 rurban
276 // Revision 1.8 2004/03/09 19:05:12 rurban
277 // new liststyle arg. default: dl (no bullets)
279 // Revision 1.7 2004/03/09 11:51:54 rurban
280 // support jshide=1: DHTML button hide/unhide TOC
282 // Revision 1.6 2004/03/09 10:25:37 rurban
283 // slightly better formatted TOC indentation
285 // Revision 1.5 2004/03/09 08:57:10 rurban
286 // convert space to "_" instead of "x20." in anchors
287 // proper heading indent
288 // handle duplicate headers
289 // allow multiple headers like "!!!,!!" or "1,2"
291 // Revision 1.4 2004/03/02 18:21:29 rurban
294 // Revision 1.1 2004/03/01 18:10:28 rurban
295 // first version, without links, anchors and jscript folding
304 // c-hanging-comment-ender-p: nil
305 // indent-tabs-mode: nil