1<?php 2/** 3 * Siteexport Plugin 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 10if (!defined('DOKU_INC')) define('DOKU_INC', /** @scrutinizer ignore-type */ realpath(dirname(__FILE__) . '/../../') . '/'); 11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 12 13/** 14 * All DokuWiki plugins to extend the parser/rendering mechanism 15 * need to inherit from this class 16 */ 17class syntax_plugin_siteexport_toctools extends DokuWiki_Syntax_Plugin { 18 19 protected $special_pattern = '<mergehint\b[^>\r\n]*?/>'; 20 protected $entry_pattern = '<mergehint\b.*?>(?=.*?</mergehint>)'; 21 protected $exit_pattern = '</mergehint>'; 22 23 private $checkArray = array(); 24 25 public function getType(){ return 'substition';} 26 public function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); } 27 public function getPType(){ return 'stack';} 28 public function getSort(){ return 999; } 29 30 /** 31 * Connect pattern to lexer 32 */ 33 public function connectTo($mode) { 34 $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_siteexport_toctools'); 35 $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_siteexport_toctools'); 36 } 37 38 public function postConnect() { 39 $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_siteexport_toctools'); 40 } 41 42 private function findPreviousSectionOpen( Doku_Handler $handler ) { 43 foreach( array_reverse( $handler->calls ) as $calls ) { 44 if ( $calls[0] == 'section_open' ) { 45 return $calls[1][0]; 46 } 47 } 48 return 1; 49 } 50 51 private function addInstructionstoHandler( $match, $state, $pos, Doku_Handler $handler, $instructions ) { 52 53 // Switch for DW Hogfather+ 54 if ( ( method_exists( $handler, 'getStatus') && $handler->getStatus('section') ) || $handler->status['section'] ) { 55 $handler->_addCall('section_close', array(), $pos); 56 } 57 58 // We need to add the current plugin first and then open the section again. 59 $level = $this->findPreviousSectionOpen( $handler ); 60 $handler->_addCall('plugin', array('siteexport_toctools', $instructions, $state), $pos); 61 $handler->_addCall('section_open', array($level), $pos+strlen($match) ); 62 } 63 64 /** 65 * Handle the match 66 */ 67 public function handle($match, $state, $pos, Doku_Handler $handler){ 68 global $conf; 69 switch ($state) { 70 case DOKU_LEXER_ENTER: 71 case DOKU_LEXER_SPECIAL: 72 $data = trim(substr($match,strpos($match,' '),-1)," \t\n/"); 73 $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'start', $data, sectionID( $data, $this->checkArray ) ) ); 74 break; 75 case DOKU_LEXER_UNMATCHED: 76 $handler->_addCall('cdata', array($match), $pos); 77 break; 78 case DOKU_LEXER_EXIT: 79 $this->addInstructionstoHandler( $match, $state, $pos, $handler, array('mergehint', 'end', 'syntax' ) ); 80 break; 81 } 82 return false; 83 } 84 85 /** 86 * Create output 87 */ 88 public function render($mode, Doku_Renderer $renderer, $data) { 89 90 list( $type, $pos, $title, $id ) = $data; 91 if ($mode == 'xhtml') { 92 if ( $type == 'mergehint' ) { 93 94 $startHint = '<!-- MergeHint Start for "'; 95 $lastDiv = '<div class="mergehintcontent">'; 96 97 if ( $pos == 'start' ) { 98 $renderer->doc .= $startHint . $title . '" -->'; 99 $renderer->doc .= '<div id="' . $id . '" class="siteexport mergehintwrapper"><aside class="mergehint">' . $title . '</aside>' . $lastDiv; 100 } else { 101 102 // check if anything was inserted. We have to strip all tags, 103 // we're just looking for real content here 104 $lastPos = strrpos($renderer->doc, $lastDiv); 105 if ( $lastPos !== false ) { 106 107 $remaining = substr( $renderer->doc, $lastPos + strlen($lastDiv) ); 108 $remaining = strip_tags( $remaining ); 109 $remaining = trim($remaining); 110 if ( strlen( $remaining ) == 0 ) { 111 // empty 112 $lastPos = strrpos($renderer->doc, $startHint); 113 $renderer->doc = substr($renderer->doc, 0, $lastPos); 114 return true; 115 } 116 } 117 118 $renderer->doc .= '</div></div>'; 119 $renderer->doc .= '<!-- MergeHint End for "' . $title . '" -->'; 120 } 121 } else { 122 $renderer->doc .= "<br style=\"page-break-after:always;\" />"; 123 } 124 return true; 125 } else if ( $mode = 'markdown' && $type == 'mergehint' && $pos == 'start' ) { 126 $renderer->doc .= DOKU_LF . '##### ' . $title . DOKU_LF; 127 return true; 128 } 129 return false; 130 } 131} 132 133//Setup VIM: ex: et ts=4 enc=utf-8 :