Merge branch 'master' of git://github.com/danvk/dygraphs into highlight3
[dygraphs.git] / generate-documentation.py
index 155393b..613dec7 100755 (executable)
@@ -1,17 +1,28 @@
-#!/usr/bin/python
+#!/usr/bin/env python
+
+# Generate docs/options.html
+
 import json
 import glob
 import re
+import sys
+
+# Set this to the path to a test file to get debug output for just that test
+# file. Can be helpful to figure out why a test is not being shown for a
+# particular option.
+debug_tests = []  # [ 'tests/zoom.html' ]
 
 # Pull options reference JSON out of dygraph.js
 js = ''
 in_json = False
-for line in file('dygraph.js'):
+for line in file('dygraph-options-reference.js'):
   if '<JSON>' in line:
     in_json = True
   elif '</JSON>' in line:
     in_json = False
   elif in_json:
+    if line.endswith("\\\n"): # hacked in line continuation support with trailing \.
+      line = line[:-2]
     js += line
 
 # TODO(danvk): better errors here.
@@ -40,14 +51,21 @@ def find_braces(txt):
 
 # Find text followed by a colon. These won't all be options, but those that
 # have the same name as a Dygraph option probably will be.
-prop_re = re.compile(r'\b([a-zA-Z0-9]+):')
-for test_file in glob.glob('tests/*.html'):
+prop_re = re.compile(r'\b([a-zA-Z0-9]+) *:')
+tests = debug_tests or glob.glob('tests/*.html')
+for test_file in tests:
   braced_html = find_braces(file(test_file).read())
+  if debug_tests:
+    print braced_html
+
   ms = re.findall(prop_re, braced_html)
   for opt in ms:
+    if debug_tests: print '\n'.join(ms)
     if opt in docs and test_file not in docs[opt]['tests']:
       docs[opt]['tests'].append(test_file)
 
+if debug_tests: sys.exit(0)
+
 # Extract a labels list.
 labels = []
 for nu, opt in docs.iteritems():
@@ -55,7 +73,7 @@ for nu, opt in docs.iteritems():
     if label not in labels:
       labels.append(label)
 
-print """
+print """<!DOCTYPE HTML>
 <html>
 <head>
   <title>Dygraphs Options Reference</title>
@@ -64,6 +82,9 @@ print """
     p.option {
       padding-left: 25px;
     }
+    div.parameters {
+      padding-left: 15px;
+    }
     #nav {
       position: fixed;
     }
@@ -76,7 +97,7 @@ print """
 """
 
 print """
-<div id=nav>
+<div id='nav'>
 <h2>Dygraphs</h2>
 <ul>
   <li><a href="index.html">Home</a>
@@ -96,12 +117,12 @@ def name(f):
   return f.replace('tests/', '').replace('.html', '')
 
 print """
-<div id=content>
+<div id='content'>
 <h2>Options Reference</h2>
 <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>
 
-<a name="usage"><h3>Usage</h3>
-<p>You specify options in the third parameter to the dygraphs constructor:
+<a name="usage"></a><h3>Usage</h3>
+<p>You specify options in the third parameter to the dygraphs constructor:</p>
 <pre>g = new Dygraph(div,
                 data,
                 {
@@ -111,13 +132,12 @@ print """
                 });
 </pre>
 
-After you've created a Dygraph, you can change an option by calling the <code>updateOptions</code> method:
+<p>After you've created a Dygraph, you can change an option by calling the <code>updateOptions</code> method:</p>
 <pre>g.updateOptions({
                   new_option1: value1,
                   new_option2: value2
                 });
 </pre>
-
 <p>And, without further ado, here's the complete list of options:</p>
 """
 for label in sorted(labels):
@@ -133,25 +153,40 @@ for label in sorted(labels):
       examples_html = ' '.join(
         '<a href="%s">%s</a>' % (f, name(f)) for f in tests)
 
+    if 'parameters' in opt:
+      parameters = opt['parameters']
+      parameters_html = '\n'.join("<i>%s</i>: %s<br/>" % (p[0], p[1]) for p in parameters)
+      parameters_html = "\n  <div class='parameters'>\n%s</div>" % (parameters_html);
+    else:
+      parameters_html = ''
+
     if not opt['type']: opt['type'] = '(missing)'
     if not opt['default']: opt['default'] = '(missing)'
     if not opt['description']: opt['description'] = '(missing)'
 
     print """
-  <p class='option'><a name="%(name)s"/><b>%(name)s</b><br/>
+  <div class='option'><a name="%(name)s"></a><b>%(name)s</b><br/>
   %(desc)s<br/>
-  <i>Type: %(type)s<br/>
-  Default: %(default)s</i><br/>
+  <i>Type: %(type)s</i><br/>%(parameters)s
+  <i>Default: %(default)s</i><br/>
   Examples: %(examples_html)s<br/>
-  <br/>
+  <br/></div>
   """ % { 'name': opt_name,
           'type': opt['type'],
+          'parameters': parameters_html,
           'default': opt['default'],
           'desc': opt['description'],
           'examples_html': examples_html}
 
 
 print """
+<a name="point_properties"></a><h3>Point Properties</h3>
+Some callbacks take a point argument. Its properties are:<br/>
+<ul>
+<li>xval/yval: The data coordinates of the point (with dates/times as millis since epoch)</li>
+<li>canvasx/canvasy: The canvas coordinates at which the point is drawn.</li>
+<li>name: The name of the data series to which the point belongs</li>
+</ul>
 </div>
 </body>
 </html>