ThirdParty/Pygments/pygments/formatters/html.py

changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 684
2f29a0b6e1c7
equal deleted inserted replaced
11:b0996e4a289e 12:1d8dd9706f46
7 7
8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 import sys, os 11 import sys, os
12 import StringIO 12 import io
13 13
14 try: 14 try:
15 set 15 set
16 except NameError: 16 except NameError:
17 from sets import Set as set 17 from sets import Set as set
415 highlighting style. ``arg`` can be a string or list of selectors to 415 highlighting style. ``arg`` can be a string or list of selectors to
416 insert before the token type classes. 416 insert before the token type classes.
417 """ 417 """
418 if arg is None: 418 if arg is None:
419 arg = ('cssclass' in self.options and '.'+self.cssclass or '') 419 arg = ('cssclass' in self.options and '.'+self.cssclass or '')
420 if isinstance(arg, basestring): 420 if isinstance(arg, str):
421 args = [arg] 421 args = [arg]
422 else: 422 else:
423 args = list(arg) 423 args = list(arg)
424 424
425 def prefix(cls): 425 def prefix(cls):
429 for arg in args: 429 for arg in args:
430 tmp.append((arg and arg + ' ' or '') + cls) 430 tmp.append((arg and arg + ' ' or '') + cls)
431 return ', '.join(tmp) 431 return ', '.join(tmp)
432 432
433 styles = [(level, ttype, cls, style) 433 styles = [(level, ttype, cls, style)
434 for cls, (style, ttype, level) in self.class2style.iteritems() 434 for cls, (style, ttype, level) in self.class2style.items()
435 if cls and style] 435 if cls and style]
436 styles.sort() 436 styles.sort()
437 lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:]) 437 lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:])
438 for (level, ttype, cls, style) in styles] 438 for (level, ttype, cls, style) in styles]
439 if arg and not self.nobackground and \ 439 if arg and not self.nobackground and \
467 # pseudo files, e.g. name == '<fdopen>' 467 # pseudo files, e.g. name == '<fdopen>'
468 raise AttributeError 468 raise AttributeError
469 cssfilename = os.path.join(os.path.dirname(filename), 469 cssfilename = os.path.join(os.path.dirname(filename),
470 self.cssfile) 470 self.cssfile)
471 except AttributeError: 471 except AttributeError:
472 print >>sys.stderr, 'Note: Cannot determine output file name, ' \ 472 print('Note: Cannot determine output file name, ' \
473 'using current directory as base for the CSS file name' 473 'using current directory as base for the CSS file name', file=sys.stderr)
474 cssfilename = self.cssfile 474 cssfilename = self.cssfile
475 # write CSS file only if noclobber_cssfile isn't given as an option. 475 # write CSS file only if noclobber_cssfile isn't given as an option.
476 try: 476 try:
477 if not os.path.exists(cssfilename) or not self.noclobber_cssfile: 477 if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
478 cf = open(cssfilename, "w") 478 cf = open(cssfilename, "w")
479 cf.write(CSSFILE_TEMPLATE % 479 cf.write(CSSFILE_TEMPLATE %
480 {'styledefs': self.get_style_defs('body')}) 480 {'styledefs': self.get_style_defs('body')})
481 cf.close() 481 cf.close()
482 except IOError, err: 482 except IOError as err:
483 err.strerror = 'Error writing CSS file: ' + err.strerror 483 err.strerror = 'Error writing CSS file: ' + err.strerror
484 raise 484 raise
485 485
486 yield 0, (DOC_HEADER_EXTERNALCSS % 486 yield 0, (DOC_HEADER_EXTERNALCSS %
487 dict(title = self.title, 487 dict(title = self.title,
496 for t, line in inner: 496 for t, line in inner:
497 yield t, line 497 yield t, line
498 yield 0, DOC_FOOTER 498 yield 0, DOC_FOOTER
499 499
500 def _wrap_tablelinenos(self, inner): 500 def _wrap_tablelinenos(self, inner):
501 dummyoutfile = StringIO.StringIO() 501 dummyoutfile = io.StringIO()
502 lncount = 0 502 lncount = 0
503 for t, line in inner: 503 for t, line in inner:
504 if t: 504 if t:
505 lncount += 1 505 lncount += 1
506 dummyoutfile.write(line) 506 dummyoutfile.write(line)

eric ide

mercurial