summaryrefslogtreecommitdiff
path: root/pygments/formatters
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/formatters')
-rw-r--r--pygments/formatters/__init__.py4
-rw-r--r--pygments/formatters/html.py4
-rw-r--r--pygments/formatters/svg.py40
-rw-r--r--pygments/formatters/terminal256.py14
4 files changed, 53 insertions, 9 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py
index 4fa4178e..6f1130a8 100644
--- a/pygments/formatters/__init__.py
+++ b/pygments/formatters/__init__.py
@@ -108,8 +108,8 @@ def load_formatter_from_file(filename, formattername="CustomFormatter",
# And finally instantiate it with the options
return formatter_class(**options)
except IOError as err:
- raise ClassNotFound('cannot read %s' % filename)
- except ClassNotFound as err:
+ raise ClassNotFound('cannot read %s: %s' % (filename, err))
+ except ClassNotFound:
raise
except Exception as err:
raise ClassNotFound('error when loading custom formatter: %s' % err)
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 042f04cf..be9d8534 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -61,7 +61,7 @@ def _get_ttype_class(ttype):
CSSFILE_TEMPLATE = '''\
/*
-generated by Pygments <http://pygments.org>
+generated by Pygments <https://pygments.org/>
Copyright 2006-2019 by the Pygments team.
Licensed under the BSD license, see LICENSE for details.
*/
@@ -75,7 +75,7 @@ DOC_HEADER = '''\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!--
-generated by Pygments <http://pygments.org>
+generated by Pygments <https://pygments.org/>
Copyright 2006-2019 by the Pygments team.
Licensed under the BSD license, see LICENSE for details.
-->
diff --git a/pygments/formatters/svg.py b/pygments/formatters/svg.py
index ccfd2b3f..fb75e494 100644
--- a/pygments/formatters/svg.py
+++ b/pygments/formatters/svg.py
@@ -10,6 +10,7 @@
"""
from pygments.formatter import Formatter
+from pygments.token import Comment
from pygments.util import get_bool_opt, get_int_opt
__all__ = ['SvgFormatter']
@@ -52,6 +53,19 @@ class SvgFormatter(Formatter):
The value to give the wrapping ``<g>`` element's ``font-size``
attribute, defaults to ``"14px"``.
+ `linenos`
+ If ``True``, add line numbers (default: ``False``).
+
+ `linenostart`
+ The line number for the first line (default: ``1``).
+
+ `linenostep`
+ If set to a number n > 1, only every nth line number is printed.
+
+ `linenowidth`
+ Maximum width devoted to line numbers (default: ``3*ystep``, sufficient
+ for up to 4-digit line numbers. Increase width for longer code blocks).
+
`xoffset`
Starting offset in X direction, defaults to ``0``.
@@ -92,6 +106,10 @@ class SvgFormatter(Formatter):
self.yoffset = get_int_opt(options, 'yoffset', int_fs)
self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
self.spacehack = get_bool_opt(options, 'spacehack', True)
+ self.linenos = get_bool_opt(options,'linenos',False)
+ self.linenostart = get_int_opt(options,'linenostart',1)
+ self.linenostep = get_int_opt(options,'linenostep',1)
+ self.linenowidth = get_int_opt(options,'linenowidth', 3*self.ystep)
self._stylecache = {}
def format_unencoded(self, tokensource, outfile):
@@ -115,7 +133,19 @@ class SvgFormatter(Formatter):
outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
outfile.write('<g font-family="%s" font-size="%s">\n' %
(self.fontfamily, self.fontsize))
- outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (x, y))
+
+ counter = self.linenostart
+ counter_step = self.linenostep
+ counter_style = self._get_style(Comment)
+ line_x = x
+
+ if self.linenos:
+ if counter % counter_step == 0:
+ outfile.write('<text x="%s" y="%s" %s text-anchor="end">%s</text>' % (x+self.linenowidth,y,counter_style,counter))
+ line_x += self.linenowidth + self.ystep
+ counter += 1
+
+ outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (line_x, y))
for ttype, value in tokensource:
style = self._get_style(ttype)
tspan = style and '<tspan' + style + '>' or ''
@@ -127,8 +157,12 @@ class SvgFormatter(Formatter):
for part in parts[:-1]:
outfile.write(tspan + part + tspanend)
y += self.ystep
- outfile.write('</text>\n<text x="%s" y="%s" '
- 'xml:space="preserve">' % (x, y))
+ outfile.write('</text>\n')
+ if self.linenos and counter % counter_step == 0:
+ outfile.write('<text x="%s" y="%s" text-anchor="end" %s>%s</text>' % (x+self.linenowidth,y,counter_style,counter))
+
+ counter += 1
+ outfile.write('<text x="%s" y="%s" ' 'xml:space="preserve">' % (line_x,y))
outfile.write(tspan + parts[-1] + tspanend)
outfile.write('</text>')
diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py
index 43ec01c2..b6bf9376 100644
--- a/pygments/formatters/terminal256.py
+++ b/pygments/formatters/terminal256.py
@@ -35,11 +35,12 @@ __all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter']
class EscapeSequence:
- def __init__(self, fg=None, bg=None, bold=False, underline=False):
+ def __init__(self, fg=None, bg=None, bold=False, underline=False, italic=False):
self.fg = fg
self.bg = bg
self.bold = bold
self.underline = underline
+ self.italic = italic
def escape(self, attrs):
if len(attrs):
@@ -68,6 +69,8 @@ class EscapeSequence:
attrs.append("01")
if self.underline:
attrs.append("04")
+ if self.italic:
+ attrs.append("03")
return self.escape(attrs)
def true_color_string(self):
@@ -80,6 +83,8 @@ class EscapeSequence:
attrs.append("01")
if self.underline:
attrs.append("04")
+ if self.italic:
+ attrs.append("03")
return self.escape(attrs)
def reset_string(self):
@@ -88,7 +93,7 @@ class EscapeSequence:
attrs.append("39")
if self.bg is not None:
attrs.append("49")
- if self.bold or self.underline:
+ if self.bold or self.underline or self.italic:
attrs.append("00")
return self.escape(attrs)
@@ -135,6 +140,7 @@ class Terminal256Formatter(Formatter):
self.usebold = 'nobold' not in options
self.useunderline = 'nounderline' not in options
+ self.useitalic = 'noitalic' not in options
self._build_color_table() # build an RGB-to-256 color conversion table
self._setup_styles() # convert selected style's colors to term. colors
@@ -227,6 +233,8 @@ class Terminal256Formatter(Formatter):
escape.bold = True
if self.useunderline and ndef['underline']:
escape.underline = True
+ if self.useitalic and ndef['italic']:
+ escape.italic = True
self.style_string[str(ttype)] = (escape.color_string(),
escape.reset_string())
@@ -311,5 +319,7 @@ class TerminalTrueColorFormatter(Terminal256Formatter):
escape.bold = True
if self.useunderline and ndef['underline']:
escape.underline = True
+ if self.useitalic and ndef['italic']:
+ escape.italic = True
self.style_string[str(ttype)] = (escape.true_color_string(),
escape.reset_string())