xref: /plugin/dw2pdf/renderer.php (revision 57a1b6f9c7920e3f5997e179597ea717ff928fde)
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
1260e59de7SGerrit Uitslag/**
1360e59de7SGerrit Uitslag * Render xhtml suitable as input for mpdf library
1460e59de7SGerrit Uitslag */
15a876b55bSAndreas Gohrclass renderer_plugin_dw2pdf extends Doku_Renderer_xhtml {
16a876b55bSAndreas Gohr
17475fd248SIvan Poliakov    private $lastheadlevel = -1;
18475fd248SIvan Poliakov    private $current_bookmark_level = 0;
1960e59de7SGerrit Uitslag    /**
2060e59de7SGerrit Uitslag     * Stores action instance
2160e59de7SGerrit Uitslag     *
2260e59de7SGerrit Uitslag     * @var action_plugin_dw2pdf
2360e59de7SGerrit Uitslag     */
2460e59de7SGerrit Uitslag    private $actioninstance = null;
2560e59de7SGerrit Uitslag
2660e59de7SGerrit Uitslag    /**
2760e59de7SGerrit Uitslag     * load action plugin instance
2860e59de7SGerrit Uitslag     */
2960e59de7SGerrit Uitslag    public function __construct() {
3060e59de7SGerrit Uitslag        $this->actioninstance = plugin_load('action', 'dw2pdf');
3160e59de7SGerrit Uitslag    }
3260e59de7SGerrit Uitslag
3360e59de7SGerrit Uitslag    public function document_start() {
3460e59de7SGerrit Uitslag        global $ID;
3560e59de7SGerrit Uitslag
3660e59de7SGerrit Uitslag        parent::document_start();
3760e59de7SGerrit Uitslag
3860e59de7SGerrit Uitslag        //ancher for rewritten links to included pages
3960e59de7SGerrit Uitslag        $check = false;
4060e59de7SGerrit Uitslag        $pid = sectionID($ID, $check);
4160e59de7SGerrit Uitslag
4260e59de7SGerrit Uitslag        $this->doc .= "<a name=\"{$pid}__\">";
4360e59de7SGerrit Uitslag        $this->doc .= "</a>";
4460e59de7SGerrit Uitslag    }
4583bac986SAndreas Gohr
46a876b55bSAndreas Gohr    /**
47a876b55bSAndreas Gohr     * Make available as XHTML replacement renderer
48a876b55bSAndreas Gohr     */
49a876b55bSAndreas Gohr    public function canRender($format){
50a876b55bSAndreas Gohr        if($format == 'xhtml') return true;
51a876b55bSAndreas Gohr        return false;
52a876b55bSAndreas Gohr    }
53a876b55bSAndreas Gohr
54a876b55bSAndreas Gohr    /**
55a876b55bSAndreas Gohr     * Simplified header printing with PDF bookmarks
56a876b55bSAndreas Gohr     */
57a876b55bSAndreas Gohr    function header($text, $level, $pos) {
58a876b55bSAndreas Gohr        if(!$text) return; //skip empty headlines
5960e59de7SGerrit Uitslag        global $ID;
60a876b55bSAndreas Gohr
61244aaa51SMichael Hamann        $hid = $this->_headerToLink($text, true);
62*57a1b6f9SGerrit Uitslag
63*57a1b6f9SGerrit Uitslag        //only add items within global configured levels (doesn't check the pdf toc settings)
64*57a1b6f9SGerrit Uitslag        $this->toc_additem($hid, $text, $level);
65*57a1b6f9SGerrit Uitslag
6660e59de7SGerrit Uitslag        $check = false;
6760e59de7SGerrit Uitslag        $pid = sectionID($ID, $check);
6860e59de7SGerrit Uitslag        $hid = $pid . '__' . $hid;
69244aaa51SMichael Hamann
70a876b55bSAndreas Gohr            // add PDF bookmark
717fa15500SAndreas Gohr        $bookmark = '';
7202f9a447SGerrit Uitslag        $bmlevel = $this->actioninstance->getExportConfig('maxbookmarks');
73a876b55bSAndreas Gohr        if($bmlevel && $bmlevel >= $level){
7483bac986SAndreas Gohr            // PDF readers choke on invalid nested levels
75475fd248SIvan Poliakov
76475fd248SIvan Poliakov            if ($this->lastheadlevel == -1)
7783bac986SAndreas Gohr            	$this->lastheadlevel = $level;
7883bac986SAndreas Gohr
79475fd248SIvan Poliakov            $step = $level - $this->lastheadlevel;
80475fd248SIvan Poliakov
81475fd248SIvan Poliakov            if ($step > 0)
82475fd248SIvan Poliakov            	$this->current_bookmark_level += 1;
83475fd248SIvan Poliakov            else if ($step <0)  {
84475fd248SIvan Poliakov            	$this->current_bookmark_level -= 1;
85475fd248SIvan Poliakov                if ($this->current_bookmark_level < 0)
86475fd248SIvan Poliakov                    $this->current_bookmark_level = 0;
87475fd248SIvan Poliakov            }
88475fd248SIvan Poliakov
89475fd248SIvan Poliakov            $this->lastheadlevel = $level;
90475fd248SIvan Poliakov
917fa15500SAndreas Gohr            $bookmark = '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($this->current_bookmark_level).'" />';
92a876b55bSAndreas Gohr        }
93a876b55bSAndreas Gohr
94a876b55bSAndreas Gohr        // print header
957fa15500SAndreas Gohr        $this->doc .= DOKU_LF."<h$level>$bookmark";
96244aaa51SMichael Hamann        $this->doc .= "<a name=\"$hid\">";
97a876b55bSAndreas Gohr        $this->doc .= $this->_xmlEntities($text);
98244aaa51SMichael Hamann        $this->doc .= "</a>";
99a876b55bSAndreas Gohr        $this->doc .= "</h$level>".DOKU_LF;
100a876b55bSAndreas Gohr    }
101a876b55bSAndreas Gohr
102aab792a5SAndreas Gohr    /**
10360e59de7SGerrit Uitslag     * Render a page local link
10460e59de7SGerrit Uitslag     *
10560e59de7SGerrit Uitslag     * @param string $hash hash link identifier
10660e59de7SGerrit Uitslag     * @param string $name name for the link
10760e59de7SGerrit Uitslag     *
10860e59de7SGerrit Uitslag     * // modified copy of parent function
10960e59de7SGerrit Uitslag     * @see Doku_Renderer_xhtml::locallink
11060e59de7SGerrit Uitslag     */
111528811ffSAnael Mobilia    function locallink($hash, $name = null, $returnonly = false) {
11260e59de7SGerrit Uitslag        global $ID;
11360e59de7SGerrit Uitslag        $name  = $this->_getLinkTitle($name, $hash, $isImage);
11460e59de7SGerrit Uitslag        $hash  = $this->_headerToLink($hash);
11560e59de7SGerrit Uitslag        $title = $ID.' ↵';
11660e59de7SGerrit Uitslag
11760e59de7SGerrit Uitslag        $check = false;
11860e59de7SGerrit Uitslag        $pid = sectionID($ID, $check);
11960e59de7SGerrit Uitslag
12060e59de7SGerrit Uitslag        $this->doc .= '<a href="#'. $pid . '__' . $hash.'" title="'.$title.'" class="wikilink1">';
12160e59de7SGerrit Uitslag        $this->doc .= $name;
12260e59de7SGerrit Uitslag        $this->doc .= '</a>';
12360e59de7SGerrit Uitslag    }
12460e59de7SGerrit Uitslag
12560e59de7SGerrit Uitslag    /**
126aab792a5SAndreas Gohr     * Wrap centered media in a div to center it
127aab792a5SAndreas Gohr     */
128aab792a5SAndreas Gohr    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
129aab792a5SAndreas Gohr                      $height=NULL, $cache=NULL, $render = true) {
130aab792a5SAndreas Gohr
131aab792a5SAndreas Gohr        $out = '';
132aab792a5SAndreas Gohr        if($align == 'center'){
133aab792a5SAndreas Gohr            $out .= '<div align="center" style="text-align: center">';
134aab792a5SAndreas Gohr        }
135aab792a5SAndreas Gohr
136aab792a5SAndreas Gohr        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
137aab792a5SAndreas Gohr
138aab792a5SAndreas Gohr        if($align == 'center'){
139aab792a5SAndreas Gohr            $out .= '</div>';
140aab792a5SAndreas Gohr        }
141aab792a5SAndreas Gohr
142aab792a5SAndreas Gohr        return $out;
143aab792a5SAndreas Gohr    }
144aab792a5SAndreas Gohr
145551dd41eSAndreas Gohr    /**
146551dd41eSAndreas Gohr     * hover info makes no sense in PDFs, so drop acronyms
147551dd41eSAndreas Gohr     */
148551dd41eSAndreas Gohr    function acronym($acronym) {
149551dd41eSAndreas Gohr        $this->doc .= $this->_xmlEntities($acronym);
150551dd41eSAndreas Gohr    }
151551dd41eSAndreas Gohr
1529eb4c81fSAndreas Gohr
1539eb4c81fSAndreas Gohr    /**
1549eb4c81fSAndreas Gohr     * reformat links if needed
1559eb4c81fSAndreas Gohr     */
15647b0d67bSAndreas Gohr
1579eb4c81fSAndreas Gohr    function _formatLink($link){
15860e59de7SGerrit Uitslag
15960e59de7SGerrit Uitslag        // for internal links contains the title the pageid
16060e59de7SGerrit Uitslag        if(in_array($link['title'], $this->actioninstance->getExportedPages())) {
16160e59de7SGerrit Uitslag            list(/* $url */, $hash) = explode('#', $link['url'], 2);
16260e59de7SGerrit Uitslag
16360e59de7SGerrit Uitslag            $check = false;
16460e59de7SGerrit Uitslag            $pid = sectionID($link['title'], $check);
16560e59de7SGerrit Uitslag            $link['url'] = "#" . $pid . '__' . $hash;
16660e59de7SGerrit Uitslag        }
16760e59de7SGerrit Uitslag
16860e59de7SGerrit Uitslag
1699eb4c81fSAndreas Gohr        // prefix interwiki links with interwiki icon
1709eb4c81fSAndreas Gohr        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
1719eb4c81fSAndreas Gohr            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
1729eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
1739eb4c81fSAndreas Gohr            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
1749eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
1759eb4c81fSAndreas Gohr            }else{
1769eb4c81fSAndreas Gohr                $img = DOKU_BASE.'lib/images/interwiki.png';
1779eb4c81fSAndreas Gohr            }
1789eb4c81fSAndreas Gohr
17947b0d67bSAndreas Gohr            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" class="'.$link['class'].'" />'.$link['name'];
1809eb4c81fSAndreas Gohr        }
1819eb4c81fSAndreas Gohr        return parent::_formatLink($link);
1829eb4c81fSAndreas Gohr    }
183ccdd3126SAndreas Gohr
184ccdd3126SAndreas Gohr    /**
185ccdd3126SAndreas Gohr     * no obfuscation for email addresses
186ccdd3126SAndreas Gohr     */
1870a23c2bcSAnael Mobilia    function emaillink($address, $name = NULL, $returnonly = false) {
188ccdd3126SAndreas Gohr        global $conf;
189ccdd3126SAndreas Gohr        $old = $conf['mailguard'];
190ccdd3126SAndreas Gohr        $conf['mailguard'] = 'none';
1919e1a2ac6SAnael Mobilia        parent::emaillink($address, $name, $returnonly);
192ccdd3126SAndreas Gohr        $conf['mailguard'] = $old;
193ccdd3126SAndreas Gohr    }
194ccdd3126SAndreas Gohr
195a876b55bSAndreas Gohr}
196a876b55bSAndreas Gohr
197