Browse Source

Add state.level everywhere

pull/14/head
Alex Kocharin 10 years ago
parent
commit
46dc1f93ac
  1. 4
      lib/lexer_block/blockquote.js
  2. 3
      lib/lexer_block/code.js
  3. 3
      lib/lexer_block/fences.js
  4. 7
      lib/lexer_block/heading.js
  5. 2
      lib/lexer_block/hr.js
  6. 1
      lib/lexer_block/htmlblock.js
  7. 15
      lib/lexer_block/lheading.js
  8. 14
      lib/lexer_block/list.js
  9. 7
      lib/lexer_block/paragraph.js
  10. 2
      lib/lexer_block/state_block.js
  11. 23
      lib/lexer_block/table.js
  12. 20
      lib/lexer_inline/autolink.js
  13. 3
      lib/lexer_inline/backticks.js
  14. 21
      lib/lexer_inline/emphasis.js
  15. 3
      lib/lexer_inline/escape.js
  16. 3
      lib/lexer_inline/htmltag.js
  17. 17
      lib/lexer_inline/links.js
  18. 9
      lib/lexer_inline/newline.js
  19. 6
      lib/lexer_inline/state_inline.js
  20. 4
      lib/renderer.js

4
lib/lexer_block/blockquote.js

@ -105,9 +105,9 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
oldListMode = state.listMode; oldListMode = state.listMode;
state.listMode = false; state.listMode = false;
state.tokens.push({ type: 'blockquote_open' }); state.tokens.push({ type: 'blockquote_open', level: state.level++ });
state.lexer.tokenize(state, startLine, nextLine); state.lexer.tokenize(state, startLine, nextLine);
state.tokens.push({ type: 'blockquote_close' }); state.tokens.push({ type: 'blockquote_close', level: --state.level });
state.listMode = oldListMode; state.listMode = oldListMode;
// Restore original tShift; this might not be necessary since the parser // Restore original tShift; this might not be necessary since the parser

3
lib/lexer_block/code.js

