迁移

迁移到 nbdev 的实用工具

迁移 notebooks


source

MigrateProc

 MigrateProc (nb)

将 fastpages notebook 中的 front matter 迁移到 raw cell。

在迁移 fastpages notebook 之前,front matter 是这样用 Markdown 指定的

_tst_nb = '../../tests/2020-09-01-fastcore.ipynb'
print(read_nb(_tst_nb).cells[0].source)
# "fastcore: An Underrated Python Library"

> A unique python library that extends the python programming language and provides utilities that enhance productivity.
- author: "<a href='https://twitter.com/HamelHusain'>Hamel Husain</a>"
- toc: false
- image: images/copied_from_nb/fastcore_imgs/td.png
- comments: true
- search_exclude: true
- hide: true
- categories: [fastcore, fastai]
- permalink: /fastcore/
- badges: true

迁移 notebook 后,front matter 会被移至 raw cell,并且一些字段会转换为符合 Quarto 的格式。此外,可能会添加别名以防止链接断裂

nbp = NBProcessor('../../tests/2020-09-01-fastcore.ipynb', procs=[FrontmatterProc, MigrateProc])
nbp.process()
_fm1 = _get_raw_fm(nbp.nb)
print(_fm1)
---
aliases:
- /fastcore/
author: <a href='https://twitter.com/HamelHusain'>Hamel Husain</a>
badges: true
categories:
- fastcore
- fastai
date: '2020-09-01'
description: A unique python library that extends the python programming language
  and provides utilities that enhance productivity.
draft: 'true'
image: fastcore_imgs/td.png
output-file: 2020-09-01-fastcore.html
permalink: /fastcore/
search: 'false'
title: 'fastcore: An Underrated Python Library'
toc: false

---

迁移 Fastpages Markdown Front Matter


source

fp_md_fm

 fp_md_fm (path)

使 markdown 文件中的 fastpages front matter 符合 quarto 规范。

这是 fastpages markdown 帖子在迁移前的 front matter 样子

print(run('head -n13 ../../tests/2020-01-14-test-markdown-post.md'))
---

toc: true
layout: post
description: A minimal example of using markdown with fastpages.
categories: [markdown]
title: An Example Markdown Post


---

# Example Markdown Post

这是迁移后的样子

_res = fp_md_fm('../../tests/2020-01-14-test-markdown-post.md')
print(_res[:300])
---
aliases:
- /markdown/2020/01/14/test-markdown-post
categories:
- markdown
date: '2020-01-14'
description: A minimal example of using markdown with fastpages.
layout: post
title: An Example Markdown Post
toc: true

---

# Example Markdown Post

## Basic setup

Jekyll requires blog post files to b
#hide
_res = fp_md_fm('../../tests/2022-09-06-homeschooling.md')
test_eq(_res,
"""---
aliases:
- /2022/09/06/homeschooling
author: Rachel Thomas
categories:
- advice
- health
date: '2022-09-06'
description: You can permanently damage your back, neck, and wrists from working without
  an ergonomic setup.  Learn how to create one for less at home.
image: /images/ergonomic1-short.jpg
summary: You can permanently damage your back, neck, and wrists from working without
  an ergonomic setup.  Learn how to create one for less at home.
tags: advice health
title: 'Essential Work-From-Home Advice: Cheap and Easy Ergonomic Setups'

---

Lorem ipsum
""")

将 nbdev v1 项目转换为 nbdev v2

指令

nbdev v2 指令以 #| 开头,而 v1 指令是没有管道符 | 的注释。

_test_dir = """
#default_exp
 #export
# collapse-show
#collapse-hide
#collapse
# collapse_output
not_dir='#export'
# hide_input
foo
# hide
"""
test_eq(_repl_directives(_test_dir),
"""
#| default_exp
#| export
#| code-fold: show
#| code-fold: true
#| code-fold: true
# collapse_output
not_dir='#export'
#| echo: false
foo
#| include: false
""")

source

_repl_v1dir

 _repl_v1dir (cell)

将 nbdev v1 指令替换为 v2 指令。

例如,如果下面任何一行是有效的 nbdev v1 指令,它们将被替换为 #|,但只在第一行代码之前

标注

在 fastpages 中,有一个使用块引用创建 NoteTipImportantWarning 标注的 markdown 快捷方式(这些只在 notebook 中有效)。由于 Quarto 有自己的使用 markdown 语法的 标注块,我们在 nbdev 中没有实现这些快捷方式。相反,我们为这些标注提供了一个手动转换实用工具,以便您可以从 fastpages 迁移到 Quarto。


source

_convert_callout

 _convert_callout (s)

将 nbdev v1 标注转换为 v2 标注。

例如,下面的 markdown

_callouts="""
## Boxes / Callouts

> Warning: There will be no second warning!

Other text

> Important: Pay attention! It's important.

> Tip: This is my tip.

> Note: Take note of `this.`
"""

转换为


## Boxes / Callouts

:::{.callout-warning}

There will be no second warning!

:::

Other text

:::{.callout-important}

Pay attention! It's important.

:::

视频嵌入

在 fastpages 中,你可以使用一个简单的 markdown 快捷方式来嵌入视频,该快捷方式涉及一个带有前缀 youtube: 的块引用,看起来像这样

> youtube: https://youtu.be/XfoYk_Z5AkI

然而,在 Quarto 中,你可以使用 视频扩展 来嵌入视频。


source

_convert_video

 _convert_video (s)

将 nbdev v1 视频嵌入替换为 v2。

_videos="""
## Videos

> youtube: https://youtu.be/XfoYk_Z5AkI
"""
print(_convert_video(_videos))

## Videos



source

migrate_nb

 migrate_nb (path, overwrite=True)

迁移 nbdev v1 和 fastpages 的 Notebooks。


source

migrate_md

 migrate_md (path, overwrite=True)

迁移 fastpages 的 Markdown 文件。


source

nbdev_migrate

 nbdev_migrate (path:str=None, no_skip:bool=False)

path 中所有的 markdown 和 notebook 文件从 v1 转换为 v2

类型 默认值 详情
path str None 包含要迁移的 notebook 和 markdown 文件的路径或 glob
no_skip bool False 不跳过以下划线开头的目录