Ticket #528: render_docs.py.2.diff

File render_docs.py.2.diff, 3.2 KB (added by Jeff Anderson, 17 years ago)
  • extras/README.TXT

    :100644 100644 3fb2a84... dd3cd26... M	extras/README.TXT
    :000000 100644 0000000... 06172d5... A	extras/render_docs.py
    
    diff --git a/extras/README.TXT b/extras/README.TXT
    index 3fb2a84..dd3cd26 100644
    a b  
    11This directory contains extra stuff that can improve your Django experience.
     2
     3
     4render_docs.py
     5
     6This is a python script that generates html from your django documentation.
     7It is run from the command line, and takes two arguments.
     8
     9$ python render_docs.py <source directory> <destination directory>
     10
     11Optionally, if you want it to generate one big page instead of multiple smaller
     12ones, you can supply the --single argument.
     13
     14$ python render_docs.py --single <source directory> <destination directory>
     15
  • new file extras/render_docs.py

    diff --git a/extras/render_docs.py b/extras/render_docs.py
    new file mode 100644
    index 0000000..06172d5
    - +  
     1#!/usr/bin/env python
     2
     3import optparse
     4import os
     5import re
     6import sys
     7
     8try:
     9    from docutils.core import publish_parts
     10    from docutils.writers import html4css1
     11except:
     12    print "The python module docutils is required to run this script."
     13    print "It doesn't seem like it is installed in your pythonpath."
     14    sys.exit(1)
     15
     16def main():
     17    # parse with command line
     18    parser = optparse.OptionParser()
     19    parser.add_option('-1', '--single', action="store_true", dest="single", default=False, help="Write one single combined doc instead of several.")
     20    options, dirs = parser.parse_args()
     21    if len(dirs) != 2:
     22        print "Need exactly one source and one output directory."
     23        sys.exit(1)
     24    source, dest = dirs
     25   
     26    # find all docs in source dir
     27    if options.single:
     28        single = ""
     29    files = [f for f in os.listdir(source) if f.endswith('.txt')]
     30    files.sort()
     31    docs = []
     32    for doc in files:
     33        out = doc[:-4] + ".html"
     34        parts = publish_parts(open(os.path.join(source, doc)).read(), writer=html4css1.Writer(), settings_overrides={'initial_header_level': 2})
     35   
     36        # save filename and title for table of contents
     37        try:
     38            docs.append([out, parts['title']])
     39        except IndexError:
     40            docs.append([out, 'UNKNOWN (%s)' % doc])
     41   
     42        # append to single doc
     43        if options.single:
     44            single += '<a name="%s" />' % out
     45            single += parts['html_body']
     46        # write out the doc
     47        else:
     48            open(os.path.join(dest, out), 'w').write(parts['whole'].encode('utf-8'))
     49   
     50    # output toc/single doc
     51    if options.single:
     52        toc = "django.html"
     53    else:
     54        toc = "index.html"
     55    output = open(os.path.join(dest, toc), 'w')
     56    output.write(parts['html_prolog'] % 'utf-8')
     57    output.write(parts['html_head'] % 'utf-8')
     58    output.write('<title>Django Documentation</title></head></body>')
     59    output.write('<h1>Table of Contents</h1><ul>')
     60    for href, title in docs:
     61        if options.single:
     62            href = "#" + href
     63        output.write('<li><a href="%s">%s</a></li>' % (href, title))
     64    output.write('</ul>')
     65    if options.single:
     66        output.write(single.encode('utf-8'))
     67    output.write('</body></html>')
     68
     69if __name__ == "__main__":
     70    main()
Back to Top