2 rcs_id('$Id: CreateToc.php,v 1.19 2004-05-08 16:59:27 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.19 $");
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 if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX) {
74 $theading = TransformInline($heading);
75 $qheading = preg_quote($theading->asString());
77 $qheading = preg_quote($heading);
79 for ($j=$start_index; $j < count($content); $j++) {
80 if (is_string($content[$j])) {
81 if (preg_match("/<$h>$qheading<\/$h>/",$content[$j])) {
84 } elseif (isa($content[$j],'cached_wikilink')) {
85 // shortcut for single wikiword headers
86 $content[$j] = $content[$j]->asString();
87 if ($content[$j] == $heading and
88 substr($content[$j-1],-4,4) == "<$h>" and
89 substr($content[$j+1],0,5) == "</$h>") {
90 return $j; // single wikiword
92 //DONE: To allow "!! WikiWord link"
93 // Split heading into WikiWords and check against
94 // joined content (after cached_plugininvocation).
95 // The first wikilink is the anchor then.
97 for ($k=max($j-1,$start_index); $k < count($content); $k++) {
98 $joined .= is_string($content[$k]) ? $content[$k]
99 : $content[$k]->asString();
101 if (preg_match("/<$h>$qheading<\/$h>/",$joined))
106 trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
110 /** prevent from duplicate anchors,
111 * beautify spaces: " " => "_" and not "x20."
113 function _nextAnchor($s) {
114 static $anchors = array();
116 $s = str_replace(' ','_',$s);
119 while (!empty($anchors[$anchor])) {
120 $anchor = sprintf("%s_%d",$s,$i++);
122 $anchors[$anchor] = $i;
126 // Feature request: proper nesting; multiple levels (e.g. 1,3)
127 function extractHeaders (&$content, &$markup, $backlink=0, $levels=false, $basepage='') {
128 if (!$levels) $levels = array(1,2);
133 for ($i=0; $i<count($content); $i++) {
134 foreach ($levels as $level) {
135 if ($level < 1 or $level > 3) continue;
136 if (preg_match('/^\s*(!{'.$level.','.$level.'})([^!].*)$/',$content[$i],$match)) {
137 if (!strstr($content[$i],'#[')) {
138 $s = trim($match[2]);
139 $anchor = $this->_nextAnchor($s);
140 $manchor = MangleXmlIdentifier($anchor);
141 $headers[] = array('text' => $s, 'anchor' => $anchor, 'level' => $level);
142 // Change original wikitext, but that is useless art...
143 $content[$i] = $match[1]." #[|$manchor][$s|#TOC]";
144 // And now change the to be printed markup (XmlTree):
145 // Search <hn>$s</hn> line in markup
146 //Fixme: Find non-singleworded wikilink'ed headers also.
147 $j = $this->searchHeader($markup->_content, $j, $s, $match[1]);
148 if ($j and isset($markup->_content[$j])) {
149 $x = $markup->_content[$j];
150 if (is_string($markup->_content[$j])) {
151 $x = $markup->_content[$j];
152 $heading = preg_quote($s);
153 if ($x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
154 "\$1<a name=\"$manchor\">\$2</a>\$3",$x,1)) {
156 $url = WikiURL(new WikiPageName($basepage,false,"TOC"));
157 $x = preg_replace('/(<h\d>)('.$heading.')(<\/h\d>)/',
158 "\$1<a href=\"$url\" name=\"$manchor\">\$2</a>\$3",
159 $markup->_content[$j],1);
161 $markup->_content[$j] = $x;
164 elseif (isa($markup->_content[$j],'cached_wikilink')) {
165 $label = isset($x->_label) ? $x->_label : false;
166 $markup->_content[$j] = HTML::a(array('name'=>$manchor),WikiLink($x->_page, 'auto', $label));
176 function run($dbi, $argstr, $request, $basepage) {
177 extract($this->getArgs($argstr, $request));
179 // Expand relative page names.
180 $page = new WikiPageName($pagename, $basepage);
181 $pagename = $page->name;
184 return $this->error(_("no page specified"));
186 $page = $dbi->getPage($pagename);
187 $current = $page->getCurrentRevision();
188 $content = $current->getContent();
189 $html = HTML::div(array('class' => 'toc','align' => $align));
190 if ($liststyle == 'dl')
191 $list = HTML::dl(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
192 elseif ($liststyle == 'ul')
193 $list = HTML::ul(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
194 elseif ($liststyle == 'ol')
195 $list = HTML::ol(array('name'=>'toclist','id'=>'toclist','class' => 'toc'));
196 if (!strstr($headers,",")) {
197 $headers = array($headers);
199 $headers = explode(",",$headers);
202 foreach ($headers as $h) {
203 //replace !!! with level 1, ...
204 if (strstr($h,"!")) {
205 $hcount = substr_count($h,'!');
206 $level = min(max(1, $hcount),3);
209 $level = min(max(1, (int) $h), 3);
213 if (defined('TOC_FULL_SYNTAX') and TOC_FULL_SYNTAX)
214 require_once("lib/InlineParser.php");
215 if ($headers = $this->extractHeaders(&$content, &$dbi->_markup, $with_toclink, $levels, $basepage)) {
216 foreach ($headers as $h) {
217 // proper heading indent
218 $level = $h['level'];
219 $indent = 3 - $level;
220 $link = new WikiPageName($pagename,$page,$h['anchor']);
221 $li = WikiLink($link,'known',$h['text']);
222 if ($liststyle == 'dl')
223 $list->pushContent(HTML::dt(HTML::raw(str_repeat($indentstr,$indent)),$li));
225 $list->pushContent(HTML::li(HTML::raw(str_repeat($indentstr,$indent)),$li));
229 $list->setAttr('style','display:none;');
230 $html->pushContent(Javascript("
231 function toggletoc(a) {
232 toc=document.getElementById('toclist');
233 if (toc.style.display=='none') {
234 toc.style.display='block';
235 a.title='"._("Click to hide the TOC")."';
237 toc.style.display='none';
238 a.title='"._("Click to display")."';
241 $html->pushContent(HTML::h4(HTML::a(array('name'=>'TOC',
242 'class'=>'wikiaction',
243 'title'=>_("Click to display"),
244 'onclick'=>"toggletoc(this)"),
245 _("Table Of Contents"))));
248 $html->pushContent(HTML::h2(HTML::a(array('name'=>'TOC'),_("Table Of Contents"))));
250 $html->pushContent(HTML::a(array('name'=>'TOC'),""));
252 $html->pushContent($list);
257 // $Log: not supported by cvs2svn $
258 // Revision 1.18 2004/04/29 21:55:15 rurban
259 // fixed TOC backlinks with USE_PATH_INFO false
260 // with_toclink=1, sf.net bug #940682
262 // Revision 1.17 2004/04/26 19:43:03 rurban
263 // support most cases of header markup. fixed duplicate MangleXmlIdentifier name
265 // Revision 1.16 2004/04/26 14:46:14 rurban
268 // Revision 1.14 2004/04/21 04:29:50 rurban
269 // write WikiURL consistently (not WikiUrl)
271 // Revision 1.12 2004/03/22 14:13:53 rurban
272 // fixed links to equal named headers
274 // Revision 1.11 2004/03/15 09:52:59 rurban
275 // jshide button: dynamic titles
277 // Revision 1.10 2004/03/14 20:30:21 rurban
280 // Revision 1.9 2004/03/09 19:24:20 rurban
284 // Revision 1.8 2004/03/09 19:05:12 rurban
285 // new liststyle arg. default: dl (no bullets)
287 // Revision 1.7 2004/03/09 11:51:54 rurban
288 // support jshide=1: DHTML button hide/unhide TOC
290 // Revision 1.6 2004/03/09 10:25:37 rurban
291 // slightly better formatted TOC indentation
293 // Revision 1.5 2004/03/09 08:57:10 rurban
294 // convert space to "_" instead of "x20." in anchors
295 // proper heading indent
296 // handle duplicate headers
297 // allow multiple headers like "!!!,!!" or "1,2"
299 // Revision 1.4 2004/03/02 18:21:29 rurban
302 // Revision 1.1 2004/03/01 18:10:28 rurban
303 // first version, without links, anchors and jscript folding
312 // c-hanging-comment-ender-p: nil
313 // indent-tabs-mode: nil