home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from pygments.formatter import Formatter
- from pygments.token import Token, STANDARD_TYPES
- from pygments.util import get_bool_opt, get_int_opt, StringIO
- __all__ = [
- 'LatexFormatter']
-
- def escape_tex(text, commandprefix):
- 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)
-
- 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'
- 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'
-
- def _get_ttype_name(ttype):
- fname = STANDARD_TYPES.get(ttype)
- if fname:
- return fname
- aname = ''
- while fname is None:
- aname = ttype[-1] + aname
- ttype = ttype.parent
- fname = STANDARD_TYPES.get(ttype)
- continue
- fname
- return fname + aname
-
-
- class LatexFormatter(Formatter):
- name = 'LaTeX'
- aliases = [
- 'latex',
- 'tex']
- filenames = [
- '*.tex']
-
- def __init__(self, **options):
- Formatter.__init__(self, **options)
- self.docclass = options.get('docclass', 'article')
- self.preamble = options.get('preamble', '')
- self.linenos = get_bool_opt(options, 'linenos', False)
- self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
- self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
- self.verboptions = options.get('verboptions', '')
- self.nobackground = get_bool_opt(options, 'nobackground', False)
- self.commandprefix = options.get('commandprefix', 'PY')
- self.texcomments = get_bool_opt(options, 'texcomments', False)
- self.mathescape = get_bool_opt(options, 'mathescape', False)
- self._create_stylesheet()
-
-
- def _create_stylesheet(self):
- t2n = self.ttype2name = {
- Token: '' }
- c2d = self.cmd2def = { }
- cp = self.commandprefix
-
- def rgbcolor(col):
- if col:
- return []([ '%.2f' % int(col[i] + col[i + 1], 16) / 255 for i in (0, 2, 4) ])
- return '1,1,1'
-
- for ttype, ndef in self.style:
- name = _get_ttype_name(ttype)
- cmndef = ''
- if ndef['bold']:
- cmndef += '\\let\\$$@bf=\\textbf'
-
- if ndef['italic']:
- cmndef += '\\let\\$$@it=\\textit'
-
- if ndef['underline']:
- cmndef += '\\let\\$$@ul=\\underline'
-
- if ndef['roman']:
- cmndef += '\\let\\$$@ff=\\textrm'
-
- if ndef['sans']:
- cmndef += '\\let\\$$@ff=\\textsf'
-
- if ndef['mono']:
- cmndef += '\\let\\$$@ff=\\textsf'
-
- if ndef['color']:
- cmndef += '\\def\\$$@tc##1{\\textcolor[rgb]{%s}{##1}}' % rgbcolor(ndef['color'])
-
- if ndef['border']:
- cmndef += '\\def\\$$@bc##1{\\fcolorbox[rgb]{%s}{%s}{##1}}' % (rgbcolor(ndef['border']), rgbcolor(ndef['bgcolor']))
- elif ndef['bgcolor']:
- cmndef += '\\def\\$$@bc##1{\\colorbox[rgb]{%s}{##1}}' % rgbcolor(ndef['bgcolor'])
-
- if cmndef == '':
- continue
-
- cmndef = cmndef.replace('$$', cp)
- t2n[ttype] = name
- c2d[name] = cmndef
-
-
-
- def get_style_defs(self, arg = ''):
- cp = self.commandprefix
- styles = []
- for name, definition in self.cmd2def.iteritems():
- styles.append('\\def\\%s@tok@%s{%s}' % (cp, name, definition))
-
- return STYLE_TEMPLATE % {
- 'cp': self.commandprefix,
- 'styles': '\n'.join(styles) }
-
-
- def format_unencoded(self, tokensource, outfile):
- t2n = self.ttype2name
- cp = self.commandprefix
- if self.full:
- realoutfile = outfile
- outfile = StringIO()
-
- outfile.write('\\begin{Verbatim}[commandchars=\\\\\\{\\}')
- if self.linenos:
- start = self.linenostart
- step = self.linenostep
- if not start or ',firstnumber=%d' % start:
- pass
- if not step or ',stepnumber=%d' % step:
- pass
- outfile.write(',numbers=left' + '' + '')
-
- if self.mathescape or self.texcomments:
- outfile.write(',codes={\\catcode`\\$=3\\catcode`\\^=7\\catcode`\\_=8}')
-
- if self.verboptions:
- outfile.write(',' + self.verboptions)
-
- outfile.write(']\n')
- for ttype, value in tokensource:
- if ttype in Token.Comment:
- if self.texcomments:
- start = value[0:1]
- for i in xrange(1, len(value)):
- if start[0] != value[i]:
- break
-
- start += value[i]
-
- value = value[len(start):]
- start = escape_tex(start, self.commandprefix)
- value = start + value
- elif self.mathescape:
- parts = value.split('$')
- in_math = False
- for i, part in enumerate(parts):
- if not in_math:
- parts[i] = escape_tex(part, self.commandprefix)
-
- in_math = not in_math
-
- value = '$'.join(parts)
- else:
- value = escape_tex(value, self.commandprefix)
- else:
- value = escape_tex(value, self.commandprefix)
- styles = []
- while ttype is not Token:
-
- try:
- styles.append(t2n[ttype])
- except KeyError:
- styles.append(_get_ttype_name(ttype))
-
- ttype = ttype.parent
- styleval = '+'.join(reversed(styles))
- if styleval:
- spl = value.split('\n')
- for line in spl[:-1]:
- if line:
- outfile.write('\\%s{%s}{%s}' % (cp, styleval, line))
-
- outfile.write('\n')
-
- if spl[-1]:
- outfile.write('\\%s{%s}{%s}' % (cp, styleval, spl[-1]))
-
- spl[-1]
- outfile.write(value)
-
- outfile.write('\\end{Verbatim}\n')
- if self.full:
- if not self.encoding:
- pass
- realoutfile.write(DOC_TEMPLATE % dict(docclass = self.docclass, preamble = self.preamble, title = self.title, encoding = 'latin1', styledefs = self.get_style_defs(), code = outfile.getvalue()))
-
-
-
-