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