簡體   English   中英

如何使Jekyll摘錄與Jupyter筆記本一起使用

[英]How to make Jekyll excerpts work with Jupyter Notebooks

我有一個github.io博客,其中包含文本(Markdown)和Jupyter Notebook帖子。 使用nbconvert將Jupyter Notebook帖子轉換為HTML。 這很好用,但是索引頁上有一個except部分,不適用於HTML筆記本。 對於Markdown帖子,它從每個帖子中摘錄到索引頁面,但對於HTML而言則不然。 我希望能夠包括摘錄,例如Markdown的第一個單元格,作為索引頁面上的例外。 我試圖找到一個既實用又看起來不錯的解決方案。

該博客是Barry Clark的Jekyll Now模板的分支。

我嘗試像這樣使用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轉換和代碼塊的輸入/輸出丟失,因此格式看起來效果不佳。

我想出了一種使它起作用的方法,盡管這也許不是最好的解決方案。 編輯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單元中提供該字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM