Update document reference; add docs for pointClickCallback, and clarify
[dygraphs.git] / generate-documentation.py
CommitLineData
0e37f8e5
DV
1#!/usr/bin/python
2import json
3import glob
4import re
d1d19c3e
DV
5import sys
6
7# Set this to the path to a test file to get debug output for just that test
8# file. Can be helpful to figure out why a test is not being shown for a
9# particular option.
10debug_tests = [] # [ 'tests/zoom.html' ]
0e37f8e5 11
88b1e052 12# Pull options reference JSON out of dygraph.js
0e37f8e5
DV
13js = ''
14in_json = False
730852d8 15for line in file('dygraph-options-reference.js'):
0e37f8e5
DV
16 if '<JSON>' in line:
17 in_json = True
18 elif '</JSON>' in line:
19 in_json = False
20 elif in_json:
21 js += line
22
23# TODO(danvk): better errors here.
24assert js
25docs = json.loads(js)
26
27# Go through the tests and find uses of each option.
28for opt in docs:
29 docs[opt]['tests'] = []
30
88b1e052
DV
31# This is helpful for differentiating uses of options like 'width' and 'height'
32# from appearances of identically-named options in CSS.
0e37f8e5
DV
33def find_braces(txt):
34 """Really primitive method to find text inside of {..} braces.
35 Doesn't work if there's an unmatched brace in a string, e.g. '{'. """
36 out = ''
37 level = 0
38 for char in txt:
39 if char == '{':
40 level += 1
41 if level >= 1:
42 out += char
43 if char == '}':
44 level -= 1
45 return out
46
88b1e052
DV
47# Find text followed by a colon. These won't all be options, but those that
48# have the same name as a Dygraph option probably will be.
d1d19c3e
DV
49prop_re = re.compile(r'\b([a-zA-Z0-9]+) *:')
50tests = debug_tests or glob.glob('tests/*.html')
51for test_file in tests:
0e37f8e5 52 braced_html = find_braces(file(test_file).read())
d1d19c3e
DV
53 if debug_tests:
54 print braced_html
55
0e37f8e5
DV
56 ms = re.findall(prop_re, braced_html)
57 for opt in ms:
d1d19c3e 58 if debug_tests: print '\n'.join(ms)
0e37f8e5
DV
59 if opt in docs and test_file not in docs[opt]['tests']:
60 docs[opt]['tests'].append(test_file)
61
d1d19c3e
DV
62if debug_tests: sys.exit(0)
63
a38e9336
DV
64# Extract a labels list.
65labels = []
66for nu, opt in docs.iteritems():
67 for label in opt['labels']:
68 if label not in labels:
69 labels.append(label)
70
71print """
72<html>
73<head>
74 <title>Dygraphs Options Reference</title>
5af2de24 75 <link rel="stylesheet" href="style.css">
a38e9336
DV
76 <style type="text/css">
77 p.option {
78 padding-left: 25px;
5af2de24
DV
79 }
80 #nav {
81 position: fixed;
82 }
83 #content {
a38e9336
DV
84 max-width: 800px;
85 }
86 </style>
87</head>
88<body>
89"""
90
5af2de24
DV
91print """
92<div id=nav>
93<h2>Dygraphs</h2>
94<ul>
95 <li><a href="index.html">Home</a>
96 <li><a href="data.html">Data Formats</a></li>
97 <li><a href="annotations.html">Annotations</a></li>
98</ul>
99<h2>Options Reference</h2>
100<ul>
101 <li><a href="#usage">Usage</a>
102"""
a38e9336
DV
103for label in sorted(labels):
104 print ' <li><a href="#%s">%s</a>\n' % (label, label)
5af2de24 105print '</ul>\n</div>\n\n'
a38e9336 106
0e37f8e5 107def name(f):
88b1e052 108 """Takes 'tests/demo.html' -> 'demo'"""
0e37f8e5
DV
109 return f.replace('tests/', '').replace('.html', '')
110
5af2de24
DV
111print """
112<div id=content>
113<h2>Options Reference</h2>
114<p>Dygraphs tries to do a good job of displaying your data without any further configuration. But inevitably, you're going to want to tinker. Dygraphs provides a rich set of options for configuring its display and behavior.</p>
115
116<a name="usage"><h3>Usage</h3>
117<p>You specify options in the third parameter to the dygraphs constructor:
118<pre>g = new Dygraph(div,
119 data,
120 {
121 option1: value1,
122 option2: value2,
123 ...
124 });
125</pre>
126
127After you've created a Dygraph, you can change an option by calling the <code>updateOptions</code> method:
128<pre>g.updateOptions({
129 new_option1: value1,
130 new_option2: value2
131 });
132</pre>
133
134<p>And, without further ado, here's the complete list of options:</p>
135"""
a38e9336 136for label in sorted(labels):
5af2de24 137 print '<a name="%s"><h3>%s</h3>\n' % (label, label)
a38e9336
DV
138
139 for opt_name in sorted(docs.keys()):
140 opt = docs[opt_name]
141 if label not in opt['labels']: continue
142 tests = opt['tests']
143 if not tests:
144 examples_html = '<font color=red>NONE</font>'
145 else:
146 examples_html = ' '.join(
147 '<a href="%s">%s</a>' % (f, name(f)) for f in tests)
148
5af2de24
DV
149 if not opt['type']: opt['type'] = '(missing)'
150 if not opt['default']: opt['default'] = '(missing)'
151 if not opt['description']: opt['description'] = '(missing)'
152
a38e9336 153 print """
b5f3dd4e 154 <p class='option'><a name="%(name)s"/><b>%(name)s</b><br/>
a38e9336
DV
155 %(desc)s<br/>
156 <i>Type: %(type)s<br/>
157 Default: %(default)s</i><br/>
158 Examples: %(examples_html)s<br/>
159 <br/>
160 """ % { 'name': opt_name,
161 'type': opt['type'],
162 'default': opt['default'],
163 'desc': opt['description'],
164 'examples_html': examples_html}
0e37f8e5 165
0e37f8e5 166
5af2de24
DV
167print """
168</div>
169</body>
170</html>
171"""
172
a38e9336
DV
173# This page was super-helpful:
174# http://jsbeautifier.org/