Browse Source

Updated parsers API - don't rewrite token arrays in StateCore

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
c70700151a
  1. 2
      CHANGELOG.md
  2. 41
      lib/parser_block.js
  3. 8
      lib/parser_inline.js
  4. 2
      lib/rules_block/state_block.js
  5. 4
      lib/rules_core/abbr.js
  6. 3
      lib/rules_core/block.js
  7. 2
      lib/rules_core/inline.js
  8. 2
      lib/rules_core/references.js
  9. 6
      lib/rules_inline/state_inline.js

2
CHANGELOG.md

@ -5,7 +5,7 @@
- Added `renderInline()` and `parseInline()` methods. - Added `renderInline()` and `parseInline()` methods.
- Added abbreviations support. - Added abbreviations support.
- Fixed problem with tables, having single column. - Fixed problem with tables, having single column.
- Changed internal api, related to Ruler use. - Changed internal api (ruler, inline, block classes).
- Removed typographer chain (rules moved to `core`). - Removed typographer chain (rules moved to `core`).
- Removed all typographer options. Quote chars defs moved to `options.quotes`. - Removed all typographer options. Quote chars defs moved to `options.quotes`.

41
lib/parser_block.js

@ -5,7 +5,7 @@
var Ruler = require('./ruler'); var Ruler = require('./ruler');
var State = require('./rules_block/state_block'); var StateBlock = require('./rules_block/state_block');
var _rules = [ var _rules = [
@ -88,7 +88,7 @@ var TABS_SCAN_RE = /[\n\t]/g;
var NEWLINES_RE = /\r[\n\u0085]|[\u2424\u2028\u0085]/g; var NEWLINES_RE = /\r[\n\u0085]|[\u2424\u2028\u0085]/g;
var SPACES_RE = /\u00a0/g; var SPACES_RE = /\u00a0/g;
ParserBlock.prototype.parse = function (src, options, env) { ParserBlock.prototype.parse = function (src, options, env, outTokens) {
var state, lineStart = 0, lastTabPos = 0; var state, lineStart = 0, lastTabPos = 0;
if (!src) { return []; } if (!src) { return []; }
@ -115,28 +115,25 @@ ParserBlock.prototype.parse = function (src, options, env) {
} }
if (env.inlineMode) { if (env.inlineMode) {
return [ outTokens.push({
{ type: 'inline',
type: 'inline', content: src.replace(/\n/g, ' ').trim(),
content: src.replace(/\n/g, ' ').trim(), level: 0,
level: 0, lines: [ 0, 1 ],
lines: [ 0, 1 ], children: []
children: [] });
}
];
}
state = new State(
src,
this,
[],
options,
env
);
this.tokenize(state, state.line, state.lineMax); } else {
state = new StateBlock(
src,
this,
options,
env,
outTokens
);
return state.tokens; this.tokenize(state, state.line, state.lineMax);
}
}; };

8
lib/parser_inline.js

@ -111,17 +111,15 @@ ParserInline.prototype.tokenize = function (state) {
if (state.pending) { if (state.pending) {
state.pushPending(); state.pushPending();
} }
return state.tokens;
}; };
// Parse input string. // Parse input string.
// //
ParserInline.prototype.parse = function (str, options, env) { ParserInline.prototype.parse = function (str, options, env, outTokens) {
var state = new StateInline(str, this, options, env); var state = new StateInline(str, this, options, env, outTokens);
return this.tokenize(state); this.tokenize(state);
}; };

2
lib/rules_block/state_block.js

@ -3,7 +3,7 @@
'use strict'; 'use strict';
function StateBlock(src, parser, tokens, options, env) { function StateBlock(src, parser, options, env, tokens) {
var ch, s, start, pos, len, indent, indent_found; var ch, s, start, pos, len, indent, indent_found;
this.src = src; this.src = src;

4
lib/rules_core/abbr.js

@ -8,7 +8,7 @@ var StateInline = require('../rules_inline/state_inline');
var parseLinkLabel = require('../helpers/parse_link_label'); var parseLinkLabel = require('../helpers/parse_link_label');
function parseAbbr(str, parser, options, env) { function parseAbbr(str, parserInline, options, env) {
var state, labelEnd, pos, max, label, title; var state, labelEnd, pos, max, label, title;
if (str.charCodeAt(0) !== 0x2A/* * */) { return -1; } if (str.charCodeAt(0) !== 0x2A/* * */) { return -1; }
@ -16,7 +16,7 @@ function parseAbbr(str, parser, options, env) {
if (str.indexOf(']:') === -1) { return -1; } if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env); state = new StateInline(str, parserInline, options, env, []);
labelEnd = parseLinkLabel(state, 1); labelEnd = parseLinkLabel(state, 1);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; } if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }

3
lib/rules_core/block.js

@ -1,6 +1,5 @@
'use strict'; 'use strict';
module.exports = function block(state) { module.exports = function block(state) {
var tokens = state.block.parse(state.src, state.options, state.env); state.block.parse(state.src, state.options, state.env, state.tokens);
state.tokens = state.tokens.concat(tokens);
}; };

2
lib/rules_core/inline.js

@ -7,7 +7,7 @@ module.exports = function inline(state) {
for (i = 0, l = tokens.length; i < l; i++) { for (i = 0, l = tokens.length; i < l; i++) {
tok = tokens[i]; tok = tokens[i];
if (tok.type === 'inline') { if (tok.type === 'inline') {
tok.children = state.inline.parse(tok.content, state.options, state.env); state.inline.parse(tok.content, state.options, state.env, tok.children);
} }
} }
}; };

2
lib/rules_core/references.js

@ -15,7 +15,7 @@ function parseReference(str, parser, options, env) {
if (str.indexOf(']:') === -1) { return -1; } if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env); state = new StateInline(str, parser, options, env, []);
labelEnd = parseLinkLabel(state, 0); labelEnd = parseLinkLabel(state, 0);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; } if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }

6
lib/rules_inline/state_inline.js

@ -3,12 +3,12 @@
'use strict'; 'use strict';
function StateInline(src, parser, options, env) { function StateInline(src, parserInline, options, env, outTokens) {
this.src = src; this.src = src;
this.env = env; this.env = env;
this.options = options; this.options = options;
this.parser = parser; this.parser = parserInline;
this.tokens = []; this.tokens = outTokens;
this.pos = 0; this.pos = 0;
this.posMax = this.src.length; this.posMax = this.src.length;
this.level = 0; this.level = 0;

Loading…
Cancel
Save