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) { Parser.prototype.render = function (src) {
var state; var state, lineStart = 0, lastTabPos = 0;
if (!src) { return ''; } 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( state = new State(
src, src,
this.lexerBlock, this.lexerBlock,

6
lib/state.js

@ -15,11 +15,7 @@ function State(src, lexerBlock, lexerInline, renderer, tokens, options) {
// - replace tabs with spaces // - replace tabs with spaces
// - remove `\r` to simplify newlines check (???) // - remove `\r` to simplify newlines check (???)
this.src = src this.src = src;
.replace(/\t/g, ' ')
.replace(/\r/g, '')
.replace(/\u00a0/g, ' ')
.replace(/\u2424/g, '\n');
// Shortcuts to simplify nested calls // Shortcuts to simplify nested calls
this.lexerBlock = lexerBlock; 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 src line: 395
@ -1802,20 +1767,6 @@ error:
<p>`hi`lo`</p> <p>`hi`lo`</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3616
.
\ \A\a\ \3\φ\«
.
<p>\ \A\a\ \3\φ\«</p>
.
error:
<p>\ \A\a\ \3\φ\«</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3647 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 src line: 241
@ -1741,6 +1763,15 @@ src line: 3607
<p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p> <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
. .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3616
.
\ \A\a\ \3\φ\«
.
<p>\ \A\a\ \3\φ\«</p>
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src line: 3625 src line: 3625

Loading…
Cancel
Save