| | 1 | #!/usr/bin/env python |
| | 2 | |
| | 3 | from docutils.core import publish_parts |
| | 4 | from docutils.writers import html4css1 |
| | 5 | import optparse, os, re, sys |
| | 6 | |
| | 7 | # parse with command line |
| | 8 | parser = optparse.OptionParser() |
| | 9 | parser.add_option('-1', '--single', action="store_true", dest="single", default=False, help="Write one single combined doc instead of several.") |
| | 10 | options, dirs = parser.parse_args() |
| | 11 | if len(dirs) != 2: |
| | 12 | print "Need exactly one source and one output directory." |
| | 13 | sys.exit(1) |
| | 14 | source, dest = dirs |
| | 15 | |
| | 16 | # find all docs in source dir |
| | 17 | if options.single: |
| | 18 | single = "" |
| | 19 | files = [f for f in os.listdir(source) if f.endswith('.txt')] |
| | 20 | files.sort() |
| | 21 | docs = [] |
| | 22 | for doc in files: |
| | 23 | out = doc[:-4] + ".html" |
| | 24 | parts = publish_parts(open(os.path.join(source, doc)).read(), writer=html4css1.Writer(), settings_overrides={'initial_header_level': 2}) |
| | 25 | |
| | 26 | # save filename and title for table of contents |
| | 27 | try: |
| | 28 | docs.append([out, parts['title']]) |
| | 29 | except IndexError: |
| | 30 | docs.append([out, 'UNKNOWN (%s)' % doc]) |
| | 31 | |
| | 32 | # append to single doc |
| | 33 | if options.single: |
| | 34 | single += '<a name="%s" />' % out |
| | 35 | single += parts['html_body'] |
| | 36 | # write out the doc |
| | 37 | else: |
| | 38 | open(os.path.join(dest, out), 'w').write(parts['whole'].encode('utf-8')) |
| | 39 | |
| | 40 | # output toc/single doc |
| | 41 | if options.single: |
| | 42 | toc = "django.html" |
| | 43 | else: |
| | 44 | toc = "index.html" |
| | 45 | output = open(os.path.join(dest, toc), 'w') |
| | 46 | output.write(parts['html_prolog'] % 'utf-8') |
| | 47 | output.write(parts['html_head'] % 'utf-8') |
| | 48 | output.write('<title>Django Documentation</title></head></body>') |
| | 49 | output.write('<h1>Table of Contents</h1><ul>') |
| | 50 | for href, title in docs: |
| | 51 | if options.single: |
| | 52 | href = "#" + href |
| | 53 | output.write('<li><a href="%s">%s</a></li>' % (href, title)) |
| | 54 | output.write('</ul>') |
| | 55 | if options.single: |
| | 56 | output.write(single.encode('utf-8')) |
| | 57 | output.write('</body></html>') |