#NEWLINE# def post_render_content( sender , instance , **kwargs ) : #NEWLINE# #INDENT# '\n    Render the post content on save.\n    Converts all code blocks to syntax highlighted pygments\n    ' #NEWLINE# if ( instance . source_content is None ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# content = instance . source_content #NEWLINE# code_regex = re . compile ( '(<code(.*?)</code>)' , re . DOTALL ) #NEWLINE# code_blocks = [ ] #NEWLINE# for ( i , m ) in enumerate ( code_regex . finditer ( content ) ) : #NEWLINE# #INDENT# code = m . group ( 0 ) #NEWLINE# if ( code . find ( 'lang' ) > 0 ) : #NEWLINE# #INDENT# soup = BeautifulSoup ( code ) #NEWLINE# language = soup . code [ 'lang' ] #NEWLINE# try : #NEWLINE# #INDENT# lexer = lexers . get_lexer_by_name ( language ) #NEWLINE# #UNINDENT# except ClassNotFound : #NEWLINE# #INDENT# lexer = lexers . get_lexer_by_name ( 'text' ) #NEWLINE# #UNINDENT# code = code . replace ( '</code>' , '' ) #NEWLINE# code = code . replace ( '<code>' , '' ) #NEWLINE# code = re . sub ( '<code(.*?)>' , '' , code ) #NEWLINE# pygmented_string = pygments . highlight ( code , lexer , formatters . HtmlFormatter ( ) ) #NEWLINE# code_blocks . append ( pygmented_string ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# code_blocks . append ( code ) #NEWLINE# #UNINDENT# content = content . replace ( m . string [ m . start ( ) : m . end ( ) ] , ( 'OMCB|%s|OMCB' % i ) ) #NEWLINE# #UNINDENT# for ( i , code ) in enumerate ( code_blocks ) : #NEWLINE# #INDENT# content = content . replace ( ( '<p>OMCB|%s|OMCB</p>' % i ) , code ) #NEWLINE# content = content . replace ( ( 'OMCB|%s|OMCB' % i ) , code ) #NEWLINE# #UNINDENT# content = markdown ( content , extras = [ 'footnotes' , 'header-ids' , 'markdown-in-html' , 'smarty-pants' ] ) #NEWLINE# c = template . Context ( { #NEWLINE# #INDENT# 'instance' : instance , #NEWLINE# #UNINDENT# } ) #NEWLINE# content = ( '{% load omblog_tags %}' + content ) #NEWLINE# t = template . Template ( content ) #NEWLINE# content = t . render ( c ) #NEWLINE# instance . rendered_content = content