@ -36,7 +36,8 @@ module.exports = function code(state, startLine, endLine, silent) {
state.tokens.push({ state.tokens.push({
type: 'code', type: 'code',
content: getLines(state, startLine, last, 4 + state.blkIndent, true), content: getLines(state, startLine, last, 4 + state.blkIndent, true),
block: true block: true,
level: state.level
}); });
state.line = nextLine; state.line = nextLine;

3
lib/lexer_block/fences.js

@ -86,7 +86,8 @@ module.exports = function fences(state, startLine, endLine, silent) {
state.tokens.push({ state.tokens.push({
type: 'fence', type: 'fence',
params: params, params: params,
content: getLines(state, startLine + 1, nextLine, len, true) content: getLines(state, startLine + 1, nextLine, len, true),
level: state.level
}); });
state.line = nextLine + (haveEndMarker ? 1 : 0); state.line = nextLine + (haveEndMarker ? 1 : 0);

7
lib/lexer_block/heading.js

@ -50,15 +50,16 @@ module.exports = function heading(state, startLine, endLine, silent) {
if (silent) { return true; } if (silent) { return true; }
state.tokens.push({ type: 'heading_open', level: level }); state.tokens.push({ type: 'heading_open', hLevel: level, level: state.level });
// only if header is not empty // only if header is not empty
if (pos < max) { if (pos < max) {
state.tokens.push({ state.tokens.push({
type: 'inline', type: 'inline',
content: state.src.slice(pos, max).trim() content: state.src.slice(pos, max).trim(),
level: state.level + 1
}); });
} }
state.tokens.push({ type: 'heading_close', level: level }); state.tokens.push({ type: 'heading_close', hLevel: level, level: state.level });
state.line = startLine + 1; state.line = startLine + 1;
return true; return true;

2
lib/lexer_block/hr.js

@ -37,7 +37,7 @@ module.exports = function hr(state, startLine, endLine, silent) {
if (silent) { return true; } if (silent) { return true; }
state.tokens.push({ type: 'hr' }); state.tokens.push({ type: 'hr', level: state.level });
state.line = startLine + 1; state.line = startLine + 1;
return true; return true;

1
lib/lexer_block/htmlblock.js

@ -67,6 +67,7 @@ module.exports = function htmlblock(state, startLine, endLine, silent) {
state.tokens.push({ state.tokens.push({
type: 'htmlblock', type: 'htmlblock',
level: state.level,
content: getLines(state, startLine, nextLine, 0, true) content: getLines(state, startLine, nextLine, 0, true)
}); });

15
lib/lexer_block/lheading.js

@ -37,12 +37,21 @@ module.exports = function lheading(state, startLine, endLine, silent) {
pos = state.bMarks[startLine] + state.tShift[startLine]; pos = state.bMarks[startLine] + state.tShift[startLine];
max = skipCharsBack(state, state.eMarks[startLine], 0x20/* space */, pos); max = skipCharsBack(state, state.eMarks[startLine], 0x20/* space */, pos);
state.tokens.push({ type: 'heading_open', level: marker === 0x3D/* = */ ? 1 : 2 }); state.tokens.push({
type: 'heading_open',
hLevel: marker === 0x3D/* = */ ? 1 : 2,
level: state.level
});
state.tokens.push({ state.tokens.push({
type: 'inline', type: 'inline',
content: state.src.slice(pos, max).trim() content: state.src.slice(pos, max).trim(),
level: state.level + 1
});
state.tokens.push({
type: 'heading_close',
hLevel: marker === 0x3D/* = */ ? 1 : 2,
level: state.level
}); });
state.tokens.push({ type: 'heading_close', level: marker === 0x3D/* = */ ? 1 : 2 });
state.line = next + 1; state.line = next + 1;
return true; return true;

14
lib/lexer_block/list.js

@ -116,13 +116,15 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tokens.push({ state.tokens.push({
type: 'ordered_list_open', type: 'ordered_list_open',
order: markerValue, order: markerValue,
tight: true tight: true,
level: state.level++
}); });
} else { } else {
state.tokens.push({ state.tokens.push({
type: 'bullet_list_open', type: 'bullet_list_open',
tight: true tight: true,
level: state.level++
}); });
} }
@ -157,7 +159,7 @@ module.exports = function list(state, startLine, endLine, silent) {
indent = (posAfterMarker - state.bMarks[nextLine]) + indentAfterMarker; indent = (posAfterMarker - state.bMarks[nextLine]) + indentAfterMarker;
// Run sublexer & write tokens // Run sublexer & write tokens
state.tokens.push({ type: 'list_item_open' }); state.tokens.push({ type: 'list_item_open', level: state.level++ });
//nextLine++; //nextLine++;
@ -185,7 +187,7 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tight = oldTight; state.tight = oldTight;
state.listMode = oldListMode; state.listMode = oldListMode;
state.tokens.push({ type: 'list_item_close' }); state.tokens.push({ type: 'list_item_close', level: --state.level });
nextLine = startLine = state.line; nextLine = startLine = state.line;
contentStart = state.bMarks[startLine]; contentStart = state.bMarks[startLine];
@ -226,9 +228,9 @@ module.exports = function list(state, startLine, endLine, silent) {
// Finilize list // Finilize list
if (isOrdered) { if (isOrdered) {
state.tokens.push({ type: 'ordered_list_close' }); state.tokens.push({ type: 'ordered_list_close', level: --state.level });
} else { } else {
state.tokens.push({ type: 'bullet_list_close' }); state.tokens.push({ type: 'bullet_list_close', level: --state.level });
} }
state.line = nextLine; state.line = nextLine;

7
lib/lexer_block/paragraph.js

@ -43,12 +43,13 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
} }
if (content) { if (content) {
state.tokens.push({ type: 'paragraph_open' }); state.tokens.push({ type: 'paragraph_open', level: state.level });
state.tokens.push({ state.tokens.push({
type: 'inline', type: 'inline',
content: content content: content,
level: state.level + 1
}); });
state.tokens.push({ type: 'paragraph_close' }); state.tokens.push({ type: 'paragraph_close', level: state.level });
} }
state.line = nextLine; state.line = nextLine;

2
lib/lexer_block/state_block.js

@ -94,6 +94,8 @@ function State(src, lexer, tokens, options, env) {
this.bqMarks.push(0); this.bqMarks.push(0);
} }
this.level = 0;
// renderer // renderer
this.result = ''; this.result = '';
} }

23
lib/lexer_block/table.js

@ -47,36 +47,37 @@ module.exports = function table(state, startLine, endLine, silent) {
if (aligns.length !== rows.length) { return false; } if (aligns.length !== rows.length) { return false; }
if (silent) { return true; } if (silent) { return true; }
state.tokens.push({ type: 'table_open' }); state.tokens.push({ type: 'table_open', level: state.level++ });
state.tokens.push({ type: 'tr_open' }); state.tokens.push({ type: 'tr_open', level: state.level++ });
for (i = 0; i < rows.length; i++) { for (i = 0; i < rows.length; i++) {
state.tokens.push({ type: 'th_open', align: aligns[i] }); state.tokens.push({ type: 'th_open', align: aligns[i], level: state.level++ });
state.tokens.push({ state.tokens.push({
type: 'inline', type: 'inline',
content: rows[i].trim() content: rows[i].trim(),
level: state.level
}); });
state.tokens.push({ type: 'th_close' }); state.tokens.push({ type: 'th_close', level: --state.level });
} }
state.tokens.push({ type: 'tr_close' }); state.tokens.push({ type: 'tr_close', level: --state.level });
for (nextLine = startLine + 2; nextLine < endLine; nextLine++) { for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/); m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/);
if (!m) { break; } if (!m) { break; }
rows = m[1].split('|'); rows = m[1].split('|');
state.tokens.push({ type: 'tr_open' }); state.tokens.push({ type: 'tr_open', level: state.level++ });
for (i = 0; i < rows.length; i++) { for (i = 0; i < rows.length; i++) {
state.tokens.push({ type: 'td_open', align: aligns[i] }); state.tokens.push({ type: 'td_open', align: aligns[i], level: state.level++ });
state.tokens.push({ state.tokens.push({
type: 'inline', type: 'inline',
content: rows[i].replace(/^\|? *| *\|?$/g, '') content: rows[i].replace(/^\|? *| *\|?$/g, '')
}); });
state.tokens.push({ type: 'td_close' }); state.tokens.push({ type: 'td_close', level: --state.level });
} }
state.tokens.push({ type: 'tr_close' }); state.tokens.push({ type: 'tr_close', level: --state.level });
} }
state.tokens.push({ type: 'table_close' }); state.tokens.push({ type: 'table_close', level: --state.level });
state.line = nextLine; state.line = nextLine;
return true; return true;

