diff --git a/lib/parser.js b/lib/parser.js index bad3970..498bdca 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -32,10 +32,39 @@ Parser.prototype.set = function (options) { Parser.prototype.render = function (src) { - var state; + var state, lineStart = 0, lastTabPos = 0; if (!src) { return ''; } + if (src.indexOf('\r') >= 0) { + src = src.replace(/\r/, ''); + } + + if (src.indexOf('\u00a0') >= 0) { + src = src.replace(/\u00a0/g, ' '); + } + + if (src.indexOf('\u2424') >= 0) { + src = src.replace(/\u2424/g, '\n'); + } + + // TODO: benchmark it + // Replace tabs with proper number of spaces (1..4) + if (src.indexOf('\t') >= 0) { + src = src.replace(/[\n\t]/g, function (match, offset) { + var result; + if (src.charCodeAt(offset) === 0x0A) { + lineStart = offset + 1; + lastTabPos = 0; + return match; + } + result = ' '.slice((offset - lineStart - lastTabPos) % 4); + lastTabPos = offset - lineStart + 1; + return result; + }); + } + + state = new State( src, this.lexerBlock, diff --git a/lib/state.js b/lib/state.js index 247d175..cf60342 100644 --- a/lib/state.js +++ b/lib/state.js @@ -15,11 +15,7 @@ function State(src, lexerBlock, lexerInline, renderer, tokens, options) { // - replace tabs with spaces // - remove `\r` to simplify newlines check (???) - this.src = src - .replace(/\t/g, ' ') - .replace(/\r/g, '') - .replace(/\u00a0/g, ' ') - .replace(/\u2424/g, '\n'); + this.src = src; // Shortcuts to simplify nested calls this.lexerBlock = lexerBlock; diff --git a/test/fixtures/stmd/bad.txt b/test/fixtures/stmd/bad.txt index 488ed3f..1cc5f9f 100644 --- a/test/fixtures/stmd/bad.txt +++ b/test/fixtures/stmd/bad.txt @@ -1,38 +1,3 @@ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 205 - -. - foo baz bim -. -
foo baz bim
-
-.
-
-error:
-
-foo baz bim
-
-
-
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-src line: 212
-
-.
- a a
- ὐ a
-.
-a a
-ὐ a
-
-.
-
-error:
-
-a a
-ὐ a
-
-
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 395
@@ -1802,20 +1767,6 @@ error:
`hi`lo`
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -src line: 3616 - -. -\ \A\a\ \3\φ\« -. -\ \A\a\ \3\φ\«
-. - -error: - -\ \A\a\ \3\φ\«
- - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 3647 diff --git a/test/fixtures/stmd/good.txt b/test/fixtures/stmd/good.txt index 4d369fe..28f61d5 100644 --- a/test/fixtures/stmd/good.txt +++ b/test/fixtures/stmd/good.txt @@ -1,3 +1,25 @@ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 205 + +. + foo baz bim +. +foo baz bim
+
+.
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+src line: 212
+
+.
+ a a
+ ὐ a
+.
+a a
+ὐ a
+
+.
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 241
@@ -1741,6 +1763,15 @@ src line: 3607
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +src line: 3616 + +. +\ \A\a\ \3\φ\« +. +\ \A\a\ \3\φ\«
+. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src line: 3625