summaryrefslogtreecommitdiff
path: root/utils/rockbox_api
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-10-06 22:19:54 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-10-06 22:19:54 +0000
commit20fb47ec778abe215ca69692434ff753596319e6 (patch)
tree69665298c0f58c07a1aa9677d33ad395ad9ccd13 /utils/rockbox_api
parent34148b9a2173b0bd80b4acd18d23216ae54cb865 (diff)
downloadrockbox-20fb47ec778abe215ca69692434ff753596319e6.tar.gz
rockbox-20fb47ec778abe215ca69692434ff753596319e6.zip
Commit FS#9462: an semi-automatic plugin API documentation generator
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18722 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/rockbox_api')
-rw-r--r--utils/rockbox_api/README29
-rwxr-xr-xutils/rockbox_api/functions.php337
-rwxr-xr-xutils/rockbox_api/gen_html.php110
-rwxr-xr-xutils/rockbox_api/generate.php63
-rw-r--r--utils/rockbox_api/layout.css108
-rwxr-xr-xutils/rockbox_api/update.php119
6 files changed, 766 insertions, 0 deletions
diff --git a/utils/rockbox_api/README b/utils/rockbox_api/README
new file mode 100644
index 0000000000..1ed251c918
--- /dev/null
+++ b/utils/rockbox_api/README
@@ -0,0 +1,29 @@
1==============================
2generate.php
3==============================
4
5 php generate.php > output.txt
6
7--------------
8 Generates output.txt of apps/plugin.h
9
10==============================
11update.php
12==============================
13
14 php update.php input_file.txt > output.txt
15
16--------------
17 Updates input_file.txt with newer values of apps/plugin.h
18
19==============================
20gen_html.php
21==============================
22
23 php gen_html.php input_file.txt
24
25--------------
26 Generates HTML output of input_file.txt in output/*.html
27
28 Known issues:
29 * [F[function]] doesn't work with functions outside of the current .html \ No newline at end of file
diff --git a/utils/rockbox_api/functions.php b/utils/rockbox_api/functions.php
new file mode 100755
index 0000000000..0f34fc69e0
--- /dev/null
+++ b/utils/rockbox_api/functions.php
@@ -0,0 +1,337 @@
1<?
2$svn = "http://svn.rockbox.org/viewvc.cgi/trunk/";
3$wiki = "http://www.rockbox.org/wiki/";
4
5function func_sort($a, $b)
6{
7 $a = preg_replace('/^((unsigned|const|struct|enum) [^ ]*|[a-z0-9 \*_]*) [\*]?/i', '', $a);
8 $b = preg_replace('/^((unsigned|const|struct|enum) [^ ]*|[a-z0-9 \*_]*) [\*]?/i', '', $b);
9 return strnatcasecmp($a, $b);
10}
11
12function get_newest()
13{
14 global $svn;
15
16 $text = file_get_contents("../../apps/plugin.h");
17
18 $text = str_replace(array("\r\n", "\r"), "\n", $text);
19
20 /* Located plugin_api struct */
21 foreach(explode("\n", $text) as $line_nr => $line)
22 {
23 if(trim($line) == "struct plugin_api {")
24 {
25 $text = explode("\n", $text);
26 $text = array_slice($text, $line_nr+1);
27 break;
28 }
29 }
30
31 foreach($text as $line_nr => $line)
32 {
33 if(trim($line) == "};")
34 {
35 $text = array_slice($text, 0, $line_nr-1);
36 break;
37 }
38 }
39 /* Locating done */
40
41 /* Clean up stuff a bit .. */
42 for($i=0; $i<count($text); $i++)
43 $text[$i] = trim($text[$i]);
44
45
46 /* Fake preprocesser */
47 $ret = array();
48 $_groups = array();
49 $conditions = array();
50 $strip_next = 0;
51 $group = "";
52 for($i=0; $i<count($text); $i++)
53 {
54 $tmp = trim($text[$i]);
55
56 if(substr($tmp, 0, 1) == '#')
57 {
58 $tmp = trim(substr($tmp, 1));
59 if(strtolower(substr($tmp, 0, 2)) == "if")
60 {
61 if(strtolower(substr($tmp, 2, 3)) == "def")
62 $conditions[] = "defined(".substr($tmp, 6).")";
63 else if(strtolower(substr($tmp, 2, 4)) == "ndef")
64 $conditions[] = "!defined(".substr($tmp, 7).")";
65 else
66 {
67 while(substr($tmp, strlen($tmp)-1, 1) == "\\")
68 {
69 $i++;
70 $tmp = substr($tmp, 0, strlen($tmp)-1)." ".trim($text[$i]);
71 }
72
73 $conditions[] = substr($tmp, 3);
74 }
75 }
76 else if(strtolower(substr($tmp, 0, 4)) == "elif")
77 {
78 while(substr($tmp, strlen($tmp)-1, 1) == "\\")
79 {
80 $i++;
81 $tmp = substr($tmp, 0, strlen($tmp)-1)." ".trim($text[$i]);
82 }
83 $conditions[count($conditions)-1] = substr($tmp, 5);
84 }
85 else if(strtolower(substr($tmp, 0, 4)) == "else")
86 $conditions[count($conditions)-1] = "!( ".$conditions[count($conditions)-1]." )";
87 else if(strtolower(substr($tmp, 0, 5)) == "endif")
88 array_pop($conditions);
89 }
90 else if(strlen($tmp) == 0)
91 $group = "";
92 else if(substr($tmp, 0, 2) == "/*")
93 {
94 while(strpos($tmp, "*/") === false)
95 {
96 $i++;
97 $tmp .= " ".trim($text[$i]);
98 }
99 $group = explode("/*", trim($tmp));
100 $group = explode("*/", $group[1]);
101 $group = trim($group[0]);
102 }
103 else
104 {
105 while(strpos($tmp, ";") === false)
106 {
107 $i++;
108 $tmp .= " ".trim($text[$i]);
109 }
110
111 /* Replace those (*func)(int args) with func(int args) */
112 $tmp = preg_replace('/\(\*([^\)]*)\)/i', '\1', $tmp, 1);
113 $tmp = substr($tmp, 0, strlen($tmp)-1);
114 $ret[$tmp] = array("func" => $tmp, "cond" => "(".implode(") && (", $conditions).")", "group" => $group);
115 }
116 }
117
118 uksort($ret, "func_sort");
119
120 return $ret;
121}
122
123function parse_documentation($data)
124{
125 $data = explode("\n", $data);
126
127 $ret = array();
128 $cur_func = "";
129 foreach($data as $line)
130 {
131 if(substr($line, 0, 1) == "#")
132 continue;
133 else if(substr($line, 0, 4) == " ")
134 {
135 $tmp = trim($line);
136 if(strpos($tmp, " ") !== false)
137 $tmp = array(substr($tmp, 1, strpos($tmp, " ")-1), substr($tmp, strpos($tmp, " ")) );
138 else
139 $tmp = array(substr($tmp, 1), "");
140
141 $ret[$cur_func][$tmp[0]][] = $tmp[1];
142 }
143 else if(strlen($line) == 0)
144 continue;
145 else
146 $cur_func = substr($line, 0);
147 }
148
149 $_ret = array();
150 foreach($ret as $func => $el)
151 {
152 if(isset($el["group"]))
153 $group = trim($el["group"][0]);
154 else
155 $group = "misc";
156
157 $_ret[$group][$func] = $el;
158 }
159
160 return $_ret;
161}
162
163function get_func($func)
164{
165 $func = preg_replace('/^((unsigned|const|struct|enum) [^ ]*|[a-z0-9 \*_]*) [\*]?/i', '', $func);
166 if(strpos($func, "PREFIX") !== false)
167 $func = substr($func, 0, strrpos($func, "("));
168 else if(strpos($func, "(") !== false)
169 $func = substr($func, 0, strpos($func, "("));
170
171 return $func;
172}
173
174function get_args($func)
175{
176 /* Check if this _is_ a function */
177 if(strpos($func, "(") === false)
178 return array();
179
180 /* Get rid of return value */
181 $func = preg_replace('/^((unsigned|const|struct|enum) [^ ]*|[a-z0-9 \*_]*) [\*]?/i', '', $func);
182
183 /* Get rid of function name */
184 if(strpos($func, "(") !== false)
185 $func = substr($func, strpos($func, "("));
186
187 /* Get rid of ATTRIBUTE_PRINTF */
188 if(strpos($func, "ATTRIBUTE_PRINTF") !== false)
189 $func = substr($func, 0, strpos($func, "ATTRIBUTE_PRINTF"));
190
191 $level = 0;
192 $args = array();
193 $buffer = "";
194 for($i=0; $i<strlen($func); $i++)
195 {
196 switch($func{$i})
197 {
198 case "(":
199 $level++;
200 if($level > 1)
201 $buffer .= "(";
202 break;
203 case ")":
204 $level--;
205 if($level > 0)
206 {
207 $buffer .= ")";
208 break;
209 }
210 case ",":
211 if($level <= 1)
212 {
213 if(strpos($buffer, "(,") !== false)
214 {
215 $tmp = array();
216 preg_match_all('/[^ ]*, [^)]*\)/', $buffer, $tmp);
217 $tmp = $tmp[0];
218 foreach($tmp as $el)
219 {
220 if(strlen($el) > 0)
221 $args[] = trim($el);
222 }
223 $tmp = preg_replace('/[^ ]*, [^)]*\)/', '', $buffer);
224 $args[] = trim($tmp);
225 }
226 else
227 $args[] = trim($buffer);
228 $buffer = "";
229 }
230 else
231 $buffer .= ",";
232 break;
233 default:
234 $buffer .= $func{$i};
235 break;
236 }
237 }
238
239 /* Filter out void */
240 for($i=0; $i<count($args); $i++)
241 {
242 if($args[$i] == "void")
243 unset($args[$i]);
244 }
245
246 return $args;
247}
248
249function get_return($func)
250{
251 $ret = array();
252 preg_match('/^((unsigned|const|struct|enum) [^ ]*|[a-z0-9 \*_]*) [\*]?/i', $func, $ret);
253
254 if(trim($ret[0]) == "void")
255 return false;
256 else
257 return trim($ret[0]);
258}
259
260function split_var($var)
261{
262 if(strpos($var, "(,") !== false)
263 {
264 $p1 = substr($var, 0, strrpos($var, " "));
265 $p2 = substr($var, strrpos($var, " "));
266 $p2 = substr($p2, 0, strlen($p2)-1);
267 }
268 else if(strpos($var, "(*") !== false)
269 {
270 $p2 = array();
271 preg_match('/\(\*\w*\)/', $var, $p2);
272 $p2 = $p2[0];
273
274 $p1 = substr($var, strpos($var, $p2));
275 $p2 = substr($p2, 2, strlen($p2)-3);
276 }
277 else
278 {
279 $p1 = substr($var, 0, strrpos($var, " "));
280 $p2 = substr($var, strrpos($var, " "));
281 }
282
283 if(strpos($p2, "*") !== false)
284 {
285 for($i=0; $i<substr_count($p2, "*"); $i++)
286 $p1 .= "*";
287 $p2 = str_replace("*", "", $p2);
288 }
289
290 return array(trim($p1), trim($p2));
291}
292
293function _simplify($text)
294{
295 $text = ereg_replace('\(!\( (.*)[ ]?\)\)', '!\1', $text);
296 $text = ereg_replace('\(\(([^ ])\)\)', '\1', $text);
297 return $text;
298}
299
300function clean_func($func)
301{
302 $func = str_replace(array(" ", " "), " ", $func);
303 $func = str_replace(" ", " ", $func);
304 return $func;
305}
306
307function do_see_markup($data)
308{
309 $ret = array();
310 foreach($data as $el)
311 {
312 $el = trim($el);
313
314 if(substr($el, 0, 1) != "[")
315 $ret[] = do_markup("[F[".$el."]]");
316 else
317 $ret[] = do_markup($el);
318 }
319
320 return implode(" &amp; ", $ret);
321}
322
323function do_markup($data)
324{
325 global $svn, $wiki;
326
327 $data = ereg_replace('=([^=]*)=', '<code>\1</code>', $data);
328 $data = ereg_replace('\[W\[([^#\[]*)([^\[]*)\]\]', '<a href="'.$wiki.'\1\2">\1</a>', $data);
329 $data = ereg_replace('\[S\[([^\[]*)\]\]', '<a href="'.$svn.'\1?content-type=text%2Fplain">\1</a>', $data);
330 $data = ereg_replace('\[F\[([^\[]*)\]\]', '<a href="#\1">\1</a>', $data);
331 $data = ereg_replace('\[\[([^#\[]*)([^\[]*)\]\]', '<a href="\1\2">\1</a>', $data);
332 $data = str_replace("%BR%", "<br />", $data);
333 $data = nl2br($data);
334
335 return $data;
336}
337?>
diff --git a/utils/rockbox_api/gen_html.php b/utils/rockbox_api/gen_html.php
new file mode 100755
index 0000000000..34e124f411
--- /dev/null
+++ b/utils/rockbox_api/gen_html.php
@@ -0,0 +1,110 @@
1#!/usr/bin/php
2<?
3require_once("functions.php");
4
5function get_group($text)
6{
7 return str_replace(array(" ", "/"), "_", $text);
8}
9
10$input = file_get_contents($argv[1]);
11
12$inh = parse_documentation($input);
13
14@mkdir("output");
15
16$h = fopen("output/index.html", "w");
17
18fwrite($h, '<html><head><link href="layout.css" rel="stylesheet" type="text/css" /><title>Plugin API - INDEX</title></head><body>');
19
20fwrite($h, "<h1>Plugin API reference</h1>");
21fwrite($h, "<ul>");
22
23foreach($inh as $group_name => $group)
24{
25 if(strlen($group_name) > 0)
26 {
27 fwrite($h, '<li>'.ucwords($group_name)."<ul>");
28
29 foreach($group as $el_name => $el)
30 fwrite($h, "<li><a href=\"".get_group($group_name).".html#".get_func($el_name)."\">".$el_name."</a></li>");
31
32 fwrite($h, "</ul></li>");
33 }
34}
35fwrite($h, "</ul></body></html>");
36
37fclose($h);
38
39$menu = '<ul><li><a href="index.html">INDEX</a></li><ul>';
40$_menu = array();
41foreach($inh as $group_name => $group)
42{
43 if(strlen($group_name) > 0)
44 $_menu[strtolower($group_name)] = '<li><a href="'.get_group($group_name).'.html">'.ucwords($group_name).'</a></li>';
45}
46
47ksort($_menu);
48$menu .= implode("\n", $_menu);
49$menu .= "</ul></ul>";
50
51foreach($inh as $group_name => $group)
52{
53 $h = fopen("output/".get_group($group_name).".html", "w");
54
55 fwrite($h, '<html><head><link href="layout.css" rel="stylesheet" type="text/css" /><title>Plugin API - '.ucwords($group_name).'</title></head><body>');
56 fwrite($h, '<div id="menu">'.ucwords($menu).'</div>');
57 fwrite($h, '<div id="content">');
58 fwrite($h, '<a link="top"></a>');
59
60 fwrite($h, "<h2>".ucwords($group_name)."</h2>");
61 fwrite($h, '<span class="group">');
62 foreach($group as $func_name => $func)
63 {
64 fwrite($h, '<a id="'.get_func($func_name).'"></a>');
65
66 fwrite($h, "<h3>$func_name</h3>");
67
68 if(strlen($func["description"][0]) > 0)
69 fwrite($h, do_markup($func["description"][0])."<br /><br />");
70
71 if(isset($func["param"]))
72 {
73 $params = "";
74 foreach($func["param"] as $param)
75 {
76 $param = trim($param);
77 $p1 = substr($param, 0, strpos($param, " "));
78 $p2 = substr($param, strpos($param, " "));
79 if(strlen($p1) > 0 && strlen($p2) > 0)
80 $params .= '<dt>'.$p1.'</dt><dd> '.do_markup($p2).'</dd>';
81 }
82
83 if(strlen($params) > 0)
84 {
85 fwrite($h, '<span class="extra">Parameters:</span><dl>');
86 fwrite($h, $params);
87 fwrite($h, "</dl>");
88 }
89 }
90
91 if(isset($func["return"]) && strlen($func["return"][0]) > 0)
92 fwrite($h, '<span class="extra">Returns:</span> '.do_markup($func["return"][0]).'<br /><br />');
93
94 if(isset($func["conditions"]))
95 fwrite($h, '<span class="extra">Conditions:</span> '.$func["conditions"][0].'<br /><br />');
96
97 if(isset($func["see"]))
98 fwrite($h, '<span class="see">Also see '.do_see_markup(explode(" ", trim($func["see"][0]))).'</span><br /><br />');
99
100 fwrite($h, '<a href="#top" class="top">To top</a><hr />');
101 }
102 fwrite($h, "</span>");
103
104 fwrite($h, "</div></body></html>");
105
106 fclose($h);
107}
108
109copy("layout.css", "output/layout.css");
110?> \ No newline at end of file
diff --git a/utils/rockbox_api/generate.php b/utils/rockbox_api/generate.php
new file mode 100755
index 0000000000..29dd69588a
--- /dev/null
+++ b/utils/rockbox_api/generate.php
@@ -0,0 +1,63 @@
1#!/usr/bin/php
2<?
3require_once("functions.php");
4
5echo '# Auto generated documentation by Rockbox plugin API generator v2'."\n";
6echo '# Made by Maurus Cuelenaere'."\n";
7echo <<<MOO
8# __________ __ ___.
9# Open \______ \ ____ ____ | | _\_ |__ _______ ___
10# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
11# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
12# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
13# \/ \/ \/ \/ \/
14# \$Id$
15#
16# Generated from $svn\x61pps/plugin.h
17#
18# Format:
19# \\group memory and strings
20# \\conditions defined(HAVE_BACKLIGHT)
21# \\param fmt
22# \\return
23# \\description
24# \\see func1 func2 [S[apps/plugin.c]]
25#
26# Markup:
27# [W[wiki url]]
28# [S[svn url]]
29# [F[function]]
30# [[url]]
31# %BR%
32# =code=
33
34MOO;
35
36foreach(get_newest() as $line)
37{
38 echo "\n".clean_func($line["func"])."\n";
39
40 if(strlen($line["group"]) > 0)
41 echo " \\group ".$line["group"]."\n";
42
43 if(strlen($line["cond"]) > 2)
44 echo " \\conditions "._simplify($line["cond"])."\n";
45
46 foreach(get_args($line["func"]) as $param)
47 {
48 if(strlen($param) > 0 && $param != "...")
49 {
50 $param = split_var($param);
51 $param = $param[1];
52 echo " \\param $param\n";
53 }
54 }
55
56 if(get_return($line["func"]) !== false)
57 echo " \\return\n";
58
59 echo " \\description\n";
60}
61
62echo "\n# END\n";
63?> \ No newline at end of file
diff --git a/utils/rockbox_api/layout.css b/utils/rockbox_api/layout.css
new file mode 100644
index 0000000000..09e7391197
--- /dev/null
+++ b/utils/rockbox_api/layout.css
@@ -0,0 +1,108 @@
1body
2{
3 font-family: Verdana;
4}
5
6li
7{
8 font-size: 10px;
9}
10
11code
12{
13 color: #00A;
14}
15
16.see, .see a
17{
18 color: #559;
19}
20
21.group
22{
23 font-size: 12px;
24}
25
26.group h3
27{
28 font-size: 14px;
29}
30
31h2
32{
33 color: #D11;
34}
35
36a
37{
38 color: blue;
39}
40
41a:hover
42{
43 color: red;
44}
45
46.top
47{
48 font-size: 10px;
49 color: green;
50}
51
52.extra
53{
54 font-weight: bold;
55 color: #992;
56}
57
58dl
59{
60 border-left: 1px solid #CCC;
61 padding-left: 10px;
62}
63
64dt, dd
65{
66 font-style: normal;
67}
68
69dd
70{
71 margin-left: 12em;
72}
73
74dt
75{
76 clear: left;
77 display: block;
78 float: left;
79 font-weight: bold;
80 width: 12em;
81}
82
83hr
84{
85 border: 1px solid #CCCCCC;
86 margin: 1em 0;
87}
88
89#content
90{
91 margin-left: 14em;
92 margin-right: 1em;
93 padding: 0;
94}
95
96#menu
97{
98 position: fixed;
99 top: 0px;
100 left: 0px;
101 width: 14em;
102 padding: 0;
103}
104
105#menu ul
106{
107 padding-left: 1em;
108} \ No newline at end of file
diff --git a/utils/rockbox_api/update.php b/utils/rockbox_api/update.php
new file mode 100755
index 0000000000..ef4a456d12
--- /dev/null
+++ b/utils/rockbox_api/update.php
@@ -0,0 +1,119 @@
1#!/usr/bin/php
2<?
3require_once("functions.php");
4
5$input = file_get_contents($argv[1]);
6
7$input = parse_documentation($input);
8
9foreach($input as $rootname => $rootel)
10{
11 foreach($rootel as $name => $el)
12 $input[$name] = $el;
13 unset($input[$rootname]);
14}
15
16$new = get_newest();
17
18foreach($new as $name => $el)
19{
20 unset($new[$name]);
21 $name = clean_func($el["func"]);
22
23 $new[$name] = array(
24 "group" => array($el["group"]),
25 "description" => array("")
26 );
27
28 if(strlen($el["cond"]) > 2)
29 $new[$name]["conditions"][0] = $el["cond"];
30
31 $args = get_args($el["func"]);
32 if(count($args) > 0)
33 {
34 foreach($args as $n => $arg)
35 {
36 $tmp = split_var($arg);
37 $args[$n] = $tmp[1];
38 }
39 $new[$name]["param"] = $args;
40 }
41
42 if(get_return($el["func"]) !== false)
43 $new[$name]["return"][0] = "";
44}
45
46
47$merged = array_merge($new, $input);
48
49uksort($merged, "func_sort");
50
51echo '# Auto generated documentation by Rockbox plugin API generator v2'."\n";
52echo '# Made by Maurus Cuelenaere'."\n";
53echo <<<MOO
54# __________ __ ___.
55# Open \______ \ ____ ____ | | _\_ |__ _______ ___
56# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
57# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
58# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
59# \/ \/ \/ \/ \/
60# \$Id$
61#
62# Generated from $svn\x61pps/plugin.h
63#
64# Format:
65# \\group memory and strings
66# \\conditions defined(HAVE_BACKLIGHT)
67# \\param fmt
68# \\return
69# \\description
70# \\see func1 func2 [S[apps/plugin.c]]
71#
72# Markup:
73# [W[wiki url]]
74# [S[svn url]]
75# [F[function]]
76# [[url]]
77# %BR%
78# =code=
79
80MOO;
81
82foreach($merged as $func => $line)
83{
84 echo "\n".clean_func($func)."\n";
85
86 if(strlen($line["group"]) > 0)
87 echo " \\group ".trim($line["group"][0])."\n";
88
89 if(strlen($line["conditions"]) > 2)
90 echo " \\conditions ".trim(_simplify($line["conditions"][0]))."\n";
91
92 if(isset($line["param"]))
93 {
94 foreach($line["param"] as $param)
95 {
96 if($param != "...")
97 echo " \\param ".trim($param)."\n";
98 }
99 }
100
101 if(isset($line["return"]))
102 {
103 if(trim($line["return"]) == "")
104 echo " \\return\n";
105 else
106 echo " \\return ".trim($line["return"][0])."\n";
107 }
108
109 if(trim($line["description"]) == "")
110 echo " \\description\n";
111 else
112 echo " \\description ".trim($line["description"][0])."\n";
113
114 if(isset($line["see"]))
115 echo " \\see ".trim($line["see"][0])."\n";
116}
117
118echo "\n# END\n";
119?> \ No newline at end of file