Commits

Anonymous committed f8d5baa

First stab at modindex

Comments (0)

Files changed (3)

sphinx/builders/pdf.py

             self.titles.append((docname, entry[2]))
 
     def assemble_doctree(self, docname, title, author):
+        # Page transitions
+        output='.. raw:: pdf\n\n    PageBreak\n\n'
+        pb=docutils.core.publish_doctree(output)[0]
+        output='.. raw:: pdf\n\n    PageBreak oneColumn\n\n'
+        pb_oneColumn=docutils.core.publish_doctree(output)[0]
+        output='.. raw:: pdf\n\n    PageBreak cutePage\n\n'
+        pb_cutePage=docutils.core.publish_doctree(output)[0]
+        output='.. raw:: pdf\n\n    PageBreak twoColumn\n\n'
+        pb_twoColumn=docutils.core.publish_doctree(output)[0]
+        
         self.docnames = set([docname])
         self.info(darkgreen(docname) + " ", nonl=1)
         def process_tree(docname, tree):
         if self.config.pdf_use_index:
             # Add index at the end of the document
             genindex = self.env.create_index(self)
-            pb,index_nodes=genindex_nodes(genindex)
-            tree.append(pb)
+            _pb,index_nodes=genindex_nodes(genindex)
+            tree.append(_pb)
             tree.append(index_nodes)
 
-        # Page transitions
-        output='.. raw:: pdf\n\n    PageBreak\n\n'
-        pb=docutils.core.publish_doctree(output)[0]
-        output='.. raw:: pdf\n\n    PageBreak oneColumn\n\n'
-        pb_oneColumn=docutils.core.publish_doctree(output)[0]
-        output='.. raw:: pdf\n\n    PageBreak cutePage\n\n'
-        pb_cutePage=docutils.core.publish_doctree(output)[0]
+        # This is stolen from the HTML builder
+        if self.config.pdf_use_modindex and self.env.modules:
+            modules = sorted(((mn, ('#module-' + mn, sy, pl, dep)) 
+                for (mn, (fn, sy, pl, dep)) in self.env.modules.iteritems()),
+                key=lambda x: x[0].lower())
+            # collect all platforms
+            platforms = set()
+            letters = []
+            pmn = ''
+            fl = '' # first letter
+            modindexentries = []
+            num_toplevels = 0
+            num_collapsables = 0
+            cg = 0 # collapse group
+            for mn, (fn, sy, pl, dep) in modules:
+                pl = pl and pl.split(', ') or []
+                platforms.update(pl)
+                ignore = self.env.config['modindex_common_prefix']
+                ignore = sorted(ignore, key=len, reverse=True)
+                for i in ignore:
+                    if mn.startswith(i):
+                        mn = mn[len(i):]
+                        stripped = i
+                        break
+                else:
+                    stripped = ''
 
+                if fl != mn[0].lower() and mn[0] != '_':
+                    # heading
+                    letter = mn[0].upper()
+                    if letter not in letters:
+                        modindexentries.append(['', False, 0, False,
+                                                letter, '', [], False, ''])
+                        letters.append(letter)
+                tn = mn.split('.')[0]
+                if tn != mn:
+                    # submodule
+                    if pmn == tn:
+                        # first submodule - make parent collapsable
+                        modindexentries[-1][1] = True
+                        num_collapsables += 1
+                    elif not pmn.startswith(tn):
+                        # submodule without parent in list, add dummy entry
+                        cg += 1
+                        modindexentries.append([tn, True, cg, False, '', '',
+                                                [], False, stripped])
+                else:
+                    num_toplevels += 1
+                    cg += 1
+                modindexentries.append([mn, False, cg, (tn != mn), fn, sy, pl,
+                                        dep, stripped])
+                pmn = mn
+                fl = mn[0].lower()
+            platforms = sorted(platforms)
+            # As some parts of the module names may have been stripped, those
+            # names have changed, thus it is necessary to sort the entries.
+            if ignore:
+                def sorthelper(entry):
+                    name = entry[0]
+                    if name == '':
+                        # heading
+                        name = entry[4]
+                    return name.lower()
 
+                modindexentries.sort(key=sorthelper)
+                letters.sort()
+
+            # Now, let's try to do the same thing
+            # modindex.html does, more or less
+            
+            output=['DUMMY','=====','',
+                    '.. raw:: pdf\n\n    PageBreak twoColumn\n\n.. _modindex:\n\n']
+            t=_('Global Module Index')
+            t+='\n'+'='*len(t)+'\n'
+            output.append(t)
+            
+            for modname, collapse, cgroup, indent,\
+                fname, synops, pform, dep, stripped in modindexentries:
+                
+                if not modname: # A letter
+                    output.append('.. cssclass:: heading4\n\n%s\n\n'%fname)
+                else: # A module
+                    output.append('`%s <%s>`_ '%(stripped or modname,fname))
+                    if pform and pform[0]:
+                        output[-1]+='*(%s)* '%', '.join(pform)
+                    if dep:
+                        output[-1]+='**%s** '%_('Deprecated')
+                    output[-1]+='*%s*'%synops
+            
+            dt = docutils.core.publish_doctree('\n'.join(output))
+            tree.append(pb_twoColumn)
+            tree.extend(dt[1:])
+            
+            
         # Generate Contents topic manually
         contents=nodes.topic(classes=['contents'])
         contents+=nodes.title('')
             # This needs work, need to keep track of all targets
             # so I don't replace and create hanging refs, which
             # crash
-            if pendingnode['reftarget'] in ['genindex']:
+            if pendingnode['reftarget'] == 'genindex'\
+                and self.config.pdf_use_index:
+                pendingnode.replace_self(nodes.reference(text=pendingnode.astext(),
+                    refuri=pendingnode['reftarget']))
+            if pendingnode['reftarget'] == 'modindex'\
+                and self.config.pdf_use_modindex:
                 pendingnode.replace_self(nodes.reference(text=pendingnode.astext(),
                     refuri=pendingnode['reftarget']))
             else:
         pdf_inline_footnotes = (True, None),
         pdf_verbosity = (0,None),
         pdf_use_index = (True, None),
+        pdf_use_modindex = (True, None),
         pdf_use_coverpage = (True, None),
     )
 

sphinx/quickstart.py

 # If false, no index is generated.
 #pdf_use_index = True
 
+# If false, no modindex is generated.
+#pdf_use_modindex = True
+
 # If false, no coverpage is generated.
 #pdf_use_coverpage = True