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