-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFunctions.php
More file actions
220 lines (185 loc) · 6.01 KB
/
CodeFunctions.php
File metadata and controls
220 lines (185 loc) · 6.01 KB
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
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Hook to parse <snippet>...</snippet> tags into pretty code snipets.
*
* @author Dustin Frisch <dustin.frisch@gmail.com>
* @copyright Copyright © 2010 Dustin Frisch
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 3.0 or later
*/
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
require_once "geshi.php";
$wgHooks['ParserFirstCallInit'][] = 'codeFunctions_Setup';
$wgHooks['ParserAfterTidy'][] = 'codeFunctions_Insert';
$wgExtensionCredits['parserhook'][] = array(
'version' => '0.0.1',
'description' => 'Provides pretty code snipets in the wikitext',
'name' => 'CodeFunctions',
'author' => 'Dustin Frisch'
);
$markers = array();
function codeFunctions_Setup(&$parser) {
$parser->setHook('snippet', 'codeFunctions_Render');
return true;
}
function codeFunctions_Render($input, $args, $parser, $frame) {
global $markers;
// Read arguments
$prefix = (isset($args['prefix']))
? $args['prefix']
: null;
$prefix1 = (isset($args['prefix1']))
? $args['prefix1']
: $prefix;
$prefix2 = (isset($args['prefix2']))
? $args['prefix2']
: $prefix;
$target1 = (isset($args['target1']))
? $args['target1']
: null;
$target2 = (isset($args['target2']))
? $args['target2']
: null;
$language = (isset($args['language']))
? $args['language']
: null;
// Build target line
if ($target1 != null && $target2 != null) {
$target = $target1 . ' @ ' . $target2;
} else if ($target1 != null) {
$target = $target1;
} else if ($target2 != null) {
$target = $target2;
} else {
$target = null;
}
// Parsing tags in arguments and lines
$target = $parser->recursiveTagParse($target, $frame);
$prefix1 = $parser->recursiveTagParse($prefix1, $frame);
$prefix2 = $parser->recursiveTagParse($prefix2, $frame);
$language = $parser->recursiveTagParse($language, $frame);
$input = $parser->replaceVariables($input, $frame);
// Strip leading and tailing spaces
$input = trim($input);
// Split input in lines for indention
$lines = preg_split('/[\\n\\r]/', $input);
// Apply syntax hilighting if requested
if ($language) {
$geshi = new GeSHi($input, $language);
$geshi->set_header_type(GESHI_HEADER_NONE);
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
$code = $geshi->parse_code();
} else {
$code = '';
for ($i = 0; $i < count($lines); $i++) {
$code .= preg_replace('/ /', ' ', $lines[$i]);
$code .= '<br />';
}
}
// Build output
$result = '';
$result .= '<div class="snippet">';
$result .= '<table class="snippet" cellpadding="0" cellspacing="0" style="';
$result .= ' font-family : \'Bitstream Vera Sans Mono\', Courier, monospace;';
$result .= ' border-style : solid;';
$result .= ' border-width : 1px;';
$result .= ' border-color : #ECECEC;';
$result .= '">';
// Render header
if ($target) {
$result .= '<tr><th class="snippet" colspan="3" style="';
$result .= ' padding-left : 3px;';
$result .= ' border-bottom-style : solid;';
$result .= ' border-bottom-width : 1px;';
$result .= ' border-bottom-color : #DDDDDD;';
$result .= ' background-color : #ECECEC;';
$result .= ' text-align : left;';
$result .= ' font-weight : normal;';
$result .= '">';
$result .= $target;
$result .= '</th></tr>';
}
$result .= '<tr>';
// Render line number
$result .= '<td><div class="snippet line_nr" style="';
$result .= ' display : block;';
$result .= ' whitespace : pre;';
$result .= ' line-height : 1.5em;';
$result .= ' padding-left : 0.5em;';
$result .= ' padding-right : 0.5em;';
$result .= ' margin : 0;';
$result .= ' text-align : right;';
$result .= ' color : #AAAAAA;';
$result .= ' background-color : #ECECEC;';
$result .= ' border-right-style : solid;';
$result .= ' border-right-width : 1px;';
$result .= ' border-right-color : #DDDDDD;';
$result .= '">';
for ($i = 0; $i < count($lines); $i++) {
$result .= '' . $i . '<br />';
}
$result .= '</td>';
// Render prefix
$result .= '<td><div class="snippet prefix" style="';
$result .= ' display : block;';
$result .= ' whitespace : pre;';
$result .= ' color : #AAAAAA;';
$result .= ' line-height : 1.5em;';
$result .= ' padding-left : 0.5em;';
$result .= ' padding-right : 0.5em;';
$result .= ' margin : 0;';
$result .= '">';
for ($i = 0; $i < count($lines); $i++) {
if (substr($lines[$i], 0, 2) != ' ') {
$result .= $prefix1;
} else {
$result .= $prefix2;
}
$result .= '<br />';
}
$result .= '</td>';
// Render lines
$result .= '<td width="100%"><div class="snippet line" style="';
$result .= ' display : block;';
$result .= ' whitespace : pre;';
$result .= ' line-height : 1.5em;';
$result .= ' margin : 0;';
$result .= '"> ';
$result .= $code;
$result .= '</td>';
$result .= '</tr></table>';
$result .= '</div>';
// Build marker output
$output = 'code_functions_marker_' . count($markers) .'_code_functions';
// Store result in marker
$markers[] = $result;
// Output marker
return $output;
}
function codeFunctions_Insert($parser, &$text) {
global $markers;
// Build list of keys
$keys = array();
for ($i = 0; $i < count($markers); $i++) {
$keys[] = 'code_functions_marker_' . $i .'_code_functions';
}
// Replace markers with output
$text = str_replace($keys, $markers, $text);
return true;
}
?>