如何将类属性及其值传递给Markdown语法

3 投票
4 回答
4076 浏览
提问于 2025-04-16 04:57

我在我的Django项目中使用Python的Markdown,当我有这个值时

#/usr/bin/env python
print "this is converted to html code block"

输出是

<pre><code>    
#/usr/bin/env python
print "this is converted to html code block"
</code><pre>

现在我的问题是,如何把类属性和它的值传递给代码元素。

举个例子:

#i should be using some markdown syntax below
[class="python"]
#/usr/bin/env python
print "this is converted to html code block"

然后这里的输出是

<pre><code class="python">    
#/usr/bin/env python
print "this is converted to html code block"
</code><pre>

这可能吗?怎么做呢?

4 个回答

0

你可以把生成的HTML通过其他工具处理一下,这样可以找到并解析#!这一行,然后根据它添加相应的Python类。使用lxml是个不错的选择。不过,我不太确定在Django中该怎么安排这个操作。

1

如果你在使用Github风格的Markdown,你可以这样做:

```python
    print "I am python!"
```

这会添加一个“lang-python”的类。我需要这个来让highlightjs高亮显示代码。

你可以在这里查看相关信息。

3

你可以在Markdown中写HTML代码,但不能像在HTML中那样添加类名和ID。

想了解更多,可以看看 这个问题 或者 这个问题

撰写回答