-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse-docs.php
163 lines (136 loc) · 3.7 KB
/
parse-docs.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
<?php
use Gregwar\RST\Nodes\ListNode;
use Gregwar\RST\Nodes\Node;
use Gregwar\RST\Nodes\TableNode;
use Gregwar\RST\Nodes\TitleNode;
use Gregwar\RST\Parser;
error_reporting(E_ALL);
ini_set('display_errors', 'On');
require __DIR__ . '/vendor/autoload.php';
$parser = new Parser(null, new \One\Gregwar\RST\HTML\Kernel());
$parser->getEnvironment()->getErrorManager()->abortOnError(false);
// parse docs and get nodes we are interested in
$nodes = $parser->parseFile('./api.rst')->getNodes(function(Node $node){
if ($node instanceof TitleNode) {
$method = (string)$node->getValue();
if (preg_match('/^one\.(.+)\.(.+)$/', $method, $m)) {
return true;
}
}
return ($node instanceof TableNode || $node instanceof ListNode);
});
$method = null;
$methods = [];
foreach ($nodes as $node) {
if ($node instanceof TitleNode) {
$method = (string)$node->getValue();
}
if ($method) {
if (!array_key_exists($method, $methods)) {
$methods[$method] = [
'desc' => '',
'table' => [],
];
}
}
if ($method && $node instanceof ListNode) {
$html = $node->render();
if(preg_match('/Description\<\/strong\>\:\s(.+)\<\/li\>/', $html, $m)) {
$methods[$method]['desc'] = $m[1];
}
}
if ($method && empty($methods[$method]['table']) && $node instanceof TableNode) {
$methods[$method]['table'] = $node->renderArray();
}
}
function dotToCamelCase($string) {
$ex = explode('.', $string);
$string = '';
$first = true;
foreach ($ex as $w) {
if ($w === 'one') continue;
if ($first) {
$string .= $w;
$first = false;
continue;
}
$string .= ucfirst($w);
}
return $string;
}
function numToChar($n) {
$alphabet = array( 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y',
'z'
);
return $alphabet[$n];
}
function type($t) {
$t = strtolower($t);
if($t === 'boolean') return 'bool';
return $t;
}
$methodDefs = '';
foreach ($methods as $m => $d) {
$method = dotToCamelCase($m). '(';
$table = $d['table'];
$desc = $d['desc'];
$argsIn = [];
$argsPass = [];
$argsDoc = [];
$i = 0;
foreach ($table as $r) {
if ($r[0] === 'IN') {
// skip session arg
if (strpos($r[2], 'session') !== false) continue;
$argsIn[] = type($r[1]) . ' $' . numToChar($i);
$argsPass[] = '$' . numToChar($i);
$argsDoc[] = ' * @param ' . type($r[1]) . ' $' . numToChar($i) . ' ' . strip_tags($r[2]);
$i++;
}
}
$method .= implode(', ', $argsIn) . ')';
$argsPass = implode(', ', $argsPass);
$argsDoc = implode("\n", $argsDoc);
$def =<<<EOF
/**
* $desc
*
$argsDoc
* @return \One\Resource
*/
public function $method {
return \$this->makeCall('$m', $argsPass);
}
EOF;
$methodDefs .= $def . "\n\n";
}
$classFile =<<<EOF
<?php
namespace One;
class One extends OneBase {
/**
* Generate and return VNC token only
*
* @param int \$a The VM ID.
* @return \One\Resource
*/
public function vmVnctokenonly(int \$a) {
return \$this->makeCall('one.vm.vnctokenonly', \$a);
}
/**
* Generate and return VNC token including VM VNC details
*
* @param int \$a The VM ID.
* @return \One\Resource
*/
public function vmVnc(int \$a) {
return \$this->makeCall('one.vm.vnc', \$a);
}
$methodDefs}
EOF;
// save file
file_put_contents('src/One.php', $classFile);