Login

Parsing and Highlighting <code> Blocks

Author:
joshua
Posted:
February 27, 2007
Language:
Python
Version:
Pre .96
Score:
6 (after 10 ratings)

This function takes a string (most likely from a template), searches it for <code>[...]</code>, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the <code>[...]</code> must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from BeautifulSoup import BeautifulSoup

from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import guess_lexer, get_lexer_by_name

import re 

def Parse ( content ): 

    tree    = BeautifulSoup ( content )

    for code in tree.findAll ( 'code' ): 
        
        if not code['class']: code['class'] = 'text'

        lexer = get_lexer_by_name(code['class'])

        new_content = highlight ( code.contents[0], lexer, HtmlFormatter() )
        code.replaceWith ( "%s\n%s" % (info,new_content) )
   
    content = str(tree)
    return content

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

kaikuehne (on March 4, 2007):

What is "info"?

#

ludvig.ericson (on March 7, 2007):

Might be a good idea to mention that it requires two libraries, BeautifulSoup and pygments. Also on line 20, you pass the format string an info variable which, from what I can see, isn't set.

Both content and new_content are excessive variables, especially content.

#

alexdong (on March 8, 2007):

This is a good idea. But how am I supposed to use this feature? ie where does the content come from?

#

Please login first before commenting.