Track list prefixes in the list start handler and use them to output

serialized text in list item handlers.

Change-Id: Ic7562d531d2313bedcf3b7450b4f28f02bc2b5a3
This commit is contained in:
Subramanya Sastry 2012-05-17 10:51:18 -05:00
parent 04fc74c76a
commit 58da03bc85

View file

@ -22,11 +22,14 @@ WSP.defaultOptions = {
var id = function( v ) { return function() { return v; }; }; var id = function( v ) { return function() { return v; }; };
WSP._listHandler = function( bullet, state, token ) { WSP._listHandler = function( bullet, state, token ) {
var res = bullet; var bullets, res;
if ( ! state.listStack.length ) { var stack = state.listStack;
res = '\n' + res; if (stack.length === 0) {
bullets = "\n" + bullet;
res = bullets;
} else { } else {
var curList = state.listStack[state.listStack.length - 1]; var curList = stack[stack.length - 1];
bullets = curList.bullets + bullet;
curList.items++; curList.items++;
if ( // deeply nested list if ( // deeply nested list
curList.items > 2 || curList.items > 2 ||
@ -34,14 +37,12 @@ WSP._listHandler = function( bullet, state, token ) {
( curList.items > 1 && ( curList.items > 1 &&
! ( state.lastToken.constructor === TagTk && ! ( state.lastToken.constructor === TagTk &&
state.lastToken.name === 'li') )) { state.lastToken.name === 'li') )) {
res = "\n" + res = bullets;
state.listStack } else {
.map( function ( i ) { res = bullet;
return i.bullet;
}).join('') + bullet;
} }
} }
state.listStack.push({ bullet: bullet, items: 0}); stack.push({ items: 0, bullets: bullets});
return res; return res;
}; };
@ -54,22 +55,15 @@ WSP._listEndHandler = function( state, token ) {
WSP._listItemHandler = function ( state, token ) { WSP._listItemHandler = function ( state, token ) {
//console.warn( JSON.stringify( state.listStack ) ); //console.warn( JSON.stringify( state.listStack ) );
var stack = state.listStack;
state.needParagraphLines = true; state.needParagraphLines = true;
if ( ! state.listStack.length ) { if (stack.length === 0) {
return ''; return '';
} else { } else {
var curList = state.listStack[state.listStack.length - 1]; var curList = stack[stack.length - 1];
curList.items++; curList.items++;
if ( curList.items > 1 ) { // > 1 ==> consecutive list items
// consecutive list items return ( curList.items > 1 ) ? curList.bullets : '';
return "\n" +
state.listStack
.map( function ( i ) {
return i.bullet;
}).join('');
} else {
return '';
}
} }
}; };