script for generating HTML docs
[dygraphs.git] / generate-documentation.py
1 #!/usr/bin/python
2 import json
3 import glob
4 import re
5
6 js = ''
7 in_json = False
8 for line in file('dygraph.js'):
9 if '<JSON>' in line:
10 in_json = True
11 elif '</JSON>' in line:
12 in_json = False
13 elif in_json:
14 js += line
15
16 # TODO(danvk): better errors here.
17 assert js
18 docs = json.loads(js)
19
20 # Go through the tests and find uses of each option.
21 for opt in docs:
22 docs[opt]['tests'] = []
23
24 def find_braces(txt):
25 """Really primitive method to find text inside of {..} braces.
26 Doesn't work if there's an unmatched brace in a string, e.g. '{'. """
27 out = ''
28 level = 0
29 for char in txt:
30 if char == '{':
31 level += 1
32 if level >= 1:
33 out += char
34 if char == '}':
35 level -= 1
36 return out
37
38 prop_re = re.compile(r'\b([a-zA-Z]+):')
39 for test_file in glob.glob('tests/*.html'):
40 braced_html = find_braces(file(test_file).read())
41 ms = re.findall(prop_re, braced_html)
42 for opt in ms:
43 if opt in docs and test_file not in docs[opt]['tests']:
44 docs[opt]['tests'].append(test_file)
45
46 def name(f):
47 return f.replace('tests/', '').replace('.html', '')
48
49 for opt_name in sorted(docs.keys()):
50 opt = docs[opt_name]
51 tests = opt['tests']
52 if not tests:
53 examples_html = '<font color=red>NONE</font>'
54 else:
55 examples_html = ' '.join(
56 '<a href="%s">%s</a>' % (f, name(f)) for f in tests)
57
58 print """
59 <p><b>%(name)s</b><br/>
60 %(desc)s<br/>
61 <i>Type: %(type)s<br/>
62 Default: %(default)s</i><br/>
63 Examples: %(examples_html)s<br/>
64 <br/>
65 """ % { 'name': opt_name,
66 'type': opt['type'],
67 'default': opt['default'],
68 'desc': opt['description'],
69 'examples_html': examples_html}
70