Markdown 和转义 Javascript 换行符

发布于 2024-08-06 04:05:26 字数 1058 浏览 3 评论 0原文

我正在 Erlang 中编写一个 markdown 编译器以供服务器端使用。因为它需要与客户端合作,所以我决定采用客户端库 (showdown.js) 作为规范,然后测试我的代码与其兼容性。

在前几次迭代中,我构建了 260 多个单元测试,这些测试检查我的程序生成的输出是否与基于读取 我认为有效的降价 兼容a href="http://daringfireball.net/projects/markdown/syntax" rel="nofollow noreferrer">语法注释。

但现在我正在尝试编写一个 javascript 程序来生成我的单元测试。

我有这样的输入:

"3 > 4\na"

我想对其运行“摊牌”以获得:

"<p>3 &gt; 4\na</p>"

并且我想将其缝合到 EUnit 断言中:

"?_assert(conv(\"3 > 4\na\") == \"<p>3 &gt; 4\na</p>\"),",

这是单元测试的有效 Erlang 语法。为了让生活变得轻松,并使单元测试生成器可移植,我在网页中执行此操作,以便通过将一些行附加到 javascript 文件,然后查看页面,您可以在 < 中获得新的单元测试集;textarea /> 然后将其复制到模块中以运行 EUnit。

问题是我无法将换行符转换为字符串中的 \n 所以我最终得到:

"?_assert(conv(\"3 > 4
a\") == \"<p>3 &gt; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-08-13 04:05:26

Tom McNulty 帮助我并指出我的正则表达式是超级裤子,特别是我没有使用全局标志:(

工作代码是:

var converter;
var text = "";
var item;
var input;
var output;
var head;
var tail;
converter = new Attacklab.showdown.converter();
item = document.getElementById("tests");
for (var test in tests) {
  input = tests[test].replace(/[\n\r]+/gi,"\\n" );
  input = input.replace(/[\"]+/gi, "\\\"");
  output = converter.makeHtml(tests[test]).replace(/[\n\r]+/gi, "\\n");
  output = output.replace(/[\"]+/gi, "\\\"");
  text += "     ?_assert(conv(\"" + input + "\") == \"" + output + "\"),\n";
};
head = "unit_test_() -> \n    [\n";
tail = "\n    ].";
text = text.slice(0, text.length - 2);
item.value = head + text + tail;

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:

var converter;
var text = "";
var item;
var input;
var output;
var head;
var tail;
converter = new Attacklab.showdown.converter();
item = document.getElementById("tests");
for (var test in tests) {
  input = tests[test].replace(/[\n\r]+/gi,"\\n" );
  input = input.replace(/[\"]+/gi, "\\\"");
  output = converter.makeHtml(tests[test]).replace(/[\n\r]+/gi, "\\n");
  output = output.replace(/[\"]+/gi, "\\\"");
  text += "     ?_assert(conv(\"" + input + "\") == \"" + output + "\"),\n";
};
head = "unit_test_() -> \n    [\n";
tail = "\n    ].";
text = text.slice(0, text.length - 2);
item.value = head + text + tail;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文