Python Markdown2 围栏代码块缩进问题
Python Markdown2 fenced-code-blocks indentation issue
上下文:
我正在使用 python 为降价文件构建一个静态站点生成器。
我选择使用 Markdown2 lib to convert *.md files into html articles and that works pretty well. I used a markdown test file including code blocks. As I want them to be highlighted, I installed Pygments-css and used the "fenced-code-blocks" Markdown2 extra。
我使用 Yattag 将降价呈现的内容包装在 <article>
.
中
代码如下:
def render_md(self, md_file_content, extras):
f = self.generate() # generates doctype, head etc
# the following returns a string like "03 minute(s) read" depending on article words count
estimated_time = self.get_reading_time(md_file_content)
# markdown2 returns a string containing html
article = markdown2.markdown(md_file_content, extras=extras)
# the two following lines handle emoji
article = emoticons_to_emoji(article)
article = emoji.emojize(article, use_aliases=True, variant="emoji_type")
doc, tag, text = Doc().tagtext()
with tag('article'):
with tag('span', klass='article__date'):
text(time.strftime(f'%Y %b %d - %H:%M {estimated_time}'))
# the following allows me to append a string containing html "as is", not altered by Yattag
doc.asis(article)
return self.close_html(f + indent(doc.getvalue())) # self.close_html adds some closing tags and js script tags
这是我的配置文件中的额外内容:
extras: # Documentation on https://github.com/trentm/python-markdown2/wiki/Extras
- code-friendly
- cuddled-lists
- fenced-code-blocks
- tables
- footnotes
- smarty-pants
- numbering
- tables
- strike
- spoiler
这是 *.md 文件摘录:
JS
```js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
```
问题:
我无法正确缩进。我觉得我遗漏了什么,这是我得到的输出:
<div class="codehilite">
<pre>
<span></span>
<code>
<span class="kd">var</span>
<span class="nx">foo</span>
<span class="o">=</span>
<span class="kd">function</span>
<span class="p">(</span>
<span class="nx">bar</span>
<span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span>
<span class="nx">bar</span>
<span class="o">++</span>
<span class="p">;</span>
<span class="p">};</span>
<span class="nx">console</span>
<span class="p">.</span>
<span class="nx">log</span>
<span class="p">(</span>
<span class="nx">foo</span>
<span class="p">(</span>
<span class="mf">5</span>
<span class="p">));</span>
</code>
</pre>
</div>
如果我删除额外内容,内容不会呈现为代码块,而是呈现为简单的 <p>
标记。
我使用 <span>
来突出显示,但是如何使结果缩进如下(从 Pycharm 捕获)?我真的不明白它应该如何输出那个结果。
indent() 方法搞砸了尝试删除它,它对我来说工作正常,你可以试试!
上下文:
我正在使用 python 为降价文件构建一个静态站点生成器。
我选择使用 Markdown2 lib to convert *.md files into html articles and that works pretty well. I used a markdown test file including code blocks. As I want them to be highlighted, I installed Pygments-css and used the "fenced-code-blocks" Markdown2 extra。
我使用 Yattag 将降价呈现的内容包装在 <article>
.
代码如下:
def render_md(self, md_file_content, extras):
f = self.generate() # generates doctype, head etc
# the following returns a string like "03 minute(s) read" depending on article words count
estimated_time = self.get_reading_time(md_file_content)
# markdown2 returns a string containing html
article = markdown2.markdown(md_file_content, extras=extras)
# the two following lines handle emoji
article = emoticons_to_emoji(article)
article = emoji.emojize(article, use_aliases=True, variant="emoji_type")
doc, tag, text = Doc().tagtext()
with tag('article'):
with tag('span', klass='article__date'):
text(time.strftime(f'%Y %b %d - %H:%M {estimated_time}'))
# the following allows me to append a string containing html "as is", not altered by Yattag
doc.asis(article)
return self.close_html(f + indent(doc.getvalue())) # self.close_html adds some closing tags and js script tags
这是我的配置文件中的额外内容:
extras: # Documentation on https://github.com/trentm/python-markdown2/wiki/Extras
- code-friendly
- cuddled-lists
- fenced-code-blocks
- tables
- footnotes
- smarty-pants
- numbering
- tables
- strike
- spoiler
这是 *.md 文件摘录:
JS
```js
var foo = function (bar) {
return bar++;
};
console.log(foo(5));
```
问题:
我无法正确缩进。我觉得我遗漏了什么,这是我得到的输出:
<div class="codehilite">
<pre>
<span></span>
<code>
<span class="kd">var</span>
<span class="nx">foo</span>
<span class="o">=</span>
<span class="kd">function</span>
<span class="p">(</span>
<span class="nx">bar</span>
<span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span>
<span class="nx">bar</span>
<span class="o">++</span>
<span class="p">;</span>
<span class="p">};</span>
<span class="nx">console</span>
<span class="p">.</span>
<span class="nx">log</span>
<span class="p">(</span>
<span class="nx">foo</span>
<span class="p">(</span>
<span class="mf">5</span>
<span class="p">));</span>
</code>
</pre>
</div>
如果我删除额外内容,内容不会呈现为代码块,而是呈现为简单的 <p>
标记。
我使用 <span>
来突出显示,但是如何使结果缩进如下(从 Pycharm 捕获)?我真的不明白它应该如何输出那个结果。
indent() 方法搞砸了尝试删除它,它对我来说工作正常,你可以试试!