-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphocco.php
412 lines (341 loc) · 12.2 KB
/
phocco.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
// **Phocco** is a PHP port of [Docco](http://jashkenas.github.com/docco/ ):
// the original quick-and-dirty, hundred-line-long, literate-programming-style
// documentation generator, with inspiration from
// [Pycco](http://fitzgen.github.com/pycco/) (for Python)
// and [Rocco](http://rtomayko.github.com/rocco/) (for Ruby).
// It produces HTML that displays your comments
// alongside your code. Comments are passed through
// [Markdown](http://daringfireball.net/projects/markdown/syntax), and code is
// passed through [Pygments](http://pygments.org/) syntax highlighting. This
// page is the result of running Phocco against its [own source file](http://github.com/rileydutton/phocco/blob/master/phocco.php).
//
// In addition to Docco's basic features, Phocco has basic support for PHPDocumentor-style docblocks. It can also be used on almost
// any server with PHP 5, since it can fall back to a web service if your server doesn't have Pygments installed.
//
// To run from the command-line (must have PHP CLI installed):
//
// php phocco.php -o docs *.php
//
// ...will generate linked HTML documentation for the named source files, saving
// it into a `docs` folder.
//
// There may be support added in the future for dynamic on-the-fly documentation generation (for example, uploading Phocco to a webserver).
// Contact me and let me know if that is something you would use.
### Requirements
//
// All external dependencies are included in the distribution under the `external` folder.
//
//We need the [PHP-Markdown](http://michelf.com/projects/php-markdown/) library to process the syntax for the documentation portions.
require("external/php-markdown/markdown.php");
//Some simple command-line parsing
require("external/class.args.php");
//
//In addition, we require Pygments, a Python syntax highlighting library. If your server/computer doesn't have Pygments installed,
//we will fall back to a webservice, although obviously this will be slower.
### Main Functionality
/**
* Generate the documentation. The "main loop", so to speak.
*
* @param string $file (the filename)
* @return void
*/
function generate_documentation($file) {
$code = file_get_contents($file);
$sections = parse($file, $code);
$sections = highlight($file, $sections);
generate_html($file, $sections);
}
/**
* Take each source file, and turn it into a series of sections of corresponding code blocks and documentation blocks
*
* Given a string of source code, parse out each comment and the code that follows it, and create an individual section for it.
* Sections take the form:
*
* {
* "docs_text": ...,
* "docs_html": ...,
* "code_text": ...,
* "code_html": ...,
* "num": ...
* }
* @param string $source (the filename)
* @param string $code (the actual contents of the file)
* @return array $sections
*/
function parse($source, $code) {
$lines = explode("\n", $code);
$sections = array();
//Determine the language based on the extension of the file.
$language = get_language($source);
//`true` when we are "inside" of a code block, `false` otherwise.
$has_code = false;
$docs_text = $code_text = "";
//Check to make sure we aren't treating a bash directive as a comment
if(substr($lines[0], 0, 2) == "#!")
array_pop($lines);
//For tracking docblock issues.
$prev_type = "";
$type = "";
$docblock_function_name = "";
$first_listing = false;
foreach($lines as $line_num=>$line) {
//This is a check added since we support multiple comment symbols for each language. `true` if any of them have matched.
$found_symbol = false;
//Support for PHPDocumentor-style Docblocks
$docblockmatcher = array("/\*\*", "\*");
foreach(array_merge($docblockmatcher, $language["symbols"]) as $symbol) {
//See if our symbol is found on this line.
if(preg_match("|^\s*" . $symbol . "|", $line)) {
//Symbol has been found, but are we in a code block? If so, end this **section**
if($has_code) {
$sections[] = array(
"docs_text" => $docs_text,
"code_text" => $code_text
);
$has_code = false;
$docs_text = $code_text = '';
}
//If we are in a docblock, we have special handling to try and take PHPDocumentor @properties and turn them into something nicer. We are basically trying to produce something resembling literate-style documentation based on the docblock.
if($in_docblock) {
$matches == array();
//See if we have an @property on this line.
preg_match_all("/\s@(.*?)\s(.*)/", $line, $matches);
if(count($matches[0]) != 0) {
$e = explode(" ", $matches[2][0], 3);
//It's a @param!
if($matches[1][0] == "param") {
$line = "`" . $e[0] . " " . $e[1] . "` " . $e[2];
$type = "param";
$header = "Takes ";
}
//It's a @return!
else if($matches[1][0] == "return") {
$line = "`" . $e[0] . " " . $e[1] . "` " . $e[2];
$type = "return";
$header = "Returns ";
}
//It's something we don't know how to handle...
else {
$type = "";
$header = "";
}
if($prev_type != $type)
$first = true;
else
$first = false;
if(!$first) {
$line = " and " . $line;
}
if($first) {
if($prev_type != "")
$header = ". " . $header;
$line = $header . $line;
}
$prev_type = $type;
}
else {
$type = "";
$header = "";
}
}
$line = preg_replace("|^\s*" . $symbol . "|", "", $line) . "\n";
//Determine if we are starting a docblock.
if($symbol == $docblockmatcher[0]) {
$in_docblock = true;
//Skip ahead until we find the name of this function, if possible.
$fmatch = array();
for($k = $line_num; $k < count($lines); $k++) {
if(preg_match("/function\s(.*)\(/", $lines[$k], $fmatch)) {
$docblock_function_name = $fmatch[1];
$line = "**` " . $docblock_function_name . "`**\n\n ";
break;
}
//We've encountered the next docblock without finding a function. Abort.
if($line == "/**\n")
break;
$docblock_function_name = "";
}
}
//Are we ending a docblock?
else if($symbol == $docblockmatcher[1]) {
if($line == "/\n") {
$in_docblock = false;
$line = ".";
$prev_type = "";
}
}
$docs_text .= $line;
$found_symbol = true;
break;
}
}
//If `found_symbol` is false, then this must be the beginning (or a continution) of a code block.
if(!$found_symbol) {
$has_code = true;
$code_text .= $line . "\n";
}
}
$sections[] = array(
"docs_text" => $docs_text,
"code_text" => $code_text
);
return $sections;
}
/**
* Highlight each section, using Pygments for the code, and Markdown for the documentation.
*
* @param string $filename
* @param array $sections
* @return array $sections
*/
function highlight($filename, $sections) {
$language = get_language($filename);
$code = "";
foreach($sections as $section) {
$code .= $section["code_text"] . $language["divider_text"];
}
//Use the pygmentize command if it's available.
$cmd = "pygmentize -l " . $language["name"] . " -f html";
// stdin, stdout, stderror
$spec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($cmd, $spec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $code);
fclose($pipes[0]);
//Check to see if we were actually able to use pygmentize
$errors = stream_get_contents($pipes[2]);
fclose($pipes[2]);
if(strstr($errors, "command not found")) {
//Pygmentize isn't available, fall back on the web service.
print("Using webservice...\n");
//Use the excellent webservice provided by [Flowcoder](http://flowcoder.com/) to give us access to Pygments even
//though we don't have it installed.
$postdata = http_build_query(
array(
'code' => $code,
'lang' => $language["name"]
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
//We use `file_get_contents` instead of cURL because it's more likely to be installed by default.
$results = file_get_contents('http://pygments.appspot.com/', false, $context);
}
else {
//We were able to use the pygmentize command on the local machine.
print("Using pygmentize...\n");
$results = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
}
}
$highlight_start = "<div class=\"highlight\"><pre>";
$highlight_end = "</pre></div>";
$fragments = preg_split($language["divider_html"], $results);
//Process the code and documentation for each section.
foreach($sections as $i=>$section) {
$sections[$i]["code_html"] = $highlight_start . $fragments[$i] . $highlight_end;
$sections[$i]["docs_html"] = Markdown($sections[$i]["docs_text"]);
$sections[$i]["num"] = $i;
}
return $sections;
}
/**
* Generate our final HTML file based on the processing we've done. Write out the HTML file.
*
* @param string $filename
* @param array $sections
* @return void
*/
function generate_html($filename, $sections) {
global $sources;
global $options;
if(!is_dir($options["outputdir"]))
mkdir($options["outputdir"]);
$e = explode(".", $filename);
$basename = $e[0];
$title = $basename;
ob_start();
include("template.php");
$compiled = ob_get_clean();
$file = fopen($options["outputdir"] . "/" . $basename . ".html", "w") or die("Coudn't open output file!");
fwrite($file, $compiled);
fclose($file);
}
### Configuration & Setup
/**
* A list of the languages that Phocco supports, which is a subset of the languages Pygments supports.
*
* Add more languages here!
*/
global $languages;
$languages = array(
".php" => array(
"name" => "php",
"symbols" => array(
"#",
"//"
)
),
".py" => array(
"name" => "python",
"symbols" => array(
"#",
)
),
);
/**
* Get the name and symbols of the language we're working with, based on the file extension.
*
* @param string $filename
* @return array $language
*/
function get_language($filename) {
global $languages;
$e = explode(".", $filename);
$extension = "." . $e[count($e) - 1];
if(!isset($languages[$extension])) {
print("Could not determine language for extension $extension.\n");
die();
}
$l = $languages[$extension];
// The dividing token we feed into Pygments, to delimit the boundaries between
// sections.
$l["divider_text"] = "\n" . $l["symbols"][0] . "DIVIDER\n";
// The mirror of `divider_text` that we expect Pygments to return. We can split
// on this to recover the original sections.
$l["divider_html"] = '/\n*?<span class="c[1]?">' . $l["symbols"][0] . 'DIVIDER<\/span>\n*?/s';
return $l;
}
global $options;
global $sources;
$sources = array();
$args = new Args();
//Default options
$options = array(
"outputdir" => "docs",
);
if($outputdir = $args->flag("o"))
$options["outputdir"] = $outputdir;
if(count($args->args) == 0) {
print("You must supply a filename to continue.\n");
}
foreach($args->args as $filename) {
$e = explode(".", $filename);
$basename = $e[0];
$sources[] = $basename;
}
foreach($args->args as $filename) {
generate_documentation($filename);
}