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