微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Markdown2防护代码块缩进问题 上下文: 问题:

如何解决Python Markdown2防护代码块缩进问题 上下文: 问题:

上下文

我正在使用python构建用于markdown文件的静态站点生成器。 我选择使用Markdown2库将* .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>
  

wrong output

如果我删除了多余的内容,则内容不会呈现为代码块,而是呈现为简单的<p>标签

output without fenced-code-blocks

我使用<span>来突出显示,但是如何使结果缩进如下(从Pycharm捕获)?我真的不明白应该如何输出结果。

pycharm capture

解决方法

indent()方法将其弄乱了,请尝试删除它,它对我来说很好用,您可以尝试一下!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。