开发者

Markdown and Syntax Highlighting in Django with mixed code

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

Example

<开发者_如何学JAVAcode 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 ?


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 }}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