-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
175 lines (150 loc) · 4.76 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
<?php
/**
* DokuWiki Plugin gh (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <andi@splitbrain.org>
*/
class syntax_plugin_gh extends DokuWiki_Syntax_Plugin
{
/**
* Extension to highlighting language mapping
*
* When a extenison is not found here it's assumed the extension name equals the language
*
* @var array
*/
protected $ext2lang = array(
'as' => 'actionscript3',
'bas' => 'gwbasic',
'h' => 'c',
'hpp' => 'cpp',
'hs' => 'haskell',
'htm' => 'html5',
'html' => 'html5',
'js' => 'javascript',
'pas' => 'pascal',
'pl' => 'perl6',
'py' => 'python',
'rb' => 'ruby',
'sh' => 'bash',
'yml' => 'yaml',
);
/**
* @return string Syntax mode type
*/
public function getType()
{
return 'substition';
}
/**
* @return string Paragraph type
*/
public function getPType()
{
return 'block';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort()
{
return 155;
}
/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('{{gh>[^}]*}}', $mode, 'plugin_gh');
}
/** @inheritDoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$match = trim(substr($match, 5, -2));
list($url, $lines) = explode(' ', $match, 2);
list($from, $to) = explode('-', $lines, 2);
$data = array(
'from' => (int)$from,
'to' => (int)$to
);
if (preg_match('/([\w.\-]+)\/([\w-]+\/[\w-]+(\/[\w-]+)?)\/blob\/([\w-]+)\/(.*)$/', $url, $m)) {
$data['base'] = $m[1];
$data['repo'] = $m[2];
$data['blob'] = $m[4];
$data['file'] = $m[5];
}
return $data;
}
/** @inheritDoc */
public function render($mode, Doku_Renderer $renderer, $data)
{
if ($mode != 'xhtml') return false;
if (!$data['base']) return false;
if (!$data['repo']) return false;
if (!$data['blob']) return false;
if (!$data['file']) return false;
global $ID;
global $INPUT;
if ($data['base'] == 'github.com') {
$raw = 'https://raw.githubusercontent.com/' . $data['repo'] . '/' . $data['blob'] . '/' . $data['file'];
} else {
$raw = 'https://' . $data['base'] . '/' . $data['repo'] . '/raw/' . $data['blob'] . '/' . $data['file'];
}
$url = 'https://' . $data['base'] . '/' . $data['repo'] . '/blob/' . $data['blob'] . '/' . $data['file'];
// check if there's a usable cache
$text = false;
$cache = getCacheName($raw, '.ghplugin');
$tcache = @filemtime($cache);
$tpage = @filemtime(wikiFN($ID));
if ($tcache && $tpage && !$INPUT->bool('purge')) {
$now = time();
if ($now - $tcache < ($now - $tpage) * 2) {
// use cache when it's younger than twice the age of the page
$text = io_readFile($cache);
}
}
// no cache loaded, get from HTTP
if (!$text) {
$http = new DokuHTTPClient();
$text = $http->get($raw);
if ($text) {
// save to cache
io_saveFile($cache, $text);
} else if ($tcache) {
// HTTP failed but there's an old cache - use it
$text = io_readFile($cache);
}
}
// WTF? there's nothing. we're done here
if (!$text) return true;
// apply line ranges
if ($data['from'] || $data['to']) {
$len = $data['to'] - $data['from'];
if ($len <= 0) $len = null;
$lines = explode("\n", $text);
$lines = array_slice($lines, $data['from'], $len);
$text = join("\n", $lines);
}
// add icon
list($ext) = mimetype($data['file'], false);
$class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
$class = 'mediafile mf_' . $class;
// add link
$renderer->doc .= '<dl class="file">' . DOKU_LF;
$renderer->doc .= '<dt><a href="' . $url . '" class="' . $class . '">';
$renderer->doc .= hsc($data['file']);
$renderer->doc .= '</a></dt>' . DOKU_LF . '<dd>';
if (isset($this->ext2lang[$ext])) {
$lang = $this->ext2lang[$ext];
} else {
$lang = $ext;
}
$renderer->file($text, $lang);
$renderer->doc .= '</dd>';
$renderer->doc .= '</dl>';
return true;
}
}