From 0e37f8e5b44d3e747be8ad87fa47cc734a630626 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Sun, 13 Mar 2011 15:37:07 -0400 Subject: [PATCH] script for generating HTML docs --- generate-documentation.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 generate-documentation.py diff --git a/generate-documentation.py b/generate-documentation.py new file mode 100755 index 0000000..5b6400f --- /dev/null +++ b/generate-documentation.py @@ -0,0 +1,70 @@ +#!/usr/bin/python +import json +import glob +import re + +js = '' +in_json = False +for line in file('dygraph.js'): + if '' in line: + in_json = True + elif '' in line: + in_json = False + elif in_json: + js += line + +# TODO(danvk): better errors here. +assert js +docs = json.loads(js) + +# Go through the tests and find uses of each option. +for opt in docs: + docs[opt]['tests'] = [] + +def find_braces(txt): + """Really primitive method to find text inside of {..} braces. + Doesn't work if there's an unmatched brace in a string, e.g. '{'. """ + out = '' + level = 0 + for char in txt: + if char == '{': + level += 1 + if level >= 1: + out += char + if char == '}': + level -= 1 + return out + +prop_re = re.compile(r'\b([a-zA-Z]+):') +for test_file in glob.glob('tests/*.html'): + braced_html = find_braces(file(test_file).read()) + ms = re.findall(prop_re, braced_html) + for opt in ms: + if opt in docs and test_file not in docs[opt]['tests']: + docs[opt]['tests'].append(test_file) + +def name(f): + return f.replace('tests/', '').replace('.html', '') + +for opt_name in sorted(docs.keys()): + opt = docs[opt_name] + tests = opt['tests'] + if not tests: + examples_html = 'NONE' + else: + examples_html = ' '.join( + '%s' % (f, name(f)) for f in tests) + + print """ +

%(name)s
+%(desc)s
+Type: %(type)s
+Default: %(default)s

+Examples: %(examples_html)s
+
+""" % { 'name': opt_name, + 'type': opt['type'], + 'default': opt['default'], + 'desc': opt['description'], + 'examples_html': examples_html} + -- 2.7.4