pygments-anchorlinenos / anchor-linenos.patch

diff -r c20574958ca5 pygments/formatters/html.py
--- a/pygments/formatters/html.py	Sun Feb 08 23:33:48 2009 +0100
+++ b/pygments/formatters/html.py	Tue Feb 10 13:33:57 2009 +0100
@@ -274,6 +274,10 @@
         output line in an anchor tag with a ``name`` of ``foo-linenumber``.
         This allows easy linking to certain lines. *New in Pygments 0.9.*
 
+    `anchorlinenos`
+        If set to `True`, will wrap line numbers in <a> tags. Used in
+        combination with `linenos` and `lineanchors`.
+
 
     **Subclassing the HTML formatter**
 
@@ -352,6 +356,7 @@
         self.nobackground = get_bool_opt(options, 'nobackground', False)
         self.lineseparator = options.get('lineseparator', '\n')
         self.lineanchors = options.get('lineanchors', '')
+        self.anchorlinenos = options.get('anchorlinenos', False)
         self.hl_lines = set()
         for lineno in get_list_opt(options, 'hl_lines', []):
             try:
@@ -490,16 +495,45 @@
         mw = len(str(lncount + fl - 1))
         sp = self.linenospecial
         st = self.linenostep
+        la = self.lineanchors
+        aln = self.anchorlinenos
         if sp:
-            ls = '\n'.join([(i%st == 0 and
-                             (i%sp == 0 and '<span class="special">%*d</span>'
-                              or '%*d') % (mw, i)
-                             or '')
-                            for i in range(fl, fl + lncount)])
+            lines = [ ]
+            
+            for i in range(fl, fl+lncount):
+                if i % st == 0:
+                    if i % sp == 0:
+                        if aln:
+                            lines.append('<a href="#%s-%d"' + \
+                                ' class="special">%*d</a>' % (la, i, mw, i))
+                        else:                            
+                            lines.append('<span class="special">%*d</span>' \
+                                            % (mw, i))
+                    else:
+                        if aln:
+                            lines.append('<a href="#%s-%d">%*d</a>' \
+                                            % (la, i, mw, i))
+                        else:
+                            lines.append('%*d' % (mw, i))
+                else:
+                    lines.append('')
+                    
+            ls = '\n'.join(lines)
+                
         else:
-            ls = '\n'.join([(i%st == 0 and ('%*d' % (mw, i)) or '')
-                            for i in range(fl, fl + lncount)])
-
+            lines = [ ]
+            for i in range(fl, fl+lncount):
+                if i % st == 0:
+                    if aln:
+                        lines.append('<a href="#%s-%d">%*d</a>' \
+                                        % (la, i, mw, i))
+                    else:
+                        lines.append('%*d' % (mw, i))
+                else:
+                    lines.append('')
+            
+            ls = '\n'.join(lines)
+            
         # in case you wonder about the seemingly redundant <div> here: since the
         # content in the other cell also is wrapped in a div, some browsers in
         # some configurations seem to mess up the formatting...
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.