处理器

NBProcessor 的一些处理器

在本页中,我们将使用这个私有辅助函数来处理 notebook 并返回结果,以简化测试

def _run_procs(procs=None, return_nb=False, path=_test_file):
    nbp = NBProcessor(path, procs)
    nbp.process()
    if return_nb: return nbp.nb
    return '\n'.join([str(cell) for cell in nbp.nb.cells])

source

populate_language

 populate_language (nb)

根据 NB 元数据和魔法命令设置单元格语言


source

insert_warning

 insert_warning (nb)

在第一个单元格后向 Notebook 插入自动生成警告。

这个预处理器会在 markdown 目的地插入一个警告,说明该文件是自动生成的。这个警告被插入到第二个单元格,这样就不会干扰 front matter。

res = _run_procs(insert_warning)
assert "<!-- WARNING: THIS FILE WAS AUTOGENERATED!" in res
L('foo', None, 'a').filter(lambda x:x == 1)
_tstre = re.compile('a')

source

add_show_docs

 add_show_docs (nb)

在导出的单元格后添加 show_doc 单元格,除非它们已经有文档


source

cell_lang

 cell_lang (cell)
res = _run_procs([populate_language, add_show_docs])
assert "show_doc(some_func)'" in res
assert "show_doc(and_another)'" in res
assert "show_doc(another_func)'" not in res

source

fdiv

 fdiv (attrs='')

在 quarto 中创建一个 fenced div markdown 单元格

a = fdiv('.py-2')
test_eq(a.cell_type, 'markdown')
test_eq(a.source, '::: {.py-2}')

source

boxify

 boxify (cells)

cells 周围添加一个框


source

mv_exports

 mv_exports (nb)

exports 单元格移动到 show_doc 之后


source

add_fold

 add_fold (cell)

exports 单元格添加 code-fold

res = _run_procs(add_fold)
assert "#| code-fold: show" in res

去除从标准输出流出的颜色,这些颜色可能会干扰静态网站生成器


source

strip_ansi

 strip_ansi (cell)

去除 Ansi 字符。

res = _run_procs(strip_ansi)
assert not _re_ansi_escape.findall(res)

source

strip_hidden_metadata

 strip_hidden_metadata (cell)

去除代码单元格中的“hidden”元数据属性,以免干扰文档渲染


source

hide_

 hide_ (cell)

从输出中隐藏单元格

res = _run_procs(hide_)
assert 'you will not be able to see this cell at all either' not in res

source

hide_line

 hide_line (cell)

在代码单元格中,通过在代码行末尾使用 hide_line 指令来隐藏代码行

res = _run_procs(hide_line)
assert r"def show():\n    a = 2\n    b = 3" not in res
assert r"def show():\n    a = 2"                in res

source

filter_stream_

 filter_stream_ (cell, *words)

移除 cell 流输出中包含任意 words 的输出行

res = _run_procs(filter_stream_)
exp=r"'A line\n', 'Another line.\n'"
assert exp in res

source

ai_magics

 ai_magics (cell)

一个将 AI 魔法命令转换为 markdown 的预处理器

res = _run_procs(ai_magics)
assert "'source': 'This is a test.'" in res

source

clean_magics

 clean_magics (cell)

一个移除单元格魔法命令的预处理器

res = _run_procs(clean_magics)
assert "%%" not in res

source

rm_header_dash

 rm_header_dash (cell)

移除以破折号 - 结尾的标题

res = _run_procs(rm_header_dash)
assert 'some words' in res
assert 'A heading to Hide' not in res
assert 'Yet another heading to hide' not in res

source

rm_export

 rm_export (cell)

移除已导出或隐藏的单元格

res = _run_procs(rm_export)
assert 'dontshow' not in res

source

clean_show_doc

 clean_show_doc (cell)

移除 ShowDoc 输入单元格


source

exec_show_docs

 exec_show_docs (nb)

执行 show_docs 输出所需的单元格,包括导出的单元格和导入

res = _run_procs([add_show_docs, exec_show_docs])
assert res

source

FilterDefaults

 FilterDefaults ()

覆盖 FilterDefaults 以更改使用的 notebook 处理器