Show
Ignore:
Timestamp:
01/06/08 14:41:54 (12 months ago)
Author:
simon
Message:

implemented syntax highlighting for codeblocks

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bitstructures/trunk/substructure/templatetags/substructure_tags.py

    r15 r42  
    11from django import template 
    22from django.utils.tzinfo import LocalTimezone 
     3from markdown import markdown 
     4from pygments import highlight 
     5from pygments.lexers import get_lexer_for_mimetype 
     6from pygments.formatters import HtmlFormatter 
     7from bitstructures.substructure.codeblocks import MarkdownCodeblocksParser 
    38 
    49def rfc3339(dt): 
     
    914    return dt_out.isoformat() 
    1015 
     16def syntax_highlighted_markdown(markdown_text): 
     17    parser = MarkdownCodeblocksParser() 
     18    formatter = HtmlFormatter() 
     19    sections = parser.parse(markdown_text) 
     20    for_markdown = list() 
     21    html = list() 
     22    for section in sections: 
     23        if section.is_codeblock() and section.content_type: 
     24            # run Markdown on any text that isn't to be syntax highlighted 
     25            if for_markdown: 
     26                html.append(markdown(''.join(for_markdown))) 
     27                for_markdown = list() 
     28            # run Pygments on code to be syntax highlighted 
     29            html.append(highlight(section.get_code(), 
     30                get_lexer_for_mimetype(section.content_type), formatter)) 
     31        else: 
     32            for_markdown.append(section.get_text()) 
     33    # run Markdown on any remaining text that isn't to be syntax highlighted 
     34    if for_markdown: 
     35        html.append(markdown(''.join(for_markdown))) 
     36    return ''.join(html) 
     37 
    1138register = template.Library() 
    12 register.filter("rfc3339", rfc3339) 
     39register.filter('rfc3339', rfc3339) 
     40register.filter('syntax_highlighted_markdown', syntax_highlighted_markdown)