home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from pygments.formatter import Formatter
- __all__ = [
- 'RtfFormatter']
-
- class RtfFormatter(Formatter):
- name = 'RTF'
- aliases = [
- 'rtf']
- filenames = [
- '*.rtf']
- unicodeoutput = False
-
- def __init__(self, **options):
- Formatter.__init__(self, **options)
- if not options.get('fontface'):
- pass
- self.fontface = ''
-
-
- def _escape(self, text):
- return text.replace('\\', '\\\\').replace('{', '\\{').replace('}', '\\}')
-
-
- def _escape_text(self, text):
- if not text:
- return ''
- text = self._escape(text)
- if self.encoding in ('utf-8', 'utf-16', 'utf-32'):
- encoding = 'iso-8859-15'
- elif not self.encoding:
- pass
- encoding = 'iso-8859-15'
- buf = []
- for c in text:
- if ord(c) > 128:
- if not c.encode(encoding, 'ignore'):
- pass
- ansic = '?'
- if ord(ansic) > 128:
- ansic = "\\'%x" % ord(ansic)
- else:
- ansic = c
- buf.append('\\ud{\\u%d%s}' % (ord(c), ansic))
- continue
- buf.append(str(c))
-
- return ''.join(buf).replace('\n', '\\par\n')
-
-
- def format_unencoded(self, tokensource, outfile):
- if not self.fontface or ' ' + self._escape(self.fontface):
- pass
- outfile.write('{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}{\\colortbl;' % '')
- color_mapping = { }
- offset = 1
- for _, style in self.style:
- for color in (style['color'], style['bgcolor'], style['border']):
- if color and color not in color_mapping:
- color_mapping[color] = offset
- outfile.write('\\red%d\\green%d\\blue%d;' % (int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16)))
- offset += 1
- continue
-
-
- outfile.write('}\\f0')
- for ttype, value in tokensource:
- while not self.style.styles_token(ttype) and ttype.parent:
- ttype = ttype.parent
- style = self.style.style_for_token(ttype)
- buf = []
- if style['bgcolor']:
- buf.append('\\cb%d' % color_mapping[style['bgcolor']])
-
- if style['color']:
- buf.append('\\cf%d' % color_mapping[style['color']])
-
- if style['bold']:
- buf.append('\\b')
-
- if style['italic']:
- buf.append('\\i')
-
- if style['underline']:
- buf.append('\\ul')
-
- if style['border']:
- buf.append('\\chbrdr\\chcfpat%d' % color_mapping[style['border']])
-
- start = ''.join(buf)
- if start:
- outfile.write('{%s ' % start)
-
- outfile.write(self._escape_text(value))
- if start:
- outfile.write('}')
- continue
-
- outfile.write('}')
-
-
-