Commits

Pepijn de Vos committed afbb67c

single file ctags

Comments (0)

Files changed (1)

pygments/formatters/html.py

 from pygments.token import Token, Text, STANDARD_TYPES
 from pygments.util import get_bool_opt, get_int_opt, get_list_opt, bytes
 
+try:
+    from ctags import CTags, TagEntry
+except ImportError:
+    pass
 
 __all__ = ['HtmlFormatter']
 
         self.prestyles = self._decodeifneeded(options.get('prestyles', ''))
         self.cssfile = self._decodeifneeded(options.get('cssfile', ''))
         self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
+        self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
+
+        if self.tagsfile:
+            try:
+                self.ct = CTags(self.tagsfile)
+            except NameError:
+                print >> sys.stderr, 'Hey! ctags doesn\'t seem to be installed. Try \'pip install python-ctags\'.'
 
         linenos = options.get('linenos', False)
         if linenos == 'inline':
         # for <span style=""> lookup only
         getcls = self.ttype2class.get
         c2s = self.class2style
+        tagsfile = self.tagsfile
         escape_table = _escape_html_table
 
         lspan = ''
 
             parts = value.translate(escape_table).split('\n')
 
+            if tagsfile and ttype in Token.Name:
+                filename, lineNumber = self._lookup_ctag(value)
+                if filename:
+                    parts[0] = "<a href=\"#%s-%s\">%s" % (self.lineanchors, lineNumber, parts[0])
+                    parts[-1] = "%s</a>" % parts[-1]
+
             # for all but the last line
             for part in parts[:-1]:
                 if line:
         if line:
             yield 1, line + (lspan and '</span>') + lsep
 
+    def _lookup_ctag(self, token):
+        entry = TagEntry()
+        if self.ct.find(entry, token, 0):
+            return entry['file'], entry['lineNumber']
+        else:
+            return None, None
+
     def _highlight_lines(self, tokensource):
         """
         Highlighted the lines specified in the `hl_lines` option by
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.