Browse Source

Add "lines" attribute to all block-level tokens

(except for the closing ones)
pull/14/head
Alex Kocharin 10 years ago
parent
commit
065eba8bda
  1. 14
      lib/rules_block/blockquote.js
  2. 3
      lib/rules_block/code.js
  3. 3
      lib/rules_block/fences.js
  4. 11
      lib/rules_block/heading.js
  5. 8
      lib/rules_block/hr.js
  6. 3
      lib/rules_block/htmlblock.js
  7. 4
      lib/rules_block/lheading.js
  8. 12
      lib/rules_block/list.js
  9. 14
      lib/rules_block/paragraph.js
  10. 36
      lib/rules_block/table.js

14
lib/rules_block/blockquote.js

@ -4,7 +4,7 @@
module.exports = function blockquote(state, startLine, endLine, silent) {
var nextLine, lastLineEmpty, oldTShift, oldBMarks, oldIndent, oldParentType,
var nextLine, lastLineEmpty, oldTShift, oldBMarks, oldIndent, oldParentType, lines,
terminatorRules = state.parser._rulesBlockquoteTerm, i, l, terminate,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
@ -105,10 +105,18 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
oldParentType = state.parentType;
state.parentType = 'blockquote';
state.tokens.push({ type: 'blockquote_open', level: state.level++ });
state.tokens.push({
type: 'blockquote_open',
lines: lines = [ startLine, 0 ],
level: state.level++
});
state.parser.tokenize(state, startLine, nextLine);
state.tokens.push({ type: 'blockquote_close', level: --state.level });
state.tokens.push({
type: 'blockquote_close',
level: --state.level
});
state.parentType = oldParentType;
lines[1] = state.lines;
// Restore original tShift; this might not be necessary since the parser
// has already been here, but just to make sure we can do that.

3
lib/rules_block/code.js

@ -25,13 +25,14 @@ module.exports = function code(state, startLine, endLine, silent) {
if (silent) { return true; }
state.line = nextLine;
state.tokens.push({
type: 'code',
content: state.getLines(startLine, last, 4 + state.blkIndent, true),
block: true,
lines: [ startLine, state.line ],
level: state.level
});
state.line = nextLine;
return true;
};

3
lib/rules_block/fences.js

@ -73,13 +73,14 @@ module.exports = function fences(state, startLine, endLine, silent) {
// If a fence has heading spaces, they should be removed from its inner block
len = state.tShift[startLine];
state.line = nextLine + (haveEndMarker ? 1 : 0);
state.tokens.push({
type: 'fence',
params: params,
content: state.getLines(startLine + 1, nextLine, len, true),
lines: [ startLine, state.line ],
level: state.level
});
state.line = nextLine + (haveEndMarker ? 1 : 0);
return true;
};

11
lib/rules_block/heading.js

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

8
lib/rules_block/hr.js

@ -34,8 +34,12 @@ module.exports = function hr(state, startLine, endLine, silent) {
if (silent) { return true; }
state.tokens.push({ type: 'hr', level: state.level });
state.line = startLine + 1;
state.tokens.push({
type: 'hr',
lines: [ startLine, state.line ],
level: state.level
});
return true;
};

3
lib/rules_block/htmlblock.js

@ -62,12 +62,13 @@ module.exports = function htmlblock(state, startLine, endLine, silent) {
nextLine++;
}
state.line = nextLine;
state.tokens.push({
type: 'htmlblock',
level: state.level,
lines: [ startLine, state.line ],
content: state.getLines(startLine, nextLine, 0, true)
});
state.line = nextLine;
return true;
};

4
lib/rules_block/lheading.js

