xref: /plugin/siteexport/syntax/toc.php (revision d04831ea6fcb33d431ef89830771757b7a256c7f)
1<?php
2/**
3 * Search with Scopes
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15
16class syntax_plugin_siteexport_toc extends DokuWiki_Syntax_Plugin {
17
18	var $insideToc = false;
19	var $savedToc = array();
20	var $options = array();
21
22	var $mergedPages = array();
23	var $includedPages = array();
24
25	function getType() { return 'protected'; }
26	function getPType() { return 'block'; }
27	function getAllowedTypes() { return array('container'); }
28	function getSort() { return 100; }
29
30	/**
31	 * Connect pattern to lexer
32	 */
33	function connectTo($mode) {
34		$this->Lexer->addEntryPattern('<toc>(?=.*?</toc>)',$mode,'plugin_siteexport_toc');
35		$this->Lexer->addEntryPattern('<toc .+?>(?=.*?</toc>)',$mode,'plugin_siteexport_toc');
36		$this->Lexer->addSpecialPattern("\[\[.+?\]\]",$mode,'plugin_siteexport_toc');
37	}
38
39	function postConnect() {
40		$this->Lexer->addExitPattern('</toc.*?>', 'plugin_siteexport_toc');
41	}
42
43	function handle($match, $state, $pos, Doku_Handler $handler){
44		global $ID, $INFO;
45
46		switch ($state) {
47			case DOKU_LEXER_ENTER:
48
49				$this->insideToc = true;
50
51				$this->options = explode(' ', substr($match, 5, -1));
52
53				return array('start' => true, 'pos' => $pos, 'options' => $this->options);
54				break;
55
56			case DOKU_LEXER_SPECIAL:
57
58				if ( $this->insideToc ) {
59
60					$link = preg_replace(array('/^\[\[/','/\]\]$/u'),'',$match);
61					// Split title from URL
62					$link = explode('|',$link,2);
63					if ( !isset($link[1]) ) {
64						$link[1] = NULL;
65					} else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) {
66						// If the title is an image, convert it to an array containing the image details
67						$link[1] = Doku_Handler_Parse_Media($link[1]);
68					}
69					$link[0] = trim($link[0]);
70
71					if ( ! (preg_match('/^[a-zA-Z0-9\.]+>{1}.*$/u',$link[0]) ||
72					preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$link[0]) ||
73					preg_match('#^([a-z0-9\-\.+]+?)://#i',$link[0]) ||
74					preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>',$link[0]) ||
75					preg_match('!^#.+!',$link[0]) )
76					) {
77
78						// Get current depth from call stack
79						$depth = 1;
80						if ( $handler->CallWriter instanceof Doku_Handler_List ) {
81
82							$calls = array_reverse($handler->CallWriter->calls);
83							$call = $calls[0];
84							foreach ( $calls as $item ) {
85								if ( in_array( $item[0], array( 'list_item', 'list_open') ) ) { $call = $item; break;}
86							}
87
88							$depth = $handler->CallWriter->interpretSyntax($call[1][0], $listType);
89
90						}
91
92						if ( empty( $link[0] ) ) { break; } // No empty elements. This would lead to problems
93						return array($link[0], $link[1], $depth);
94						break;
95					} else {
96						// use parser! - but with another p
97						$handler->internallink($match, $state, $pos);
98					}
99				} else {
100					// use parser!
101					$handler->internallink($match, $state, $pos);
102				}
103
104				return false;
105			case DOKU_LEXER_UNMATCHED:
106
107    			$handler->_addCall('cdata',array($match), $pos);
108
109				return false;
110				break;
111			case DOKU_LEXER_EXIT:
112
113				$this->insideToc = false;
114				return 'save__meta';
115				break;
116		}
117		return false;
118	}
119
120	function render($mode, Doku_Renderer $renderer, $data) {
121		global $ID, $lang, $INFO;
122
123		list( $SID, $NAME, $DEPTH ) = $data;
124
125		resolve_pageid(getNS($ID),$SID,$exists);
126//		$SID = cleanID($SID); // hier kein cleanID, da sonst m�glicherweise der anker verloren geht
127
128        //    Render XHTML and ODT
129		if ($mode == 'xhtml' || $mode == 'odt') {
130
131		    // TOC Title
132			if ( is_array($data) && $data['start'] == true ) {
133
134			    if ( is_Array($data['options']) ) {
135                    foreach( $data['options'] as $opt ) {
136    					switch( $opt ) {
137    						case 'description' : $renderer->meta['sitetoc']['showDescription'] = true; break;
138    						case 'notoc' : $renderer->meta['sitetoc']['noTOC'] = true; break;
139    						case 'merge' : $renderer->meta['sitetoc']['mergeDoc'] = true; break;
140    						case 'nohead' : $renderer->meta['sitetoc']['noTocHeader'] = true; break;
141    						case 'mergeheader' : $renderer->meta['sitetoc']['mergeHeader'] = true; break;
142    					}
143    				}
144			    }
145
146				$renderer->section_open("1 sitetoc");
147				if ( $renderer->meta['sitetoc']['noTocHeader'] === false ) {
148					$renderer->header($lang['toc'], 1, $data['pos']);
149				}
150
151				return true;
152			} else
153
154			// All Output has been done
155			if ( !is_array($data) && $data == 'save__meta' ) {
156
157				// Close TOC
158				$renderer->section_close();
159
160				if ( $renderer->meta['sitetoc']['noTOC'] === true ) {
161					$renderer->doc = preg_replace("/<div.*?sitetoc.*?$/si", "", $renderer->doc);
162				}
163
164				// If this is not set, we may have it as Metadata
165				if ( !$this->mergedPages && $renderer->meta['sitetoc']['mergeDoc'] ) {
166					$toc = $renderer->meta['sitetoc']['siteexportTOC'];
167					if ( is_array($toc)) {
168						foreach ($toc as $tocItem ) {
169							$this->mergedPages[] = $tocItem['id'];
170						}
171					}
172				}
173
174				// If there is some data to be merged
175				if ( count($this->mergedPages) > 0) {
176
177					$renderer->doc = ''; // Start fresh!
178
179					$renderer->section_open("1 mergedsite");
180
181					// Prepare lookup Array
182					foreach ( $this->mergedPages as $tocItem ) {
183						$this->includedPages[] = array_shift(explode('#', $tocItem));
184					}
185
186					// Load the instructions
187					$instr = array();
188					foreach ( $this->mergedPages as $tocItem ) {
189						$file    = wikiFN($tocItem);
190
191						if(@file_exists($file)) {
192							$instructions = p_cached_instructions($file, false, $tocItem);
193						} else {
194							$instructions = p_get_instructions(io_readWikiPage($file,$tocItem));
195						}
196
197						// Convert Link instructions
198						$instructions = $this->_convertInstructions($instructions, $addID, $renderer);
199
200						if ( $renderer->meta['sitetoc']['mergeHeader'] && !empty($instr) ) {
201							// Merge
202							$instr = $this->_mergeWithHeaders($instr, $instructions, 1);
203
204						} else {
205							// Concat
206							$instr = array_merge($instr, $instructions);
207						}
208					}
209
210					if (!empty($instr)) {
211    					$this->_cleanInstructions($instr, '/section_(close|open)/');
212    					$this->_cleanInstructions($instr, '/listu_(close|open)/');
213    					$this->_cleanInstructions($instr, '/listo_(close|open)/');
214
215    					$this->_render_output($renderer, $mode, $instr);
216					}
217
218					$renderer->section_close();
219				}
220				return true;
221			}
222
223			// Save the current ID
224			$LNID = $SID;
225
226			// Add ID to flags['mergeDoc']
227			if ( $renderer->meta['sitetoc']['mergeDoc'] === true ) { // || (count($renderer->meta['sitetoc']['siteexportTOC']) > 0 && $renderer->meta['sitetoc']['siteexportMergeDoc'] === true) ) {
228				$this->mergedPages[] = $SID;
229				$default = $renderer->_simpleTitle($SID); $isImage = false;
230				resolve_pageid(getNS($ID),$SID,$exists);
231
232				$NAME = empty($NAME) ? p_get_first_heading($SID,true) : $NAME;
233				$LNID = "$ID#" . sectionID($SID, $check);
234			} else {
235				// // print normal internal link (XHTML odt)
236				$renderer->internallink($LNID, $NAME, null);
237
238				// Display Description underneath
239				if ( $renderer->meta['sitetoc']['showDescription'] === true ) {
240					// $renderer->p_open();
241					$renderer->cdata(p_get_metadata($SID, 'description abstract', true));
242					// $renderer->p_close();
243				}
244			}
245
246			// Render Metadata
247		} else if ($mode == 'metadata') {
248			if ( !is_array($data) && $data == 'save__meta' ) {
249				$renderer->meta['sitetoc']['siteexportTOC'] = $this->savedToc;
250
251                foreach ($this->savedToc as $page) {
252                    $renderer->meta['relation']['references'][$page['id']] = $page['exists'];
253                }
254
255				$this->savedToc = array();
256			} else if ( !isset($data['start']) && !isset($data['pos']) ) {
257				$this->savedToc[] = $this->__addTocItem($SID, $NAME, $DEPTH, $renderer);
258			}
259		} else {
260			return false;
261		}
262
263		return true;
264	}
265
266	/*
267	 * pull apart the ID and create an Entry for the TOC
268	 */
269	function __addTocItem($id, $name, $depth, $renderer) {
270		global $conf;
271		global $ID;
272
273		// Render Title
274		$default = $renderer->_simpleTitle($id);
275		$exists = false; $isImage = false; $linktype = null;
276		resolve_pageid(getNS($ID),$id,$exists);
277		$name = $renderer->_getLinkTitle($name, $default, $isImage, $id, $linktype);
278
279		//keep hash anchor
280		list($id,$hash) = explode('#',$id,2);
281		if(!empty($hash)) $hash = $renderer->_headerToLink($hash);
282
283		// Build Sitetoc Item
284		$item = array();
285		$item['id'] = $id;
286		$item['name'] = $name;
287		$item['anchor'] = $hash;
288		$item['depth'] = $depth;
289		$item['exists'] = $exists;
290		if(!$conf['skipacl'] && auth_quickaclcheck($item['id']) < AUTH_READ){
291			return false;
292		}
293
294		return $item;
295	}
296
297	/*
298	 * Render the output of one page
299	 */
300	function _render_output($renderer, $mode, $instr) {
301		global $ID;
302
303		// Section IDs
304		// $addID = sectionID($addID, $check);	//not possible to use a:b:c for id
305
306		if ( $mode == 'xhtml' ) {
307
308			//--------RENDER
309			//renderer information(TOC build / Cache used)
310			$info = array();
311			$content = p_render($mode, $instr, $info);
312
313			//Remove TOC`s, section edit buttons and tags
314			$content = $this->_cleanXHTML($content);
315
316			// embed the included page
317			// $renderer->doc .= '<div class="include">';
318			//add an anchor to find start of a inserted page
319			// $renderer->doc .= "<a name='$addID' id='$addID'>";
320			$renderer->doc .= $content;
321			// $renderer->doc .= '</div>';
322		} else if ( $mode == 'odt') {
323
324			// Loop through the instructions
325			foreach ( $instr as $instruction ) {
326				// Execute the callback against the Renderer
327				call_user_func_array(array($renderer, $instruction[0]),$instruction[1]);
328			}
329		}
330	}
331
332	/*
333	 * Corrects relative internal links and media and
334	 * converts headers of included pages to subheaders of the current page
335	 */
336	function _convertInstructions($instr, $id, &$renderer) {
337		global $ID;
338		global $conf;
339
340		$n = count($instr);
341
342		for ($i = 0; $i < $n; $i++){
343			//internal links(links inside this wiki) an relative links
344			if((substr($instr[$i][0], 0, 12) == 'internallink')){
345				$this->_convert_link($renderer,$instr[$i],$id);
346			}
347			else if((substr($instr[$i][0], 0, 13) == 'internalmedia')){
348				$this->_convert_media($renderer,$instr[$i],$id);
349			}
350		}
351
352		//if its the document start, cut off the first element(document information)
353		if ($instr[0][0] == 'document_start')
354		return array_slice($instr, 1, -1);
355		else
356		return $instr;
357	}
358
359	/*
360	 * Convert link of given instruction
361	 */
362	function _convert_link(&$renderer,&$instr,$id) {
363		global $ID;
364
365		$exists = false;
366
367		resolve_pageid(getNS($id),$instr[1][0],$exists);
368		list( $pageID, $pageReference ) = explode("#", $instr[1][0], 2);
369
370		if ( in_array($pageID, $this->includedPages) ) {
371			// Crate new internal Links
372			$check = null;
373
374			// Either get existing reference or create from first heading. If still not there take the alternate ID
375			$pageNameLink = empty( $pageReference ) ? sectionID($pageID,$check) : $pageReference;
376
377			$instr[1][0] = $ID . "#" . $pageNameLink;
378
379		} else {
380			// Convert external Links to plain Text
381
382			$instr = array(
383						"cdata",
384			array($instr[1][1]),
385			$instr[2]
386			);
387		}
388	}
389
390	/*
391	 * Convert internalmedia of given instruction
392	 */
393	function _convert_media(&$renderer,&$instr,$id) {
394		global $ID;
395
396		// Resolvemedia returns the absolute path to media by reference
397		$exists = false;
398		resolve_mediaid(getNS($id),$instr[1][0],$exists);
399	}
400
401	function _mergeWithHeaders($existing, $newInstructions, $level = 1) {
402
403		$returnInstructions = array();
404		$preparedInstructions = array();
405		$existingStart = $existingEnd = 0;
406		$firstRun = true;
407
408		while ( $this->_findNextHeaderSection($existing, $level, $existingStart, $existingEnd) ) {
409
410			if ( $firstRun ) {
411				$returnInstructions = array_merge($returnInstructions, array_slice($existing, 0, $existingStart));
412				$firstRun = false;
413			}
414
415			$currentSlice = array_slice($existing, $existingStart, $existingEnd - $existingStart);
416
417			// Find matching part with headername
418			$newStart = $newEnd = 0;
419			if ( $this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd, $currentSlice[0][1][0]) ) {
420
421				$newSlice = array_slice($newInstructions, $newStart, $newEnd - $newStart);
422				if ( $newSlice[0][0] == 'header' )
423					array_shift($newSlice); // Remove Heading
424
425				// merge found parts on next level.
426				$returnedInstructions = $this->_mergeWithHeaders($currentSlice, $newSlice, $level+1);
427
428				// Put them at the end!
429				$preparedInstructions = array_merge($preparedInstructions, $returnedInstructions);
430
431				// Remove from input
432				array_splice($newInstructions, $newStart, $newEnd - $newStart);
433			} else {
434				$preparedInstructions = array_merge($preparedInstructions, $currentSlice);
435			}
436
437			$existingStart = $existingEnd;
438		}
439
440		// Append the rest
441		$returnInstructions = array_merge($returnInstructions, array_slice($existing, $existingStart));
442
443		// Check for section close inconsistencies and put one at the very end ...
444		$section_postpend = array();
445		if (
446		    (
447			($tmp = array_slice($newInstructions, -1))
448			&& ($tmp[0][0] == 'section_close')
449		    )
450		    &&
451		    (
452			($tmp = array_slice($newInstructions, -2))
453			&& ($tmp[0][0] == 'section_close' )
454		    )
455		) {
456			$section_postpend = array_splice($newInstructions, -1);
457		}
458		if (
459		    (
460			($tmp = array_slice($returnInstructions, -1))
461			&& ($tmp[0][0] == 'section_close')
462		    )
463		    &&
464		    (
465			($tmp = array_slice($returnInstructions, -2))
466			&& ($tmp[0][0] == 'section_close' )
467		    )
468		) {
469			$section_postpend = array_merge($section_postpend, array_splice($returnInstructions, -1));
470		}
471
472		// What if there are headings left inside the $newInstructions?????
473		// Find matching part with headername
474		$newStart = $newEnd = 0;
475		$section_prepend = array();
476		if ( $this->_findNextHeaderSection($newInstructions, $level, $newStart, $newEnd) ) {
477			// If there are header in here, build a prepend and have the rest at the end
478			$section_prepend = array_splice($newInstructions, 0, $newStart);
479		} else {
480			// If not, prepend all of it.
481			$section_prepend = $newInstructions;
482			$newInstructions = array();
483		}
484
485		$returnInstructions = array_merge($returnInstructions, $section_prepend, $preparedInstructions, $newInstructions, $section_postpend);
486
487		return $returnInstructions;
488	}
489
490	function _findNextHeaderSection($section, $level, &$start, &$end, $headerName = null) {
491
492		$inCount = count($section);
493		$currentSlice = -1;
494
495		// Find Level 1 Header that matches.
496		for( $i=$start ; $i < $inCount ; $i++ ) {
497
498			$instruction = $section[$i];
499			$end = $i; // Or it will be lost and a section close will be missing.
500
501			// First Level Header
502			if ( $instruction[0] == 'header' && $instruction[1][1] == $level ) {
503
504				if ( $currentSlice > 0 ) {
505					return true;
506				}
507
508				if ( $headerName == null || ( $headerName == $instruction[1][0] ) ) {
509					// Begin of new slice ...
510					$start = $currentSlice = $i;
511				}
512			}
513		}
514
515		// Nothing found
516		$end = $i; // Or it will be lost and a section close will be missing.
517		return $currentSlice > 0;
518	}
519
520	function _cleanInstructions(&$instructions, $tag) {
521
522		$inCount = count($instructions);
523		for( $i=0 ; $i < $inCount ; $i++ ) {
524
525			// Last instruction
526			if ( $i == $inCount-1 ) {
527				break;
528			}
529
530			if ( preg_match($tag, $instructions[$i][0]) && preg_match($tag, $instructions[$i+1][0]) && $instructions[$i][0] != $instructions[$i+1][0] ) {
531
532				// found different tags, but both match the expression and follow each other - so they can be elliminated
533				array_splice($instructions, $i, 2);
534				$inCount -= 2;
535				$i--;
536			}
537
538		}
539	}
540
541	/**
542	 * Remove TOC, section edit buttons and tags
543	 */
544	function _cleanXHTML($xhtml){
545		$replace  = array(
546			'!<div class="toc">.*?(</div>\n</div>)!s' => '', // remove TOCs
547			'#<!-- SECTION \[(\d*-\d*)\] -->#e'       => '', // remove section edit buttons
548			'!<div id="tags">.*?(</div>)!s'           => ''  // remove category tags
549		);
550		$xhtml  = preg_replace(array_keys($replace), array_values($replace), $xhtml);
551		return $xhtml;
552	}
553
554
555	/**
556	 * Allow the plugin to prevent DokuWiki creating a second instance of itself
557	 *
558	 * @return bool   true if the plugin can not be instantiated more than once
559	 */
560	function isSingleton() {
561		return true;
562	}
563}
564// vim:ts=4:sw=4:et:enc=utf-8:
565