This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.php
394 lines (351 loc) · 9.95 KB
/
generate.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/*
This is a script that will create snipMate snippets for Drupal to be used with Vim.
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/>.
*/
if (!isset($_SERVER['argv'][1])) {
print "Usage: php generate.php [path to drupal]\n"; exit;
}
$api = rtrim($_SERVER['argv'][1], '/');
// Which files are we intersted in.
$files = '/\.(inc|module|php)$/';
// Special cases that we will want to place our tabstops at first.
$keywords = array(
'HOOK',
'FORM_ID',
'MODULE',
'DELTA',
'N',
);
// Keeps a record of the functions/hooks that have been processed so that they are
// not duplicated.
$functions = $hooks = array();
$code_message = 'Your code here';
/**
* @TODO
*
* Had a long informative message here but wasn't committed
* and acctidenally deleted. I will put back soon.
*/
if (is_file('./extra/extra_snippets.php')) {
require_once './extra/extra_snippets.php';
}
// Create snippets.
if (!is_dir('snippets')) {
mkdir('snippets');
}
chdir('snippets');
// Create snippets/drupal.
/*
if (!is_dir('drupal')) {
mkdir('drupal');
}
chdir('drupal');
*/
// Do API files first so that there are not duplicate hooks().
recurse($api, TRUE);
// Do EVERYTHING else.
recurse($api);
// Add custom snippets.
custom_snippets();
/**
* Recurse the given directory for source code files.
*
* @param string $dir
* The directory to recursively process.
* @param bool $hook
* If set to TRUE ONLY process hook_functions.
*/
function recurse($dir, $hook = FALSE) {
global $files;
$handle = opendir($dir);
while ($file = readdir($handle)) {
$path = $dir .'/'. $file;
if (is_dir($path) && $file[0] != '.') {
recurse($path, $hook);
}
if (preg_match($files, $file)) {
// We only ever want to do api files once.
if ($hook && preg_match('/\.(api\.php)$/', $file)) {
parse(file_get_contents($path), $file);
}
elseif (!$hook) {
parse(file_get_contents($path), $file);
}
}
}
closedir($handle);
}
/**
* Parse the given data for functions.
*
* @param $data
* The contents of the file to be processed.
*/
function parse($data, $file) {
global $functions;
// Match functions.
preg_match_all('/(?:void|bool|boolean|float|int|resource|string|mixed|array|object|function) +([A-Za-z0-9_]+)(\([^{\n]+) \{/', $data, $matches, PREG_SET_ORDER);
if (!empty($matches)) {
write_snippet('# ' . strtoupper($file));
}
foreach ($matches as $func) {
// Don't include constructs, private functions, or theme functions.
if (!preg_match('`^__`', $func[1]) && !preg_match('`^_`', $func[1]) && !preg_match('`theme_`', $func[1])) {
if (!array_key_exists($func[1], $functions)) {
list($process, $hook_func) = check_function($func);
// Only process functions that have are allowed.
if ($process) {
// Get and write the snippet.
if ($snippet = snippet($func, $hook_func)) {
print $func[1] . $func[2] ."\n";
write_snippet('snippet ' . $func[1] . PHP_EOL . $snippet);
}
$functions[$func[1]] = $func[1];
}
}
}
}
}
/**
* Writes to the snippet file.
*
* @param string $snippet
*
*/
function write_snippet($snippet) {
$f = fopen('./drupal.snippets', 'a+');
fwrite($f, $snippet . "\n");
fclose($f);
}
/**
* Checks the function to see if it should be process and if it's a hook.
*
* @param array $func
* The contents of the function.
*
* @return array
*/
function check_function($func) {
global $hooks;
// All functions should be processed unless otherwise noted.
$process = TRUE;
// Check to see if the current function is a hook.
$hook_func = preg_match('`^hook_`', $func[1]);
if (!$hook_func) {
switch ($func[1]) {
// Do not process theme functions.
case 'theme':
if (!strpos($func[2], '$hook')) {
$process = FALSE;
break;
}
default:
// Check if the current function is a hook implementation,
// if it is then do not process it.
foreach ($hooks as $hook) {
if (preg_replace('`^([A-Za-z]*)_`', '', $func[1]) == $hook) {
$process = FALSE;
}
}
}
}
// Record hook functions so that implementations are not processed.
if ($hook_func) {
$hook = str_replace('hook_', '', $func[1]);
$hooks[$hook] = $hook;
}
return array($process, $hook_func);
}
/**
* Generate a snippet for the given function.
*
* @param $func
* The contents of the function.
* @param $hook_func
* Determines if the function being processed is a hook or not.
*
* @return string
*/
function snippet($func, $hook_func) {
if ($hook_func) {
return process_hook_function($func);
}
return process_function($func);
}
/**
* Processes a hook function.
*
* @param array $func
* The contents of the function.
*
* @return string
*/
function process_hook_function($func) {
global $keywords;
$tabstop = 1;
// Set the name of the function.
$snippet_name = $func_name = $func[1];
$func[1] = '`Filename()`'. substr($func[1], 4);
// Replace keywords with tabstops.
foreach ($keywords as $keyword) {
if (strpos($func_name, $keyword)) {
$func_name = str_replace($keyword, '${' . $tabstop . ':' . $keyword . '}', $func_name);
$tabstop++;
$func[1] = str_replace($keyword, '${' . $tabstop . ':' . $keyword . '}', $func[1]);
$tabstop++;
}
}
// Get function expansion.
$expansion = expand_hook_function($snippet_name, $tabstop);
return <<<DOC
\t/**
\t * Implementation of $func_name().
\t */
\tfunction $func[1]$func[2] {
\t$expansion
\t}
DOC;
}
/**
* Processes a non-hook function.
*
* @aram array $func
* The contents of the function.
*
* @return string
*/
function process_function($func) {
$tabstop = 1;
// Set the name of the function.
$func_name = $func[1];
// Create an array of arguments.
$arguments = explode(', ', $func[2]);
// Get the last element key.
$last = count($arguments)-1;
$args = array();
foreach ($arguments as $key => $argument) {
$argument = trim($argument);
// Strips out the parenthese from the beginning and end of the arguments.
switch ($key) {
case 0:
$argument = substr($argument, 1);
if ($key != $last) { break; }
case $last:
$argument = substr($argument, 0, -1);
break;
}
if ($argument != '') {
// Replace arguments with tabstops.
$args[] .= '${' . $tabstop . ':' . $argument . '}';
$tabstop++;
}
else {
return;
}
}
$func[2] = '(' . implode(', ', $args) . ')';
return "\t$func[1]$func[2]\${{$tabstop}}";
}
/**
* Expand hook functions.
*
* @param string $func_name
* The name of the function.
* @param int $tabstop
* The current tabstop position.
*
* @return string
*/
function expand_hook_function($func_name, $tabstop) {
global $code_message;
$exp = array();
switch ($func_name) {
case 'hook_help':
$exp[] = 'switch ($path) {';
$exp[] = " case '\${" . $tabstop++ . ":path}':";
$exp[] = " return '<p>' . t('\${" . $tabstop++ . ":/* Text */}') . '</p>'";
$exp[] = ' break;';
$exp[] = '}';
break;
case 'hook_menu':
$first_tabstop = $tabstop++;
$exp[] = '$${' . $first_tabstop . ':items} = array();';
$exp[] = '';
$exp[] = '// Put your menu items here.';
$exp[] = "$${$first_tabstop}['\${" . $tabstop++ . ":path}'] = array(";
$exp[] = ' ${' . $tabstop++ . ":/* $code_message */}";
$exp[] = ');';
$exp[] = '';
$exp[] = "return $${$first_tabstop};";
break;
case 'hook_permission':
$exp[] = 'return array(';
$exp[] = " 'title' => t('\${" . $tabstop++ . ":/* Title */}')";
$exp[] = " 'description' => t('\${" . $tabstop++ . ":/* Text */}')";
$exp[] = ');';
break;
case 'hook_theme':
$exp[] = '${' . $tabstop++ . ':theme_function} = array(';
$exp[] = " 'arguments' => array(\${" . $tabstop++ . ':/* Theme function arguments */}),';
$exp[] = ' ${' . $tabstop++ . ':/* See http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_theme/7 for options */}';
$exp[] = ' ),';
$exp[] = ');';
break;
case 'hook_update_N':
$exp[] = '$ret = array();';
$exp[] = '${' . $tabstop++ . ":/* $code_message */}";
$exp[] = 'return $ret';
break;
case 'hook_user_operations':
$exp[] = '$operations = array(';
$exp[] = "'\${" . $tabstop++ . ":operation}' => array(";
$exp[] = " 'label' => t('\${" . $tabstop++ . ": /* Label */}')";
$exp[] = " 'callback' => t('\${" . $tabstop++ . ":callback}')";
$exp[] = 'return $operations;';
default:
// @TODO: Create a way for people to tie into this.
$exp[] = '${' . $tabstop++ . ":/* $code_message */}";
break;
}
if (!empty($exp)) {
ksort($exp);
// Add spaces to all code.
foreach ($exp as $key => $value) {
if ($value != '') {
$exp[$key] = ' ' . $exp[$key];
}
}
return implode(PHP_EOL . "\t", $exp);
}
return FALSE;
}
/**
*
*/
function form_elements() {
}
/**
*
*/
function custom_snippets() {
write_snippet('# Custom snippets');
$snips = array();
$snips['**'][] = '/**';
$snips['**'][] = ' * ${1:Your documentation}';
$snips['**'][] = ' */';
// @TODO Get extra snipps.
foreach ($snips as $key => $value) {
print $key . PHP_EOL;
write_snippet("snippet $key" . PHP_EOL . "\t" . implode(PHP_EOL . "\t", $value));
}
}