xref: /plugin/dw2pdf/renderer.php (revision 9eb4c81f1dcbe69e7750e459da9574afc1351341)
1a876b55bSAndreas Gohr<?php
2a876b55bSAndreas Gohr/**
3a876b55bSAndreas Gohr * DokuWiki Plugin dw2pdf (Renderer Component)
4a876b55bSAndreas Gohr *
5a876b55bSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6a876b55bSAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
7a876b55bSAndreas Gohr */
8a876b55bSAndreas Gohr
9a876b55bSAndreas Gohr// must be run within Dokuwiki
10a876b55bSAndreas Gohrif (!defined('DOKU_INC')) die();
11a876b55bSAndreas Gohr
12a876b55bSAndreas Gohrif (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13a876b55bSAndreas Gohrif (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14a876b55bSAndreas Gohrrequire_once DOKU_INC.'inc/parser/xhtml.php';
15a876b55bSAndreas Gohr
16a876b55bSAndreas Gohrclass renderer_plugin_dw2pdf extends Doku_Renderer_xhtml {
17a876b55bSAndreas Gohr
18a876b55bSAndreas Gohr    /**
19a876b55bSAndreas Gohr     * Make available as XHTML replacement renderer
20a876b55bSAndreas Gohr     */
21a876b55bSAndreas Gohr    public function canRender($format){
22a876b55bSAndreas Gohr        if($format == 'xhtml') return true;
23a876b55bSAndreas Gohr        return false;
24a876b55bSAndreas Gohr    }
25a876b55bSAndreas Gohr
26a876b55bSAndreas Gohr    // FIXME override any methods of Doku_Renderer_xhtml here
27a876b55bSAndreas Gohr
28a876b55bSAndreas Gohr
29a876b55bSAndreas Gohr    /**
30a876b55bSAndreas Gohr     * Simplified header printing with PDF bookmarks
31a876b55bSAndreas Gohr     */
32a876b55bSAndreas Gohr    function header($text, $level, $pos) {
33a876b55bSAndreas Gohr        if(!$text) return; //skip empty headlines
34a876b55bSAndreas Gohr
35a876b55bSAndreas Gohr        // add PDF bookmark
36a876b55bSAndreas Gohr        $bmlevel = $this->getConf('maxbookmarks');
37a876b55bSAndreas Gohr        if($bmlevel && $bmlevel >= $level){
38a876b55bSAndreas Gohr            $this->doc .= '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($level-1).'" />';
39a876b55bSAndreas Gohr        }
40a876b55bSAndreas Gohr
41a876b55bSAndreas Gohr        // print header
42a876b55bSAndreas Gohr        $this->doc .= DOKU_LF."<h$level>";
43a876b55bSAndreas Gohr        $this->doc .= $this->_xmlEntities($text);
44a876b55bSAndreas Gohr        $this->doc .= "</h$level>".DOKU_LF;
45a876b55bSAndreas Gohr    }
46a876b55bSAndreas Gohr
47aab792a5SAndreas Gohr    /**
48aab792a5SAndreas Gohr     * Wrap centered media in a div to center it
49aab792a5SAndreas Gohr     */
50aab792a5SAndreas Gohr    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
51aab792a5SAndreas Gohr                      $height=NULL, $cache=NULL, $render = true) {
52aab792a5SAndreas Gohr
53aab792a5SAndreas Gohr        $out = '';
54aab792a5SAndreas Gohr        if($align == 'center'){
55aab792a5SAndreas Gohr            $out .= '<div align="center" style="text-align: center">';
56aab792a5SAndreas Gohr        }
57aab792a5SAndreas Gohr
58aab792a5SAndreas Gohr        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
59aab792a5SAndreas Gohr
60aab792a5SAndreas Gohr        if($align == 'center'){
61aab792a5SAndreas Gohr            $out .= '</div>';
62aab792a5SAndreas Gohr        }
63aab792a5SAndreas Gohr
64aab792a5SAndreas Gohr        return $out;
65aab792a5SAndreas Gohr    }
66aab792a5SAndreas Gohr
67551dd41eSAndreas Gohr    /**
68551dd41eSAndreas Gohr     * hover info makes no sense in PDFs, so drop acronyms
69551dd41eSAndreas Gohr     */
70551dd41eSAndreas Gohr    function acronym($acronym) {
71551dd41eSAndreas Gohr        $this->doc .= $this->_xmlEntities($acronym);
72551dd41eSAndreas Gohr    }
73551dd41eSAndreas Gohr
74*9eb4c81fSAndreas Gohr
75*9eb4c81fSAndreas Gohr    /**
76*9eb4c81fSAndreas Gohr     * reformat links if needed
77*9eb4c81fSAndreas Gohr     */
78*9eb4c81fSAndreas Gohr    function _formatLink($link){
79*9eb4c81fSAndreas Gohr        // prefix interwiki links with interwiki icon
80*9eb4c81fSAndreas Gohr        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
81*9eb4c81fSAndreas Gohr            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
82*9eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
83*9eb4c81fSAndreas Gohr            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
84*9eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
85*9eb4c81fSAndreas Gohr            }else{
86*9eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki.png';
87*9eb4c81fSAndreas Gohr            }
88*9eb4c81fSAndreas Gohr
89*9eb4c81fSAndreas Gohr            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" />'.$link['name'];
90*9eb4c81fSAndreas Gohr        }
91*9eb4c81fSAndreas Gohr        return parent::_formatLink($link);
92*9eb4c81fSAndreas Gohr    }
93a876b55bSAndreas Gohr}
94a876b55bSAndreas Gohr
95