Code

Ticket #528: render_docs.py.diff

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

Patch: a script to render HTML docs

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