svg.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """
  2. pygments.formatters.svg
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Formatter for SVG output.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.formatter import Formatter
  9. from pygments.token import Comment
  10. from pygments.util import get_bool_opt, get_int_opt
  11. __all__ = ['SvgFormatter']
  12. def escape_html(text):
  13. """Escape &, <, > as well as single and double quotes for HTML."""
  14. return text.replace('&', '&amp;'). \
  15. replace('<', '&lt;'). \
  16. replace('>', '&gt;'). \
  17. replace('"', '&quot;'). \
  18. replace("'", '&#39;')
  19. class2style = {}
  20. class SvgFormatter(Formatter):
  21. """
  22. Format tokens as an SVG graphics file. This formatter is still experimental.
  23. Each line of code is a ``<text>`` element with explicit ``x`` and ``y``
  24. coordinates containing ``<tspan>`` elements with the individual token styles.
  25. By default, this formatter outputs a full SVG document including doctype
  26. declaration and the ``<svg>`` root element.
  27. .. versionadded:: 0.9
  28. Additional options accepted:
  29. `nowrap`
  30. Don't wrap the SVG ``<text>`` elements in ``<svg><g>`` elements and
  31. don't add a XML declaration and a doctype. If true, the `fontfamily`
  32. and `fontsize` options are ignored. Defaults to ``False``.
  33. `fontfamily`
  34. The value to give the wrapping ``<g>`` element's ``font-family``
  35. attribute, defaults to ``"monospace"``.
  36. `fontsize`
  37. The value to give the wrapping ``<g>`` element's ``font-size``
  38. attribute, defaults to ``"14px"``.
  39. `linenos`
  40. If ``True``, add line numbers (default: ``False``).
  41. `linenostart`
  42. The line number for the first line (default: ``1``).
  43. `linenostep`
  44. If set to a number n > 1, only every nth line number is printed.
  45. `linenowidth`
  46. Maximum width devoted to line numbers (default: ``3*ystep``, sufficient
  47. for up to 4-digit line numbers. Increase width for longer code blocks).
  48. `xoffset`
  49. Starting offset in X direction, defaults to ``0``.
  50. `yoffset`
  51. Starting offset in Y direction, defaults to the font size if it is given
  52. in pixels, or ``20`` else. (This is necessary since text coordinates
  53. refer to the text baseline, not the top edge.)
  54. `ystep`
  55. Offset to add to the Y coordinate for each subsequent line. This should
  56. roughly be the text size plus 5. It defaults to that value if the text
  57. size is given in pixels, or ``25`` else.
  58. `spacehack`
  59. Convert spaces in the source to ``&#160;``, which are non-breaking
  60. spaces. SVG provides the ``xml:space`` attribute to control how
  61. whitespace inside tags is handled, in theory, the ``preserve`` value
  62. could be used to keep all whitespace as-is. However, many current SVG
  63. viewers don't obey that rule, so this option is provided as a workaround
  64. and defaults to ``True``.
  65. """
  66. name = 'SVG'
  67. aliases = ['svg']
  68. filenames = ['*.svg']
  69. def __init__(self, **options):
  70. Formatter.__init__(self, **options)
  71. self.nowrap = get_bool_opt(options, 'nowrap', False)
  72. self.fontfamily = options.get('fontfamily', 'monospace')
  73. self.fontsize = options.get('fontsize', '14px')
  74. self.xoffset = get_int_opt(options, 'xoffset', 0)
  75. fs = self.fontsize.strip()
  76. if fs.endswith('px'): fs = fs[:-2].strip()
  77. try:
  78. int_fs = int(fs)
  79. except:
  80. int_fs = 20
  81. self.yoffset = get_int_opt(options, 'yoffset', int_fs)
  82. self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
  83. self.spacehack = get_bool_opt(options, 'spacehack', True)
  84. self.linenos = get_bool_opt(options,'linenos',False)
  85. self.linenostart = get_int_opt(options,'linenostart',1)
  86. self.linenostep = get_int_opt(options,'linenostep',1)
  87. self.linenowidth = get_int_opt(options,'linenowidth', 3*self.ystep)
  88. self._stylecache = {}
  89. def format_unencoded(self, tokensource, outfile):
  90. """
  91. Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
  92. tuples and write it into ``outfile``.
  93. For our implementation we put all lines in their own 'line group'.
  94. """
  95. x = self.xoffset
  96. y = self.yoffset
  97. if not self.nowrap:
  98. if self.encoding:
  99. outfile.write('<?xml version="1.0" encoding="%s"?>\n' %
  100. self.encoding)
  101. else:
  102. outfile.write('<?xml version="1.0"?>\n')
  103. outfile.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
  104. '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/'
  105. 'svg10.dtd">\n')
  106. outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
  107. outfile.write('<g font-family="%s" font-size="%s">\n' %
  108. (self.fontfamily, self.fontsize))
  109. counter = self.linenostart
  110. counter_step = self.linenostep
  111. counter_style = self._get_style(Comment)
  112. line_x = x
  113. if self.linenos:
  114. if counter % counter_step == 0:
  115. outfile.write('<text x="%s" y="%s" %s text-anchor="end">%s</text>' %
  116. (x+self.linenowidth,y,counter_style,counter))
  117. line_x += self.linenowidth + self.ystep
  118. counter += 1
  119. outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (line_x, y))
  120. for ttype, value in tokensource:
  121. style = self._get_style(ttype)
  122. tspan = style and '<tspan' + style + '>' or ''
  123. tspanend = tspan and '</tspan>' or ''
  124. value = escape_html(value)
  125. if self.spacehack:
  126. value = value.expandtabs().replace(' ', '&#160;')
  127. parts = value.split('\n')
  128. for part in parts[:-1]:
  129. outfile.write(tspan + part + tspanend)
  130. y += self.ystep
  131. outfile.write('</text>\n')
  132. if self.linenos and counter % counter_step == 0:
  133. outfile.write('<text x="%s" y="%s" text-anchor="end" %s>%s</text>' %
  134. (x+self.linenowidth,y,counter_style,counter))
  135. counter += 1
  136. outfile.write('<text x="%s" y="%s" ' 'xml:space="preserve">' % (line_x,y))
  137. outfile.write(tspan + parts[-1] + tspanend)
  138. outfile.write('</text>')
  139. if not self.nowrap:
  140. outfile.write('</g></svg>\n')
  141. def _get_style(self, tokentype):
  142. if tokentype in self._stylecache:
  143. return self._stylecache[tokentype]
  144. otokentype = tokentype
  145. while not self.style.styles_token(tokentype):
  146. tokentype = tokentype.parent
  147. value = self.style.style_for_token(tokentype)
  148. result = ''
  149. if value['color']:
  150. result = ' fill="#' + value['color'] + '"'
  151. if value['bold']:
  152. result += ' font-weight="bold"'
  153. if value['italic']:
  154. result += ' font-style="italic"'
  155. self._stylecache[otokentype] = result
  156. return result