nfo
nfo

Reputation: 647

Markdown and Syntax Highlighting in Django with mixed code

I have some problems with following string while trying to syntax highlight them:

Example

<code class="php"><? echo "<input type=\"text\">"; ?></code>

The php part is rendered correctly, but the html part breaks.

I use the Markdown and Syntax Highlighting snippet from

http://www.djangosnippets.org/snippets/119/

Any idea how to escape the html part inside the php code correctly ?

Upvotes: 2

Views: 2053

Answers (2)

stefanB
stefanB

Reputation: 79790

Python markdown integrates with Pygments that do the syntax highlighting.

You can go from markdown to html formatted text with source code with highlighted syntax.

The short version is:

import markdown
html = markdown.markdown(text,['codehilite'])

html contains the html formatted text with the source code highlighted. You just need to point to css style, that's it.

Have a look at how to setup markdow and pygments to do syntax highlight for blogger.

In your solution you can just include a reference to css which makes it even easier.

Upvotes: 1

Dominic Rodger
Dominic Rodger

Reputation: 99761

Looks like you need to pass your PHP/HTML hybrid code through the escape filter, to convert instances of < to &lt; etc.

Use it like this in a template, assuming you've got your code in a template context variable called mycode:

{{ mycode|escape }}

Upvotes: 1

Related Questions