Browse Source

Replace tabs properly

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
80b4223040
  1. 31
      lib/parser.js
  2. 6
      lib/state.js
  3. 49
      test/fixtures/stmd/bad.txt
  4. 31
      test/fixtures/stmd/good.txt

31
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,

6
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;

49
test/fixtures/stmd/bad.txt

@ -1,38 +1,3 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 205
.
foo baz bim
.
<pre><code>foo baz bim
</code></pre>
.
error:
<pre><code>foo baz bim
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 212
.
a a
ὐ a
.
<pre><code>a a
ὐ a
</code></pre>
.
error:
<pre><code>a a
ὐ a
</code></pre>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 395
@ -1802,20 +1767,6 @@ error:
<p>`hi`lo`</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3616
.
\ \A\a\ \3\φ\«
.
<p>\ \A\a\ \3\φ\«</p>
.
error:
<p>\ \A\a\ \3\φ\«</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3647

31
test/fixtures/stmd/good.txt

@ -1,3 +1,25 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 205
.
foo baz bim
.
<pre><code>foo baz bim
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 212
.
a a
ὐ a
.
<pre><code>a a
ὐ a
</code></pre>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 241
@ -1741,6 +1763,15 @@ src line: 3607
<p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3616
.
\ \A\a\ \3\φ\«
.
<p>\ \A\a\ \3\φ\«</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3625

Loading…
Cancel
Save