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