1<?php 2/** 3 * DokuWiki Plugin Pdfjs (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Sahara Satoshi <sahara.satoshi@gmail.com> 7 * @author Szymon Olewniczak <solewniczak@rid.pl> 8 * 9 * SYNTAX: {{pdfjs [size] > mediaID?zoom|title }} 10 * 11 */ 12 13// must be run within Dokuwiki 14if(!defined('DOKU_INC')) die(); 15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 16require_once DOKU_PLUGIN . 'syntax.php'; 17 18class syntax_plugin_pdfjs extends DokuWiki_Syntax_Plugin { 19 20 public function getType() { 21 return 'substition'; 22 } 23 24 public function getPType() { 25 return 'normal'; 26 } 27 28 public function getSort() { 29 return 305; 30 } 31 32 public function connectTo($mode) { 33 $this->Lexer->addSpecialPattern('{{pdfjs.*?>.*?}}', $mode, 'plugin_pdfjs'); 34 } 35 36 /* 37 * @var possible zoom values 38 * 39 * @see https://github.com/mozilla/pdf.js/wiki/Viewer-options 40 */ 41 protected $zoom_opts = array( 42 'auto', 'page-actual', 'page-fit', 'page-width', 43 '50', '75', '100', '125', '150', '200', '300', '400' 44 ); 45 protected $display_opts = array('tab', 'embed'); 46 47 /** 48 * handle syntax 49 */ 50 public function handle($match, $state, $pos, Doku_Handler $handler) { 51 52 $opts = array( // set default 53 'id' => '', 54 'title' => '', 55 'width' => '100%', 56 'height' => '600px', 57 'zoom' => '', 58 'display' => 'embed', 59 ); 60 61 list($params, $media) = explode('>', trim($match, '{}'), 2); 62 63 // handle media parameters (linkId and title) 64 list($link, $title) = explode('|', $media, 2); 65 66 list($idzoom, $display) = explode('?disp=', $link, 2); 67 $display = trim($display); 68 //get the display 69 if($display) { 70 if(in_array($display, $this->display_opts)) { 71 $opts['display'] = $display; 72 } else { 73 msg('pdfjs: unknown display: ' . $display, -1); 74 } 75 } 76 77 //get the zoom 78 list($id, $zoom) = explode('?', $idzoom, 2); 79 if($zoom) { 80 if(in_array($zoom, $this->zoom_opts)) { 81 $opts['zoom'] = $zoom; 82 } else { 83 msg('pdfjs: unknown zoom: ' . $zoom, -1); 84 } 85 } 86 87 // handle viewer parameters 88 $params = trim(substr($params, strlen('pdfjs'))); 89 $size = explode(',', $params, 2); 90 //only height 91 if(count($size) == 1) { 92 $opts['height'] = preg_replace('/\s/', '', $size[0]); 93 94 //width, height 95 } else if(count($size) == 2) { 96 $opts['width'] = preg_replace('/\s/', '', $size[0]); 97 $opts['height'] = preg_replace('/\s/', '', $size[1]); 98 } 99 100 //add default px unit 101 if(is_numeric($opts['width'])) $opts['width'] .= 'px'; 102 if(is_numeric($opts['height'])) $opts['height'] .= 'px'; 103 104 $opts['id'] = trim($id); 105 if(!empty($title)) $opts['title'] = trim($title); 106 107 return array($state, $opts); 108 } 109 110 public function render($format, Doku_Renderer $renderer, $data) { 111 112 if($format != 'xhtml') return false; 113 114 list($state, $opts) = $data; 115 if($opts['id'] == '') return false; 116 117 $html = $this->_html_embed_pdfjs($opts); 118 $renderer->doc .= $html; 119 120 return true; 121 } 122 123 /** 124 * Generate html for sytax {{pdfjs>}} 125 * 126 */ 127 private function _html_embed_pdfjs($opts) { 128 // make reference link 129 $src = DOKU_URL . 'lib/plugins/pdfjs/pdfjs/web/viewer.html'; 130 $src .= '?file=' . rawurlencode(ml($opts['id'])); 131 if($opts['display'] == 'tab') { 132 $html = '<a href="' . $src . '" class="media mediafile mf_pdf"'; 133 $html .= 'target="_blank"'; 134 $html .= '>' . $opts['title'] . '</a>'; 135 } else { 136 if($opts['zoom']) $src .= '#zoom=' . $opts['zoom']; 137 $html = '<iframe class="plugin__pdfjs" src="' . $src . '"'; 138 $html .= ' style="'; 139 if($opts['width']) $html .= ' width: ' . $opts['width'] . ';'; 140 if($opts['height']) $html .= ' height: ' . $opts['height'] . ';'; 141 $html .= ' border: none;'; 142 $html .= '"></iframe>' . NL; 143 } 144 145 return $html; 146 } 147 148} 149