【发布时间】:2021-01-25 12:30:58
【问题描述】:
上下文:
我正在使用 python 为 markdown 文件构建一个静态站点生成器。
我选择使用Markdown2 lib 将 *.md 文件转换为 html 文章,效果很好。我使用了降价 test file 包括代码块。由于我希望它们被突出显示,我安装了Pygments-css 并额外使用了"fenced-code-blocks" Markdown2。
我使用Yattag 将markdown 呈现的内容包装在<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 捕获)?我真的不明白它应该如何输出该结果。
【问题讨论】:
标签: python html css markdown github-flavored-markdown