Skip to content

Commit

Permalink
Simplified loose/tight logic. Closes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Oct 22, 2014
1 parent 7146f91 commit d5c588b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
49 changes: 6 additions & 43 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ rules.ordered_list_close = function (tokens, idx /*, options*/) {
};


rules.paragraph_open = function (/*tokens, idx, options*/) {
return '<p>';
rules.paragraph_open = function (tokens, idx/*, options*/) {
return tokens[idx].tight ? '' : '<p>';
};
rules.paragraph_close = function (tokens, idx /*, options*/) {
return '</p>' + getBreak(tokens, idx);
return (tokens[idx].tight ? '' : '</p>') + getBreak(tokens, idx);
};


Expand Down Expand Up @@ -240,52 +240,15 @@ Renderer.prototype.renderInline = function (tokens, options) {


Renderer.prototype.render = function (tokens, options) {
var i, len, name,
var i, len,
result = '',
rules = this.rules,
tightStack = [];

// wrap paragraphs on top level by default
var tight = false;
rules = this.rules;

for (i = 0, len = tokens.length; i < len; i++) {
name = tokens[i].type;

// Dirty stack machine to track lists style (loose/tight)
if (name === 'ordered_list_open' || name === 'bullet_list_open') {
tightStack.push(tight);
tight = tokens[i].tight;
}
if (name === 'ordered_list_close' || name === 'bullet_list_close') {
tight = tightStack.pop();
}
if (name === 'blockquote_open') {
tightStack.push(tight);
tight = false;
}
if (name === 'blockquote_close') {
tight = tightStack.pop();
}


// in tight mode just ignore paragraphs for lists
// TODO - track right nesting to blockquotes
if (name === 'paragraph_open' && tight) {
continue;
}
if (name === 'paragraph_close' && tight) {
// Quick hack - texts should have LF if followed by blocks
if (tokens[i + 1].type !== 'list_item_close') {
result += '\n';
}

continue;
}

if (tokens[i].type === 'inline') {
result += this.renderInline(tokens[i].children, options);
} else {
result += rules[name](tokens, i, options);
result += rules[tokens[i].type](tokens, i, options);
}
}

Expand Down
37 changes: 27 additions & 10 deletions lib/rules_block/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ module.exports = function list(state, startLine, endLine, silent) {
prevEmptyEnd,
listLines,
itemLines,
terminatorRules = state.parser._rulesListTerm, i, l, terminate;
tight = true,
terminatorRules = state.parser._rulesListTerm,
i, l, terminate, level, tokens;

// Detect list type and position after marker
if ((posAfterMarker = skipOrderedListMarker(state, startLine)) >= 0) {
Expand Down Expand Up @@ -117,15 +119,13 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tokens.push({
type: 'ordered_list_open',
order: markerValue,
tight: true,
lines: listLines = [ startLine, 0 ],
level: state.level++
});

} else {
state.tokens.push({
type: 'bullet_list_open',
tight: true,
lines: listLines = [ startLine, 0 ],
level: state.level++
});
Expand Down Expand Up @@ -183,7 +183,7 @@ module.exports = function list(state, startLine, endLine, silent) {

// If any of list item is tight, mark list as tight
if (!state.tight || prevEmptyEnd) {
state.tokens[listTokIdx].tight = false;
tight = false;
}
// Item become loose if finish with empty line,
// but we should filter last element, because it means list finish
Expand All @@ -194,7 +194,10 @@ module.exports = function list(state, startLine, endLine, silent) {
state.tight = oldTight;
state.parentType = oldParentType;

state.tokens.push({ type: 'list_item_close', level: --state.level });
state.tokens.push({
type: 'list_item_close',
level: --state.level
});

nextLine = startLine = state.line;
itemLines[1] = nextLine;
Expand Down Expand Up @@ -234,13 +237,27 @@ module.exports = function list(state, startLine, endLine, silent) {
}

// Finilize list
if (isOrdered) {
state.tokens.push({ type: 'ordered_list_close', level: --state.level });
} else {
state.tokens.push({ type: 'bullet_list_close', level: --state.level });
}
state.tokens.push({
type: isOrdered ? 'ordered_list_close' : 'bullet_list_close',
level: --state.level
});
listLines[1] = nextLine;

state.line = nextLine;

// mark paragraphs tight if needed
if (tight) {
level = state.level + 2;
tokens = state.tokens;

for (i = listTokIdx + 2, l = tokens.length - 2; i < l; i++) {
if (tokens[i].level === level && tokens[i].type === 'paragraph_open') {
tokens[i].tight = true;
i += 2;
tokens[i].tight = true;
}
}
}

return true;
};
2 changes: 2 additions & 0 deletions lib/rules_block/paragraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
if (content.length) {
state.tokens.push({
type: 'paragraph_open',
tight: false,
lines: [ startLine, state.line ],
level: state.level
});
Expand All @@ -54,6 +55,7 @@ module.exports = function paragraph(state, startLine/*, endLine*/) {
});
state.tokens.push({
type: 'paragraph_close',
tight: false,
level: state.level
});
}
Expand Down

0 comments on commit d5c588b

Please sign in to comment.