1<?php 2/** 3 * DokuWiki Plugin dw2pdf (Renderer Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <gohr@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12if (!defined('DOKU_LF')) define('DOKU_LF', "\n"); 13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t"); 14require_once DOKU_INC.'inc/parser/xhtml.php'; 15 16class renderer_plugin_dw2pdf extends Doku_Renderer_xhtml { 17 18 private $lastheadlevel = -1; 19 private $current_bookmark_level = 0; 20 21 /** 22 * Make available as XHTML replacement renderer 23 */ 24 public function canRender($format){ 25 if($format == 'xhtml') return true; 26 return false; 27 } 28 29 /** 30 * Simplified header printing with PDF bookmarks 31 */ 32 function header($text, $level, $pos) { 33 if(!$text) return; //skip empty headlines 34 35 $hid = $this->_headerToLink($text,true); 36 37 // add PDF bookmark 38 $bookmark = ''; 39 $bmlevel = $this->getConf('maxbookmarks'); 40 if($bmlevel && $bmlevel >= $level){ 41 // PDF readers choke on invalid nested levels 42 43 if ($this->lastheadlevel == -1) 44 $this->lastheadlevel = $level; 45 46 $step = $level - $this->lastheadlevel; 47 48 if ($step > 0) 49 $this->current_bookmark_level += 1; 50 else if ($step <0) { 51 $this->current_bookmark_level -= 1; 52 if ($this->current_bookmark_level < 0) 53 $this->current_bookmark_level = 0; 54 } 55 56 $this->lastheadlevel = $level; 57 58 $bookmark = '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($this->current_bookmark_level).'" />'; 59 } 60 61 // print header 62 $this->doc .= DOKU_LF."<h$level>$bookmark"; 63 $this->doc .= "<a name=\"$hid\">"; 64 $this->doc .= $this->_xmlEntities($text); 65 $this->doc .= "</a>"; 66 $this->doc .= "</h$level>".DOKU_LF; 67 } 68 69 /** 70 * Wrap centered media in a div to center it 71 */ 72 function _media ($src, $title=NULL, $align=NULL, $width=NULL, 73 $height=NULL, $cache=NULL, $render = true) { 74 75 $out = ''; 76 if($align == 'center'){ 77 $out .= '<div align="center" style="text-align: center">'; 78 } 79 80 $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render); 81 82 if($align == 'center'){ 83 $out .= '</div>'; 84 } 85 86 return $out; 87 } 88 89 /** 90 * hover info makes no sense in PDFs, so drop acronyms 91 */ 92 function acronym($acronym) { 93 $this->doc .= $this->_xmlEntities($acronym); 94 } 95 96 97 /** 98 * reformat links if needed 99 */ 100 101 function _formatLink($link){ 102 // prefix interwiki links with interwiki icon 103 if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){ 104 if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){ 105 $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png'; 106 }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){ 107 $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif'; 108 }else{ 109 $img = DOKU_BASE.'lib/images/interwiki.png'; 110 } 111 112 $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" class="'.$link['class'].'" />'.$link['name']; 113 } 114 return parent::_formatLink($link); 115 } 116 117 /** 118 * no obfuscation for email addresses 119 */ 120 function emaillink($address, $name = NULL) { 121 global $conf; 122 $old = $conf['mailguard']; 123 $conf['mailguard'] = 'none'; 124 parent::emaillink($address, $name); 125 $conf['mailguard'] = $old; 126 } 127 128} 129 130