- 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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
What is "info"?
#
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.
#
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.