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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pygments.formatter import Formatter
  5. from pygments.util import get_bool_opt
  6. __all__ = [
  7.     'BBCodeFormatter']
  8.  
  9. class BBCodeFormatter(Formatter):
  10.     name = 'BBCode'
  11.     aliases = [
  12.         'bbcode',
  13.         'bb']
  14.     filenames = []
  15.     
  16.     def __init__(self, **options):
  17.         Formatter.__init__(self, **options)
  18.         self._code = get_bool_opt(options, 'codetag', False)
  19.         self._mono = get_bool_opt(options, 'monofont', False)
  20.         self.styles = { }
  21.         self._make_styles()
  22.  
  23.     
  24.     def _make_styles(self):
  25.         for ttype, ndef in self.style:
  26.             start = end = ''
  27.             if ndef['color']:
  28.                 start += '[color=#%s]' % ndef['color']
  29.                 end = '[/color]' + end
  30.             
  31.             if ndef['bold']:
  32.                 start += '[b]'
  33.                 end = '[/b]' + end
  34.             
  35.             if ndef['italic']:
  36.                 start += '[i]'
  37.                 end = '[/i]' + end
  38.             
  39.             if ndef['underline']:
  40.                 start += '[u]'
  41.                 end = '[/u]' + end
  42.             
  43.             self.styles[ttype] = (start, end)
  44.         
  45.  
  46.     
  47.     def format_unencoded(self, tokensource, outfile):
  48.         if self._code:
  49.             outfile.write('[code]')
  50.         
  51.         if self._mono:
  52.             outfile.write('[font=monospace]')
  53.         
  54.         lastval = ''
  55.         lasttype = None
  56.         for ttype, value in tokensource:
  57.             while ttype not in self.styles:
  58.                 ttype = ttype.parent
  59.             if ttype == lasttype:
  60.                 lastval += value
  61.                 continue
  62.             if lastval:
  63.                 (start, end) = self.styles[lasttype]
  64.                 outfile.write(''.join((start, lastval, end)))
  65.             
  66.             lastval = value
  67.             lasttype = ttype
  68.         
  69.         if lastval:
  70.             (start, end) = self.styles[lasttype]
  71.             outfile.write(''.join((start, lastval, end)))
  72.         
  73.         if self._mono:
  74.             outfile.write('[/font]')
  75.         
  76.         if self._code:
  77.             outfile.write('[/code]')
  78.         
  79.         if self._code or self._mono:
  80.             outfile.write('\n')
  81.         
  82.  
  83.  
  84.