Ticket #528: render_docs.py.diff

File render_docs.py.diff, 2.2 KB (added by Harkins, 17 years ago)

Patch: a script to render HTML docs

  • extras/render_docs.py

     
     1#!/usr/bin/env python
     2
     3from docutils.core import publish_parts
     4from docutils.writers import html4css1
     5import optparse, os, re, sys
     6
     7# parse with command line
     8parser = optparse.OptionParser()
     9parser.add_option('-1', '--single', action="store_true", dest="single", default=False, help="Write one single combined doc instead of several.")
     10options, dirs = parser.parse_args()
     11if len(dirs) != 2:
     12    print "Need exactly one source and one output directory."
     13    sys.exit(1)
     14source, dest = dirs
     15
     16# find all docs in source dir
     17if options.single:
     18    single = ""
     19files = [f for f in os.listdir(source) if f.endswith('.txt')]
     20files.sort()
     21docs = []
     22for 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
     41if options.single:
     42    toc = "django.html"
     43else:
     44    toc = "index.html"
     45output = open(os.path.join(dest, toc), 'w')
     46output.write(parts['html_prolog'] % 'utf-8')
     47output.write(parts['html_head'] % 'utf-8')
     48output.write('<title>Django Documentation</title></head></body>')
     49output.write('<h1>Table of Contents</h1><ul>')
     50for href, title in docs:
     51    if options.single:
     52        href = "#" + href
     53    output.write('<li><a href="%s">%s</a></li>' % (href, title))
     54output.write('</ul>')
     55if options.single:
     56    output.write(single.encode('utf-8'))
     57output.write('</body></html>')
Back to Top