Index: net/tools/net_docs/net_docs.py |
diff --git a/net/tools/net_docs/net_docs.py b/net/tools/net_docs/net_docs.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..f8016d40b52bd1581931368c79e5f815b681f56a |
--- /dev/null |
+++ b/net/tools/net_docs/net_docs.py |
@@ -0,0 +1,72 @@ |
+#!/usr/bin/env python |
+ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
Randy Smith (Not in Mondays)
2015/01/23 15:04:18
I'm finding myself struck by two things about this
Deprecated (see juliatuttle)
2015/01/23 20:13:00
I haven't split it out entirely into separate file
Randy Smith (Not in Mondays)
2015/01/26 17:29:34
Hmmm. I wouldn't think that would argue against d
Deprecated (see juliatuttle)
2015/01/26 20:49:21
Eh, let's sit down in person for a couple of minut
|
+ |
Randy Smith (Not in Mondays)
2015/01/23 15:04:18
nit: extra blank line.
Deprecated (see juliatuttle)
2015/01/23 20:13:00
Done.
|
+import sys |
+ |
+ |
+import argparse |
+import markdown |
+import os.path |
+ |
+ |
+def ReadFile(filename): |
+ with open(filename, 'r') as file: |
+ return file.read() |
+ |
+ |
+def WriteFile(filename, contents): |
+ dir = os.path.dirname(filename) |
+ if not os.path.isdir(dir): |
+ os.mkdir(dir) |
+ with open(filename, 'w') as file: |
+ file.write(contents) |
+ |
+ |
+def FormatPage(markdown_html, title): |
+ # TODO(ttuttle): Add navigation? |
+ TEMPLATE = '<html><head><title>%s</title></head><body>%s</body></html>' |
Randy Smith (Not in Mondays)
2015/01/23 15:04:18
Suggestion: Pull this out of the function and make
Deprecated (see juliatuttle)
2015/01/23 20:13:00
Done.
|
+ return TEMPLATE % (title, markdown_html) |
+ |
+ |
+def ProcessDocs(input_filenames, input_pathname, output_pathname): |
+ outputting = (input_pathname is not None) and (output_pathname is not None) |
+ |
+ markdown_parser = markdown.Markdown() |
+ |
+ for input_filename in input_filenames: |
+ markdown_text = ReadFile(input_filename) |
+ markdown_html = markdown_parser.reset().convert(markdown_text) |
+ if not outputting: |
+ continue |
+ |
+ # TODO(ttuttle): Sanitize HTML? |
+ full_html = FormatPage(markdown_html, title=input_filename) |
+ rel_filename = os.path.relpath(input_filename, start=input_pathname) |
+ output_filename = os.path.join(output_pathname, rel_filename) + '.html' |
+ print "%s -> %s -> %s" % (input_filename, rel_filename, output_filename) |
+ WriteFile(output_filename, full_html) |
+ |
+ |
+def main(): |
+ parser = argparse.ArgumentParser( |
+ description='Parse and render Markdown documentation') |
+ parser.add_argument('--input_path', default=None, |
+ help="Input path for Markdown; required only if output_path set") |
+ parser.add_argument('--output_path', default=None, |
+ help="Output path for rendered HTML; if unspecified, won't output") |
+ parser.add_argument('filenames', nargs=argparse.REMAINDER) |
+ args = parser.parse_args() |
+ |
+ print args |
+ |
+ ProcessDocs(args.filenames, args.input_path, args.output_path) |
+ |
+ return 0 |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main()) |