home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2354 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.2 KB  |  104 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pygments.formatter import Formatter
  5. __all__ = [
  6.     'RtfFormatter']
  7.  
  8. class RtfFormatter(Formatter):
  9.     name = 'RTF'
  10.     aliases = [
  11.         'rtf']
  12.     filenames = [
  13.         '*.rtf']
  14.     unicodeoutput = False
  15.     
  16.     def __init__(self, **options):
  17.         Formatter.__init__(self, **options)
  18.         if not options.get('fontface'):
  19.             pass
  20.         self.fontface = ''
  21.  
  22.     
  23.     def _escape(self, text):
  24.         return text.replace('\\', '\\\\').replace('{', '\\{').replace('}', '\\}')
  25.  
  26.     
  27.     def _escape_text(self, text):
  28.         if not text:
  29.             return ''
  30.         text = self._escape(text)
  31.         if self.encoding in ('utf-8', 'utf-16', 'utf-32'):
  32.             encoding = 'iso-8859-15'
  33.         elif not self.encoding:
  34.             pass
  35.         encoding = 'iso-8859-15'
  36.         buf = []
  37.         for c in text:
  38.             if ord(c) > 128:
  39.                 if not c.encode(encoding, 'ignore'):
  40.                     pass
  41.                 ansic = '?'
  42.                 if ord(ansic) > 128:
  43.                     ansic = "\\'%x" % ord(ansic)
  44.                 else:
  45.                     ansic = c
  46.                 buf.append('\\ud{\\u%d%s}' % (ord(c), ansic))
  47.                 continue
  48.             buf.append(str(c))
  49.         
  50.         return ''.join(buf).replace('\n', '\\par\n')
  51.  
  52.     
  53.     def format_unencoded(self, tokensource, outfile):
  54.         if not self.fontface or ' ' + self._escape(self.fontface):
  55.             pass
  56.         outfile.write('{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}{\\colortbl;' % '')
  57.         color_mapping = { }
  58.         offset = 1
  59.         for _, style in self.style:
  60.             for color in (style['color'], style['bgcolor'], style['border']):
  61.                 if color and color not in color_mapping:
  62.                     color_mapping[color] = offset
  63.                     outfile.write('\\red%d\\green%d\\blue%d;' % (int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16)))
  64.                     offset += 1
  65.                     continue
  66.             
  67.         
  68.         outfile.write('}\\f0')
  69.         for ttype, value in tokensource:
  70.             while not self.style.styles_token(ttype) and ttype.parent:
  71.                 ttype = ttype.parent
  72.             style = self.style.style_for_token(ttype)
  73.             buf = []
  74.             if style['bgcolor']:
  75.                 buf.append('\\cb%d' % color_mapping[style['bgcolor']])
  76.             
  77.             if style['color']:
  78.                 buf.append('\\cf%d' % color_mapping[style['color']])
  79.             
  80.             if style['bold']:
  81.                 buf.append('\\b')
  82.             
  83.             if style['italic']:
  84.                 buf.append('\\i')
  85.             
  86.             if style['underline']:
  87.                 buf.append('\\ul')
  88.             
  89.             if style['border']:
  90.                 buf.append('\\chbrdr\\chcfpat%d' % color_mapping[style['border']])
  91.             
  92.             start = ''.join(buf)
  93.             if start:
  94.                 outfile.write('{%s ' % start)
  95.             
  96.             outfile.write(self._escape_text(value))
  97.             if start:
  98.                 outfile.write('}')
  99.                 continue
  100.         
  101.         outfile.write('}')
  102.  
  103.  
  104.