Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
authored July 14, 2005
66  django_website/apps/docs/parts/build_documentation.py
@@ -2,12 +2,16 @@
2 2
 
3 3
 """
4 4
 Script to build the documentation for Django from ReST -> HTML.
  5
+
  6
+Builds each text file in sys.argv (or settings.DJANGO_DOCUMENT_ROOT_PATH) into
  7
+two files: a ".html" file with the document contents and a "_toc.html" file
  8
+with the TOC.
5 9
 """
6 10
 
7 11
 import os
8 12
 import sys
9 13
 import glob
10  
-import locale
  14
+from docutils import nodes, utils
11 15
 from docutils.core import publish_parts
12 16
 from docutils.writers import html4css1
13 17
 from django.conf.settings import DJANGO_DOCUMENT_ROOT_PATH
@@ -16,31 +20,73 @@
16 20
     'initial_header_level': 2
17 21
 }
18 22
 
19  
-locale.setlocale(locale.LC_ALL, '')
20  
-
21 23
 def build(dirs):
22  
-    writer = html4css1.Writer()
23  
-    writer.translator_class = DjangoHTMLTranslator
  24
+    writer = DjangoHTMLWriter()
24 25
     for dir in dirs:
25 26
         for fname in glob.glob1(dir, "*.txt"):
26 27
             in_file = os.path.join(dir, fname)
27 28
             out_file = os.path.join(dir, os.path.splitext(fname)[0] + ".html")
  29
+            toc_file = os.path.join(dir, os.path.splitext(fname)[0] + "_toc.html")
28 30
             parts = publish_parts(
29 31
                 open(in_file).read(),
30 32
                 source_path=in_file,
31 33
                 destination_path=out_file,
32 34
                 writer=writer,
33  
-                settings_overrides={
34  
-                    'initial_header_level' : 2,
35  
-                }
  35
+                settings_overrides=SETTINGS,
36 36
             )
37 37
             open(out_file, 'w').write(parts['html_body'])
  38
+            open(toc_file, 'w').write(parts['toc'])
38 39
 
  40
+class DjangoHTMLWriter(html4css1.Writer):
  41
+    def __init__(self):
  42
+        html4css1.Writer.__init__(self)
  43
+        self.translator_class = DjangoHTMLTranslator
  44
+        
  45
+    def translate(self):
  46
+        # build the document
  47
+        html4css1.Writer.translate(self)
  48
+        
  49
+        # build the contents
  50
+        contents = self.build_contents(self.document)
  51
+        contents_doc = self.document.copy()
  52
+        contents_doc.children = contents
  53
+        contents_visitor = self.translator_class(contents_doc)
  54
+        contents_doc.walkabout(contents_visitor)
  55
+        self.parts['toc'] = "<ul class='toc'>%s</ul>" % ''.join(contents_visitor.fragment)
  56
+        
  57
+    def build_contents(self, node, level=0):
  58
+        level += 1
  59
+        sections = []
  60
+        i = len(node) - 1
  61
+        while i >= 0 and isinstance(node[i], nodes.section):
  62
+            sections.append(node[i])
  63
+            i -= 1
  64
+        sections.reverse()
  65
+        entries = []
  66
+        autonum = 0
  67
+        depth = 4   # XXX FIXME
  68
+        for section in sections:
  69
+            title = section[0]
  70
+            entrytext = title
  71
+            reference = nodes.reference('', '', refid=section['ids'][0], *entrytext)
  72
+            ref_id = self.document.set_id(reference)
  73
+            entry = nodes.paragraph('', '', reference)
  74
+            item = nodes.list_item('', entry)
  75
+            if level < depth:
  76
+                subsects = self.build_contents(section, level)
  77
+                item += subsects
  78
+            entries.append(item)
  79
+        if entries:
  80
+            contents = nodes.bullet_list('', *entries)
  81
+            return contents
  82
+        else:
  83
+            return []
  84
+        
39 85
 class DjangoHTMLTranslator(html4css1.HTMLTranslator):
40  
-    """Remove the damn border=1 from the standard HTML writer"""
41 86
     def visit_table(self, node):
  87
+        """Remove the damn border=1 from the standard HTML writer"""
42 88
         self.body.append(self.starttag(node, 'table', CLASS='docutils'))
43  
-
  89
+        
44 90
 if __name__ == "__main__":
45 91
     if len(sys.argv) > 1:
46 92
         build(sys.argv[1:])

0 notes on commit db84abe

Please sign in to comment.
Something went wrong with that request. Please try again.