上下文
我正在构建一个静态站点生成器,用于使用python的标记文件。我选择使用Markdown2库将*.md文件转换为html文章,这很好。我使用了一个包含代码块的标记测试文件。由于我希望突出显示它们,所以我安装了Pygments css并额外使用了“围栏-代码块” Markdown2。我使用亚塔格将呈现的标记内容包装在<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));
```The issue
我不能把它适当地缩进去。我觉得我错过了一些东西,下面是我的输出:
<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捕获)?我真的不明白它是如何输出这个结果的。

发布于 2020-10-11 16:22:53
indent()方法正在搞糟它,尝试删除它,它对我很好,您可以尝试它!
https://stackoverflow.com/questions/64296755
复制相似问题