Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从md到ipynb的转义与反转义 #97

Open
liujuanjuan1984 opened this issue Jan 15, 2020 · 1 comment
Open

从md到ipynb的转义与反转义 #97

liujuanjuan1984 opened this issue Jan 15, 2020 · 1 comment
Labels
困惑已解决 原来的困惑已解决,但没有整理为文章。

Comments

@liujuanjuan1984
Copy link
Owner

liujuanjuan1984 commented Jan 15, 2020

最近遇到一个难点,把 markdown 文本转换为 ipynb 文件,同时对 ipynb 文件进行正则搜索替换操作。期间反复遭遇嵌套了好几层的转义与反转义的情境,感到棘手,我也一直想认真梳理一下,今天且试试看。

第一层:从 markdown 文本转换为 ipynb 文件时,需要把一些特殊符号进行反转义。

首先,从一个最简单的例子说起。这段 markdown 文本中存在英文双引号。

"How are you?" she asked.

生成 ipynb 文件时,需要对 markdown 文本中存在的英文双引号添加反转义符号。这是由于 ipynb 文件的语法是 json 样式的,双引号是非常关键的语法符号。

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"How are you?\" she asked."
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}

接下来,就可以说一个稍微复杂点的案例了。即,markdown 文本中,自带反转义符号。比如:

这是一个新的例子,想在markdown文本中分别敲出:(英文圆括号)和\[英文中括号\],但中括号需要加上反转义避免被markdown语法解析为超链接。

生成 ipynb 文件时,需要对 markdown 文本中自带的反转义符增加一层反转义。

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"How are you?\" she asked.\n",
    "\n",
    "这是一个新的例子,想在markdown文本中分别敲出:(英文圆括号)和\\[英文中括号\\],但中括号需要加上反转义避免被markdown语法解析为超链接。\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}

更复杂一点的是,我们在 markdown 文本中表达:

```markdown
"How are you?" she asked. 在 ipynb 文件中因为增加了反转义符,文本将会变成:\"How are you?\" she asked.

实际上 markdown 文本中,就需要为反转义符添加一层反转义符,于是变成\\" 才会显示为\"

"How are you?" she asked. 在 ipynb 文件中因为增加了反转义符,文本将会变成:\\"How are you?\\" she asked.

那么上面这段 markdown 文本生成 ipynb 时,会有几个反转义符呢?答案是:5个。

"\"How are you?\" she asked. 在 ipynb 文件中因为增加了反转义符,文本将会变成:\\\\\"How are you?\\\\\" she asked. "

@liujuanjuan1984 liujuanjuan1984 added the 困惑已解决 原来的困惑已解决,但没有整理为文章。 label Jan 15, 2020
@liujuanjuan1984
Copy link
Owner Author

加上正则后,就更复杂了。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
困惑已解决 原来的困惑已解决,但没有整理为文章。
Projects
None yet
Development

No branches or pull requests

1 participant