OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
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
| |
7 | |
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.
| |
8 import sys | |
9 | |
10 | |
11 import argparse | |
12 import markdown | |
13 import os.path | |
14 | |
15 | |
16 def ReadFile(filename): | |
17 with open(filename, 'r') as file: | |
18 return file.read() | |
19 | |
20 | |
21 def WriteFile(filename, contents): | |
22 dir = os.path.dirname(filename) | |
23 if not os.path.isdir(dir): | |
24 os.mkdir(dir) | |
25 with open(filename, 'w') as file: | |
26 file.write(contents) | |
27 | |
28 | |
29 def FormatPage(markdown_html, title): | |
30 # TODO(ttuttle): Add navigation? | |
31 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.
| |
32 return TEMPLATE % (title, markdown_html) | |
33 | |
34 | |
35 def ProcessDocs(input_filenames, input_pathname, output_pathname): | |
36 outputting = (input_pathname is not None) and (output_pathname is not None) | |
37 | |
38 markdown_parser = markdown.Markdown() | |
39 | |
40 for input_filename in input_filenames: | |
41 markdown_text = ReadFile(input_filename) | |
42 markdown_html = markdown_parser.reset().convert(markdown_text) | |
43 if not outputting: | |
44 continue | |
45 | |
46 # TODO(ttuttle): Sanitize HTML? | |
47 full_html = FormatPage(markdown_html, title=input_filename) | |
48 rel_filename = os.path.relpath(input_filename, start=input_pathname) | |
49 output_filename = os.path.join(output_pathname, rel_filename) + '.html' | |
50 print "%s -> %s -> %s" % (input_filename, rel_filename, output_filename) | |
51 WriteFile(output_filename, full_html) | |
52 | |
53 | |
54 def main(): | |
55 parser = argparse.ArgumentParser( | |
56 description='Parse and render Markdown documentation') | |
57 parser.add_argument('--input_path', default=None, | |
58 help="Input path for Markdown; required only if output_path set") | |
59 parser.add_argument('--output_path', default=None, | |
60 help="Output path for rendered HTML; if unspecified, won't output") | |
61 parser.add_argument('filenames', nargs=argparse.REMAINDER) | |
62 args = parser.parse_args() | |
63 | |
64 print args | |
65 | |
66 ProcessDocs(args.filenames, args.input_path, args.output_path) | |
67 | |
68 return 0 | |
69 | |
70 | |
71 if __name__ == '__main__': | |
72 sys.exit(main()) | |
OLD | NEW |