【问题标题】:Python Markdown2 fenced-code-blocks indentation issuePython Markdown2 防护代码块缩进问题
【发布时间】: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>
  

如果我删除多余的内容,内容不会呈现为代码块,而是呈现为简单的&lt;p&gt; 标记。

我使用&lt;span&gt; 进行突出显示,但是如何获得如下缩进的结果(从 Pycharm 捕获)?我真的不明白它应该如何输出该结果。

【问题讨论】:

    标签: python html css markdown github-flavored-markdown


    【解决方案1】:

    indent() 方法搞砸了尝试删除它,它对我来说工作正常,你可以试试!

    【讨论】:

    • 谢谢!当我发现自己时,我回答了自己,我不知道我是怎么错过你的答案的。愚蠢的我,我认为 indent() 方法仅用于 html 可读性目的。现在我知道了!
    猜你喜欢
    • 2012-11-20
    • 1970-01-01
    • 2015-12-03
    • 2018-04-26
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多