2012-01-17 18:22:10 +00:00
|
|
|
/*
|
|
|
|
* Insert paragraphs for comment-only lines after template expansion
|
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Include general utilities
|
2012-07-18 20:23:12 +00:00
|
|
|
var u = require('./ext.Util.js').Util;
|
2012-01-17 18:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
function PostExpandParagraphHandler ( dispatcher ) {
|
|
|
|
this.tokens = [];
|
|
|
|
this.newLines = 0;
|
|
|
|
this.register( dispatcher );
|
|
|
|
}
|
|
|
|
|
|
|
|
// constants
|
2012-05-03 11:05:28 +00:00
|
|
|
PostExpandParagraphHandler.prototype.newlineRank = 2.5;
|
|
|
|
PostExpandParagraphHandler.prototype.anyRank = 2.501; // Just after regular quote and newline
|
2012-01-17 18:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Register this transformer with the TokenTransformer
|
|
|
|
PostExpandParagraphHandler.prototype.register = function ( dispatcher ) {
|
|
|
|
this.dispatcher = dispatcher;
|
|
|
|
// Register for NEWLINE tokens
|
2012-07-18 00:18:50 +00:00
|
|
|
dispatcher.addTransform( this.onNewLine.bind(this), "PostExpandParagraphHandler:onNewLine",
|
2012-01-17 18:22:10 +00:00
|
|
|
this.newlineRank, 'newline' );
|
|
|
|
// Reset internal state when we are done
|
2012-07-18 00:18:50 +00:00
|
|
|
dispatcher.addTransform( this.onEnd.bind(this), "PostExpandParagraphHandler:onEnd",
|
2012-01-17 18:22:10 +00:00
|
|
|
this.newlineRank, 'end' );
|
|
|
|
};
|
|
|
|
|
2012-02-07 11:53:29 +00:00
|
|
|
PostExpandParagraphHandler.prototype.reset = function ( token, frame, cb ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'PostExpandParagraphHandler.reset ' + JSON.stringify( this.tokens ) );
|
2012-01-17 18:22:10 +00:00
|
|
|
if ( this.newLines ) {
|
2012-06-05 21:44:23 +00:00
|
|
|
this.tokens.push( token );
|
|
|
|
return this._finish();
|
2012-01-17 18:22:10 +00:00
|
|
|
} else {
|
2012-06-05 21:44:23 +00:00
|
|
|
return [token];
|
2012-01-17 18:22:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PostExpandParagraphHandler.prototype._finish = function ( ) {
|
|
|
|
var tokens = this.tokens;
|
|
|
|
this.tokens = [];
|
|
|
|
// remove 'any' registration
|
|
|
|
this.dispatcher.removeTransform( this.anyRank, 'any' );
|
|
|
|
this.newLines = 0;
|
|
|
|
return tokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-04 09:21:23 +00:00
|
|
|
// Handle NEWLINE tokens
|
2012-02-07 11:53:29 +00:00
|
|
|
PostExpandParagraphHandler.prototype.onNewLine = function ( token, frame, cb ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'PostExpandParagraphHandler.onNewLine: ' + JSON.stringify( token, null , 2 ) );
|
2012-01-17 18:22:10 +00:00
|
|
|
var res;
|
|
|
|
this.tokens.push( token );
|
|
|
|
|
|
|
|
if( ! this.newLines ) {
|
2012-07-18 00:18:50 +00:00
|
|
|
this.dispatcher.addTransform( this.onAny.bind(this), "PostExpandParagraphHandler:onAny",
|
2012-01-17 18:22:10 +00:00
|
|
|
this.anyRank, 'any' );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.newLines++;
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2012-05-29 06:04:19 +00:00
|
|
|
PostExpandParagraphHandler.prototype.onEnd = function ( token, frame, cb ) {
|
2012-06-05 21:44:23 +00:00
|
|
|
return { tokens: this.reset( token ) };
|
2012-05-29 06:04:19 +00:00
|
|
|
}
|
2012-01-17 18:22:10 +00:00
|
|
|
|
2012-02-07 11:53:29 +00:00
|
|
|
PostExpandParagraphHandler.prototype.onAny = function ( token, frame, cb ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'PostExpandParagraphHandler.onAny' );
|
2012-03-07 20:06:54 +00:00
|
|
|
if ( token.constructor === CommentTk ||
|
2012-04-16 12:47:56 +00:00
|
|
|
( token.constructor === String && token.match( /^[\t ]*$/ ) )
|
2012-01-17 18:22:10 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// Continue with collection..
|
2012-06-05 21:44:23 +00:00
|
|
|
this.tokens.push( token );
|
2012-01-17 18:22:10 +00:00
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
// XXX: Only open paragraph if inline token follows!
|
|
|
|
|
|
|
|
// None of the tokens we are interested in, so abort processing..
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'PostExpandParagraphHandler.onAny: ' + JSON.stringify( this.tokens, null , 2 ) );
|
2012-01-17 18:30:22 +00:00
|
|
|
if ( this.newLines >= 2 && ! u.isBlockToken( token ) ) {
|
2012-06-05 21:44:23 +00:00
|
|
|
this.tokens.push( token );
|
2012-06-02 14:39:33 +00:00
|
|
|
var nlTks = [];
|
|
|
|
while ( this.tokens[0].constructor === NlTk ) {
|
|
|
|
nlTks.push( this.tokens.shift() );
|
|
|
|
}
|
2012-06-05 21:44:23 +00:00
|
|
|
var res = { tokens: nlTks.concat([ new TagTk( 'p' ) ], this._finish() ) };
|
|
|
|
//console.warn( 'insert p:' + JSON.stringify( res, null, 2 ) );
|
|
|
|
return res;
|
2012-01-17 18:22:10 +00:00
|
|
|
} else {
|
2012-06-05 21:44:23 +00:00
|
|
|
return { tokens: this.reset(token) };
|
2012-01-17 18:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.PostExpandParagraphHandler = PostExpandParagraphHandler;
|
|
|
|
}
|