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