| | 16 | def 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 | |