@environmentfilter
def syntax(env, value, lexer=None, filename=None):
 try:
  import pygments
  from pygments import lexers
  from pygments import formatters
 except ImportError:
  logger.error(u'pygments library is required to use syntax highlighting tags.')
  raise TemplateError('Cannot load pygments')
 pyg = (lexers.get_lexer_by_name(lexer) if lexer else lexers.guess_lexer(value))
 settings = {}
 if hasattr(env.config, 'syntax'):
  settings = getattr(env.config.syntax, 'options', Expando({})).to_dict()
 formatter = formatters.HtmlFormatter(**settings)
 code = pygments.highlight(value, pyg, formatter)
 code = code.replace('\n\n', '\n&nbsp;\n').replace('\n', '<br />')
 caption = (filename if filename else pyg.name)
 if hasattr(env.config, 'syntax'):
  if (not getattr(env.config.syntax, 'use_figure', True)):
   return Markup(code)
 return Markup(('<div class="codebox"><figure class="code">%s<figcaption>%s</figcaption></figure></div>\n\n' % (code, caption)))

@ environmentfilter def syntax ( env , value , lexer = None , filename = None ) : try : import pygments from pygments import lexers from pygments import formatters except ImportError : logger . error ( u'pygments library is required to use syntax highlighting tags.' ) raise TemplateError ( 'Cannot load pygments' ) pyg = ( lexers . get_lexer_by_name ( lexer ) if lexer else lexers . guess_lexer ( value ) ) settings = { } if hasattr ( env . config , 'syntax' ) : settings = getattr ( env . config . syntax , 'options' , Expando ( { } ) ) . to_dict ( ) formatter = formatters . HtmlFormatter ( ** settings ) code = pygments . highlight ( value , pyg , formatter ) code = code . replace ( '\n\n' , '\n&nbsp;\n' ) . replace ( '\n' , '<br />' ) caption = ( filename if filename else pyg . name ) if hasattr ( env . config , 'syntax' ) : if ( not getattr ( env . config . syntax , 'use_figure' , True ) ) : return Markup ( code ) return Markup ( ( '<div class="codebox"><figure class="code">%s<figcaption>%s</figcaption></figure></div>\n\n' % ( code , caption ) ) )
processes the contained block using pygments .