Login

Pygmentify a code using template filter

Author:
tamizhgeek
Posted:
May 11, 2011
Language:
Python
Version:
1.3
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.