Browse Source

Moved empty lines logic from rules to block parser

pull/14/head
Alex Kocharin 10 years ago
parent
commit
1b60163ba3
  1. 23
      lib/lexer_block.js
  2. 3
      lib/lexer_block/blockquote.js
  3. 3
      lib/lexer_block/fences.js
  4. 3
      lib/lexer_block/heading.js
  5. 3
      lib/lexer_block/hr.js
  6. 3
      lib/lexer_block/lheading.js
  7. 3
      lib/lexer_block/list.js
  8. 3
      lib/lexer_block/paragraph.js
  9. 5
      lib/lexer_block/table.js
  10. 10
      lib/parser.js

23
lib/lexer_block.js

@ -4,6 +4,10 @@
'use strict'; 'use strict';
var isEmpty = require('./helpers').isEmpty;
var skipEmptyLines = require('./helpers').skipEmptyLines;
var rules = []; var rules = [];
// `list` should be after `hr`, but before `heading` // `list` should be after `hr`, but before `heading`
@ -113,7 +117,9 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) {
len = this.rules.length, len = this.rules.length,
line = startLine; line = startLine;
while (line < endLine) { for (;;) {
line = state.line = skipEmptyLines(state, line, endLine);
if (line >= endLine) { break; }
// Try all possible rules. // Try all possible rules.
// On success, rule should: // On success, rule should:
@ -127,9 +133,18 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) {
if (ok) { break; } if (ok) { break; }
} }
if (ok) { if (!ok) { throw new Error('No matching rules found'); }
line = state.line;
continue; if (line >= state.line) {
throw new Error("Parser didn't update state.line");
}
line = state.line;
if (isEmpty(state, line)) {
line++;
// two empty lines should stop the parser
if (isEmpty(state, line + 1)) { break; }
} }
} }
}; };

3
lib/lexer_block/blockquote.js

@ -3,7 +3,6 @@
'use strict'; 'use strict';
var skipEmptyLines = require('../helpers').skipEmptyLines;
var skipSpaces = require('../helpers').skipSpaces; var skipSpaces = require('../helpers').skipSpaces;
var getLines = require('../helpers').getLines; var getLines = require('../helpers').getLines;
@ -85,6 +84,6 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
state.tokens.push({ type: 'blockquote_close' }); state.tokens.push({ type: 'blockquote_close' });
state.line = skipEmptyLines(state, nextLine); state.line = nextLine;
return true; return true;
}; };

3
lib/lexer_block/fences.js

@ -3,7 +3,6 @@
'use strict'; 'use strict';
var skipEmptyLines = require('../helpers').skipEmptyLines;
var skipSpaces = require('../helpers').skipSpaces; var skipSpaces = require('../helpers').skipSpaces;
var skipChars = require('../helpers').skipChars; var skipChars = require('../helpers').skipChars;
var getLines = require('../helpers').getLines; var getLines = require('../helpers').getLines;
@ -73,6 +72,6 @@ module.exports = function fences(state, startLine, endLine, silent) {
content: getLines(state, startLine + 1, nextLine, true) content: getLines(state, startLine + 1, nextLine, true)
}); });
state.line = skipEmptyLines(state, nextLine + 1); state.line = nextLine + 1;
return true; return true;
}; };

3
lib/lexer_block/heading.js

@ -4,7 +4,6 @@
var isWhiteSpace = require('../helpers').isWhiteSpace; var isWhiteSpace = require('../helpers').isWhiteSpace;
var skipEmptyLines = require('../helpers').skipEmptyLines;
var skipSpaces = require('../helpers').skipSpaces; var skipSpaces = require('../helpers').skipSpaces;
@ -67,6 +66,6 @@ module.exports = function heading(state, startLine, endLine, silent) {
} }
state.tokens.push({ type: 'heading_close', level: level }); state.tokens.push({ type: 'heading_close', level: level });
state.line = skipEmptyLines(state, ++startLine); state.line = startLine + 1;
return true; return true;
}; };

3
lib/lexer_block/hr.js

