diff options
author | gbrandl <devnull@localhost> | 2007-07-22 19:55:49 +0200 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-07-22 19:55:49 +0200 |
commit | 9a6d5c7859f76f54de13351cba15d0a9006def9a (patch) | |
tree | 3bfcc4a3df89f8fecfd85268156801b43a4a022b | |
parent | bf7d76db34170d464e55bacb69d4ed3d0f408c66 (diff) | |
download | pygments-9a6d5c7859f76f54de13351cba15d0a9006def9a.tar.gz |
[svn] Add "lineanchors" option to HTML formatter.
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | pygments/formatters/html.py | 18 |
2 files changed, 23 insertions, 0 deletions
@@ -9,12 +9,17 @@ Version 0.9 enhanced. You shouldn't get UnicodeErrors from it anymore if you don't give an encoding option. +- Added the ``lineanchors`` option to the HTML formatter, thanks to + Ian Charnas for the idea. + - Added 256-color terminal formatter. - Added ActionScript lexer. - Added Gettext catalog lexer. +- Fixed bugs in the D and MiniD lexer. + Version 0.8.1 ------------- diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index cdc1c1ce..31135428 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -246,6 +246,11 @@ class HtmlFormatter(Formatter): e.g. set it to ``"<br>"`` to get HTML line breaks. *New in Pygments 0.7.* + `lineanchors` + If set to a nonempty string, e.g. ``foo``, the formatter will wrap each + output line in an anchor tag with a ``name`` of ``foo-linenumber``. + This allows easy linking to certain lines. *New in Pygments 0.9.* + **Subclassing the HTML formatter** @@ -321,6 +326,7 @@ class HtmlFormatter(Formatter): self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0)) self.nobackground = get_bool_opt(options, 'nobackground', False) self.lineseparator = options.get('lineseparator', '\n') + self.lineanchors = options.get('lineanchors', '') self._class_cache = {} self._create_stylesheet() @@ -479,6 +485,16 @@ class HtmlFormatter(Formatter): mw, (num%st and ' ' or num)) + line num += 1 + def _wrap_lineanchors(self, inner): + s = self.lineanchors + i = 0 + for t, line in inner: + if t: + i += 1 + yield 1, '<a name="%s-%d"></a>' % (s, i) + line + else: + yield 0, line + def _wrap_div(self, inner): yield 0, ('<div' + (self.cssclass and ' class="%s"' % self.cssclass) + (self.cssstyles and ' style="%s"' % self.cssstyles) + '>') @@ -577,6 +593,8 @@ class HtmlFormatter(Formatter): if not self.nowrap: if self.linenos == 2: source = self._wrap_inlinelinenos(source) + if self.lineanchors: + source = self._wrap_lineanchors(source) source = self.wrap(source, outfile) if self.linenos == 1: source = self._wrap_tablelinenos(source) |