-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
276 lines (228 loc) · 8.27 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Repository Plugin: show files from a remote repository with GesHi syntax highlighting
* Syntax: {{repo>[url] [cachetime]|[title]}}
* [url] - (REQUIRED) base URL of the code repository
* [cachetime] - (OPTIONAL) how often the cache should be refreshed;
* a number followed by one of these chars:
* d for day, h for hour or m for minutes;
* the minimum accepted value is 10 minutes.
* [title] - (OPTIONAL) a string to display as the base path
* of the repository.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <wikidesign@gmail.com>
* @author Christopher Smith <chris@jalakai.co.uk>
* @auuthor Doug Daniels <Daniels.Douglas@gmail.com>
*/
// must be run inside DokuWiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_repo extends DokuWiki_Syntax_Plugin {
/**
* return some info
*/
function getInfo(){
return array(
'author' => 'Esther Brunner',
'email' => 'wikidesign@gmail.com',
'date' => '2007-03-14',
'name' => 'Repository Plugin',
'desc' => 'Show files from a remote repository with GesHi syntax highlighting',
'url' => 'http://www.wikidesign.ch/en/plugin/repo/start',
);
}
function getType(){ return 'substition'; }
function getSort(){ return 301; }
function getPType(){ return 'block'; }
function connectTo($mode){
$this->Lexer->addSpecialPattern("{{repo>.+?}}", $mode, 'plugin_repo');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler){
$match = substr($match, 7, -2);
list($base, $title) = explode('|', $match, 2);
list($base, $refresh) = explode(' ', $base, 2);
if (preg_match('/(\d+)([dhm])/', $refresh, $match)){
$period = array('d' => 86400, 'h' => 3600, 'm' => 60);
// n * period in seconds, minimum 10 minutes
$refresh = max(600, $match[1] * $period[$match[2]]);
} else {
// default to 4 hours
$refresh = 14400;
}
return array(trim($base), trim($title), $pos, $refresh);
}
/**
* Create output
*/
function render($mode, &$renderer, $data){
// construct requested URL
$base = hsc($data[0]);
$title = ($data[1] ? hsc($data[1]) : $base);
$path = hsc($_REQUEST['repo']);
$url = $base.$path;
if ($mode == 'xhtml'){
// prevent caching to ensure the included page is always fresh
$renderer->info['cache'] = false;
// output
$renderer->header($title.$path, 5, $data[2]);
$renderer->section_open(5);
if ($url{strlen($url) - 1} == '/'){ // directory
$this->_directory($base, $renderer, $path, $data[3]);
} elseif (preg_match('/(jpe?g|gif|png)$/i', $url)){ // image
$this->_image($url, $renderer);
} else { // source code file
$this->_codefile($url, $renderer, $data[3]);
}
if ($path) $this->_location($path, $title, $renderer);
$renderer->section_close();
// for metadata renderer
} elseif ($mode == 'metadata'){
$renderer->meta['relation']['haspart'][$url] = 1;
}
return $ok;
}
/**
* Handle remote directories
*/
function _directory($url, &$renderer, $path, $refresh){
global $conf;
$cache = getCacheName($url.$path, '.repo');
$mtime = @filemtime($cache); // 0 if it doesn't exist
if (($mtime != 0) && !$_REQUEST['purge'] && ($mtime > time() - $refresh)){
$idx = io_readFile($cache, false);
if ($conf['allowdebug']) $idx .= "\n<!-- cachefile $cache used -->\n";
} else {
$items = $this->_index($url, $path);
$idx = html_buildlist($items, 'idx', 'repo_list_index', 'html_li_index');
io_saveFile($cache, $idx);
if ($conf['allowdebug']) $idx .= "\n<!-- no cachefile used, but created -->\n";
}
$renderer->doc .= $idx;
}
/**
* Extract links and list them as directory contents
*/
function _index($url, $path, $base = '', $lvl = 0){
// download the index html file
$http = new DokuHTTPClient();
$http->timeout = 25; //max. 25 sec
$data = $http->get($url.$base);
preg_match_all('/<li><a href="(.*?)">/i', $data, $results);
$lvl++;
foreach ($results[1] as $result){
if ($result == '../') continue;
$type = ($result{strlen($result) - 1} == '/' ? 'd' : 'f');
$open = (($type == 'd') && (strpos($path, $base.$result) === 0));
$items[] = array(
'level' => $lvl,
'type' => $type,
'path' => $base.$result,
'open' => $open,
);
if ($open){
$items = array_merge($items, $this->_index($url, $path, $base.$result, $lvl));
}
}
return $items;
}
/**
* Handle remote images
*/
function _image($url, &$renderer){
$renderer->p_open();
$renderer->externalmedia($url, NULL, NULL, NULL, NULL, 'recache');
$renderer->p_close();
}
/**
* Handle remote source code files: display as code box with link to file at the end
*/
function _codefile($url, &$renderer, $refresh){
// output the code box with syntax highlighting
$renderer->doc .= $this->_cached_geshi($url, $refresh);
// and show a link to the original file
$renderer->p_open();
$renderer->externallink($url);
$renderer->p_close();
}
/**
* Wrapper for GeSHi Code Highlighter, provides caching of its output
* Modified to calculate cache from URL so we don't have to re-download time and again
*
* @author Christopher Smith <chris@jalakai.co.uk>
* @author Esther Brunner <wikidesign@gmail.com>
*/
function _cached_geshi($url, $refresh){
global $conf;
$cache = getCacheName($url, '.code');
$mtime = @filemtime($cache); // 0 if it doesn't exist
if (($mtime != 0) && !$_REQUEST['purge'] &&
($mtime > time() - $refresh) &&
($mtime > filemtime(DOKU_INC.'inc/geshi.php'))){
$hi_code = io_readFile($cache, false);
if ($conf['allowdebug']) $hi_code .= "\n<!-- cachefile $cache used -->\n";
} else {
require_once(DOKU_INC . 'inc/geshi.php');
// get the source code language first
$search = array('/^htm/', '/^js$/');
$replace = array('html4strict', 'javascript');
$lang = preg_replace($search, $replace, substr(strrchr($url, '.'), 1));
// download external file
$http = new DokuHTTPClient();
$http->timeout = 25; //max. 25 sec
$code = $http->get($url);
$geshi = new GeSHi($code, strtolower($lang), DOKU_INC.'inc/geshi');
$geshi->set_encoding('utf-8');
$geshi->enable_classes();
$geshi->set_header_type(GESHI_HEADER_PRE);
$geshi->set_overall_class("code $language");
$geshi->set_link_target($conf['target']['extern']);
$hi_code = $geshi->parse_code();
io_saveFile($cache, $hi_code);
if ($conf['allowdebug']) $hi_code .= "\n<!-- no cachefile used, but created -->\n";
}
return $hi_code;
}
/**
* Show where we are with link back to main repository
*/
function _location($path, $title, &$renderer){
global $ID;
$renderer->p_open();
$renderer->internallink($ID, $title);
$base = '';
$dirs = explode('/', $path);
$n = count($dirs);
for ($i = 0; $i < $n-1; $i++){
$base .= hsc($dirs[$i]).'/';
$renderer->doc .= '<a href="'.wl($ID, 'repo='.$base).'" class="idx_dir">'.
hsc($dirs[$i]).'/</a>';
}
$renderer->doc .= hsc($dirs[$n-1]);
$renderer->p_close();
}
}
/**
* For html_buildlist()
*/
function repo_list_index($item){
global $ID;
if ($item['type'] == 'd'){
$title = substr($item['path'], 0, -1);
$class = 'idx_dir';
} else {
$title = $item['path'];
$class = 'wikilink1';
}
$title = substr(strrchr('/'.$title, '/'), 1);
return '<a href="'.wl($ID, 'repo='.$item['path']).'" class="'.$class.'">'.$title.'</a>';
}
//Setup VIM: ex: et ts=4 enc=utf-8 :