home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2355 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.6 KB  |  105 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, get_int_opt
  6. __all__ = [
  7.     'SvgFormatter']
  8.  
  9. def escape_html(text):
  10.     return text.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
  11.  
  12. class2style = { }
  13.  
  14. class SvgFormatter(Formatter):
  15.     name = 'SVG'
  16.     aliases = [
  17.         'svg']
  18.     filenames = [
  19.         '*.svg']
  20.     
  21.     def __init__(self, **options):
  22.         Formatter.__init__(self, **options)
  23.         self.nowrap = get_bool_opt(options, 'nowrap', False)
  24.         self.fontfamily = options.get('fontfamily', 'monospace')
  25.         self.fontsize = options.get('fontsize', '14px')
  26.         self.xoffset = get_int_opt(options, 'xoffset', 0)
  27.         fs = self.fontsize.strip()
  28.         if fs.endswith('px'):
  29.             fs = fs[:-2].strip()
  30.         
  31.         
  32.         try:
  33.             int_fs = int(fs)
  34.         except:
  35.             int_fs = 20
  36.  
  37.         self.yoffset = get_int_opt(options, 'yoffset', int_fs)
  38.         self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
  39.         self.spacehack = get_bool_opt(options, 'spacehack', True)
  40.         self._stylecache = { }
  41.  
  42.     
  43.     def format_unencoded(self, tokensource, outfile):
  44.         x = self.xoffset
  45.         y = self.yoffset
  46.         if not self.nowrap:
  47.             if self.encoding:
  48.                 outfile.write('<?xml version="1.0" encoding="%s"?>\n' % self.encoding)
  49.             else:
  50.                 outfile.write('<?xml version="1.0"?>\n')
  51.             outfile.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n')
  52.             outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
  53.             outfile.write('<g font-family="%s" font-size="%s">\n' % (self.fontfamily, self.fontsize))
  54.         
  55.         outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (x, y))
  56.         for ttype, value in tokensource:
  57.             style = self._get_style(ttype)
  58.             if not style or '<tspan' + style + '>':
  59.                 pass
  60.             tspan = ''
  61.             if not tspan or '</tspan>':
  62.                 pass
  63.             tspanend = ''
  64.             value = escape_html(value)
  65.             if self.spacehack:
  66.                 value = value.expandtabs().replace(' ', ' ')
  67.             
  68.             parts = value.split('\n')
  69.             for part in parts[:-1]:
  70.                 outfile.write(tspan + part + tspanend)
  71.                 y += self.ystep
  72.                 outfile.write('</text>\n<text x="%s" y="%s" xml:space="preserve">' % (x, y))
  73.             
  74.             outfile.write(tspan + parts[-1] + tspanend)
  75.         
  76.         outfile.write('</text>')
  77.         if not self.nowrap:
  78.             outfile.write('</g></svg>\n')
  79.         
  80.  
  81.     
  82.     def _get_style(self, tokentype):
  83.         if tokentype in self._stylecache:
  84.             return self._stylecache[tokentype]
  85.         otokentype = tokentype
  86.         while not self.style.styles_token(tokentype):
  87.             tokentype = tokentype.parent
  88.             continue
  89.             tokentype in self._stylecache
  90.         value = self.style.style_for_token(tokentype)
  91.         result = ''
  92.         if value['color']:
  93.             result = ' fill="#' + value['color'] + '"'
  94.         
  95.         if value['bold']:
  96.             result += ' font-weight="bold"'
  97.         
  98.         if value['italic']:
  99.             result += ' font-style="italic"'
  100.         
  101.         self._stylecache[otokentype] = result
  102.         return result
  103.  
  104.  
  105.