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