Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Document builder now works!

git-svn-id: http://code.djangoproject.com/svn/djangoproject.com@35 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit db84abe4c429f847ad30700197b0960562b0e2ad 1 parent 1a02136
jacob authored
Showing with 56 additions and 10 deletions.
  1. +56 −10 django_website/apps/docs/parts/build_documentation.py
View
66 django_website/apps/docs/parts/build_documentation.py
@@ -2,12 +2,16 @@
"""
Script to build the documentation for Django from ReST -> HTML.
+
+Builds each text file in sys.argv (or settings.DJANGO_DOCUMENT_ROOT_PATH) into
+two files: a ".html" file with the document contents and a "_toc.html" file
+with the TOC.
"""
import os
import sys
import glob
-import locale
+from docutils import nodes, utils
from docutils.core import publish_parts
from docutils.writers import html4css1
from django.conf.settings import DJANGO_DOCUMENT_ROOT_PATH
@@ -16,31 +20,73 @@
'initial_header_level': 2
}
-locale.setlocale(locale.LC_ALL, '')
-
def build(dirs):
- writer = html4css1.Writer()
- writer.translator_class = DjangoHTMLTranslator
+ writer = DjangoHTMLWriter()
for dir in dirs:
for fname in glob.glob1(dir, "*.txt"):
in_file = os.path.join(dir, fname)
out_file = os.path.join(dir, os.path.splitext(fname)[0] + ".html")
+ toc_file = os.path.join(dir, os.path.splitext(fname)[0] + "_toc.html")
parts = publish_parts(
open(in_file).read(),
source_path=in_file,
destination_path=out_file,
writer=writer,
- settings_overrides={
- 'initial_header_level' : 2,
- }
+ settings_overrides=SETTINGS,
)
open(out_file, 'w').write(parts['html_body'])
+ open(toc_file, 'w').write(parts['toc'])
+class DjangoHTMLWriter(html4css1.Writer):
+ def __init__(self):
+ html4css1.Writer.__init__(self)
+ self.translator_class = DjangoHTMLTranslator
+
+ def translate(self):
+ # build the document
+ html4css1.Writer.translate(self)
+
+ # build the contents
+ contents = self.build_contents(self.document)
+ contents_doc = self.document.copy()
+ contents_doc.children = contents
+ contents_visitor = self.translator_class(contents_doc)
+ contents_doc.walkabout(contents_visitor)
+ self.parts['toc'] = "<ul class='toc'>%s</ul>" % ''.join(contents_visitor.fragment)
+
+ def build_contents(self, node, level=0):
+ level += 1
+ sections = []
+ i = len(node) - 1
+ while i >= 0 and isinstance(node[i], nodes.section):
+ sections.append(node[i])
+ i -= 1
+ sections.reverse()
+ entries = []
+ autonum = 0
+ depth = 4 # XXX FIXME
+ for section in sections:
+ title = section[0]
+ entrytext = title
+ reference = nodes.reference('', '', refid=section['ids'][0], *entrytext)
+ ref_id = self.document.set_id(reference)
+ entry = nodes.paragraph('', '', reference)
+ item = nodes.list_item('', entry)
+ if level < depth:
+ subsects = self.build_contents(section, level)
+ item += subsects
+ entries.append(item)
+ if entries:
+ contents = nodes.bullet_list('', *entries)
+ return contents
+ else:
+ return []
+
class DjangoHTMLTranslator(html4css1.HTMLTranslator):
- """Remove the damn border=1 from the standard HTML writer"""
def visit_table(self, node):
+ """Remove the damn border=1 from the standard HTML writer"""
self.body.append(self.starttag(node, 'table', CLASS='docutils'))
-
+
if __name__ == "__main__":
if len(sys.argv) > 1:
build(sys.argv[1:])
Please sign in to comment.
Something went wrong with that request. Please try again.