File: make_man.php

package info (click to toggle)
phpdoc 20020310-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 35,272 kB
  • ctags: 354
  • sloc: xml: 799,767; php: 1,395; cpp: 500; makefile: 200; sh: 140; awk: 51
file content (177 lines) | stat: -rwxr-xr-x 6,250 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
#!/usr/local/bin/php -q
<?php

/*
 * Script to convert (most of the) php documentation from docbook to unix man format.
 * Roel Vanhout - roel@2e-systems.com - 20010413
 * No long license statements here - do whatever you want.
 */

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

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

if(!is_dir($lang)) {
  if(is_dir("../$lang")) {
	  $lang="../$lang";
		$outdir="../$outdir";
	} else {
	  die("language '$lang' not found");
	}
}
$file = `cat \`find $lang | grep .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'] = $matches[1];
    } else {
        $functions[$i]['shortdesc'] = '';
    }

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

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

    preg_match_all('/<paramdef>.*?<\/paramdef>/s', $funcprototype, $matches);
    $first = 1;
    foreach($matches[0] as $param) {
        if(preg_match('/<optional>.*<\/optional>/s', $param)) {
            if($first != 1 ) {
                $functions[$i]['prototype'] .= ' [, ' . trim(preg_replace('/<.*?>/s', '', $param)) . ']';
            } else {
                $functions[$i]['prototype'] .= ' [' . trim(preg_replace('/<.*?>/s', '', $param)) . ']';
                $first = 0;
            }
        } else {
            if($first != 1 ) {
                $functions[$i]['prototype'] .= ', ';
            } else {
                $first = 0;
            }
            $functions[$i]['prototype'] .= trim(preg_replace('/<.*?>/s', '', $param));
        }
    }
    $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];
            // 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']);
        } 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 = fopen($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);
    }
}


?>