@ -33,15 +33,18 @@ module.exports = function lheading(state, startLine, endLine, silent) {
pos = state.bMarks[startLine] + state.tShift[startLine];
state.line = next + 1;
state.tokens.push({
type: 'heading_open',
hLevel: marker === 0x3D/* = */ ? 1 : 2,
lines: [ startLine, state.line ],
level: state.level
});
state.tokens.push({
type: 'inline',
content: state.src.slice(pos, state.eMarks[startLine]).trim(),
level: state.level + 1,
lines: [ startLine, state.line - 1 ],
children: []
});
state.tokens.push({
@ -50,6 +53,5 @@ module.exports = function lheading(state, startLine, endLine, silent) {
level: state.level
});
state.line = next + 1;
return true;
};

12
lib/rules_block/list.js

@ -86,6 +86,8 @@ module.exports = function list(state, startLine, endLine, silent) {
contentStart,
listTokIdx,
prevEmptyEnd,
listLines,
itemLines,
terminatorRules = state.parser._rulesListTerm, i, l, terminate;
// Detect list type and position after marker
@ -116,6 +118,7 @@ module.exports = function list(state, startLine, endLine, silent) {
type: 'ordered_list_open',
order: markerValue,
tight: true,
lines: listLines = [ startLine, 0 ],
level: state.level++
});
@ -123,6 +126,7 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tokens.push({
type: 'bullet_list_open',
tight: true,
lines: listLines = [ startLine, 0 ],
level: state.level++
});
}
@ -158,7 +162,11 @@ module.exports = function list(state, startLine, endLine, silent) {
indent = (posAfterMarker - state.bMarks[nextLine]) + indentAfterMarker;
// Run subparser & write tokens
state.tokens.push({ type: 'list_item_open', level: state.level++ });
state.tokens.push({
type: 'list_item_open',
lines: itemLines = [ startLine, 0 ],
level: state.level++
});
//nextLine++;
@ -189,6 +197,7 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tokens.push({ type: 'list_item_close', level: --state.level });
nextLine = startLine = state.line;
itemLines[1] = nextLine;
contentStart = state.bMarks[startLine];
if (nextLine >= endLine) { break; }
@ -230,6 +239,7 @@ module.exports = function list(state, startLine, endLine, silent) {
} else {
state.tokens.push({ type: 'bullet_list_close', level: --state.level });
}
listLines[1] = nextLine;
state.line = nextLine;
return true;

14
lib/rules_block/paragraph.js

@ -38,17 +38,25 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
content = content.slice(pos).trim();
}
state.line = nextLine;
if (content.length) {
state.tokens.push({ type: 'paragraph_open', level: state.level });
state.tokens.push({
type: 'paragraph_open',
lines: [ startLine, state.line ],
level: state.level
});
state.tokens.push({
type: 'inline',
content: content,
level: state.level + 1,
lines: [ startLine, state.line ],
children: []
});
state.tokens.push({ type: 'paragraph_close', level: state.level });
state.tokens.push({
type: 'paragraph_close',
level: state.level
});
}
state.line = nextLine;
return true;
};

36
lib/rules_block/table.js

@ -13,7 +13,7 @@ function lineMatch(state, line, reg) {
module.exports = function table(state, startLine, endLine, silent) {
var ch, firstLineMatch, secondLineMatch, pos, i, nextLine, m, rows,
aligns, t;
aligns, t, tableLines, tbodyLines;
// should have at least three lines
if (startLine + 2 > endLine) { return false; }
@ -54,15 +54,33 @@ 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', level: state.level++ });
state.tokens.push({ type: 'thead_open', level: state.level++ });
state.tokens.push({
type: 'table_open',
lines: tableLines = [ startLine, 0 ],
level: state.level++
});
state.tokens.push({
type: 'thead_open',
lines: [ startLine, startLine + 1 ],
level: state.level++
});
state.tokens.push({ type: 'tr_open', level: state.level++ });
state.tokens.push({
type: 'tr_open',
lines: [ startLine, startLine + 1 ],
level: state.level++
});
for (i = 0; i < rows.length; i++) {
state.tokens.push({ type: 'th_open', align: aligns[i], level: state.level++ });
state.tokens.push({
type: 'th_open',
align: aligns[i],
lines: [ startLine, startLine + 1 ],
level: state.level++
});
state.tokens.push({
type: 'inline',
content: rows[i].trim(),
lines: [ startLine, startLine + 1 ],
level: state.level,
children: []
});
@ -70,7 +88,12 @@ module.exports = function table(state, startLine, endLine, silent) {
}
state.tokens.push({ type: 'tr_close', level: --state.level });
state.tokens.push({ type: 'thead_close', level: --state.level });
state.tokens.push({ type: 'tbody_open', level: state.level++ });
state.tokens.push({
type: 'tbody_open',
lines: tbodyLines = [ startLine + 2, 0 ],
level: state.level++
});
for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
if (state.tShift[nextLine] < state.blkIndent) { break; }
@ -95,6 +118,7 @@ module.exports = function table(state, startLine, endLine, silent) {
state.tokens.push({ type: 'tbody_close', level: --state.level });
state.tokens.push({ type: 'table_close', level: --state.level });
tableLines[1] = tbodyLines[1] = nextLine;
state.line = nextLine;
return true;
};

Loading…
Cancel
Save