home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import sys
- import StringIO
- from pygments.formatter import Formatter
- from pygments.token import Token, Text, STANDARD_TYPES
- from pygments.util import get_bool_opt, get_int_opt, get_list_opt, bytes
- __all__ = [
- 'HtmlFormatter']
-
- def escape_html(text):
- return text.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
-
-
- def get_random_id():
- random = random
- import random
- time = time
- import time
-
- try:
- sha = sha1
- import hashlib
- except ImportError:
- import sha
- sha = sha.new
-
- return sha('%s|%s' % (random(), time())).hexdigest()
-
-
- def _get_ttype_class(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
-
- CSSFILE_TEMPLATE = 'td.linenos { background-color: #f0f0f0; padding-right: 10px; }\nspan.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\npre { line-height: 125%%; }\n%(styledefs)s\n'
- DOC_HEADER = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">\n\n<html>\n<head>\n <title>%(title)s</title>\n <meta http-equiv="content-type" content="text/html; charset=%(encoding)s">\n <style type="text/css">\n' + CSSFILE_TEMPLATE + '\n </style>\n</head>\n<body>\n<h2>%(title)s</h2>\n\n'
- DOC_HEADER_EXTERNALCSS = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">\n\n<html>\n<head>\n <title>%(title)s</title>\n <meta http-equiv="content-type" content="text/html; charset=%(encoding)s">\n <link rel="stylesheet" href="%(cssfile)s" type="text/css">\n</head>\n<body>\n<h2>%(title)s</h2>\n\n'
- DOC_FOOTER = '</body>\n</html>\n'
-
- class HtmlFormatter(Formatter):
- name = 'HTML'
- aliases = [
- 'html']
- filenames = [
- '*.html',
- '*.htm']
-
- def __init__(self, **options):
- Formatter.__init__(self, **options)
- self.title = self._decodeifneeded(self.title)
- self.nowrap = get_bool_opt(options, 'nowrap', False)
- self.noclasses = get_bool_opt(options, 'noclasses', False)
- self.classprefix = options.get('classprefix', '')
- self.cssclass = self._decodeifneeded(options.get('cssclass', 'highlight'))
- self.cssstyles = self._decodeifneeded(options.get('cssstyles', ''))
- self.prestyles = self._decodeifneeded(options.get('prestyles', ''))
- self.cssfile = self._decodeifneeded(options.get('cssfile', ''))
- self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
- linenos = options.get('linenos', False)
- if linenos == 'inline':
- self.linenos = 2
- elif linenos:
- self.linenos = 1
- else:
- self.linenos = 0
- self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
- self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
- self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0))
- self.nobackground = get_bool_opt(options, 'nobackground', False)
- self.lineseparator = options.get('lineseparator', '\n')
- self.lineanchors = options.get('lineanchors', '')
- self.anchorlinenos = options.get('anchorlinenos', False)
- self.hl_lines = set()
- for lineno in get_list_opt(options, 'hl_lines', []):
-
- try:
- self.hl_lines.add(int(lineno))
- continue
- except ValueError:
- continue
-
-
-
- self._class_cache = { }
- self._create_stylesheet()
-
-
- def _get_css_class(self, ttype):
- if ttype in self._class_cache:
- return self._class_cache[ttype]
- return self.classprefix + _get_ttype_class(ttype)
-
-
- def _create_stylesheet(self):
- t2c = self.ttype2class = {
- Token: '' }
- c2s = self.class2style = { }
- cp = self.classprefix
- for ttype, ndef in self.style:
- name = cp + _get_ttype_class(ttype)
- style = ''
- if ndef['color']:
- style += 'color: #%s; ' % ndef['color']
-
- if ndef['bold']:
- style += 'font-weight: bold; '
-
- if ndef['italic']:
- style += 'font-style: italic; '
-
- if ndef['underline']:
- style += 'text-decoration: underline; '
-
- if ndef['bgcolor']:
- style += 'background-color: #%s; ' % ndef['bgcolor']
-
- if ndef['border']:
- style += 'border: 1px solid #%s; ' % ndef['border']
-
- if style:
- t2c[ttype] = name
- c2s[name] = (style[:-2], ttype, len(ttype))
- continue
-
-
-
- def get_style_defs(self, arg = None):
- if arg is None:
- if not 'cssclass' in self.options or '.' + self.cssclass:
- pass
- arg = ''
-
- if isinstance(arg, basestring):
- args = [
- arg]
- else:
- args = list(arg)
-
- def prefix(cls):
- if cls:
- cls = '.' + cls
-
- tmp = []
- for arg in args:
- if not arg or arg + ' ':
- pass
- tmp.append('' + cls)
-
- return ', '.join(tmp)
-
- styles = _[1]
- styles.sort()
- lines = [ '%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:]) for level, ttype, cls, style in styles ]
- return '\n'.join(lines)
-
-
- def _decodeifneeded(self, value):
- if isinstance(value, bytes):
- if self.encoding:
- return value.decode(self.encoding)
- return value.decode()
- return value
-
-
- def _wrap_full(self, inner, outfile):
- if self.cssfile:
- if os.path.isabs(self.cssfile):
- cssfilename = self.cssfile
- else:
-
- try:
- filename = outfile.name
- if not filename or filename[0] == '<':
- raise AttributeError
- filename[0] == '<'
- cssfilename = os.path.join(os.path.dirname(filename), self.cssfile)
- except AttributeError:
- print >>sys.stderr, 'Note: Cannot determine output file name, using current directory as base for the CSS file name'
- cssfilename = self.cssfile
-
-
- try:
- if not os.path.exists(cssfilename) or not (self.noclobber_cssfile):
- cf = open(cssfilename, 'w')
- cf.write(CSSFILE_TEMPLATE % {
- 'styledefs': self.get_style_defs('body') })
- cf.close()
- except IOError:
- err = None
- err.strerror = 'Error writing CSS file: ' + err.strerror
- raise
-
- yield (0, DOC_HEADER_EXTERNALCSS % dict(title = self.title, cssfile = self.cssfile, encoding = self.encoding))
- else:
- yield (0, DOC_HEADER % dict(title = self.title, styledefs = self.get_style_defs('body'), encoding = self.encoding))
- for t, line in inner:
- yield (t, line)
-
- yield (0, DOC_FOOTER)
-
-
- def _wrap_tablelinenos(self, inner):
- dummyoutfile = StringIO.StringIO()
- lncount = 0
- for t, line in inner:
- if t:
- lncount += 1
-
- dummyoutfile.write(line)
-
- fl = self.linenostart
- mw = len(str(lncount + fl - 1))
- sp = self.linenospecial
- st = self.linenostep
- la = self.lineanchors
- aln = self.anchorlinenos
- if sp:
- lines = []
- for i in range(fl, fl + lncount):
- if i % st == 0:
- if i % sp == 0:
- if aln:
- lines.append('<a href="#%s-%d" class="special">%*d</a>' % (la, i, mw, i))
- else:
- lines.append('<span class="special">%*d</span>' % (mw, i))
- elif aln:
- lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
- else:
- lines.append('%*d' % (mw, i))
- i % sp == 0
- lines.append('')
-
- ls = '\n'.join(lines)
- else:
- lines = []
- for i in range(fl, fl + lncount):
- if i % st == 0:
- if aln:
- lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i))
- else:
- lines.append('%*d' % (mw, i))
- aln
- lines.append('')
-
- ls = '\n'.join(lines)
- yield (0, '<table class="%stable">' % self.cssclass + '<tr><td class="linenos"><div class="linenodiv"><pre>' + ls + '</pre></div></td><td class="code">')
- yield (0, dummyoutfile.getvalue())
- yield (0, '</td></tr></table>')
-
-
- def _wrap_inlinelinenos(self, inner):
- lines = list(inner)
- sp = self.linenospecial
- st = self.linenostep
- num = self.linenostart
- mw = len(str(len(lines) + num - 1))
- if sp:
- for t, line in lines:
- if not num % sp == 0 or ' special':
- pass
- if not num % st or ' ':
- pass
- yield (1, '<span class="lineno%s">%*s</span> ' % ('', mw, num) + line)
- num += 1
-
- else:
- for t, line in lines:
- if not num % st or ' ':
- pass
- yield (1, '<span class="lineno">%*s</span> ' % (mw, num) + line)
- num += 1
-
-
-
- def _wrap_lineanchors(self, inner):
- s = self.lineanchors
- i = 0
- for t, line in inner:
- if t:
- i += 1
- yield (1, '<a name="%s-%d"></a>' % (s, i) + line)
- continue
- yield (0, line)
-
-
-
- def _wrap_div(self, inner):
- style = []
- if self.noclasses and not (self.nobackground) and self.style.background_color is not None:
- style.append('background: %s' % (self.style.background_color,))
-
- if self.cssstyles:
- style.append(self.cssstyles)
-
- style = '; '.join(style)
- if self.cssclass:
- pass
- if style:
- pass
- yield (0, '<div' + ' class="%s"' % self.cssclass + ' style="%s"' % style + '>')
- for tup in inner:
- yield tup
-
- yield (0, '</div>\n')
-
-
- def _wrap_pre(self, inner):
- style = []
- if self.prestyles:
- style.append(self.prestyles)
-
- if self.noclasses:
- style.append('line-height: 125%')
-
- style = '; '.join(style)
- if style:
- pass
- yield (0, '<pre' + ' style="%s"' % style + '>')
- for tup in inner:
- yield tup
-
- yield (0, '</pre>')
-
-
- def _format_lines(self, tokensource):
- nocls = self.noclasses
- lsep = self.lineseparator
- getcls = self.ttype2class.get
- c2s = self.class2style
- lspan = ''
- line = ''
- for ttype, value in tokensource:
- if nocls:
- cclass = getcls(ttype)
- while cclass is None:
- ttype = ttype.parent
- cclass = getcls(ttype)
- if not cclass or '<span style="%s">' % c2s[cclass][0]:
- pass
- cspan = ''
- else:
- cls = self._get_css_class(ttype)
- if not cls or '<span class="%s">' % cls:
- pass
- cspan = ''
- parts = escape_html(value).split('\n')
- for part in parts[:-1]:
- if line:
- if lspan != cspan:
- if lspan:
- pass
- if cspan:
- pass
- line += '</span>' + cspan + part + '</span>' + lsep
- elif lspan:
- pass
- line += part + '</span>' + lsep
- yield (1, line)
- line = ''
- continue
- if part:
- if cspan:
- pass
- yield (1, cspan + part + '</span>' + lsep)
- continue
- yield (1, lsep)
-
- if line and parts[-1]:
- if lspan != cspan:
- if lspan:
- pass
- line += '</span>' + cspan + parts[-1]
- lspan = cspan
- else:
- line += parts[-1]
- lspan != cspan
- if parts[-1]:
- line = cspan + parts[-1]
- lspan = cspan
- continue
-
- if line:
- if lspan:
- pass
- yield (1, line + '</span>' + lsep)
-
-
-
- def _highlight_lines(self, tokensource):
- hls = self.hl_lines
- for t, value in enumerate(tokensource):
- if i + 1 in hls:
- if self.noclasses:
- style = ''
- if self.style.highlight_color is not None:
- style = ' style="background-color: %s"' % (self.style.highlight_color,)
-
- yield (1, '<span%s>%s</span>' % (style, value))
- else:
- yield (1, '<span class="hll">%s</span>' % value)
- None if t != 1 else None
- self.noclasses
- yield (1, value)
- None if t != 1 else None
-
-
-
- def wrap(self, source, outfile):
- return self._wrap_div(self._wrap_pre(source))
-
-
- def format_unencoded(self, tokensource, outfile):
- source = self._format_lines(tokensource)
- if self.hl_lines:
- source = self._highlight_lines(source)
-
- if not self.nowrap:
- if self.linenos == 2:
- source = self._wrap_inlinelinenos(source)
-
- if self.lineanchors:
- source = self._wrap_lineanchors(source)
-
- source = self.wrap(source, outfile)
- if self.linenos == 1:
- source = self._wrap_tablelinenos(source)
-
- if self.full:
- source = self._wrap_full(source, outfile)
-
-
- for t, piece in source:
- outfile.write(piece)
-
-
-
-