rtf.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.formatters.rtf
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. A formatter that generates RTF files.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.formatter import Formatter
  10. from pygments.util import get_int_opt, _surrogatepair
  11. __all__ = ['RtfFormatter']
  12. class RtfFormatter(Formatter):
  13. """
  14. Format tokens as RTF markup. This formatter automatically outputs full RTF
  15. documents with color information and other useful stuff. Perfect for Copy and
  16. Paste into Microsoft(R) Word(R) documents.
  17. Please note that ``encoding`` and ``outencoding`` options are ignored.
  18. The RTF format is ASCII natively, but handles unicode characters correctly
  19. thanks to escape sequences.
  20. .. versionadded:: 0.6
  21. Additional options accepted:
  22. `style`
  23. The style to use, can be a string or a Style subclass (default:
  24. ``'default'``).
  25. `fontface`
  26. The used font family, for example ``Bitstream Vera Sans``. Defaults to
  27. some generic font which is supposed to have fixed width.
  28. `fontsize`
  29. Size of the font used. Size is specified in half points. The
  30. default is 24 half-points, giving a size 12 font.
  31. .. versionadded:: 2.0
  32. """
  33. name = 'RTF'
  34. aliases = ['rtf']
  35. filenames = ['*.rtf']
  36. def __init__(self, **options):
  37. r"""
  38. Additional options accepted:
  39. ``fontface``
  40. Name of the font used. Could for example be ``'Courier New'``
  41. to further specify the default which is ``'\fmodern'``. The RTF
  42. specification claims that ``\fmodern`` are "Fixed-pitch serif
  43. and sans serif fonts". Hope every RTF implementation thinks
  44. the same about modern...
  45. """
  46. Formatter.__init__(self, **options)
  47. self.fontface = options.get('fontface') or ''
  48. self.fontsize = get_int_opt(options, 'fontsize', 0)
  49. def _escape(self, text):
  50. return text.replace(u'\\', u'\\\\') \
  51. .replace(u'{', u'\\{') \
  52. .replace(u'}', u'\\}')
  53. def _escape_text(self, text):
  54. # empty strings, should give a small performance improvement
  55. if not text:
  56. return u''
  57. # escape text
  58. text = self._escape(text)
  59. buf = []
  60. for c in text:
  61. cn = ord(c)
  62. if cn < (2**7):
  63. # ASCII character
  64. buf.append(str(c))
  65. elif (2**7) <= cn < (2**16):
  66. # single unicode escape sequence
  67. buf.append(u'{\\u%d}' % cn)
  68. elif (2**16) <= cn:
  69. # RTF limits unicode to 16 bits.
  70. # Force surrogate pairs
  71. buf.append(u'{\\u%d}{\\u%d}' % _surrogatepair(cn))
  72. return u''.join(buf).replace(u'\n', u'\\par\n')
  73. def format_unencoded(self, tokensource, outfile):
  74. # rtf 1.8 header
  75. outfile.write(u'{\\rtf1\\ansi\\uc0\\deff0'
  76. u'{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}'
  77. u'{\\colortbl;' % (self.fontface and
  78. u' ' + self._escape(self.fontface) or
  79. u''))
  80. # convert colors and save them in a mapping to access them later.
  81. color_mapping = {}
  82. offset = 1
  83. for _, style in self.style:
  84. for color in style['color'], style['bgcolor'], style['border']:
  85. if color and color not in color_mapping:
  86. color_mapping[color] = offset
  87. outfile.write(u'\\red%d\\green%d\\blue%d;' % (
  88. int(color[0:2], 16),
  89. int(color[2:4], 16),
  90. int(color[4:6], 16)
  91. ))
  92. offset += 1
  93. outfile.write(u'}\\f0 ')
  94. if self.fontsize:
  95. outfile.write(u'\\fs%d' % (self.fontsize))
  96. # highlight stream
  97. for ttype, value in tokensource:
  98. while not self.style.styles_token(ttype) and ttype.parent:
  99. ttype = ttype.parent
  100. style = self.style.style_for_token(ttype)
  101. buf = []
  102. if style['bgcolor']:
  103. buf.append(u'\\cb%d' % color_mapping[style['bgcolor']])
  104. if style['color']:
  105. buf.append(u'\\cf%d' % color_mapping[style['color']])
  106. if style['bold']:
  107. buf.append(u'\\b')
  108. if style['italic']:
  109. buf.append(u'\\i')
  110. if style['underline']:
  111. buf.append(u'\\ul')
  112. if style['border']:
  113. buf.append(u'\\chbrdr\\chcfpat%d' %
  114. color_mapping[style['border']])
  115. start = u''.join(buf)
  116. if start:
  117. outfile.write(u'{%s ' % start)
  118. outfile.write(self._escape_text(value))
  119. if start:
  120. outfile.write(u'}')
  121. outfile.write(u'}')