Mercurial > mozilla > hg > DocumentIt
annotate document_it.py @ 11:df6c2e71b87e
actually render
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 01 Aug 2011 23:25:46 -0700 |
parents | 853214384bd0 |
children | 54e27f046cd2 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
9 | 4 update MDN documentation from markdown |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
5 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
6 see: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
7 http://developer.mindtouch.com/en/ref/MindTouch_API/POST%3Apages%2F%2F%7Bpageid%7D%2F%2Fcontents |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
8 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
9 The manifest format is in the form: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
10 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
11 mozrunner/README.txt https://developer.mozilla.org/en/Mozrunner |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
12 jsbridge/README.txt https://developer.mozilla.org/en/JSbridge |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
13 mozmill/README.txt https://developer.mozilla.org/en/Mozmill |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
14 mozmill/docs/ https://developer.mozilla.org/en/Mozmill/ |
9 | 15 |
16 --dest sets the destination, e.g. | |
17 | |
18 --dest http://developer.mozilla.org/ | |
19 --dest https://developer-stage9.mozilla.org/jhammel | |
20 --dest path/to directory | |
21 | |
22 By default, a new temporary directory will be created | |
0 | 23 """ |
24 | |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
25 import optparse |
0 | 26 import os |
27 import sys | |
9 | 28 import tempfile |
0 | 29 import urllib2 |
30 | |
31 # necessary imports | |
32 try: | |
33 import markdown | |
34 except ImportError: | |
35 raise ImportError("markdown is not installed, run (e.g.):\neasy_install Markdown") | |
36 | |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
37 DIR=os.path.dirname(os.path.abspath(__file__)) # XXX currently unused |
0 | 38 |
39 def find_readme(directory): | |
40 """find a README file in a directory""" | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
41 # XXX currently unused |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
42 README=['README.md', 'README.txt', 'README'] |
0 | 43 for name in README: |
44 path = os.path.join(directory, name) | |
45 if os.path.exists(path): | |
46 return path | |
47 | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
48 def parse_manifest(filename, directory=None): |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
49 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
50 reads a documentation manifest; returns a list of two-tuples: |
6 | 51 [(filename, destination)] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
52 """ |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
53 |
6 | 54 assert os.path.exists(filename) and os.path.isfile(filename), "%s not found" % filename |
55 | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
56 if directory is None: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
57 directory = os.path.dirname(os.path.abspath(filename)) |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
58 lines = [line.strip() for line in file(filename).readlines()] |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
59 lines = [line for line in lines |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
60 if line and not line.startswith('#')] |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
61 items = [] |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
62 for line in lines: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
63 try: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
64 f, url = line.split() |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
65 # TODO: include options as third segment (e.g. format=ReST) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
66 except ValueError: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
67 raise ValueError("illegal manifest line: '%s'" % line) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
68 |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
69 filename = os.path.join(directory, f) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
70 if os.path.isdir(filename): |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
71 pass # TODO |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
72 else: |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
73 items.append((filename, url)) |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
74 return items |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
75 |
0 | 76 def main(args=sys.argv[1:]): |
77 | |
9 | 78 # default output directory |
79 default_dir = tempfile.mktemp() | |
80 | |
0 | 81 # parse command line options |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
82 usage = '%prog [options] manifest <manifest> <...>' |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
83 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
84 # description formatter |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
85 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
86 def format_description(self, description): |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
87 if description: |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
88 return description + '\n' |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
89 else: |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
90 return '' |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
91 |
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
92 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
0 | 93 parser.add_option('-d', '--directory', dest='directory', |
6 | 94 help='render the documentation from this directory') |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
95 parser.add_option('--dest', dest='dest', |
9 | 96 default=default_dir, |
97 help='base directory or URL of destination [DEFAULT: %default]') | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
98 parser.add_option('-u', '--user', dest='user', |
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
99 help='user name') |
6 | 100 parser.add_option('--list', dest='list', action='store_true', default=False, |
101 help="list files") | |
9 | 102 parser.add_option('--validate', dest='validate', # TODO unused |
103 action='store_true', default=False, | |
104 help="validate the rendering but don't output") | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
105 options, manifests = parser.parse_args(args) |
0 | 106 |
6 | 107 # print help if no manifests given |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
108 if not args: |
5
3464eda1af80
better formatting for command line
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
109 parser.print_help() |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
110 parser.exit() |
0 | 111 |
7 | 112 # get base url |
11 | 113 assert options.dest |
7 | 114 if '://' in options.dest: |
115 baseurl = options.dest | |
116 else: | |
117 baseurl = 'file://' + options.dest | |
118 | |
6 | 119 # read the manifests |
120 files = [] | |
121 for manifest in manifests: | |
11 | 122 for item in parse_manifest(manifest, options.directory): |
6 | 123 if item not in files: |
124 files.append(item) | |
125 if options.list: | |
126 for item in files: | |
7 | 127 print '%s -> %s/%s' % (item[0], baseurl.rstrip('/'), item[1].lstrip('/')) |
0 | 128 |
9 | 129 if not files: |
130 return # you're done | |
131 | |
0 | 132 # render and upload READMEs |
11 | 133 if baseurl.startswith('file://'): |
134 options.dest = baseurl[len('file://'):] # deals with --dest file:///foo from command line | |
8 | 135 |
11 | 136 # ensure a directory |
137 if os.path.exists(options.dest): | |
138 assert os.path.isdir(options.dest), "'%s' - not a directory" % options.dest | |
8 | 139 |
140 # TODO render to directory | |
9 | 141 for src, dest in files: |
142 dest = os.path.join(options.dest, dest) | |
11 | 143 |
144 # create a directory if needed | |
145 dirname = os.path.dirname(dest) | |
146 os.makedirs(dirname) | |
147 | |
148 # render | |
149 f = file(dest, 'w') | |
150 buffer = markdown.Markdown().convert(file(src).read()) | |
151 f.write(buffer) | |
152 f.close() | |
6 | 153 else: |
154 # TODO check credentials | |
155 raise NotImplementedError | |
4
550f4b240e20
make almost look like what we want to do
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
156 |
11 | 157 |
10 | 158 if options.dest == default_dir: |
11 | 159 print "Files rendered to\n%s" % default_dir |
9 | 160 |
0 | 161 if __name__ == '__main__': |
162 main() |