Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
MoinMoin.support.pygments.formatters.svg.SvgFormatter Class Reference
Inheritance diagram for MoinMoin.support.pygments.formatters.svg.SvgFormatter:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.pygments.formatters.svg.SvgFormatter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def format_unencoded

Public Attributes

 nowrap
 fontfamily
 fontsize
 xoffset
 yoffset
 ystep
 spacehack

Static Public Attributes

string name = 'SVG'
list aliases = ['svg']
list filenames = ['*.svg']

Private Member Functions

def _get_style

Private Attributes

 _stylecache

Detailed Description

Format tokens as an SVG graphics file.  This formatter is still experimental.
Each line of code is a ``<text>`` element with explicit ``x`` and ``y``
coordinates containing ``<tspan>`` elements with the individual token styles.

By default, this formatter outputs a full SVG document including doctype
declaration and the ``<svg>`` root element.

*New in Pygments 0.9.*

Additional options accepted:

`nowrap`
    Don't wrap the SVG ``<text>`` elements in ``<svg><g>`` elements and
    don't add a XML declaration and a doctype.  If true, the `fontfamily`
    and `fontsize` options are ignored.  Defaults to ``False``.

`fontfamily`
    The value to give the wrapping ``<g>`` element's ``font-family``
    attribute, defaults to ``"monospace"``.

`fontsize`
    The value to give the wrapping ``<g>`` element's ``font-size``
    attribute, defaults to ``"14px"``.

`xoffset`
    Starting offset in X direction, defaults to ``0``.

`yoffset`
    Starting offset in Y direction, defaults to the font size if it is given
    in pixels, or ``20`` else.  (This is necessary since text coordinates
    refer to the text baseline, not the top edge.)

`ystep`
    Offset to add to the Y coordinate for each subsequent line.  This should
    roughly be the text size plus 5.  It defaults to that value if the text
    size is given in pixels, or ``25`` else.

`spacehack`
    Convert spaces in the source to ``&#160;``, which are non-breaking
    spaces.  SVG provides the ``xml:space`` attribute to control how
    whitespace inside tags is handled, in theory, the ``preserve`` value
    could be used to keep all whitespace as-is.  However, many current SVG
    viewers don't obey that rule, so this option is provided as a workaround
    and defaults to ``True``.

Definition at line 29 of file svg.py.


Constructor & Destructor Documentation

Definition at line 80 of file svg.py.

00080 
00081     def __init__(self, **options):
00082         # XXX outencoding
00083         Formatter.__init__(self, **options)
00084         self.nowrap = get_bool_opt(options, 'nowrap', False)
00085         self.fontfamily = options.get('fontfamily', 'monospace')
00086         self.fontsize = options.get('fontsize', '14px')
00087         self.xoffset = get_int_opt(options, 'xoffset', 0)
00088         fs = self.fontsize.strip()
00089         if fs.endswith('px'): fs = fs[:-2].strip()
00090         try:
00091             int_fs = int(fs)
00092         except:
00093             int_fs = 20
00094         self.yoffset = get_int_opt(options, 'yoffset', int_fs)
00095         self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
00096         self.spacehack = get_bool_opt(options, 'spacehack', True)
00097         self._stylecache = {}


Member Function Documentation

def MoinMoin.support.pygments.formatters.svg.SvgFormatter._get_style (   self,
  tokentype 
) [private]

Definition at line 139 of file svg.py.

00139 
00140     def _get_style(self, tokentype):
00141         if tokentype in self._stylecache:
00142             return self._stylecache[tokentype]
00143         otokentype = tokentype
00144         while not self.style.styles_token(tokentype):
00145             tokentype = tokentype.parent
00146         value = self.style.style_for_token(tokentype)
00147         result = ''
00148         if value['color']:
00149             result = ' fill="#' + value['color'] + '"'
00150         if value['bold']:
00151             result += ' font-weight="bold"'
00152         if value['italic']:
00153             result += ' font-style="italic"'
00154         self._stylecache[otokentype] = result
00155         return result

Here is the caller graph for this function:

def MoinMoin.support.pygments.formatters.svg.SvgFormatter.format_unencoded (   self,
  tokensource,
  outfile 
)
Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
tuples and write it into ``outfile``.

For our implementation we put all lines in their own 'line group'.

Definition at line 98 of file svg.py.

00098 
00099     def format_unencoded(self, tokensource, outfile):
00100         """
00101         Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
00102         tuples and write it into ``outfile``.
00103 
00104         For our implementation we put all lines in their own 'line group'.
00105         """
00106         x = self.xoffset
00107         y = self.yoffset
00108         if not self.nowrap:
00109             if self.encoding:
00110                 outfile.write('<?xml version="1.0" encoding="%s"?>\n' %
00111                               self.encoding)
00112             else:
00113                 outfile.write('<?xml version="1.0"?>\n')
00114             outfile.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
00115                           '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/'
00116                           'svg10.dtd">\n')
00117             outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
00118             outfile.write('<g font-family="%s" font-size="%s">\n' %
00119                           (self.fontfamily, self.fontsize))
00120         outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (x, y))
00121         for ttype, value in tokensource:
00122             style = self._get_style(ttype)
00123             tspan = style and '<tspan' + style + '>' or ''
00124             tspanend = tspan and '</tspan>' or ''
00125             value = escape_html(value)
00126             if self.spacehack:
00127                 value = value.expandtabs().replace(' ', '&#160;')
00128             parts = value.split('\n')
00129             for part in parts[:-1]:
00130                 outfile.write(tspan + part + tspanend)
00131                 y += self.ystep
00132                 outfile.write('</text>\n<text x="%s" y="%s" '
00133                               'xml:space="preserve">' % (x, y))
00134             outfile.write(tspan + parts[-1] + tspanend)
00135         outfile.write('</text>')
00136 
00137         if not self.nowrap:
00138             outfile.write('</g></svg>\n')

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 96 of file svg.py.

Definition at line 77 of file svg.py.

Definition at line 78 of file svg.py.

Definition at line 84 of file svg.py.

Definition at line 85 of file svg.py.

Definition at line 76 of file svg.py.

Definition at line 83 of file svg.py.

Definition at line 95 of file svg.py.

Definition at line 86 of file svg.py.

Definition at line 93 of file svg.py.

Definition at line 94 of file svg.py.


The documentation for this class was generated from the following file: