Skip to content

Commit

Permalink
Fix bug when language to highlight does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Sokolov committed Dec 2, 2011
1 parent fde2b65 commit de222fb
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions site/source/apps/techblog/functions.py
Expand Up @@ -7,14 +7,20 @@
from pygments.lexers import get_lexer_by_name
from pygments.styles import get_style_by_name
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound

def html_parser(value):
value = re.sub('(?is)(<script[^>]*>.*?</script[^>]*>|<link[^>/]*/?>)', '', value).strip() # cleaning
if value:
formatter = HtmlFormatter(nowrap=True, lineseparator='<br/>', style=get_style_by_name('native'))
def render_raw(match):
lexer = get_lexer_by_name(match.groups()[0])
return '<code class="highlight">%s</code>' % highlight(match.groups()[1], lexer, formatter)
syntax, data = match.groups()
try:
lexer = get_lexer_by_name(syntax)
return '<code class="highlight">%s</code>' % highlight(data, lexer, formatter)
except ClassNotFound:
return '<code class="highlight">%s</code>' % data

value = re.sub('(?is)<code[^>]*language="(\w+)"[^>]*>(.*?)</code[^>]*>', render_raw, value)
return value

Expand Down

0 comments on commit de222fb

Please sign in to comment.