Commits

Chris McDonough committed 2e666ce

- Add LaTeX references and labels.

- Generate references after section header.

- Include pageref in internal references.

- Include URL in external href renderings.

Comments (0)

Files changed (1)

sphinx/writers/latex.py

         self.literal_whitespace = 0
         self.no_contractions = 0
         self.compact_list = 0
+        self.section_references = []
 
     def astext(self):
         return (HEADER % self.elements + self.highlighter.get_stylesheet() +
         if not self.this_is_the_title:
             self.sectionlevel += 1
         self.body.append('\n\n')
-        if self.next_section_target:
-            self.body.append(r'\hypertarget{%s}{}' % self.next_section_target)
+        ns_target = self.next_section_target
+        if ns_target:
+            self.section_references.append(
+                r'\hypertarget{%s}{}\label{%s}' % (ns_target, ns_target))
             self.next_section_target = None
         #if node.get('ids'):
         #    for id in node['ids']:
     def depart_title(self, node):
         self.in_title = 0
         self.body.append(self.context.pop())
+        if self.section_references:
+            self.body.append(self.section_references.pop())
 
     def visit_subtitle(self, node):
         if isinstance(node.parent, nodes.sidebar):
         d = self.descstack[-1]
         d.cls = d.cls.rstrip('.')
         if node.parent['desctype'] != 'describe' and node['ids']:
-            hyper = '\\hypertarget{%s}{}' % node['ids'][0]
+            hyper = r'\hypertarget{%s}{}\label{%s}' % (node['ids'][0],
+                                                       node['ids'][0])
         else:
             hyper = ''
         if d.count == 0:
-            t1 = "\n\n%s\\begin{%s}" % (hyper, d.env)
+            t1 = "\n\n\\begin{%s}" % (d.env)
         else:
-            t1 = "\n%s\\%sline" % (hyper, d.env[:-4])
+            t1 = "\n\\%sline" % (d.env[:-4])
         d.count += 1
         if d.env in ('funcdesc', 'classdesc', 'excclassdesc'):
             t2 = "{%s}{%s}" % (d.name, d.params)
             t2 = "{%s}{%s}" % (d.name, d.params)
         elif d.env == 'describe':
             t2 = "{%s}" % d.name
-        self.body.append(t1 + t2)
+        self.body.append(t1 + t2 + hyper)
 
     def visit_desc_type(self, node):
         d = self.descstack[-1]
 
     def visit_module(self, node):
         modname = node['modname']
-        self.body.append('\n\\hypertarget{module-%s}{}' %
-                         (modname.replace(' ','')))
+        modname2 = modname.replace(' ','')
+        self.body.append('\n\\hypertarget{module-%s}{}\label{module-%s}' %
+                         (modname2, modname2))
         self.body.append('\n\\declaremodule[%s]{}{%s}' % (
             modname.replace('_', ''), self.encode(modname)))
         self.body.append('\n\\modulesynopsis{%s}' %
             # indexing uses standard LaTeX index markup, so the targets
             # will be generated differently
             if not id.startswith('index-'):
-                self.body.append(r'\hypertarget{%s}{}' % id)
+                self.section_references.append(
+                    r'\hypertarget{%s}{}\label{%s}' % (id ,id))
 
         if node.has_key('refid') and node['refid'] not in self.written_ids:
             parindex = node.parent.index(node)
                            node.line or ''))
             self.context.append('')
     def depart_reference(self, node):
+        uri = node.get('refuri', '')
+        if uri.startswith('mailto:') or uri.startswith('http:') or \
+             uri.startswith('https:') or uri.startswith('ftp:'):
+            self.body.append(' (%s)' % self.encode_uri(uri))
+        elif uri.startswith('#'):
+            self.body.append(' (pp. \pageref{%s})' % uri[1:])
+        elif uri.startswith('%') and not self.in_title:
+            if not uri.startswith('%glossary'):
+                hashindex = uri.find('#')
+                targetname = (hashindex == -1) and '--doc-' + uri[1:] \
+                                               or uri[hashindex+1:]
+                pp = ' (pp. \pageref{%s})' % targetname
+                self.body.append(pp)
+            
         self.body.append(self.context.pop())
 
     def visit_download_reference(self, node):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.