【问题标题】:How to make Jekyll excerpts work with Jupyter Notebooks如何使 Jekyll 摘录与 Jupyter Notebooks 一起使用
【发布时间】:2017-09-12 11:24:55
【问题描述】:

我有一个 github.io 博客,其中包含文本 (Markdown) 和 Jupyter Notebook 帖子。 Jupyter Notebook 帖子使用 nbconvert 转换为 HTML。这很好用,但索引页面有一个例外部分,它不适用于 HTML 笔记本。对于 Markdown 帖子,它会将每个帖子的摘录拉入索引页面,但不适用于 HTML 帖子。我希望能够包含一个摘录,例如 Markdown 的第一个单元格,作为索引页面上的一个例外。我正在努力寻找一个既实用又好看的解决方案。

该博客是来自Barry Clark's Jekyll Now template 的一个分支。

我尝试像这样使用 strip_html:

    <div class="entry">
        {{ post.excerpt | strip_html | truncatewords: 50}}
    </div>

但它包括日期和标题等前面的内容,有点像这样:

示例博客

2017-9-6-Example-blog 这是我的例子!

我希望它的样子:

示例博客

这是我的例子!

我还尝试编写一段代码,通过在每个文件中注入类似这样的内容,为每个 Jupyter Notebook HTML 添加描述:

<font size="5"><description>This is my example!</description></font size="5">

这适用于索引页面,但是每个博客文章的页面顶部都有“这是我的示例”,这看起来与 Jupyter Notebook 格式完全不同。无论如何,这不是一个优雅的解决方案,但如果有一个 HTML 标记可以被索引拾取(不使用 strip_html)但不会在帖子中显示,那就足够了。

我也尝试将 Jupyter Notebook 直接转换为 Markdown,但由于 HTML 转换和代码块的输入/输出丢失,格式看起来不太好。

【问题讨论】:

    标签: html jekyll jupyter-notebook


    【解决方案1】:

    我想出了一个让它工作的方法,尽管它可能不是最好的解决方案。编辑 index.html 文件以包含:

        <div class="entry">
            {{ post.content | strip_html | truncatewords: 25 }}
        </div>
    

    然后你可以使用下面的脚本来移除 nbconvert 添加的标题部分。

    import os
    import re
    load_posts_path = ''
    save_posts_path = ''
    items_in_path = os.listdir(load_posts_path)
    regex_pattern = "<title>.*</title>"
    for file in items_in_path:
        # If the file is HTML
        if file[-4:].lower() == 'html':
            with open(load_posts_path+file) as f:
                text = f.readlines()
                # Check to see if the file contains the regex pattern
                if re.search(pattern=regex_pattern, string=text[3]):
                    print("Removing file name from " + file)
                    # Remove the title from the text
                    text[3] = re.sub(regex_string, "", text[3]) # The title is in the third line
                    with open(save_posts_path+file, 'w') as w:
                        for line in text:
                            w.write(line)
    

    现在索引将抓取文件的前 25 个单词,您可以在第一个 Markdown 单元格中提供这些单词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2020-02-01
      • 1970-01-01
      • 2019-06-25
      • 2013-05-01
      相关资源
      最近更新 更多