5 * This file is part of Psy Shell.
7 * (c) 2012-2017 Justin Hileman
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 define('WRAP_WIDTH', 100);
17 if (count($argv) !== 3 || !is_dir($argv[1])) {
18 echo "usage: build_manual path/to/manual output_filename.db\n";
22 function htmlwrap($text, $width = null)
24 if ($width === null) {
35 switch (substr($text, $i, 1)) {
37 $return[] = trim(substr($text, 0, $i));
38 $text = substr($text, $i);
66 if (!$inTag && ($i - $tagWidth > $width)) {
67 $lastSpace = $lastSpace ?: $width;
69 $return[] = trim(substr($text, 0, $lastSpace));
70 $text = substr($text, $lastSpace);
77 $return[] = trim($text);
79 return implode("\n", $return);
82 function extract_paragraphs($element)
84 $paragraphs = array();
85 foreach ($element->getElementsByTagName('para') as $p) {
87 foreach ($p->childNodes as $child) {
88 // @todo: figure out if there's something we can do with tables.
89 if ($child instanceof DOMElement && $child->tagName === 'table') {
93 // skip references, because ugh.
94 if (preg_match('{^\s*&[a-z][a-z\.]+;\s*$}', $child->textContent)) {
98 $text .= $child->ownerDocument->saveXML($child);
101 if ($text = trim(preg_replace('{\n[ \t]+}', ' ', $text))) {
102 $paragraphs[] = $text;
106 return implode("\n\n", $paragraphs);
109 function format_doc($doc)
113 if (!empty($doc['description'])) {
114 $chunks[] = '<comment>Description:</comment>';
115 $chunks[] = indent_text(htmlwrap(thunk_tags($doc['description']), WRAP_WIDTH - 2));
119 if (!empty($doc['params'])) {
120 $chunks[] = '<comment>Param:</comment>';
122 $typeMax = max(array_map(function ($param) {
123 return strlen($param['type']);
126 $max = max(array_map(function ($param) {
127 return strlen($param['name']);
130 $template = ' <info>%-' . $typeMax . 's</info> <strong>%-' . $max . 's</strong> %s';
131 $indent = str_repeat(' ', $typeMax + $max + 6);
132 $wrapWidth = WRAP_WIDTH - strlen($indent);
134 foreach ($doc['params'] as $param) {
135 $desc = indent_text(htmlwrap(thunk_tags($param['description']), $wrapWidth), $indent, false);
136 $chunks[] = sprintf($template, $param['type'], $param['name'], $desc);
141 if (isset($doc['return']) || isset($doc['return_type'])) {
142 $chunks[] = '<comment>Return:</comment>';
144 $type = isset($doc['return_type']) ? $doc['return_type'] : 'unknown';
145 $desc = isset($doc['return']) ? $doc['return'] : '';
147 $indent = str_repeat(' ', strlen($type) + 4);
148 $wrapWidth = WRAP_WIDTH - strlen($indent);
151 $desc = indent_text(htmlwrap(thunk_tags($doc['return']), $wrapWidth), $indent, false);
154 $chunks[] = sprintf(' <info>%s</info> %s', $type, $desc);
158 array_pop($chunks); // get rid of the trailing newline
160 return implode("\n", $chunks);
163 function thunk_tags($text)
166 'parameter>' => 'strong>',
167 'function>' => 'strong>',
168 'literal>' => 'return>',
170 'constant>' => 'info>',
175 '&true;' => '<return>true</return>',
176 '&false;' => '<return>false</return>',
177 '&null;' => '<return>null</return>',
180 return strtr(strip_tags(strtr($text, $tagMap), '<strong><return><info>'), $andBack);
183 function indent_text($text, $indent = ' ', $leading = true)
185 return ($leading ? $indent : '') . str_replace("\n", "\n" . $indent, $text);
188 function find_type($xml, $paramName)
190 foreach ($xml->getElementsByTagName('methodparam') as $param) {
191 if ($type = $param->getElementsByTagName('type')->item(0)) {
192 if ($parameter = $param->getElementsByTagName('parameter')->item(0)) {
193 if ($paramName === $parameter->textContent) {
194 return $type->textContent;
202 foreach (glob($argv[1] . '/*/*/*.xml') as $function) {
203 $funcname = basename($function);
204 if ($funcname === 'main.xml' || strpos($funcname, 'entities.') === 0) {
208 $xmlstr = str_replace('&', '&', file_get_contents($function));
210 $xml = new DOMDocument();
211 $xml->preserveWhiteSpace = false;
213 if (!@$xml->loadXml($xmlstr)) {
214 echo "XML Parse Error: $function\n";
219 $refsect1s = $xml->getElementsByTagName('refsect1');
220 foreach ($refsect1s as $refsect1) {
221 $role = $refsect1->getAttribute('role');
224 $doc['description'] = extract_paragraphs($refsect1);
226 if ($synopsis = $refsect1->getElementsByTagName('methodsynopsis')->item(0)) {
227 foreach ($synopsis->childNodes as $node) {
228 if ($node instanceof DOMElement && $node->tagName === 'type') {
229 $doc['return_type'] = $node->textContent;
238 $doc['return'] = extract_paragraphs($refsect1);
243 $vars = $refsect1->getElementsByTagName('varlistentry');
244 foreach ($vars as $var) {
245 if ($name = $var->getElementsByTagName('parameter')->item(0)) {
247 'name' => '$' . $name->textContent,
248 'type' => find_type($xml, $name->textContent),
249 'description' => extract_paragraphs($var),
254 $doc['params'] = $params;
260 if ($purpose = $xml->getElementsByTagName('refpurpose')->item(0)) {
261 $desc = htmlwrap($purpose->textContent);
262 if (isset($doc['description'])) {
263 $desc .= "\n\n" . $doc['description'];
266 $doc['description'] = trim($desc);
269 $formatted = format_doc($doc);
270 foreach ($xml->getElementsByTagName('refname') as $ref) {
271 $docs[$ref->textContent] = $formatted;
275 if (is_file($argv[2])) {
279 $db = new PDO('sqlite:' . $argv[2]);
281 $db->query('CREATE TABLE php_manual (id char(256) PRIMARY KEY, doc TEXT)');
282 $cmd = $db->prepare('INSERT INTO php_manual (id, doc) VALUES (?, ?)');
283 foreach ($docs as $id => $doc) {
284 $cmd->execute(array($id, $doc));