File: make_man.php

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (237 lines) | stat: -rwxr-xr-x 8,049 bytes parent folder | download
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
#!/usr/local/bin/php -q
<?php
/*  
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 of the PHP license,       |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_0.txt.                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors:    Roel Vanhout <roel@2e-systems.com>                       |
  |             Derick Rethans <derick@php.net>                          |
  +----------------------------------------------------------------------+
 
  $Id: make_man.php 293138 2010-01-05 10:21:11Z rquadling $
*/

/*
 * Script to convert (most of the) php documentation
 * from docbook to unix man format.
 * */

/*
 * Problems:
 * - examples are not shown correctly
 */

$lang = 'en';
$extension = '';
$outdir = 'man7';

set_time_limit (0);
error_reporting (E_ALL & !E_NOTICE);

if(!is_dir($lang)) {
	if(is_dir("../$lang")) {
		$lang="../$lang";
		$outdir="../$outdir";
	} else {
		die("language '$lang' not found");
	}
}

if (empty ($extension)) {
	$file = `find $lang | fgrep .xml | xargs cat`;
} else {
	$file = `cat {$lang}/functions/{$extension}.xml`;
}
#$file = str_replace("\n", '', $file);

// First get everything in <refentry></refentry> tags
preg_match_all('/<refentry.*?<\/refentry>/s', $file, $refentries);

$functions = array();
$i = 0;

foreach($refentries[0] as $refentry) {
	preg_match('/<refname>(.*)<\/refname>/s', $refentry, $matches);
	if(!empty($matches[1])) {
		$functions[$i]['name'] = $matches[1];
	} else {
		$functions[$i]['name'] = '';
	}

	preg_match('/<refpurpose>(.*)<\/refpurpose>/s', $refentry, $matches);
	if(!empty($matches[1])) {
		$functions[$i]['shortdesc'] = str_replace ("\n", ' ', $matches[1]);
	} else {
		$functions[$i]['shortdesc'] = '';
	}
	$functions[$i]['shortdesc'] = preg_replace('/\s{2,}/s', ' ', $functions[$i]['shortdesc']);
	$functions[$i]['shortdesc'] = preg_replace('/^\s*/', '', $functions[$i]['shortdesc']);
	$functions[$i]['shortdesc'] = preg_replace('/\s*$/', '', $functions[$i]['shortdesc']);

	preg_match('/<methodsynopsis>(.*)<\/methodsynopsis>/s', $refentry, $matches);
	if(!empty($matches[1])) {
		$funcprototype_all = $matches[0];
		$funcprototype = $matches[1];
	} else {
		$funcprototype_all = '';
		$funcprototype = '';
	}

	preg_match('/<methodsynopsis>.*<type>(.*?)<\/type>/s', $funcprototype_all, $matches);
	if(!empty($matches[1])) {
		$functions[$i]['prototype'] = $matches[1]. ' ';
	} else {
		$functions[$i]['prototype'] = '';
	}

	preg_match('/<methodname>(.*)<\/methodname>/s', $funcprototype, $matches);
	if(!empty($matches[1])) {
		$functions[$i]['prototype'] .= preg_replace('/<.*?>/s', '', $matches[1]);
		$functions[$i]['prototype'] .= '(';
	} else {
		$functions[$i]['prototype'] = '';
	}

	preg_match_all('/<methodparam(.*?)>.*?<\/methodparam>/s', $funcprototype, $matches);
	$first = 1;

	foreach($matches[0] as $param) {
		/* Get type and name */
		$do_var = 0;
		if (preg_match ('/<type>(.*?)<\/type>(.*?)<parameter>(.*?)<\/parameter>/s', $param, $matches)) {
			$data = $matches[1]. ' '. $matches[3];
			$do_var = 1;
		} else if (preg_match ('/<parameter>(.*?)<\/parameter>/s', $param, $matches)) {
			$data = $matches[1];
			$do_var = 1;
		} 

		if ($do_var) {
			if(preg_match('/<methodparam choice="opt">.*<\/methodparam>/s', $param)) {
				if($first != 1 ) {
					$functions[$i]['prototype'] .= ' [, ' . $data . ']';
				} else {
					$functions[$i]['prototype'] .= ' [' . $data . ']';
					$first = 0;
				}
			} else {
				if($first != 1 ) {
					$functions[$i]['prototype'] .= ', ';
				} else {
					$first = 0;
				}
				$functions[$i]['prototype'] .= $data;
			}
		}
	}
	$functions[$i]['prototype'] = preg_replace('/\n/', '', $functions[$i]['prototype']);
	$functions[$i]['prototype'] = preg_replace('/\s{2,}/s', ' ', $functions[$i]['prototype']);
	$functions[$i]['prototype'] .= ')';

	$y = 0;
	preg_match_all('/<para>.*?<\/para>/s', $refentry, $matches);
	foreach($matches[0] as $paragraph) {
		if(preg_match('/<example>/s', $paragraph)) {
			// If this paragraph has an example, do some special formatting.
			preg_match('/<title>(.*)<\/title>/s', $paragraph, $tmp);
			$functions[$i]['example'] = $tmp[1];
			$functions[$i]['example'] = preg_replace('/\s{2,}/', ' ', $functions[$i]['example']);
			$functions[$i]['example'] = preg_replace('/<.*?>/', '', $functions[$i]['example']);
			$functions[$i]['example'] .= "\n\n";
			preg_match('/<programlisting.*?>(.*)<\/programlisting>/s', $paragraph, $tmp);

			$programlisting = $tmp[1];

			/* Remove CDATA stuff */
			$programlisting = preg_replace ("/<!\[CDATA\[\n?/", "", $programlisting);
			$programlisting = preg_replace ("/]]>\n?/", "", $programlisting);

			// Hmm, no function for this?
			$programlisting = str_replace('&lt;', '<', $programlisting);
			$programlisting = str_replace('&gt;', '>', $programlisting);
			$programlisting = str_replace('&quot;', '"', $programlisting);
			$programlisting = str_replace('&amp;', '&', $programlisting);
			$programlisting = str_replace('&nbsp;', ' ', $programlisting);
			$programlisting = str_replace('&sp;', ' ', $programlisting);
			$programlisting = str_replace('&amp;', '&', $programlisting);
			#$functions[$i]['example'] .= `echo '$programlisting' | indent -kr` . "\n\n";
			$functions[$i]['example'] .= $programlisting;
		} elseif(preg_match('/See also/s', $paragraph))  {
			$functions[$i]['seealso'] = preg_replace('/<.*?>/', '', $paragraph);
			$functions[$i]['seealso'] = preg_replace('/See also:[ ]*/', '', $functions[$i]['seealso']);
			$functions[$i]['seealso'] = preg_replace('/\s{2,}/', ' ', $functions[$i]['seealso']);
			$functions[$i]['seealso'] = preg_replace('/^\s*/', '', $functions[$i]['seealso']);
			$functions[$i]['seealso'] = preg_replace('/\./', '', $functions[$i]['seealso']);
		} else {
			// Nothing special, just put it in.
			$functions[$i]['paragraph'][$y] = preg_replace('/<.*?>/', '', $paragraph);
			$functions[$i]['paragraph'][$y] = preg_replace('/\s{2,}/', ' ', $functions[$i]['paragraph'][$y]);
		}
		$y++;
	}

	$i++;
}

