xref: /plugin/nodetailsxhtml/renderer.php (revision c6a171b3ebb24349cdee8495cd7f07c4715cb384)
1<?php
2/**
3 * Render Plugin for XHTML  without details link for internal images.
4 *
5 * @author i-net software <tools@inetsoftware.de>
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11require_once DOKU_INC . 'inc/parser/xhtml.php';
12
13/**
14 * The Renderer
15 */
16class renderer_plugin_nodetailsxhtml extends Doku_Renderer_xhtml {
17
18    var $acronymsExchanged = null;
19    var $hasSeenHeader = false;
20    var $scriptmode = false;
21    var $sectionLevel = 0;
22
23    var $startlevel = 0; // level to start with numbered headings (default = 2)
24    var $levels = array( '======'=>1,
25                         '====='=>2,
26                         '===='=>3,
27                         '==='=>4,
28                         '=='=>5);
29
30    var $info = array(
31        'cache'      => true, // may the rendered result cached?
32        'toc'        => true, // render the TOC?
33        'forceTOC'   => false, // shall I force the TOC?
34        'scriptmode' => false, // In scriptmode, some tags will not be encoded => '<%', '%>'
35    );
36
37    var $headingCount =
38    array(  1=>0,
39    2=>0,
40    3=>0,
41    4=>0,
42    5=>0);
43
44    /**
45     * return some info
46     */
47    function getInfo(){
48        if ( method_exists(parent, 'getInfo')) {
49            $info = parent::getInfo();
50        }
51	    return array_merge(is_array($info) ? $info : confToHash(dirname(__FILE__).'/../plugin.info.txt'), array(
52
53        ));
54    }
55
56    function canRender($format) {
57        return ($format=='xhtml');
58    }
59
60    function document_start() {
61        global $TOC, $ID, $INFO, $conf;
62
63        parent::document_start();
64
65        // Cheating in again
66        $meta = p_get_metadata($ID, null, false); // 2010-10-23 This should be save to use
67
68        if (isset($meta['toc']['toptoclevel'])) {
69            $conf['toptoclevel'] = $meta['toc']['toptoclevel'];
70        }
71        if (isset($meta['toc']['maxtoclevel'])) {
72            $conf['maxtoclevel'] = $meta['toc']['maxtoclevel'];
73        }
74        if (isset($meta['toc']['toptoclevel'])||isset($INFO['meta']['toc']['maxtoclevel'])) {
75            $conf['tocminheads'] = 1;
76        }
77
78        $newMeta = $meta['description']['tableofcontents'];
79        if ( !empty( $newMeta ) && count($newMeta) > 1 ) {
80            // $TOC = $this->toc = $newMeta; // 2010-08-23 doubled the TOC
81            $TOC = $newMeta;
82        }
83    }
84
85    function document_end() {
86
87        parent::document_end();
88
89        // Prepare the TOC
90        global $TOC, $ID;
91        $meta = array();
92
93        $forceToc = $this->info['forceTOC'] || p_get_metadata($ID, 'internal forceTOC', false);
94
95        // NOTOC, and no forceTOC
96        if ( $this->info['toc'] === false && !$forceToc ) {
97            $TOC = $this->toc = array();
98            $meta['internal']['toc'] = false;
99            $meta['description']['tableofcontents'] = array();
100            $meta['internal']['forceTOC'] = false;
101
102        } else if ( $forceToc || (utf8_strlen(strip_tags($this->doc)) >= $this->getConf('documentlengthfortoc') && count($this->toc) > 1 ) ) {
103            $TOC = $this->toc;
104            // This is a little bit like cheating ... but this will force the TOC into the metadata
105            $meta = array();
106            $meta['internal']['toc'] = true;
107            $meta['internal']['forceTOC'] = $forceToc;
108            $meta['description']['tableofcontents'] = $TOC;
109        }
110
111        // allways write new metadata
112        p_set_metadata($ID, $meta);
113
114        // make sure there are no empty blocks
115        $this->doc = preg_replace('#<(div|section|article) class=".*?level\d.*?">\s*</(div|section|article)>#','',$this->doc);
116    }
117
118    function header($text, $level, $pos) {
119        global $conf;
120        global $ID;
121        global $INFO;
122
123        if($text) {
124
125	        // Check Text for hint about a CSS style class
126	        $class = "";
127	        if ( preg_match("/^class:(.*?)>(.*?)$/", $text, $matches) ) {
128		        $class = ' ' . $this->_xmlEntities($matches[1]);
129		        $text = $matches[2];
130	        }
131
132            /* There should be no class for "sectioneditX" if there is no edit perm */
133            $maxLevel = $conf['maxseclevel'];
134            if ( $INFO['perm'] <= AUTH_READ )
135            {
136                $conf['maxseclevel'] = 0;
137            }
138
139            $headingNumber = '';
140            $useNumbered = p_get_metadata($ID, 'usenumberedheading', true); // 2011-02-07 This should be save to use
141            if ( $this->getConf('usenumberedheading') || !empty($useNumbered) || !empty($INFO['meta']['usenumberedheading']) || isset($_REQUEST['usenumberedheading'])) {
142
143                // increment the number of the heading
144                $this->headingCount[$level]++;
145
146                // build the actual number
147                for ($i=1;$i<=5;$i++) {
148
149                    // reset the number of the subheadings
150                    if ($i>$level) {
151                        $this->headingCount[$i] = 0;
152                    }
153
154                    // build the number of the heading
155                    $headingNumber .= $this->headingCount[$i] . '.';
156                }
157
158                $headingNumber = preg_replace("/(\.0)+\.?$/", '', $headingNumber) . ' ';
159            }
160
161			$doc = $this->doc;
162			$this->doc = "";
163
164            parent::header($headingNumber . $text, $level, $pos);
165
166            if ( $this->getConf('useSectionArticle') ) {
167                $this->doc = $doc . preg_replace("/(<h([1-9]))/", "<".($this->sectionLevel<1?'section':'article')." class=\"level\\2{$class}\">\\1", $this->doc);
168            } else {
169                $this->doc = $doc . $this->doc;
170            }
171
172            $conf['maxseclevel'] = $maxLevel;
173
174        } else if ( $INFO['perm'] > AUTH_READ ) {
175
176            if ( $hasSeenHeader ) $this->finishSectionEdit($pos);
177
178            // write the header
179            $name = $this->startSectionEdit($pos, 'section_empty', rand() . $level);
180            if ( $this->getConf('useSectionArticle') ) {
181                $this->doc .= '<'.($this->sectionLevel<1?'section':'article').' class="'.$name.'">';
182            }
183
184            $this->doc .= DOKU_LF.'<a name="'. $name .'" class="' . $name . '" ></a>'.DOKU_LF;
185        }
186
187        $hasSeenHeader = true;
188    }
189
190    public function finishSectionEdit($end = null) {
191        global $INFO;
192        if ( $INFO['perm'] > AUTH_READ )
193        {
194            return parent::finishSectionEdit($end);
195        }
196    }
197
198    public function startSectionEdit($start, $type, $title = null) {
199        global $INFO;
200        if ( $INFO['perm'] > AUTH_READ )
201        {
202            return parent::startSectionEdit($start, $type, $title);
203        }
204
205        return "";
206    }
207
208    function section_close() {
209        $this->sectionLevel--;
210        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
211        if ( $this->getConf('useSectionArticle') ) {
212            $this->doc .= '</'.($this->sectionLevel<1?'section':'article').'>'.DOKU_LF;
213        }
214    }
215
216    function section_open($level) {
217        $this->sectionLevel++;
218        return parent::section_open($level);
219    }
220
221    function internalmedia ($src, $title=null, $align=null, $width=null,
222                            $height=null, $cache=null, $linking=null, $return=NULL) {
223        global $ID;
224        list($src,$hash) = explode('#',$src,2);
225        resolve_mediaid(getNS($ID),$src, $exists);
226
227        $noLink = false;
228        $render = ($linking == 'linkonly') ? false : true;
229        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
230
231        list($ext,$mime,$dl) = mimetype($src);
232        if(substr($mime,0,5) == 'image' && $render){
233            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
234            if ( substr($mime,0,5) == 'image' && $linking='details' ) { $noLink = true;}
235        }elseif($mime == 'application/x-shockwave-flash' && $render){
236            // don't link flash movies
237            $noLink = true;
238        }else{
239            // add file icons
240            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
241            $link['class'] .= ' mediafile mf_'.$class;
242            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
243        }
244
245        if($hash) $link['url'] .= '#'.$hash;
246
247        //markup non existing files
248        if (!$exists)
249        $link['class'] .= ' wikilink2';
250
251        //output formatted
252        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
253        else $this->doc .= $this->_formatLink($link);
254    }
255
256    /**
257     * Render an internal Wiki Link
258     *
259     * $search,$returnonly & $linktype are not for the renderer but are used
260     * elsewhere - no need to implement them in other renderers
261     *
262     * @author Andreas Gohr <andi@splitbrain.org>
263     */
264    function internallink($id, $name = null, $search=null,$returnonly=false,$linktype='content') {
265        global $conf;
266        global $ID;
267        global $INFO;
268
269        $params = '';
270        $parts = explode('?', $id, 2);
271        if (count($parts) === 2) {
272            $id = $parts[0];
273            $params = $parts[1];
274        }
275
276        // For empty $id we need to know the current $ID
277        // We need this check because _simpleTitle needs
278        // correct $id and resolve_pageid() use cleanID($id)
279        // (some things could be lost)
280        if ($id === '') {
281            $id = $ID;
282        }
283
284        // default name is based on $id as given
285        $default = $this->_simpleTitle($id);
286
287        // now first resolve and clean up the $id
288        resolve_pageid(getNS($ID),$id,$exists);
289
290        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
291        if ( !$isImage ) {
292            if ( $exists ) {
293                $class='wikilink1';
294            } else {
295                $class='wikilink2';
296                $link['rel']='nofollow';
297            }
298        } else {
299            $class='media';
300        }
301
302        //keep hash anchor
303        list($id,$hash) = explode('#',$id,2);
304        if(!empty($hash)) $hash = $this->_headerToLink($hash);
305
306        //prepare for formating
307        $link['target'] = $conf['target']['wiki'];
308        $link['style']  = '';
309        $link['pre']    = '';
310        $link['suf']    = '';
311        // highlight link to current page
312        if ($id == $INFO['id']) {
313            $link['pre']    = '<span class="curid">';
314            $link['suf']    = '</span>';
315        }
316        $link['more']   = '';
317        $link['class']  = $class;
318        $link['url']    = wl($id, $params);
319        $link['name']   = $name;
320        $link['title']  = $this->_getLinkTitle(null, $default, $isImage, $id, $linktype);
321        //add search string
322        if($search){
323            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
324            if(is_array($search)){
325                $search = array_map('rawurlencode',$search);
326                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
327            }else{
328                $link['url'] .= 's='.rawurlencode($search);
329            }
330        }
331
332        //keep hash
333        if($hash) $link['url'].='#'.$hash;
334
335        //output formatted
336        if($returnonly){
337            return $this->_formatLink($link);
338        }else{
339            $this->doc .= $this->_formatLink($link);
340        }
341    }
342
343    /**
344     * Render a page local link
345     *
346     * @param string $hash       hash link identifier
347     * @param string $name       name for the link
348     * @param bool   $returnonly whether to return html or write to doc attribute
349     * @return void|string writes to doc attribute or returns html depends on $returnonly
350     */
351    function locallink($hash, $name = null, $returnonly = false) {
352        global $ID;
353        $name  = $this->_getLinkTitle($name, $hash, $isImage);
354        $hash  = $this->_headerToLink($hash);
355        $title = $name;
356
357        $doc = '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
358        $doc .= $name;
359        $doc .= '</a>';
360
361        if($returnonly) {
362          return $doc;
363        } else {
364          $this->doc .= $doc;
365        }
366    }
367
368    function acronym($acronym) {
369
370        if ( empty($this->acronymsExchanged) ) {
371            $this->acronymsExchanged = $this->acronyms;
372            $this->acronyms = array();
373
374            foreach( $this->acronymsExchanged as $key => $value ) {
375                $this->acronyms[str_replace('_', ' ', $key)] = $value;
376            }
377        }
378
379        parent::acronym($acronym);
380    }
381
382    function entity($entity) {
383
384        if ( array_key_exists($entity, $this->entities) ) {
385            $entity = $this->entities[$entity];
386        }
387
388        $this->doc .= $this->_xmlEntities($entity);
389    }
390
391    function _xmlEntities($string) {
392
393        // No double encode ...
394        $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
395        // $string = parent::_xmlEntities($string);
396        $string = htmlentities($string, 8, 'UTF-8');
397        $string = $this->superentities($string);
398
399        if ( $this->info['scriptmode'] ) {
400            $string = str_replace(	array( "&lt;%", "%&gt;", "&lt;?", "?&gt;"),
401            array( "<%", "%>", "<?", "?>"),
402            $string);
403        }
404
405        return $string;
406    }
407
408	// Unicode-proof htmlentities.
409	// Returns 'normal' chars as chars and weirdos as numeric html entites.
410	function superentities( $str ){
411	    // get rid of existing entities else double-escape
412	    $str2 = '';
413	    $str = html_entity_decode(stripslashes($str),ENT_QUOTES,'UTF-8');
414	    $ar = preg_split('/(?<!^)(?!$)(?!\n)/u', $str );  // return array of every multi-byte character
415	    foreach ($ar as $c){
416	        $o = ord($c);
417	        if ( // (strlen($c) > 1) || /* multi-byte [unicode] */
418	            ($o > 127) // || /* <- control / latin weirdos -> */
419	            // ($o <32 || $o > 126) || /* <- control / latin weirdos -> */
420	            // ($o >33 && $o < 40) ||/* quotes + ambersand */
421	            // ($o >59 && $o < 63) /* html */
422
423	        ) {
424	            // convert to numeric entity
425	            $c = mb_encode_numericentity($c,array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
426	        }
427	        $str2 .= $c;
428	    }
429	    return $str2;
430	}
431}
432
433//Setup VIM: ex: et ts=4 enc=utf-8 :
434