xref: /plugin/siteexport/renderer/pdf.php (revision 7ddc12e6486190091a7adde7f8b77f87d8840a6d)
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_siteexport_pdf extends Doku_Renderer_xhtml {
17
18    var $acronymsExchanged = null;
19    var $hasSeenHeader = false;
20    var $scriptmode = false;
21
22    var $currentLevel = 0;
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 document_start() {
57        global $TOC, $ID, $INFO;
58
59        parent::document_start();
60
61        // Cheating in again
62        $newMeta = p_get_metadata($ID, 'description tableofcontents', false); // 2010-10-23 This should be save to use
63        if ( !empty( $newMeta ) && count($newMeta) > 1 ) {
64            // $TOC = $this->toc = $newMeta; // 2010-08-23 doubled the TOC
65            $TOC = $newMeta;
66        }
67    }
68
69    function document_end() {
70
71        parent::document_end();
72
73        // Prepare the TOC
74        global $TOC, $ID;
75        $meta = array();
76
77        // NOTOC, and no forceTOC
78        if ( $this->info['toc'] === false && !($this->info['forceTOC'] || $this->meta['forceTOC']) ) {
79            $TOC = $this->toc = array();
80            $meta['internal']['toc'] = false;
81            $meta['description']['tableofcontents'] = array();
82            $meta['forceTOC'] = false;
83
84        } else if ( $this->info['forceTOC'] || $this->meta['forceTOC'] || (utf8_strlen(strip_tags($this->doc)) >= $this->getConf('documentlengthfortoc') && count($this->toc) > 1 ) ) {
85            $TOC = $this->toc;
86            // This is a little bit like cheating ... but this will force the TOC into the metadata
87            $meta = array();
88            $meta['internal']['toc'] = true;
89            $meta['forceTOC'] = $this->info['forceTOC'] || $this->meta['forceTOC'];
90            $meta['description']['tableofcontents'] = $TOC;
91        }
92
93        // allways write new metadata
94        p_set_metadata($ID, $meta);
95        $this->doc = preg_replace('#<p( class=".*?")?>\s*</p>#','',$this->doc);
96    }
97
98    function header($text, $level, $pos) {
99        global $conf;
100        global $ID;
101        global $INFO;
102
103        if($text)
104        {
105            $hid = $this->_headerToLink($text,true);
106
107            //only add items within configured levels
108            $this->toc_additem($hid, $text, $level);
109
110            // adjust $node to reflect hierarchy of levels
111            $this->node[$level-1]++;
112            if ($level < $this->lastlevel) {
113                for ($i = 0; $i < $this->lastlevel-$level; $i++) {
114                    $this->node[$this->lastlevel-$i-1] = 0;
115                }
116            }
117            $this->lastlevel = $level;
118
119
120            /* There should be no class for "sectioneditX" if there is no edit perm */
121            if ($INFO['perm'] > AUTH_READ &&
122                $level <= $conf['maxseclevel'] &&
123                count($this->sectionedits) > 0 &&
124                $this->sectionedits[count($this->sectionedits) - 1][2] === 'section') {
125                $this->finishSectionEdit($pos - 1);
126            }
127
128            $headingNumber = '';
129            $useNumbered = p_get_metadata($ID, 'usenumberedheading', true); // 2011-02-07 This should be save to use
130            if ( $this->getConf('usenumberedheading') || !empty($useNumbered) || !empty($INFO['meta']['usenumberedheading']) || isset($_REQUEST['usenumberedheading'])) {
131
132                // increment the number of the heading
133                $this->headingCount[$level]++;
134
135                // build the actual number
136                for ($i=1;$i<=5;$i++) {
137
138                    // reset the number of the subheadings
139                    if ($i>$level) {
140                        $this->headingCount[$i] = 0;
141                    }
142
143                    // build the number of the heading
144                    $headingNumber .= $this->headingCount[$i] . '.';
145                }
146
147                $headingNumber = preg_replace("/(\.0)+\.?$/", '', $headingNumber) . ' ';
148            }
149
150            // write the header
151            $this->doc .= DOKU_LF.'<h'.$level;
152            $class = array();
153            if ($INFO['perm'] > AUTH_READ &&
154                $level <= $conf['maxseclevel']) {
155                $class[] = $this->startSectionEdit($pos, 'section', $text);
156            }
157
158            if ( !empty($headingNumber) ) {
159	            $class[] = 'level' . trim($headingNumber);
160	            if ( $headingNumber[0] != '1' ) {
161		            $class[] = 'notfirst';
162	            } else {
163		            $class[] = 'first';
164	            }
165            }
166
167			if ( !empty($class) ) {
168				$this->doc .= ' class="' . implode(' ', $class) . '"';
169			}
170
171            $this->doc .= '><a name="'.$hid.'" id="'.$hid.'">';
172            $this->doc .= $this->_xmlEntities($headingNumber . $text);
173            $this->doc .= "</a></h$level>".DOKU_LF;
174
175        } else if ( $INFO['perm'] > AUTH_READ ) {
176
177            if ( $hasSeenHeader ) $this->finishSectionEdit($pos);
178
179            // write the header
180            $name = rand() . $level;
181            $this->doc .= DOKU_LF.'<a name="'. $this->startSectionEdit($pos, 'section_empty', $name) .'" class="' . $this->startSectionEdit($pos, 'section_empty', $name) . '" ></a>'.DOKU_LF;
182        }
183
184        $hasSeenHeader = true;
185    }
186
187    function section_open($level) {
188        $this->currentLevel = $level;
189    }
190
191    function section_close() {}
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        global $INFO;
207        if ( $INFO['perm'] > AUTH_READ )
208        {
209            return parent::finishSectionEdit($end);
210        }
211    }
212
213    public function startSectionEdit($start, $type, $title = null) {
214        global $INFO;
215        if ( $INFO['perm'] > AUTH_READ )
216        {
217            return parent::startSectionEdit($start, $type, $title);
218        }
219
220        return "";
221    }
222
223    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
224    $height=NULL, $cache=NULL, $linking=NULL) {
225        global $ID;
226        list($src,$hash) = explode('#',$src,2);
227        resolve_mediaid(getNS($ID),$src, $exists);
228
229        $noLink = false;
230        $render = ($linking == 'linkonly') ? false : true;
231        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
232
233        list($ext,$mime,$dl) = mimetype($src);
234        if(substr($mime,0,5) == 'image' && $render){
235            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
236            if ( substr($mime,0,5) == 'image' && $linking='details' ) { $noLink = true;}
237        }elseif($mime == 'application/x-shockwave-flash' && $render){
238            // don't link flash movies
239            $noLink = true;
240        }else{
241            // add file icons
242            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
243            $link['class'] .= ' mediafile mf_'.$class;
244            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
245        }
246
247        if($hash) $link['url'] .= '#'.$hash;
248
249        //markup non existing files
250        if (!$exists)
251        $link['class'] .= ' wikilink2';
252
253        //output formatted
254        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
255        else $this->doc .= $this->_formatLink($link);
256    }
257
258    /**
259     * Render an internal Wiki Link
260     *
261     * $search,$returnonly & $linktype are not for the renderer but are used
262     * elsewhere - no need to implement them in other renderers
263     *
264     * @author Andreas Gohr <andi@splitbrain.org>
265     */
266    function internallink($id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content') {
267        global $conf;
268        global $ID;
269        // default name is based on $id as given
270        $default = $this->_simpleTitle($id);
271
272        // now first resolve and clean up the $id
273        resolve_pageid(getNS($ID),$id,$exists);
274        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
275        if ( !$isImage ) {
276            if ( $exists ) {
277                $class='wikilink1';
278            } else {
279                $class='wikilink2';
280                $link['rel']='nofollow';
281            }
282        } else {
283            $class='media';
284        }
285
286        //keep hash anchor
287        list($id,$hash) = explode('#',$id,2);
288        if(!empty($hash)) $hash = $this->_headerToLink($hash);
289
290        //prepare for formating
291        $link['target'] = $conf['target']['wiki'];
292        $link['style']  = '';
293        $link['pre']    = '';
294        $link['suf']    = '';
295        // highlight link to current page
296        if ($id == $ID) {
297            $link['pre']    = '<span class="curid">';
298            $link['suf']    = '</span>';
299        }
300        $link['more']   = '';
301        $link['class']  = $class;
302        $link['url']    = wl($id);
303        $link['name']   = $name;
304        $link['title']  = $this->_getLinkTitle(null, $default, $isImage, $id, $linktype);
305
306        //add search string
307        if($search){
308            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
309            if(is_array($search)){
310                $search = array_map('rawurlencode',$search);
311                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
312            }else{
313                $link['url'] .= 's='.rawurlencode($search);
314            }
315        }
316
317        //keep hash
318        if($hash) $link['url'].='#'.$hash;
319
320        //output formatted
321        if($returnonly){
322            return $this->_formatLink($link);
323        }else{
324            $this->doc .= $this->_formatLink($link);
325        }
326    }
327
328    function acronym($acronym) {
329
330        if ( empty($this->acronymsExchanged) ) {
331            $this->acronymsExchanged = $this->acronyms;
332            $this->acronyms = array();
333
334            foreach( $this->acronymsExchanged as $key => $value ) {
335                $this->acronyms[str_replace('_', ' ', $key)] = $value;
336            }
337        }
338
339        parent::acronym($acronym);
340    }
341
342    function _xmlEntities($string) {
343
344        $string = parent::_xmlEntities($string);
345
346        if ( $this->info['scriptmode'] ) {
347            $string = str_replace(	array( "&lt;%", "%&gt;", "&lt;?", "?&gt;"),
348            array( "<%", "%>", "<?", "?>"),
349            $string);
350        }
351
352        return $string;
353    }
354}
355
356//Setup VIM: ex: et ts=4 enc=utf-8 :