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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pygments.formatter import Formatter
  5. from pygments.token import Token, STANDARD_TYPES
  6. from pygments.util import get_bool_opt, get_int_opt, StringIO
  7. __all__ = [
  8.     'LatexFormatter']
  9.  
  10. def escape_tex(text, commandprefix):
  11.     return text.replace('\\', '\x00').replace('{', '\x01').replace('}', '\x02').replace('^', '\x03').replace('_', '\x04').replace('\x00', '\\%sZbs{}' % commandprefix).replace('\x01', '\\%sZob{}' % commandprefix).replace('\x02', '\\%sZcb{}' % commandprefix).replace('\x03', '\\%sZca{}' % commandprefix).replace('\x04', '\\%sZus{}' % commandprefix)
  12.  
  13. DOC_TEMPLATE = '\n\\documentclass{%(docclass)s}\n\\usepackage{fancyvrb}\n\\usepackage{color}\n\\usepackage[%(encoding)s]{inputenc}\n%(preamble)s\n\n%(styledefs)s\n\n\\begin{document}\n\n\\section*{%(title)s}\n\n%(code)s\n\\end{document}\n'
  14. STYLE_TEMPLATE = '\n\\makeatletter\n\\def\\%(cp)s@reset{\\let\\%(cp)s@it=\\relax \\let\\%(cp)s@bf=\\relax%%\n    \\let\\%(cp)s@ul=\\relax \\let\\%(cp)s@tc=\\relax%%\n    \\let\\%(cp)s@bc=\\relax \\let\\%(cp)s@ff=\\relax}\n\\def\\%(cp)s@tok#1{\\csname %(cp)s@tok@#1\\endcsname}\n\\def\\%(cp)s@toks#1+{\\ifx\\relax#1\\empty\\else%%\n    \\%(cp)s@tok{#1}\\expandafter\\%(cp)s@toks\\fi}\n\\def\\%(cp)s@do#1{\\%(cp)s@bc{\\%(cp)s@tc{\\%(cp)s@ul{%%\n    \\%(cp)s@it{\\%(cp)s@bf{\\%(cp)s@ff{#1}}}}}}}\n\\def\\%(cp)s#1#2{\\%(cp)s@reset\\%(cp)s@toks#1+\\relax+\\%(cp)s@do{#2}}\n\n%(styles)s\n\n\\def\\%(cp)sZbs{\\char`\\\\}\n\\def\\%(cp)sZus{\\char`\\_}\n\\def\\%(cp)sZob{\\char`\\{}\n\\def\\%(cp)sZcb{\\char`\\}}\n\\def\\%(cp)sZca{\\char`\\^}\n%% for compatibility with earlier versions\n\\def\\%(cp)sZat{@}\n\\def\\%(cp)sZlb{[}\n\\def\\%(cp)sZrb{]}\n\\makeatother\n'
  15.  
  16. def _get_ttype_name(ttype):
  17.     fname = STANDARD_TYPES.get(ttype)
  18.     if fname:
  19.         return fname
  20.     aname = ''
  21.     while fname is None:
  22.         aname = ttype[-1] + aname
  23.         ttype = ttype.parent
  24.         fname = STANDARD_TYPES.get(ttype)
  25.         continue
  26.         fname
  27.     return fname + aname
  28.  
  29.  
  30. class LatexFormatter(Formatter):
  31.     name = 'LaTeX'
  32.     aliases = [
  33.         'latex',
  34.         'tex']
  35.     filenames = [
  36.         '*.tex']
  37.     
  38.     def __init__(self, **options):
  39.         Formatter.__init__(self, **options)
  40.         self.docclass = options.get('docclass', 'article')
  41.         self.preamble = options.get('preamble', '')
  42.         self.linenos = get_bool_opt(options, 'linenos', False)
  43.         self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
  44.         self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
  45.         self.verboptions = options.get('verboptions', '')
  46.         self.nobackground = get_bool_opt(options, 'nobackground', False)
  47.         self.commandprefix = options.get('commandprefix', 'PY')
  48.         self.texcomments = get_bool_opt(options, 'texcomments', False)
  49.         self.mathescape = get_bool_opt(options, 'mathescape', False)
  50.         self._create_stylesheet()
  51.  
  52.     
  53.     def _create_stylesheet(self):
  54.         t2n = self.ttype2name = {
  55.             Token: '' }
  56.         c2d = self.cmd2def = { }
  57.         cp = self.commandprefix
  58.         
  59.         def rgbcolor(col):
  60.             if col:
  61.                 return []([ '%.2f' % int(col[i] + col[i + 1], 16) / 255 for i in (0, 2, 4) ])
  62.             return '1,1,1'
  63.  
  64.         for ttype, ndef in self.style:
  65.             name = _get_ttype_name(ttype)
  66.             cmndef = ''
  67.             if ndef['bold']:
  68.                 cmndef += '\\let\\$$@bf=\\textbf'
  69.             
  70.             if ndef['italic']:
  71.                 cmndef += '\\let\\$$@it=\\textit'
  72.             
  73.             if ndef['underline']:
  74.                 cmndef += '\\let\\$$@ul=\\underline'
  75.             
  76.             if ndef['roman']:
  77.                 cmndef += '\\let\\$$@ff=\\textrm'
  78.             
  79.             if ndef['sans']:
  80.                 cmndef += '\\let\\$$@ff=\\textsf'
  81.             
  82.             if ndef['mono']:
  83.                 cmndef += '\\let\\$$@ff=\\textsf'
  84.             
  85.             if ndef['color']:
  86.                 cmndef += '\\def\\$$@tc##1{\\textcolor[rgb]{%s}{##1}}' % rgbcolor(ndef['color'])
  87.             
  88.             if ndef['border']:
  89.                 cmndef += '\\def\\$$@bc##1{\\fcolorbox[rgb]{%s}{%s}{##1}}' % (rgbcolor(ndef['border']), rgbcolor(ndef['bgcolor']))
  90.             elif ndef['bgcolor']:
  91.                 cmndef += '\\def\\$$@bc##1{\\colorbox[rgb]{%s}{##1}}' % rgbcolor(ndef['bgcolor'])
  92.             
  93.             if cmndef == '':
  94.                 continue
  95.             
  96.             cmndef = cmndef.replace('$$', cp)
  97.             t2n[ttype] = name
  98.             c2d[name] = cmndef
  99.         
  100.  
  101.     
  102.     def get_style_defs(self, arg = ''):
  103.         cp = self.commandprefix
  104.         styles = []
  105.         for name, definition in self.cmd2def.iteritems():
  106.             styles.append('\\def\\%s@tok@%s{%s}' % (cp, name, definition))
  107.         
  108.         return STYLE_TEMPLATE % {
  109.             'cp': self.commandprefix,
  110.             'styles': '\n'.join(styles) }
  111.  
  112.     
  113.     def format_unencoded(self, tokensource, outfile):
  114.         t2n = self.ttype2name
  115.         cp = self.commandprefix
  116.         if self.full:
  117.             realoutfile = outfile
  118.             outfile = StringIO()
  119.         
  120.         outfile.write('\\begin{Verbatim}[commandchars=\\\\\\{\\}')
  121.         if self.linenos:
  122.             start = self.linenostart
  123.             step = self.linenostep
  124.             if not start or ',firstnumber=%d' % start:
  125.                 pass
  126.             if not step or ',stepnumber=%d' % step:
  127.                 pass
  128.             outfile.write(',numbers=left' + '' + '')
  129.         
  130.         if self.mathescape or self.texcomments:
  131.             outfile.write(',codes={\\catcode`\\$=3\\catcode`\\^=7\\catcode`\\_=8}')
  132.         
  133.         if self.verboptions:
  134.             outfile.write(',' + self.verboptions)
  135.         
  136.         outfile.write(']\n')
  137.         for ttype, value in tokensource:
  138.             if ttype in Token.Comment:
  139.                 if self.texcomments:
  140.                     start = value[0:1]
  141.                     for i in xrange(1, len(value)):
  142.                         if start[0] != value[i]:
  143.                             break
  144.                         
  145.                         start += value[i]
  146.                     
  147.                     value = value[len(start):]
  148.                     start = escape_tex(start, self.commandprefix)
  149.                     value = start + value
  150.                 elif self.mathescape:
  151.                     parts = value.split('$')
  152.                     in_math = False
  153.                     for i, part in enumerate(parts):
  154.                         if not in_math:
  155.                             parts[i] = escape_tex(part, self.commandprefix)
  156.                         
  157.                         in_math = not in_math
  158.                     
  159.                     value = '$'.join(parts)
  160.                 else:
  161.                     value = escape_tex(value, self.commandprefix)
  162.             else:
  163.                 value = escape_tex(value, self.commandprefix)
  164.             styles = []
  165.             while ttype is not Token:
  166.                 
  167.                 try:
  168.                     styles.append(t2n[ttype])
  169.                 except KeyError:
  170.                     styles.append(_get_ttype_name(ttype))
  171.  
  172.                 ttype = ttype.parent
  173.             styleval = '+'.join(reversed(styles))
  174.             if styleval:
  175.                 spl = value.split('\n')
  176.                 for line in spl[:-1]:
  177.                     if line:
  178.                         outfile.write('\\%s{%s}{%s}' % (cp, styleval, line))
  179.                     
  180.                     outfile.write('\n')
  181.                 
  182.                 if spl[-1]:
  183.                     outfile.write('\\%s{%s}{%s}' % (cp, styleval, spl[-1]))
  184.                 
  185.             spl[-1]
  186.             outfile.write(value)
  187.         
  188.         outfile.write('\\end{Verbatim}\n')
  189.         if self.full:
  190.             if not self.encoding:
  191.                 pass
  192.             realoutfile.write(DOC_TEMPLATE % dict(docclass = self.docclass, preamble = self.preamble, title = self.title, encoding = 'latin1', styledefs = self.get_style_defs(), code = outfile.getvalue()))
  193.         
  194.  
  195.  
  196.