Browse Source

Add StateCore.Token and StateCore.push()

so all state objects would have roughly the same interface
pull/82/head
Alex Kocharin 10 years ago
parent
commit
f1aff8a5b9
  1. 1
      lib/rules_block/state_block.js
  2. 8
      lib/rules_core/block.js
  3. 11
      lib/rules_core/linkify.js
  4. 24
      lib/rules_core/state_core.js
  5. 1
      lib/rules_inline/state_inline.js

1
lib/rules_block/state_block.js

@ -168,6 +168,7 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
return queue.join('');
};
// re-export Token class to use in block rules
StateBlock.prototype.Token = Token;

8
lib/rules_core/block.js

@ -1,20 +1,14 @@
'use strict';
var Token = require('../token');
module.exports = function block(state) {
var token;
if (state.inlineMode) {
token = new Token('inline', '', 0);
token = state.push('inline', '', 0);
token.content = state.src;
token.map = [ 0, 1 ];
token.children = [];
token.level = 0;
state.tokens.push(token);
} else {
state.md.block.parse(state.src, state.md, state.env, state.tokens);
}

11
lib/rules_core/linkify.js

@ -7,7 +7,6 @@
var arrayReplaceAt = require('../common/utils').arrayReplaceAt;
var normalizeLink = require('../common/utils').normalizeLink;
var Token = require('../token');
function isLinkOpen(str) {
@ -77,30 +76,30 @@ module.exports = function linkify(state) {
pos = links[ln].index;
if (pos > lastPos) {
token = new Token('text', '', 0);
token = new state.Token('text', '', 0);
token.content = text.slice(lastPos, pos);
token.level = level;
nodes.push(token);
}
token = new Token('link_open', 'a', 1);
token = new state.Token('link_open', 'a', 1);
token.attrs = [ [ 'href', normalizeLink(links[ln].url) ] ];
token.level = level++;
nodes.push(token);
token = new Token('text', '', 0);
token = new state.Token('text', '', 0);
token.content = links[ln].text;
token.level = level;
nodes.push(token);
token = new Token('link_close', 'a', -1);
token = new state.Token('link_close', 'a', -1);
token.level = --level;
nodes.push(token);
lastPos = links[ln].lastIndex;
}
if (lastPos < text.length) {
token = new Token('text', '', 0);
token = new state.Token('text', '', 0);
token.content = text.slice(lastPos);
token.level = level;
nodes.push(token);

24
lib/rules_core/state_core.js

@ -2,10 +2,32 @@
//
'use strict';
module.exports = function StateCore(src, md, env) {
var Token = require('../token');
function StateCore(src, md, env) {
this.src = src;
this.env = env;
this.tokens = [];
this.inlineMode = false;
this.md = md; // link to parser instance
}
// Push new token to "stream".
//
StateCore.prototype.push = function (type, tag, nesting) {
var token = new Token(type, tag, nesting);
if (nesting < 0) { this.level--; }
token.level = this.level;
if (nesting > 0) { this.level++; }
this.tokens.push(token);
return token;
};
// re-export Token class to use in core rules
StateCore.prototype.Token = Token;
module.exports = StateCore;

1
lib/rules_inline/state_inline.js

@ -53,6 +53,7 @@ StateInline.prototype.push = function (type, tag, nesting) {
return token;
};
// re-export Token class to use in block rules
StateInline.prototype.Token = Token;

Loading…
Cancel
Save