Commits

reima  committed 5f96484

Generate TOC in a two-pass approach (#34.)

  • Participants
  • Parent commits def11b8

Comments (0)

Files changed (4)

File create-rst.py

   os.mkdir('classes')
 for class_info in db.classes.find():
   with open(os.path.join('classes', '%s.rst' % class_info['id']), 'w') as f:
-    f.write(':tocdepth: 2\n')
-    f.write(':orphan: True\n\n')
+    f.write(':tocdepth: 2\n\n')
     f.write('.. index::\n  single: %s\n' % class_info['name'])
     f.write('.. robin:class:: %s %s\n' % (project, class_info['qualified_name']))
+    f.write('.. include:: %s.toc\n' % class_info['id'])
   with open(os.path.join('classes', '%s-members.rst' % class_info['id']), 'w') as f:
-    f.write(':tocdepth: 2\n')
-    f.write(':orphan: True\n\n')
+    f.write(':tocdepth: 2\n\n')
     f.write('.. robin:classmembers:: %s %s\n' % (project, class_info['qualified_name']))
 
-if not os.path.exists('namespaces'):
-  os.mkdir('namespaces')
-for namespace_info in db.namespaces.find():
-  with open(os.path.join('namespaces', '%s.rst' % namespace_info['id']), 'w') as f:
-    f.write(':tocdepth: 2\n')
-    f.write(':orphan: True\n\n')
-    f.write('.. robin:namespace:: %s %s\n' % (project, namespace_info['qualified_name']))
+# if not os.path.exists('namespaces'):
+#   os.mkdir('namespaces')
+# for namespace_info in db.namespaces.find():
+#   with open(os.path.join('namespaces', '%s.rst' % namespace_info['id']), 'w') as f:
+#     f.write(':tocdepth: 2\n')
+#     f.write(':orphan: True\n\n')
+#     f.write('.. robin:namespace:: %s %s\n' % (project, namespace_info['qualified_name']))
 
 if not os.path.exists('groups'):
   os.mkdir('groups')
 for group_info in db.groups.find():
   with open(os.path.join('groups', '%s.rst' % group_info['id']), 'w') as f:
-    f.write(':tocdepth: 2\n')
-    f.write(':orphan: True\n\n')
+    f.write(':tocdepth: 2\n\n')
     f.write('.. robin:group:: %s %s\n' % (project, group_info['name']))
+    f.write('.. include:: %s.toc\n' % group_info['id'])
 
-with open('groups.rst', 'w') as f:
-  write_header(f, 'Groups')
-  f.write('.. robin:grouptree:: %s\n' % project)
+# with open('groups.rst', 'w') as f:
+#   write_header(f, 'Groups')
+#   f.write('.. robin:grouptree:: %s\n' % project)
 
 if not os.path.exists('pages'):
   os.mkdir('pages')
 for page_info in db.pages.find():
   with open(os.path.join('pages', '%s.rst' % page_info['id']), 'w') as f:
-    f.write(':orphan: True\n\n')
     f.write('.. robin:page:: %s %s\n' % (project, page_info['name']))

File robin/sphinx/__init__.py

 
 """
 
+from sphinx import addnodes
+from docutils import nodes
+
 from domain import RobinDomain
+from nodes import robintocinfo
 
 
 def set_custom_directives(app):
     app.add_directive_to_domain(RobinDomain.name, name, directive)
 
 
+def resolve_toc(app, doctree, fromdocname):
+  env = app.builder.env
+
+  for tocinfo in doctree.traverse(robintocinfo):
+    with open(fromdocname + '.toc', 'w') as f:
+      f.write('.. toctree::\n')
+      f.write('  :hidden:\n\n')
+      for object_id in tocinfo['object_ids']:
+        ref = env.domaindata['robin']['objects'].get(object_id)
+        if ref is None:
+          continue
+        f.write('  /%s\n' % ref['docname'])
+      f.write('\n')
+    tocinfo.replace_self([])
+
+
 def setup(app):
   app.add_domain(RobinDomain)
 
   app.add_config_value('robin_custom_directives', {}, 'env')
   app.add_stylesheet('robin.css')
 
+  app.add_node(robintocinfo)
+
   app.connect('builder-inited', set_custom_directives)
+  app.connect('doctree-resolved', resolve_toc)

File robin/sphinx/directives.py

+from sphinx import addnodes
 from sphinx.util.compat import Directive, make_admonition
 import pymongo
 
 from formatters import *
+from nodes import robintocinfo
 from util import get_connection, create_refnode
 
 
 
     content.extend(header)
 
+    tocinfo = robintocinfo()
+    tocinfo['object_ids'] = []
+    content.append(tocinfo)
+
     # Members
     member_lists = []
     member_docs = []
 
     for group_info in group_infos:
       group_list.extend(group_formatter.format_item(group_info))
+      tocinfo['object_ids'].append(group_info['id'])
 
     if len(group_list):
       list_section = nodes.section()
     for s in compound_info['sections']:
       type_list = nodes.definition_list()
       type_list['classes'].append('robin-type-list')
-      user_section_lists.append(type_list)      
+      user_section_lists.append(type_list)
 
     for section in self._get_type_sections():
       type_list = nodes.definition_list()
 
       for class_info in class_infos:
         type_list.extend(class_formatter.format_item(class_info))
+        tocinfo['object_ids'].append(class_info['id'])
 
       # Typedefs
       typedef_ids = self._get_typedef_ids(compound_info, section)

File robin/sphinx/nodes.py

+from docutils import nodes
+
+class robintocinfo(nodes.General, nodes.Element):
+  pass