Markdown 和转义 Javascript 换行符
我正在 Erlang 中编写一个 markdown 编译器以供服务器端使用。因为它需要与客户端合作,所以我决定采用客户端库 (showdown.js) 作为规范,然后测试我的代码与其兼容性。
在前几次迭代中,我构建了 260 多个单元测试,这些测试检查我的程序生成的输出是否与基于读取 我认为有效的降价 兼容a href="http://daringfireball.net/projects/markdown/syntax" rel="nofollow noreferrer">语法注释。
但现在我正在尝试编写一个 javascript 程序来生成我的单元测试。
我有这样的输入:
"3 > 4\na"
我想对其运行“摊牌”以获得:
"<p>3 > 4\na</p>"
并且我想将其缝合到 EUnit 断言中:
"?_assert(conv(\"3 > 4\na\") == \"<p>3 > 4\na</p>\"),",
这是单元测试的有效 Erlang 语法。为了让生活变得轻松,并使单元测试生成器可移植,我在网页中执行此操作,以便通过将一些行附加到 javascript 文件,然后查看页面,您可以在 < 中获得新的单元测试集;textarea />
然后将其复制到模块中以运行 EUnit。
问题是我无法将换行符转换为字符串中的 \n
所以我最终得到:
"?_assert(conv(\"3 > 4
a\") == \"<p>3 > 4
a</p>\"),",
我尝试使用以下代码将换行符转换为其转义版本:
text.replace("\\", "\\\\");
text.replace("\n", "\\n");
但没有喜悦...
I am writing a markdown compiler in Erlang for server-side use. Because it will need to work with clients I have decided to adopt the client side library (showdown.js) as the specification and then test my code for compatibility with that.
In the first couple of iterations I built up 260-odd unit tests which checked that my programme produced output which was compatible with what I thought was valid markdown based on reading the syntax notes.
But now I am trying to write a javascript programme to generate my unit tests.
I have an input like:
"3 > 4\na"
I want to run 'showdown' on it to get:
"<p>3 > 4\na</p>"
and I want to stitch this up into an EUnit assertion:
"?_assert(conv(\"3 > 4\na\") == \"<p>3 > 4\na</p>\"),",
which is the valid Erlang syntax for the unit test. To make life easy, and to make the unit test generator portable I am doing it inside a web page so that by appending some lines to a javascript file and then view the page you get the new set of unit tests inside a <textarea />
which you then copy down into the module to run EUnit.
The problem is that I can't get the line breaks to convert to \n
in the string so I end up with:
"?_assert(conv(\"3 > 4
a\") == \"<p>3 > 4
a</p>\"),",
I've tried converting the linefeeds to their escaped versions using code like:
text.replace("\\", "\\\\");
text.replace("\n", "\\n");
but no joy...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tom McNulty 帮助我并指出我的正则表达式是超级裤子,特别是我没有使用全局标志:(
工作代码是:
Tom McNulty helped me out and pointed out that my regex's were super-pants, in particular I wasn't using the global flag :(
The working code is: