svg.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. """
  2. pygments.formatters.svg
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Formatter for SVG output.
  5. :copyright: Copyright 2006-2024 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'):
  77. fs = fs[:-2].strip()
  78. try:
  79. int_fs = int(fs)
  80. except ValueError:
  81. int_fs = 20
  82. self.yoffset = get_int_opt(options, 'yoffset', int_fs)
  83. self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
  84. self.spacehack = get_bool_opt(options, 'spacehack', True)
  85. self.linenos = get_bool_opt(options,'linenos',False)
  86. self.linenostart = get_int_opt(options,'linenostart',1)
  87. self.linenostep = get_int_opt(options,'linenostep',1)
  88. self.linenowidth = get_int_opt(options,'linenowidth', 3*self.ystep)
  89. self._stylecache = {}
  90. def format_unencoded(self, tokensource, outfile):
  91. """
  92. Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
  93. tuples and write it into ``outfile``.
  94. For our implementation we put all lines in their own 'line group'.
  95. """
  96. x = self.xoffset
  97. y = self.yoffset
  98. if not self.nowrap:
  99. if self.encoding:
  100. outfile.write(f'<?xml version="1.0" encoding="{self.encoding}"?>\n')
  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(f'<g font-family="{self.fontfamily}" font-size="{self.fontsize}">\n')
  108. counter = self.linenostart
  109. counter_step = self.linenostep
  110. counter_style = self._get_style(Comment)
  111. line_x = x
  112. if self.linenos:
  113. if counter % counter_step == 0:
  114. outfile.write(f'<text x="{x+self.linenowidth}" y="{y}" {counter_style} text-anchor="end">{counter}</text>')
  115. line_x += self.linenowidth + self.ystep
  116. counter += 1
  117. outfile.write(f'<text x="{line_x}" y="{y}" xml:space="preserve">')
  118. for ttype, value in tokensource:
  119. style = self._get_style(ttype)
  120. tspan = style and '<tspan' + style + '>' or ''
  121. tspanend = tspan and '</tspan>' or ''
  122. value = escape_html(value)
  123. if self.spacehack:
  124. value = value.expandtabs().replace(' ', '&#160;')
  125. parts = value.split('\n')
  126. for part in parts[:-1]:
  127. outfile.write(tspan + part + tspanend)
  128. y += self.ystep
  129. outfile.write('</text>\n')
  130. if self.linenos and counter % counter_step == 0:
  131. outfile.write(f'<text x="{x+self.linenowidth}" y="{y}" text-anchor="end" {counter_style}>{counter}</text>')
  132. counter += 1
  133. outfile.write(f'<text x="{line_x}" y="{y}" ' 'xml:space="preserve">')
  134. outfile.write(tspan + parts[-1] + tspanend)
  135. outfile.write('</text>')
  136. if not self.nowrap:
  137. outfile.write('</g></svg>\n')
  138. def _get_style(self, tokentype):
  139. if tokentype in self._stylecache:
  140. return self._stylecache[tokentype]
  141. otokentype = tokentype
  142. while not self.style.styles_token(tokentype):
  143. tokentype = tokentype.parent
  144. value = self.style.style_for_token(tokentype)
  145. result = ''
  146. if value['color']:
  147. result = ' fill="#' + value['color'] + '"'
  148. if value['bold']:
  149. result += ' font-weight="bold"'
  150. if value['italic']:
  151. result += ' font-style="italic"'
  152. self._stylecache[otokentype] = result
  153. return result