xref: /plugin/siteexport/renderer/pdf.php (revision 4b73700ef878a29687cb99a777c177922ca2eee3)
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 ( $headingNumber[0] != '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    }
191
192    function section_close() {}
193
194    function p_open() {
195        $this->doc .= DOKU_LF.'<p class="level' . $this->currentLevel . '">'.DOKU_LF;
196    }
197
198    function listu_open() {
199        $this->doc .= '<ul class="level' . $this->currentLevel . '">'.DOKU_LF;
200    }
201
202    function listo_open() {
203        $this->doc .= '<ol class="level' . $this->currentLevel . '">'.DOKU_LF;
204    }
205
206    public function finishSectionEdit($end = null) {
207        global $INFO;
208        if ( $INFO['perm'] > AUTH_READ )
209        {
210            return parent::finishSectionEdit($end);
211        }
212    }
213
214    public function startSectionEdit($start, $type, $title = null) {
215        global $INFO;
216        if ( $INFO['perm'] > AUTH_READ )
217        {
218            return parent::startSectionEdit($start, $type, $title);
219        }
220
221        return "";
222    }
223
224    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
225    $height=NULL, $cache=NULL, $linking=NULL) {
226        global $ID;
227        list($src,$hash) = explode('#',$src,2);
228        resolve_mediaid(getNS($ID),$src, $exists);
229
230        $noLink = false;
231        $render = ($linking == 'linkonly') ? false : true;
232        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
233
234        list($ext,$mime,$dl) = mimetype($src);
235        if(substr($mime,0,5) == 'image' && $render){
236            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
237            if ( substr($mime,0,5) == 'image' && $linking='details' ) { $noLink = true;}
238        }elseif($mime == 'application/x-shockwave-flash' && $render){
239            // don't link flash movies
240            $noLink = true;
241        }else{
242            // add file icons
243            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
244            $link['class'] .= ' mediafile mf_'.$class;
245            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
246        }
247
248        if($hash) $link['url'] .= '#'.$hash;
249
250        //markup non existing files
251        if (!$exists)
252        $link['class'] .= ' wikilink2';
253
254        //output formatted
255        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
256        else $this->doc .= $this->_formatLink($link);
257    }
258
259    /**
260     * Render an internal Wiki Link
261     *
262     * $search,$returnonly & $linktype are not for the renderer but are used
263     * elsewhere - no need to implement them in other renderers
264     *
265     * @author Andreas Gohr <andi@splitbrain.org>
266     */
267    function internallink($id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content') {
268        global $conf;
269        global $ID;
270        // default name is based on $id as given
271        $default = $this->_simpleTitle($id);
272
273        // now first resolve and clean up the $id
274        resolve_pageid(getNS($ID),$id,$exists);
275        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
276        if ( !$isImage ) {
277            if ( $exists ) {
278                $class='wikilink1';
279            } else {
280                $class='wikilink2';
281                $link['rel']='nofollow';
282            }
283        } else {
284            $class='media';
285        }
286
287        //keep hash anchor
288        list($id,$hash) = explode('#',$id,2);
289        if(!empty($hash)) $hash = $this->_headerToLink($hash);
290
291        //prepare for formating
292        $link['target'] = $conf['target']['wiki'];
293        $link['style']  = '';
294        $link['pre']    = '';
295        $link['suf']    = '';
296        // highlight link to current page
297        if ($id == $ID) {
298            $link['pre']    = '<span class="curid">';
299            $link['suf']    = '</span>';
300        }
301        $link['more']   = '';
302        $link['class']  = $class;
303        $link['url']    = wl($id);
304        $link['name']   = $name;
305        $link['title']  = $this->_getLinkTitle(null, $default, $isImage, $id, $linktype);
306
307        //add search string
308        if($search){
309            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
310            if(is_array($search)){
311                $search = array_map('rawurlencode',$search);
312                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
313            }else{
314                $link['url'] .= 's='.rawurlencode($search);
315            }
316        }
317
318        //keep hash
319        if($hash) $link['url'].='#'.$hash;
320
321        //output formatted
322        if($returnonly){
323            return $this->_formatLink($link);
324        }else{
325            $this->doc .= $this->_formatLink($link);
326        }
327    }
328
329    function acronym($acronym) {
330
331        if ( empty($this->acronymsExchanged) ) {
332            $this->acronymsExchanged = $this->acronyms;
333            $this->acronyms = array();
334
335            foreach( $this->acronymsExchanged as $key => $value ) {
336                $this->acronyms[str_replace('_', ' ', $key)] = $value;
337            }
338        }
339
340        parent::acronym($acronym);
341    }
342
343    function _xmlEntities($string) {
344
345        $string = parent::_xmlEntities($string);
346
347        if ( $this->info['scriptmode'] ) {
348            $string = str_replace(	array( "&lt;%", "%&gt;", "&lt;?", "?&gt;"),
349            array( "<%", "%>", "<?", "?>"),
350            $string);
351        }
352
353        return $string;
354    }
355}
356
357//Setup VIM: ex: et ts=4 enc=utf-8 :