xref: /plugin/dw2pdf/renderer.php (revision ccdd31264102efb6cb808223aafbecdbea9f92a4)
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    /**
27a876b55bSAndreas Gohr     * Simplified header printing with PDF bookmarks
28a876b55bSAndreas Gohr     */
29a876b55bSAndreas Gohr    function header($text, $level, $pos) {
30a876b55bSAndreas Gohr        if(!$text) return; //skip empty headlines
31a876b55bSAndreas Gohr
32a876b55bSAndreas Gohr        // add PDF bookmark
33a876b55bSAndreas Gohr        $bmlevel = $this->getConf('maxbookmarks');
34a876b55bSAndreas Gohr        if($bmlevel && $bmlevel >= $level){
35a876b55bSAndreas Gohr            $this->doc .= '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($level-1).'" />';
36a876b55bSAndreas Gohr        }
37a876b55bSAndreas Gohr
38a876b55bSAndreas Gohr        // print header
39a876b55bSAndreas Gohr        $this->doc .= DOKU_LF."<h$level>";
40a876b55bSAndreas Gohr        $this->doc .= $this->_xmlEntities($text);
41a876b55bSAndreas Gohr        $this->doc .= "</h$level>".DOKU_LF;
42a876b55bSAndreas Gohr    }
43a876b55bSAndreas Gohr
44aab792a5SAndreas Gohr    /**
45aab792a5SAndreas Gohr     * Wrap centered media in a div to center it
46aab792a5SAndreas Gohr     */
47aab792a5SAndreas Gohr    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
48aab792a5SAndreas Gohr                      $height=NULL, $cache=NULL, $render = true) {
49aab792a5SAndreas Gohr
50aab792a5SAndreas Gohr        $out = '';
51aab792a5SAndreas Gohr        if($align == 'center'){
52aab792a5SAndreas Gohr            $out .= '<div align="center" style="text-align: center">';
53aab792a5SAndreas Gohr        }
54aab792a5SAndreas Gohr
55aab792a5SAndreas Gohr        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
56aab792a5SAndreas Gohr
57aab792a5SAndreas Gohr        if($align == 'center'){
58aab792a5SAndreas Gohr            $out .= '</div>';
59aab792a5SAndreas Gohr        }
60aab792a5SAndreas Gohr
61aab792a5SAndreas Gohr        return $out;
62aab792a5SAndreas Gohr    }
63aab792a5SAndreas Gohr
64551dd41eSAndreas Gohr    /**
65551dd41eSAndreas Gohr     * hover info makes no sense in PDFs, so drop acronyms
66551dd41eSAndreas Gohr     */
67551dd41eSAndreas Gohr    function acronym($acronym) {
68551dd41eSAndreas Gohr        $this->doc .= $this->_xmlEntities($acronym);
69551dd41eSAndreas Gohr    }
70551dd41eSAndreas Gohr
719eb4c81fSAndreas Gohr
729eb4c81fSAndreas Gohr    /**
739eb4c81fSAndreas Gohr     * reformat links if needed
749eb4c81fSAndreas Gohr     */
759eb4c81fSAndreas Gohr    function _formatLink($link){
769eb4c81fSAndreas Gohr        // prefix interwiki links with interwiki icon
779eb4c81fSAndreas Gohr        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
789eb4c81fSAndreas Gohr            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
799eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
809eb4c81fSAndreas Gohr            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
819eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
829eb4c81fSAndreas Gohr            }else{
839eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki.png';
849eb4c81fSAndreas Gohr            }
859eb4c81fSAndreas Gohr
869eb4c81fSAndreas Gohr            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" />'.$link['name'];
879eb4c81fSAndreas Gohr        }
889eb4c81fSAndreas Gohr        return parent::_formatLink($link);
899eb4c81fSAndreas Gohr    }
90*ccdd3126SAndreas Gohr
91*ccdd3126SAndreas Gohr    /**
92*ccdd3126SAndreas Gohr     * no obfuscation for email addresses
93*ccdd3126SAndreas Gohr     */
94*ccdd3126SAndreas Gohr    function emaillink($address, $name = NULL) {
95*ccdd3126SAndreas Gohr        global $conf;
96*ccdd3126SAndreas Gohr        $old = $conf['mailguard'];
97*ccdd3126SAndreas Gohr        $conf['mailguard'] = 'none';
98*ccdd3126SAndreas Gohr        parent::emaillink($address, $name);
99*ccdd3126SAndreas Gohr        $conf['mailguard'] = $old;
100*ccdd3126SAndreas Gohr    }
101*ccdd3126SAndreas Gohr
102a876b55bSAndreas Gohr}
103a876b55bSAndreas Gohr
104