2 pygments.formatters.groff
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Formatter for groff output.
7 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
12 from pip._vendor.pygments.formatter import Formatter
13 from pip._vendor.pygments.util import get_bool_opt, get_int_opt
15 __all__ = ['GroffFormatter']
18 class GroffFormatter(Formatter):
20 Format tokens with groff escapes to change their color and font style.
22 .. versionadded:: 2.11
24 Additional options accepted:
27 The style to use, can be a string or a Style subclass (default:
31 If set to true, monospace font will be used (default: ``true``).
34 If set to true, print the line numbers (default: ``false``).
37 Wrap lines to the specified number of characters. Disabled if set to 0
42 aliases = ['groff','troff','roff']
45 def __init__(self, **options):
46 Formatter.__init__(self, **options)
48 self.monospaced = get_bool_opt(options, 'monospaced', True)
49 self.linenos = get_bool_opt(options, 'linenos', False)
51 self.wrap = get_int_opt(options, 'wrap', 0)
58 def _make_styles(self):
59 regular = '\\f[CR]' if self.monospaced else '\\f[R]'
60 bold = '\\f[CB]' if self.monospaced else '\\f[B]'
61 italic = '\\f[CI]' if self.monospaced else '\\f[I]'
63 for ttype, ndef in self.style:
66 start += '\\m[%s]' % ndef['color']
75 start += '\\M[%s]' % ndef['bgcolor']
78 self.styles[ttype] = start, end
81 def _define_colors(self, outfile):
83 for _, ndef in self.style:
84 if ndef['color'] is not None:
85 colors.add(ndef['color'])
88 outfile.write('.defcolor ' + color + ' rgb #' + color + '\n')
91 def _write_lineno(self, outfile):
93 outfile.write("%s% 4d " % (self._lineno != 1 and '\n' or '', self._lineno))
96 def _wrap_line(self, line):
97 length = len(line.rstrip('\n'))
98 space = ' ' if self.linenos else ''
101 if length > self.wrap:
102 for i in range(0, math.floor(length / self.wrap)):
103 chunk = line[i*self.wrap:i*self.wrap+self.wrap]
104 newline += (chunk + '\n' + space)
105 remainder = length % self.wrap
107 newline += line[-remainder-1:]
108 self._linelen = remainder
109 elif self._linelen + length > self.wrap:
110 newline = ('\n' + space) + line
111 self._linelen = length
114 self._linelen += length
119 def _escape_chars(self, text):
120 text = text.replace('\\', '\\[u005C]'). \
121 replace('.', '\\[char46]'). \
122 replace('\'', '\\[u0027]'). \
123 replace('`', '\\[u0060]'). \
124 replace('~', '\\[u007E]')
128 if len(char) != len(char.encode()):
129 uni = char.encode('unicode_escape') \
131 .replace('x', 'u00') \
133 text = text.replace(char, '\\[u' + uni[1:] + ']')
138 def format_unencoded(self, tokensource, outfile):
139 self._define_colors(outfile)
141 outfile.write('.nf\n\\f[CR]\n')
144 self._write_lineno(outfile)
146 for ttype, value in tokensource:
147 while ttype not in self.styles:
149 start, end = self.styles[ttype]
151 for line in value.splitlines(True):
153 line = self._wrap_line(line)
156 text = self._escape_chars(line.rstrip('\n'))
158 outfile.write(''.join((start, text, end)))
160 outfile.write(self._escape_chars(line.rstrip('\n')))
162 if line.endswith('\n'):
164 self._write_lineno(outfile)
170 outfile.write('\n.fi')