# -*- coding: utf-8 -*-
from pygments.formatter import Formatter
class HabrFormatter (Formatter):
_html_escape_table = (( '&' , '&' ),
( '<' , '<' ),
( '>' , '>' ),
( '"' , '"' ),
( "'" , ''' ),
( ' ' , ' ' ),
( ' \t ' , ' ' *4 ))
def escape_html ( self , value):
return reduce ( lambda value, rep: value . replace( * rep),
self . _html_escape_table, value)
def format_unencoded ( self , tokensource, outfile):
outfile . write( '<blockquote><code>' )
last_start = last_end = ''
for token_type, value in tokensource:
value = self . escape_html(value)
style = self . style . style_for_token(token_type)
start = end = ''
if style[ 'color' ]:
start += '<font color="# %s ">' % style[ 'color' ]
end = '</font>' + end
if style[ 'bold' ]:
start += '<strong>'
end = '</strong>' + end
if style[ 'italic' ]:
start += '<em>'
end = '</em>' + end
if style[ 'underline' ]:
start += '<u>'
end = '</u>' + end
if last_start != start:
outfile . write(last_end)
outfile . write(start)
outfile . write(value)
last_start, last_end = start, end
outfile . write( '</code></blockquote>' )
Source: https://habr.com/ru/post/75349/
All Articles