Login

Pygmentify a code using template filter

Author:
tamizhgeek
Posted:
May 11, 2011
Language:
Python
Version:
1.3
Tags:
template django pygments custom-filters
Score:
1 (after 1 ratings)

The above snippet can be added as a custom filter for rendering code snippets in django templates. A simple '|render' next to any source code will do. To add css, use the following command to generate css file.

pygmentize -S emacs -f html > pygments-colorful.css

And link this css file to the template.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from django import template                                                                                                  
register = template.Library()                                                                                                
                                                                                                                             
                                                                                                                             
from pygments import highlight                                                                                               
from pygments.formatters import HtmlFormatter                                                                                
from pygments.lexers import get_lexer_by_name, guess_lexer                                                                   
                                                                                                                             
@register.filter                                                                                                             
def render(content):                                                                                                         
    """Render this content for display."""                                                                                   
    formatter = HtmlFormatter(cssclass=u'source')                                                                            
                                                                                                                             
    try:                                                                                                                     
        # Guess a lexer by the contents of the block.                                                                        
        lexer = guess_lexer(content)                                                                                         
    except ValueError, e:                                                                                                    
        # Just make it plain text.                                                                                           
        lexer = get_lexer_by_name(u'text', stripnl=True, encoding=u'UTF-8')                                                  
    result = highlight(content, lexer, formatter)                                                                            
    return unicode(result)                                                                                                   

More like this

  1. Pygments Rendering Template Filter by ericflo 8 years ago
  2. typygmentdown by ubernostrum 7 years, 6 months ago
  3. pygments stylize by mgiger 7 years, 7 months ago
  4. Pygmentation by alcides 6 years, 8 months ago
  5. Markdown and Syntax Highlighting in Django by blinks 7 years, 11 months ago

Comments

Please login first before commenting.