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;
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.tokens.push({ type: 'blockquote_close' });
state.tokens.push({ type: 'blockquote_close', level: --state.level });
state.listMode = oldListMode;
// 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({
type: 'code',
content: getLines(state, startLine, last, 4 + state.blkIndent, true),
block: true
block: true,
level: state.level
});
state.line = nextLine;

3
lib/lexer_block/fences.js

@ -86,7 +86,8 @@ module.exports = function fences(state, startLine, endLine, silent) {
state.tokens.push({
type: 'fence',
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);

7
lib/lexer_block/heading.js

@ -50,15 +50,16 @@ module.exports = function heading(state, startLine, endLine, silent) {
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
if (pos < max) {
state.tokens.push({
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;
return true;

2
lib/lexer_block/hr.js

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

1
lib/lexer_block/htmlblock.js

@ -67,6 +67,7 @@ module.exports = function htmlblock(state, startLine, endLine, silent) {
state.tokens.push({
type: 'htmlblock',
level: state.level,
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];
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({
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;
return true;

14
lib/lexer_block/list.js

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

7
lib/lexer_block/paragraph.js

@ -43,12 +43,13 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
}
if (content) {
state.tokens.push({ type: 'paragraph_open' });
state.tokens.push({ type: 'paragraph_open', level: state.level });
state.tokens.push({
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;

2
lib/lexer_block/state_block.js

@ -94,6 +94,8 @@ function State(src, lexer, tokens, options, env) {
this.bqMarks.push(0);
}
this.level = 0;
// renderer
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 (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++) {
state.tokens.push({ type: 'th_open', align: aligns[i] });
state.tokens.push({ type: 'th_open', align: aligns[i], level: state.level++ });
state.tokens.push({
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++) {
m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/);
if (!m) { break; }
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++) {
state.tokens.push({ type: 'td_open', align: aligns[i] });
state.tokens.push({ type: 'td_open', align: aligns[i], level: state.level++ });
state.tokens.push({
type: 'inline',
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;
return true;

20
lib/lexer_inline/autolink.js

@ -26,13 +26,15 @@ module.exports = function autolink(state) {
state.push({
type: 'link_open',
href: linkMatch[0].slice(1, -1)
href: linkMatch[0].slice(1, -1),
level: state.level
});
state.push({
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;
return true;
@ -41,15 +43,17 @@ module.exports = function autolink(state) {
emailMatch = tail.match(EMAIL_RE);
if (emailMatch) {
state.tokens.push({
state.push({
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',
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;
return true;

3
lib/lexer_inline/backticks.js

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

21
lib/lexer_inline/emphasis.js

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

3
lib/lexer_inline/escape.js

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

3
lib/lexer_inline/htmltag.js

@ -39,7 +39,8 @@ module.exports = function htmltag(state) {
state.push({
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)
state.pos += match[0].length;

17
lib/lexer_inline/links.js

@ -280,17 +280,24 @@ function links(state) {
//
state.pos = labelStart;
state.posMax = labelEnd;
if (state.pending) { state.pushPending(); }
if (isImage) {
state.push({ type: 'image',
state.push({
type: 'image',
src: href,
title: title,
alt: state.src.substr(labelStart, labelEnd - labelStart) });
alt: state.src.substr(labelStart, labelEnd - labelStart),
level: state.level
});
} 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.push({ type: 'link_close' });
state.push({ type: 'link_close', level: --state.level });
}
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) {
state.pending = state.pending.replace(/ +$/, '');
state.push({
type: 'hardbreak'
type: 'hardbreak',
level: state.level
});
} else {
state.pending = state.pending.slice(0, -1);
state.push({
type: 'softbreak'
type: 'softbreak',
level: state.level
});
}
} else {
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.validateInsideEm = false;
this.validateInsideLink = false;
this.level = 0;
this.pendingLevel = 0;
}
@ -22,7 +24,8 @@ StateInline.prototype.pushPending = function () {
this.tokens.push({
type: 'text',
content: pending
content: pending,
level: this.pendingLevel
});
this.pending = '';
};
@ -33,6 +36,7 @@ StateInline.prototype.push = function (token) {
}
this.tokens.push(token);
this.pendingLevel = this.level;
};
module.exports = StateInline;

4
lib/renderer.js

@ -69,10 +69,10 @@ rules.fence = 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*/) {
return '</h' + tokens[idx].level + '>\n';
return '</h' + tokens[idx].hLevel + '>\n';
};

Loading…
Cancel
Save