diff options
author | gbrandl <devnull@localhost> | 2009-02-10 14:00:57 +0100 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2009-02-10 14:00:57 +0100 |
commit | 2c3612fe7c4e32662a9d5f00c7a6fc46fdae71b7 (patch) | |
tree | 3e9fde72574f8b76fe7d3906071d0a91617d1102 /pygments/formatters/html.py | |
parent | 7d01c58da95d925844f0d5aa8f7f66d728aa85ec (diff) | |
download | pygments-2c3612fe7c4e32662a9d5f00c7a6fc46fdae71b7.tar.gz |
imported patch anchor-linenos.patch
Diffstat (limited to 'pygments/formatters/html.py')
-rw-r--r-- | pygments/formatters/html.py | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index 25d1158e..caea150a 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -274,6 +274,10 @@ class HtmlFormatter(Formatter): 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 @@ class HtmlFormatter(Formatter): 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 @@ class HtmlFormatter(Formatter): 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... |