Browse Source

Tight lists + shink start of strings in inline blocks

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
4aa169634f
  1. 17
      lib/lexer_block.js
  2. 19
      lib/lexer_block/list.js
  3. 2
      lib/lexer_inline.js
  4. 27
      lib/renderer.js
  5. 3
      lib/state.js

17
lib/lexer_block.js

@ -115,16 +115,18 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) {
var ok, i,
rules = this.rules,
len = this.rules.length,
line = startLine;
line = startLine,
hasEmptyLines = false;
for (;;) {
line = state.line = skipEmptyLines(state, line, endLine);
while (line < endLine) {
line = skipEmptyLines(state, line, endLine);
state.line = line;
if (line >= endLine) { break; }
// Try all possible rules.
// On success, rule should:
//
// - update `state.pos`
// - update `state.line`
// - update `state.tokens`
// - return true
@ -135,18 +137,21 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) {
if (!ok) { throw new Error('No matching rules found'); }
if (line >= state.line) {
throw new Error("Parser didn't update state.line");
if (line === state.line) {
throw new Error('None of rules updated state.line');
}
line = state.line;
if (isEmpty(state, line)) {
hasEmptyLines = true;
line++;
// two empty lines should stop the parser
if (isEmpty(state, line + 1)) { break; }
}
}
state.tight = !hasEmptyLines;
};

19
lib/lexer_block/list.js

@ -92,6 +92,7 @@ module.exports = function list(state, startLine, endLine, silent) {
subState,
posNext,
contentStart,
listTokIdx,
rules_named = state.lexerBlock.rules_named;
// Detect list type and position after marker
@ -107,17 +108,23 @@ module.exports = function list(state, startLine, endLine, silent) {
if (silent) { return true; }
// Start list
listTokIdx = state.tokens.length;
if (isOrdered) {
start = state.bMarks[startLine] + state.tShift[startLine];
markerValue = Number(state.src.substr(start, posAfterMarker - start - 1));
state.tokens.push({
type: 'ordered_list_open',
order: markerValue
order: markerValue,
tight: true
});
} else {
state.tokens.push({ type: 'bullet_list_open' });
state.tokens.push({
type: 'bullet_list_open',
tight: true
});
}
//
@ -183,6 +190,7 @@ module.exports = function list(state, startLine, endLine, silent) {
// Check that list is not terminated with another block type
if (rules_named.fences(state, nextLine, endLine, true)) { break; }
if (rules_named.blockquote(state, nextLine, endLine, true)) { break; }
if (rules_named.hr(state, nextLine, endLine, true)) { break; }
//////////////////////////////////////////////////////////////////////////
@ -207,7 +215,6 @@ module.exports = function list(state, startLine, endLine, silent) {
if (rules_named.heading(state, nextLine, endLine, true)) { break; }
if (rules_named.lheading(state, nextLine, endLine, true)) { break; }
if (rules_named.blockquote(state, nextLine, endLine, true)) { break; }
if (rules_named.table(state, nextLine, endLine, true)) { break; }
//if (rules_named.tag(state, nextLine, endLine, true)) { break; }
//if (rules_named.def(state, nextLine, endLine, true)) { break; }
@ -226,6 +233,12 @@ module.exports = function list(state, startLine, endLine, silent) {
state.eMarks[lastNonEmptyLine])
.replace(RegExp('^ {1,' + indent + '}', 'mg'), ''));
state.lexerBlock.tokenize(subState, 0, subState.lineMax);
// If any of list item is loose, mark list as loose
if (!subState.tight) {
state.tokens[listTokIdx] = false;
}
state.tokens.push({ type: 'list_item_close' });
if (!hasNextItem) { break; }

2
lib/lexer_inline.js

@ -13,7 +13,7 @@ var rules = [];
rules.push(function text(state, begin, end) {
state.tokens.push({
type: 'text',
content: state.src.slice(begin, end)
content: state.src.slice(begin, end).replace(/^ {1,}/mg, '')
});
state.pos = end;

27
lib/renderer.js

@ -142,18 +142,37 @@ function Renderer() {
}
Renderer.prototype.render = function (state) {
var i, len, rule,
var i, len, rule, name,
tokens = state.tokens,
rules = this.rules;
rules = this.rules,
tightStack = [];
// wrap paragraphs on top level by default
state.tight = false;
for (i = 0, len = tokens.length; i < len; i++) {
rule = rules[tokens[i].type];
name = tokens[i].type;
rule = rules[name];
// TODO: temporary check
if (!rule) {
throw new Error('Renderer error: unknown token ' + tokens[i].type);
throw new Error('Renderer error: unknown token ' + name);
}
// Dirty stack machine to track lists style (loose/tight)
if (name === 'ordered_list_open' || name === 'bullet_list_open') {
tightStack.push(state.tight);
state.tight = tokens[i].tight;
}
if (name === 'ordered_list_close' || name === 'bullet_list_close') {
state.tight = tightStack.pop();
}
// in tight mode just ignore paragraphs for lists
// TODO - track right nesting to blockquotes
if ((name === 'paragraph_open' || name === 'paragraph_close') && state.tight) {
continue;
}
rule(state, tokens[i], i);
}

3
lib/state.js

@ -82,7 +82,8 @@ function State(src, lexerBlock, lexerInline, renderer, tokens, options) {
this.blkLevel = 0;
this.blkIndent = 0;
this.line = 0; // line index in src
this.lineMax = this.bMarks.length;
this.lineMax = this.bMarks.length;
this.tight = false; // loose/tight mode for lists
// renderer
this.result = '';

Loading…
Cancel
Save