首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Markdown2围栏-代码块缩进问题

Python Markdown2围栏-代码块缩进问题
EN

Stack Overflow用户
提问于 2020-10-10 18:15:40
回答 1查看 441关注 0票数 0

上下文

我正在构建一个静态站点生成器,用于使用python的标记文件。我选择使用Markdown2库将*.md文件转换为html文章,这很好。我使用了一个包含代码块的标记测试文件。由于我希望突出显示它们,所以我安装了Pygments css并额外使用了“围栏-代码块” Markdown2。我使用亚塔格将呈现的标记内容包装在<article>中。

以下是代码:

代码语言:javascript
复制
    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

下面是我的配置文件中的附加内容:

代码语言:javascript
复制
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文件的摘录:

代码语言:javascript
复制
    JS

    ```js
    var foo = function (bar) {
      return bar++;
    };

    console.log(foo(5));
    ```

The issue

我不能把它适当地缩进去。我觉得我错过了一些东西,下面是我的输出:

代码语言:javascript
复制
  <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捕获)?我真的不明白它是如何输出这个结果的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-11 16:22:53

indent()方法正在搞糟它,尝试删除它,它对我很好,您可以尝试它!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64296755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档