Django

Code

Ticket #528: render_docs.py.2.diff

File render_docs.py.2.diff, 3.2 kB (added by programmerq, 6 months ago)
  • a/extras/README.TXT

    old new  
  • /dev/null

    old new  
     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()