@ -4,7 +4,6 @@
var isWhiteSpace = require('../helpers').isWhiteSpace; var isWhiteSpace = require('../helpers').isWhiteSpace;
var skipEmptyLines = require('../helpers').skipEmptyLines;
module.exports = function hr(state, startLine, endLine, silent) { module.exports = function hr(state, startLine, endLine, silent) {
@ -43,6 +42,6 @@ module.exports = function hr(state, startLine, endLine, silent) {
state.tokens.push({ type: 'hr' }); state.tokens.push({ type: 'hr' });
state.line = skipEmptyLines(state, ++startLine); state.line = startLine + 1;
return true; return true;
}; };

3
lib/lexer_block/lheading.js

@ -3,7 +3,6 @@
'use strict'; 'use strict';
var skipEmptyLines = require('../helpers').skipEmptyLines;
var skipSpaces = require('../helpers').skipSpaces; var skipSpaces = require('../helpers').skipSpaces;
var skipChars = require('../helpers').skipChars; var skipChars = require('../helpers').skipChars;
@ -39,6 +38,6 @@ module.exports = function lheading(state, startLine, endLine, silent) {
state.lexerInline.tokenize(state, state.bMarks[startLine], state.eMarks[startLine]); state.lexerInline.tokenize(state, state.bMarks[startLine], state.eMarks[startLine]);
state.tokens.push({ type: 'heading_close', level: marker === 0x3D/* = */ ? 1 : 2 }); state.tokens.push({ type: 'heading_close', level: marker === 0x3D/* = */ ? 1 : 2 });
state.line = skipEmptyLines(state, ++next); state.line = next + 1;
return true; return true;
}; };

3
lib/lexer_block/list.js

@ -5,7 +5,6 @@
var isEmpty = require('../helpers').isEmpty; var isEmpty = require('../helpers').isEmpty;
var skipSpaces = require('../helpers').skipSpaces; var skipSpaces = require('../helpers').skipSpaces;
var skipEmptyLines = require('../helpers').skipEmptyLines;
// Search `[-+*][\n ]`, returns next pos arter marker on success // Search `[-+*][\n ]`, returns next pos arter marker on success
@ -243,6 +242,6 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tokens.push({ type: 'bullet_list_close' }); state.tokens.push({ type: 'bullet_list_close' });
} }
state.line = skipEmptyLines(state, nextLine); state.line = nextLine;
return true; return true;
}; };

3
lib/lexer_block/paragraph.js

@ -4,7 +4,6 @@
var isEmpty = require('../helpers').isEmpty; var isEmpty = require('../helpers').isEmpty;
var skipEmptyLines = require('../helpers').skipEmptyLines;
module.exports = function paragraph(state, startLine, endLine) { module.exports = function paragraph(state, startLine, endLine) {
@ -34,6 +33,6 @@ module.exports = function paragraph(state, startLine, endLine) {
); );
state.tokens.push({ type: 'paragraph_close' }); state.tokens.push({ type: 'paragraph_close' });
state.line = skipEmptyLines(state, nextLine); state.line = nextLine;
return true; return true;
}; };

5
lib/lexer_block/table.js

@ -3,9 +3,6 @@
'use strict'; 'use strict';
var skipEmptyLines = require('../helpers').skipEmptyLines;
function lineMatch(state, line, reg) { function lineMatch(state, line, reg) {
var pos = state.bMarks[line], var pos = state.bMarks[line],
max = state.eMarks[line]; max = state.eMarks[line];
@ -77,6 +74,6 @@ module.exports = function table(state, startLine, endLine, silent) {
} }
state.tokens.push({ type: 'table_close' }); state.tokens.push({ type: 'table_close' });
state.line = skipEmptyLines(state, nextLine); state.line = nextLine;
return true; return true;
}; };

10
lib/parser.js

@ -45,13 +45,9 @@ Parser.prototype.render = function (src) {
this.options this.options
); );
// TODO: skip leading empty lines while (state.line < state.lineMax) {
state.lexerBlock.tokenize(state, state.line, state.lineMax);
state.lexerBlock.tokenize(state, state.line, state.lineMax); }
// TODO: ??? eat empty paragraphs from tail
//console.log(state.tokens)
return this.renderer.render(state); return this.renderer.render(state);
}; };

Loading…
Cancel
Save