20
lib/lexer_inline/autolink.js

@ -26,13 +26,15 @@ module.exports = function autolink(state) {
state.push({ state.push({
type: 'link_open', type: 'link_open',
href: linkMatch[0].slice(1, -1) href: linkMatch[0].slice(1, -1),
level: state.level
}); });
state.push({ state.push({
type: 'text', type: 'text',
content: escapeHtml(linkMatch[0].slice(1, -1)) content: escapeHtml(linkMatch[0].slice(1, -1)),
level: state.level + 1
}); });
state.push({ type: 'link_close' }); state.push({ type: 'link_close', level: state.level });
state.pos += linkMatch[0].length; state.pos += linkMatch[0].length;
return true; return true;
@ -41,15 +43,17 @@ module.exports = function autolink(state) {
emailMatch = tail.match(EMAIL_RE); emailMatch = tail.match(EMAIL_RE);
if (emailMatch) { if (emailMatch) {
state.tokens.push({ state.push({
type: 'link_open', type: 'link_open',
href: 'mailto:' + emailMatch[0].slice(1, -1) href: 'mailto:' + emailMatch[0].slice(1, -1),
level: state.level
}); });
state.tokens.push({ state.push({
type: 'text', type: 'text',
content: escapeHtml(emailMatch[0].slice(1, -1)) content: escapeHtml(emailMatch[0].slice(1, -1)),
level: state.level + 1
}); });
state.tokens.push({ type: 'link_close' }); state.push({ type: 'link_close', level: state.level });
state.pos += emailMatch[0].length; state.pos += emailMatch[0].length;
return true; return true;

3
lib/lexer_inline/backticks.js

@ -28,7 +28,8 @@ module.exports = function backticks(state) {
content: code content: code
.replace(/[ \n]+/g,' ') .replace(/[ \n]+/g,' ')
.trim(), .trim(),
block: false block: false,
level: state.level
}); });
state.pos += marker.length * 2 + code.length; state.pos += marker.length * 2 + code.length;

21
lib/lexer_inline/emphasis.js

@ -209,12 +209,23 @@ module.exports = function emphasis(state/*, silent*/) {
// found! // found!
state.posMax = state.pos; state.posMax = state.pos;
state.pos = start + startCount; state.pos = start + startCount;
if (state.pending) { state.pushPending(); }
if (startCount === 2 || startCount === 3) { state.push({ type: 'strong_open' }); } if (startCount === 2 || startCount === 3) {
if (startCount === 1 || startCount === 3) { state.push({ type: 'em_open' }); } state.push({ type: 'strong_open', level: state.level++ });
}
if (startCount === 1 || startCount === 3) {
state.push({ type: 'em_open', level: state.level++ });
}
state.lexer.tokenize(state); state.lexer.tokenize(state);
if (startCount === 1 || startCount === 3) { state.push({ type: 'em_close' }); }
if (startCount === 2 || startCount === 3) { state.push({ type: 'strong_close' }); } if (startCount === 1 || startCount === 3) {
state.push({ type: 'em_close', level: --state.level });
}
if (startCount === 2 || startCount === 3) {
state.push({ type: 'strong_close', level: --state.level });
}
state.pos = state.posMax + startCount; state.pos = state.posMax + startCount;
state.posMax = max; state.posMax = max;
return true; return true;

3
lib/lexer_inline/escape.js

@ -33,7 +33,8 @@ module.exports = function escape(state) {
if (ch === 0x0A) { if (ch === 0x0A) {
state.push({ state.push({
type: 'hardbreak' type: 'hardbreak',
level: state.level
}); });
pos++; pos++;

3
lib/lexer_inline/htmltag.js

@ -39,7 +39,8 @@ module.exports = function htmltag(state) {
state.push({ state.push({
type: 'htmltag', type: 'htmltag',
content: state.src.slice(pos, pos + match[0].length) content: state.src.slice(pos, pos + match[0].length),
level: state.level
}); });
//console.log(state.tokens) //console.log(state.tokens)
state.pos += match[0].length; state.pos += match[0].length;

17
lib/lexer_inline/links.js

@ -280,17 +280,24 @@ function links(state) {
// //
state.pos = labelStart; state.pos = labelStart;
state.posMax = labelEnd; state.posMax = labelEnd;
if (state.pending) { state.pushPending(); }
if (isImage) { if (isImage) {
state.push({ type: 'image', state.push({
type: 'image',
src: href, src: href,
title: title, title: title,
alt: state.src.substr(labelStart, labelEnd - labelStart) }); alt: state.src.substr(labelStart, labelEnd - labelStart),
level: state.level
});
} else { } else {
state.push({ type: 'link_open', href: href, title: title }); state.push({
type: 'link_open',
href: href,
title: title,
level: state.level++
});
state.lexer.tokenize(state); state.lexer.tokenize(state);
state.push({ type: 'link_close' }); state.push({ type: 'link_close', level: --state.level });
} }
state.pos = pos; state.pos = pos;

9
lib/lexer_inline/newline.js

@ -16,18 +16,21 @@ module.exports = function escape(state) {
if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) { if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
state.pending = state.pending.replace(/ +$/, ''); state.pending = state.pending.replace(/ +$/, '');
state.push({ state.push({
type: 'hardbreak' type: 'hardbreak',
level: state.level
}); });
} else { } else {
state.pending = state.pending.slice(0, -1); state.pending = state.pending.slice(0, -1);
state.push({ state.push({
type: 'softbreak' type: 'softbreak',
level: state.level
}); });
} }
} else { } else {
state.push({ state.push({
type: 'softbreak' type: 'softbreak',
level: state.level
}); });
} }

6
lib/lexer_inline/state_inline.js

@ -14,6 +14,8 @@ function StateInline(src, lexer, options, env) {
this.posMax = this.src.length; this.posMax = this.src.length;
this.validateInsideEm = false; this.validateInsideEm = false;
this.validateInsideLink = false; this.validateInsideLink = false;
this.level = 0;
this.pendingLevel = 0;
} }
@ -22,7 +24,8 @@ StateInline.prototype.pushPending = function () {
this.tokens.push({ this.tokens.push({
type: 'text', type: 'text',
content: pending content: pending,
level: this.pendingLevel
}); });
this.pending = ''; this.pending = '';
}; };
@ -33,6 +36,7 @@ StateInline.prototype.push = function (token) {
} }
this.tokens.push(token); this.tokens.push(token);
this.pendingLevel = this.level;
}; };
module.exports = StateInline; module.exports = StateInline;

4
lib/renderer.js

@ -69,10 +69,10 @@ rules.fence = function (tokens, idx, options) {
rules.heading_open = function (tokens, idx /*, options*/) { rules.heading_open = function (tokens, idx /*, options*/) {
return '<h' + tokens[idx].level + '>'; return '<h' + tokens[idx].hLevel + '>';
}; };
rules.heading_close = function (tokens, idx /*, options*/) { rules.heading_close = function (tokens, idx /*, options*/) {
return '</h' + tokens[idx].level + '>\n'; return '</h' + tokens[idx].hLevel + '>\n';
}; };

Loading…
Cancel
Save