/*
 * We have an array now with all the data, now write it to seperate files.
 */

if(!file_exists($outdir)) {
	umask(0000);
	mkdir($outdir, 0755);
}

foreach($functions as $function) {
	if(function_exists('gzwrite')) {
		$fp = gzopen($outdir . '/php_' . $function['name'] . '.man.gz',  'w');
	} else {
		$fp = fopen($outdir . '/php_' . $function['name'] . '.man',  'w');
	}
	/*
	$function['name']
	$function['shortdesc']
	$function['prototype']
	$function['paragraph'][$y] // Array
	$function['seealso']
	$function['example']
	*/

	$page = '.TH ' . $function['name'] . " 7  \"" . date("j F, Y") . "\" \"PHPDOC MANPAGE\" \"PHP Programmer's Manual\"\n.SH NAME\n" . 
			$function['name'] . "\n.SH SYNOPSIS\n.B " . $function['prototype'] . "\n.SH DESCRIPTION\n" . $function['shortdesc'] . ".\n";
	if(!empty($function['paragraph']) && count($function['paragraph']) > 0) {
		foreach($function['paragraph'] as $para) {
			$page .= ".PP\n";
			$page .= trim($para) . "\n";
		}
	}

	if(!empty($function['example'])) {
		$page .= ".SH \"EXAMPLE\"\n";
		$page .= $function['example'] . "\n";
	}

	if(!empty($function['seealso'])) {
		$page .= ".SH \"SEE ALSO\"\n";
		$page .= $function['seealso'] . "\n";
	}

	if(function_exists('gzwrite')) {
		gzwrite($fp, $page);
	} else {
		fwrite($fp, $page);
	}
}


?>