Pandoc 用户使用指南 - Jupyter 笔记

当创建 Jupyter 笔记 时,Pandoc 会尝试推断笔记本的结构。具有 code 类的代码块将被视为代码单元格,而中间的内容则被视为 Markdown 单元格。Markdown 单元格中的图片会自动创建附件。元数据将从 jupyter 元数据字段中获取。例如:

---
title: My notebook
jupyter:
  nbformat: 4
  nbformat_minor: 5
  kernelspec:
     display_name: Python 2
     language: python
     name: python2
  language_info:
     codemirror_mode:
       name: ipython
       version: 2
     file_extension: ".py"
     mimetype: "text/x-python"
     name: "python"
     nbconvert_exporter: "python"
     pygments_lexer: "ipython2"
     version: "2.7.15"
---

# Lorem ipsum

**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus
bibendum felis dictum sodales.

\``` code
print("hello")
\```

## Pyout

\``` code
from IPython.display import HTML
HTML("""
<script>
console.log("hello");
</script>
<b>HTML</b>
""")
\```

## Image

This image # Pandoc 用户使用指南 - Jupyter 笔记

当创建 [Jupyter 笔记](https://nbformat.readthedocs.io/en/latest/) 时,Pandoc 会尝试推断笔记本的结构。具有 `code` 类的代码块将被视为代码单元格,而中间的内容则被视为 Markdown 单元格。Markdown 单元格中的图片会自动创建附件。元数据将从 `jupyter` 元数据字段中获取。例如:

````text
---
title: My notebook
jupyter:
  nbformat: 4
  nbformat_minor: 5
  kernelspec:
     display_name: Python 2
     language: python
     name: python2
  language_info:
     codemirror_mode:
       name: ipython
       version: 2
     file_extension: ".py"
     mimetype: "text/x-python"
     name: "python"
     nbconvert_exporter: "python"
     pygments_lexer: "ipython2"
     version: "2.7.15"
---

# Lorem ipsum

**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus
bibendum felis dictum sodales.

\``` code
print("hello")
\```

## Pyout

\``` code
from IPython.display import HTML
HTML("""
<script>
console.log("hello");
</script>
<b>HTML</b>
""")
\```

## Image

This image ![image](myimage.png) will be
included as a cell attachment.

如果你想添加单元格属性、以不同的方式组合单元格或向代码单元格中添加输出,那么你需要包括 div 来指示结构。你可以使用 围栏式 div原生 div 来实现这一点。这里是一个例子:

:::::: {.cell .markdown}
# Lorem

**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus
bibendum felis dictum sodales.
::::::

:::::: {.cell .code execution_count=1}
\``` {.python}
print("hello")
\```

::: {.output .stream .stdout}
\```
hello
\```
:::
::::::

:::::: {.cell .code execution_count=2}
\``` {.python}
from IPython.display import HTML
HTML("""
<script>
console.log("hello");
</script>
<b>HTML</b>
""")
\```

::: {.output .execute_result execution_count=2}
\```{=html}
<script>
console.log("hello");
</script>
<b>HTML</b>
hello
\```
:::
::::::

如果你在输出单元格中包含 原始的 HTML 或 TeX,应使用 raw 属性,如上例中的最后一个单元格所示。虽然 Pandoc 可以处理“裸露”的原始 HTML 和 TeX,但结果通常是交错的原始元素和普通的文本元素,在输出单元格中,Pandoc 期望的是一个单一且连贯的原始块。为了避免在未明确使用 raw 属性标记的情况下使用原始 HTML 或 TeX,我们建议在 Markdown 和 ipynb 笔记本之间转换时指定扩展 -raw_html-raw_tex+raw_attribute

请注意,影响 Markdown 读写选项和扩展同样会影响 ipynb 笔记本中的 Markdown 单元格。例如,--wrap=preserve 会在 Markdown 单元格中保留软换行;--markdown-headings=setext 将使 Setext 风格的标题被采用;而 --preserve-tabs 将防止制表符被转换为空格。

在本文档中