2011-12-28 01:37:06 +00:00
|
|
|
/**
|
2012-03-07 18:42:26 +00:00
|
|
|
* This module assembles parser pipelines from parser stages with
|
|
|
|
* asynchronous communnication between stages based on events. Apart from the
|
|
|
|
* default pipeline which converts WikiText to HTML DOM, it also provides
|
|
|
|
* sub-pipelines for the processing of template transclusions.
|
2011-12-28 01:37:06 +00:00
|
|
|
*
|
2012-03-07 18:42:26 +00:00
|
|
|
* See http://www.mediawiki.org/wiki/Parsoid and
|
|
|
|
* http://www.mediawiki.org/wiki/Parsoid/Token_stream_transformations
|
|
|
|
* for illustrations of the pipeline architecture.
|
2011-12-28 01:37:06 +00:00
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
* @author Neil Kandalgaonkar <neilk@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
2012-01-04 08:39:45 +00:00
|
|
|
// make this global for now
|
|
|
|
// XXX: figure out a way to get away without a global for PEG actions!
|
|
|
|
$ = require('jquery');
|
2012-01-11 00:05:51 +00:00
|
|
|
var events = require( 'events' );
|
2012-01-04 08:39:45 +00:00
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer,
|
2012-01-17 18:22:10 +00:00
|
|
|
TokenTransformManager = require('./mediawiki.TokenTransformManager.js'),
|
2012-01-20 01:46:16 +00:00
|
|
|
|
2012-01-20 22:02:23 +00:00
|
|
|
NoIncludeOnly = require('./ext.core.NoIncludeOnly.js'),
|
|
|
|
IncludeOnly = NoIncludeOnly.IncludeOnly,
|
|
|
|
NoInclude = NoIncludeOnly.NoInclude,
|
2012-02-28 13:24:35 +00:00
|
|
|
OnlyInclude = NoIncludeOnly.OnlyInclude,
|
2011-12-28 01:37:06 +00:00
|
|
|
QuoteTransformer = require('./ext.core.QuoteTransformer.js').QuoteTransformer,
|
2012-01-17 18:22:10 +00:00
|
|
|
PostExpandParagraphHandler = require('./ext.core.PostExpandParagraphHandler.js')
|
|
|
|
.PostExpandParagraphHandler,
|
2012-01-18 01:42:56 +00:00
|
|
|
Sanitizer = require('./ext.core.Sanitizer.js').Sanitizer,
|
2012-01-11 19:48:49 +00:00
|
|
|
TemplateHandler = require('./ext.core.TemplateHandler.js').TemplateHandler,
|
2012-02-09 13:44:20 +00:00
|
|
|
AttributeExpander = require('./ext.core.AttributeExpander.js').AttributeExpander,
|
2012-03-05 15:34:27 +00:00
|
|
|
LinkHandler = require('./ext.core.LinkHandler.js'),
|
|
|
|
WikiLinkHandler = LinkHandler.WikiLinkHandler,
|
|
|
|
ExternalLinkHandler = LinkHandler.ExternalLinkHandler,
|
2011-12-28 01:37:06 +00:00
|
|
|
Cite = require('./ext.Cite.js').Cite,
|
2012-04-01 02:08:54 +00:00
|
|
|
BehaviorSwitchHandler = require('./ext.core.BehaviorSwitchHandler.js').BehaviorSwitchHandler,
|
2012-01-03 18:44:31 +00:00
|
|
|
FauxHTML5 = require('./mediawiki.HTML5TreeBuilder.node.js').FauxHTML5,
|
|
|
|
DOMPostProcessor = require('./mediawiki.DOMPostProcessor.js').DOMPostProcessor,
|
2012-03-26 19:40:56 +00:00
|
|
|
DOMConverter = require('./mediawiki.DOMConverter.js').DOMConverter,
|
|
|
|
ConvertDOMToLM = require('./mediawiki.LinearModelConverter.js').ConvertDOMToLM;
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
/**
|
|
|
|
* Set up a simple parser pipeline. There will be a single pipeline overall,
|
|
|
|
* but there can be multiple sub-pipelines for template expansions etc, which
|
|
|
|
* in turn differ by input type. The main input type will be fixed at
|
|
|
|
* construction time though.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} Environment.
|
|
|
|
*/
|
|
|
|
function ParserPipeline( env, inputType ) {
|
|
|
|
|
|
|
|
if ( ! inputType ) {
|
|
|
|
// Actually the only one supported for now, but could also create
|
|
|
|
// others for serialized tokens etc
|
|
|
|
inputType = 'text/wiki';
|
|
|
|
}
|
2012-02-20 11:07:21 +00:00
|
|
|
this.inputType = inputType;
|
2012-01-09 19:33:49 +00:00
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
// Pass in a full-fledged environment based on
|
2012-01-09 19:33:49 +00:00
|
|
|
// mediawiki.parser.environment.js.
|
2012-01-09 17:49:16 +00:00
|
|
|
if ( !env ) {
|
|
|
|
this.env = {};
|
|
|
|
} else {
|
|
|
|
this.env = env;
|
2011-12-28 01:37:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
// set up a sub-pipeline cache
|
2012-02-20 11:07:21 +00:00
|
|
|
this.pipelineCache = {};
|
|
|
|
this.pipelineCache[this.inputType] = {
|
2012-04-12 13:42:09 +00:00
|
|
|
'input-toplevel': [],
|
|
|
|
'input-include': [],
|
|
|
|
'attribute-include': [],
|
|
|
|
'attribute-toplevel': []
|
2012-01-11 00:05:51 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
// Create an input pipeline for the given input type.
|
2012-04-11 14:34:27 +00:00
|
|
|
this.inputPipeline = this.makeInputPipeline ( inputType, {}, false );
|
2012-04-12 13:42:09 +00:00
|
|
|
|
|
|
|
// Mark this pipeline as the top-level input pipeline, so that it is not
|
|
|
|
// cached and its listeners removed
|
2012-01-11 19:48:49 +00:00
|
|
|
this.inputPipeline.atTopLevel = true;
|
2012-01-21 03:03:33 +00:00
|
|
|
this.inputPipeline.last.atTopLevel = true;
|
2012-01-09 17:49:16 +00:00
|
|
|
|
|
|
|
|
2012-02-20 12:56:00 +00:00
|
|
|
this.tokenPostProcessor = new TokenTransformManager
|
2012-04-11 14:34:27 +00:00
|
|
|
.SyncTokenTransformManager ( env, inputType, 3.0, false );
|
2012-01-11 19:48:49 +00:00
|
|
|
this.tokenPostProcessor.listenForTokensFrom ( this.inputPipeline );
|
2011-12-28 01:37:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Add token transformations..
|
2012-02-20 11:07:21 +00:00
|
|
|
this._addTransformers( this.inputType, 'sync23', this.tokenPostProcessor, false );
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-04 11:00:54 +00:00
|
|
|
/**
|
2012-01-04 11:06:24 +00:00
|
|
|
* The tree builder creates a DOM tree from the token soup emitted from
|
|
|
|
* the TokenTransformDispatcher.
|
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
this.treeBuilder = new FauxHTML5.TreeBuilder();
|
2012-01-09 17:49:16 +00:00
|
|
|
this.treeBuilder.listenForTokensFrom( this.tokenPostProcessor );
|
2012-01-31 16:50:16 +00:00
|
|
|
//this.tokenPostProcessor.on('chunk', function( c ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
// console.warn( JSON.stringify( c, null, 2 ));
|
2012-01-31 16:50:16 +00:00
|
|
|
//} );
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-01-04 11:00:54 +00:00
|
|
|
/**
|
2012-01-04 11:06:24 +00:00
|
|
|
* Final processing on the HTML DOM.
|
|
|
|
*/
|
2012-01-04 11:00:54 +00:00
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
/* Generic DOM transformer.
|
|
|
|
* This currently performs minor tree-dependent clean up like wrapping
|
|
|
|
* plain text in paragraphs. For HTML output, it would also be configured
|
|
|
|
* to perform more aggressive nesting cleanup.
|
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
this.postProcessor = new DOMPostProcessor();
|
2012-01-04 11:00:54 +00:00
|
|
|
this.postProcessor.listenForDocumentFrom( this.treeBuilder );
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-04 11:00:54 +00:00
|
|
|
/**
|
2012-01-04 11:06:24 +00:00
|
|
|
* Conversion from HTML DOM to WikiDOM. This is not needed if plain HTML
|
2012-01-04 12:28:41 +00:00
|
|
|
* DOM output is desired, so it should only be registered to the
|
2012-01-04 11:06:24 +00:00
|
|
|
* DOMPostProcessor 'document' event if WikiDom output is requested. We
|
|
|
|
* could emit events for 'dom', 'wikidom', 'html' and so on, but only
|
|
|
|
* actually set up the needed pipeline stages if a listener is registered.
|
|
|
|
* Overriding the addListener method should make this possible.
|
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
this.DOMConverter = new DOMConverter();
|
2012-01-04 11:00:54 +00:00
|
|
|
|
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
// Lame version for now, see above for an idea for the external async
|
2012-01-04 11:00:54 +00:00
|
|
|
// interface and pipeline setup
|
2012-01-18 23:46:01 +00:00
|
|
|
this.postProcessor.addListener( 'document', this.forwardDocument.bind( this ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
|
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
// Inherit from EventEmitter
|
|
|
|
ParserPipeline.prototype = new events.EventEmitter();
|
|
|
|
ParserPipeline.prototype.constructor = ParserPipeline;
|
|
|
|
|
2012-02-20 11:07:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Token stream transformations to register by type and per phase. The
|
|
|
|
* possible ranks for individual transformation registrations are [0,1)
|
|
|
|
* (excluding 1.0) for sync01, [1,2) for async12 and [2,3) for sync23.
|
2012-02-22 13:13:18 +00:00
|
|
|
*
|
|
|
|
* Should perhaps be moved to mediawiki.parser.environment.js, so that all
|
|
|
|
* configuration can be found in a single place.
|
2012-02-20 11:07:21 +00:00
|
|
|
*/
|
|
|
|
ParserPipeline.prototype._transformers = {
|
|
|
|
'text/wiki': {
|
|
|
|
// Synchronous in-order per input
|
|
|
|
sync01:
|
|
|
|
[
|
2012-02-28 13:24:35 +00:00
|
|
|
OnlyInclude,
|
2012-02-20 11:07:21 +00:00
|
|
|
IncludeOnly,
|
2012-02-21 11:24:20 +00:00
|
|
|
NoInclude
|
2012-02-22 13:13:18 +00:00
|
|
|
// Insert TokenCollectors for extensions here (don't expand
|
|
|
|
// templates in extension contents); wrap collected tokens in
|
|
|
|
// special extension token.
|
|
|
|
/* Extension1, */
|
|
|
|
/* Extension2, */
|
2012-02-20 11:07:21 +00:00
|
|
|
],
|
2012-02-22 13:13:18 +00:00
|
|
|
/*
|
2012-03-07 19:29:53 +00:00
|
|
|
* Asynchronous out-of-order per input. Each async transform can only
|
|
|
|
* operate on a single input token, but can emit multiple output
|
|
|
|
* tokens. If multiple tokens need to be collected per-input, then a
|
|
|
|
* separate collection transform in sync01 can be used to wrap the
|
|
|
|
* collected tokens into a single one later processed in an async12
|
|
|
|
* transform.
|
2012-02-22 13:13:18 +00:00
|
|
|
*/
|
2012-02-20 11:07:21 +00:00
|
|
|
async12:
|
|
|
|
[
|
|
|
|
TemplateHandler,
|
2012-02-22 13:13:18 +00:00
|
|
|
// Expand attributes after templates to avoid expanding unused branches
|
2012-02-29 13:56:29 +00:00
|
|
|
AttributeExpander,
|
2012-03-05 15:34:27 +00:00
|
|
|
WikiLinkHandler,
|
2012-04-01 02:08:54 +00:00
|
|
|
ExternalLinkHandler,
|
|
|
|
BehaviorSwitchHandler,
|
2012-02-22 13:13:18 +00:00
|
|
|
/* ExtensionHandler1, */
|
|
|
|
/* ExtensionHandler2, */
|
2012-02-20 11:07:21 +00:00
|
|
|
],
|
|
|
|
// Synchronous in-order on fully expanded token stream (including
|
|
|
|
// expanded templates etc).
|
|
|
|
sync23:
|
|
|
|
[
|
|
|
|
QuoteTransformer,
|
|
|
|
PostExpandParagraphHandler,
|
|
|
|
/* Cite, */
|
2012-02-22 13:13:18 +00:00
|
|
|
/* ListHandler, */
|
2012-02-20 11:07:21 +00:00
|
|
|
Sanitizer
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all transformers to a token transform manager for a given input type
|
|
|
|
* and phase.
|
|
|
|
*/
|
|
|
|
ParserPipeline.prototype._addTransformers = function ( type, phase, manager, isInclude )
|
|
|
|
{
|
|
|
|
var transformers;
|
|
|
|
try {
|
|
|
|
transformers = this._transformers[type][phase];
|
|
|
|
} catch ( e ) {
|
|
|
|
console.warn( 'Error while looking for token transformers for ' +
|
|
|
|
type + ' and phase ' + phase );
|
|
|
|
transformers = [];
|
|
|
|
}
|
|
|
|
for ( var i = 0, l = transformers.length; i < l; i++ ) {
|
|
|
|
new transformers[i]( manager, isInclude );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
/**
|
|
|
|
* Factory method for the input (up to async token transforms / phase two)
|
|
|
|
* parts of the parser pipeline.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} Input type. Try 'text/wiki'.
|
|
|
|
* @param {Object} Expanded template arguments to pass to the
|
|
|
|
* AsyncTokenTransformManager.
|
|
|
|
* @returns {Object} { first: <first stage>, last: AsyncTokenTransformManager }
|
|
|
|
* First stage is supposed to implement a process() function
|
|
|
|
* that can accept all input at once. The wikitext tokenizer for example
|
|
|
|
* accepts the wiki text this way. The last stage of the input pipeline is
|
|
|
|
* always an AsyncTokenTransformManager, which emits its output in events.
|
|
|
|
*/
|
2012-04-11 14:34:27 +00:00
|
|
|
ParserPipeline.prototype.makeInputPipeline = function ( inputType, args, isInclude ) {
|
2012-04-12 13:42:09 +00:00
|
|
|
var pipelinePart = isInclude ? 'input-include' : 'input-toplevel';
|
2012-01-10 01:09:50 +00:00
|
|
|
switch ( inputType ) {
|
|
|
|
case 'text/wiki':
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'makeInputPipeline ' + JSON.stringify( args ) );
|
2012-04-12 13:42:09 +00:00
|
|
|
if ( this.pipelineCache['text/wiki'][pipelinePart].length ) {
|
|
|
|
var pipe = this.pipelineCache['text/wiki'][pipelinePart].pop();
|
2012-01-17 22:29:26 +00:00
|
|
|
pipe.last.args = args;
|
|
|
|
return pipe;
|
2012-01-11 00:05:51 +00:00
|
|
|
} else {
|
|
|
|
var wikiTokenizer = new PegTokenizer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Token stream transformations.
|
|
|
|
* This is where all the wiki-specific functionality is implemented.
|
2012-02-20 12:56:00 +00:00
|
|
|
* See
|
|
|
|
* https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
|
2012-01-11 00:05:51 +00:00
|
|
|
*/
|
2012-02-20 12:56:00 +00:00
|
|
|
var tokenPreProcessor = new TokenTransformManager
|
2012-04-11 14:34:27 +00:00
|
|
|
.SyncTokenTransformManager ( this.env, 'text/wiki', 1, isInclude );
|
2012-01-11 00:05:51 +00:00
|
|
|
tokenPreProcessor.listenForTokensFrom ( wikiTokenizer );
|
|
|
|
|
2012-02-20 11:07:21 +00:00
|
|
|
this._addTransformers( 'text/wiki', 'sync01',
|
2012-04-11 14:34:27 +00:00
|
|
|
tokenPreProcessor, isInclude );
|
2012-02-20 11:07:21 +00:00
|
|
|
|
2012-01-20 01:46:16 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
var tokenExpander = new TokenTransformManager.AsyncTokenTransformManager (
|
|
|
|
{
|
|
|
|
'input': this.makeInputPipeline.bind( this ),
|
|
|
|
'attributes': this.makeAttributePipeline.bind( this )
|
|
|
|
},
|
2012-04-11 14:34:27 +00:00
|
|
|
args, this.env, inputType, 2.0, isInclude
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
2012-01-11 19:48:49 +00:00
|
|
|
|
|
|
|
// Register template expansion extension
|
2012-02-20 11:07:21 +00:00
|
|
|
this._addTransformers( 'text/wiki', 'async12',
|
2012-04-11 14:34:27 +00:00
|
|
|
tokenExpander, isInclude );
|
2012-01-11 19:48:49 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
tokenExpander.listenForTokensFrom ( tokenPreProcessor );
|
2012-01-11 19:48:49 +00:00
|
|
|
// XXX: hack.
|
|
|
|
tokenExpander.inputType = inputType;
|
2012-01-14 00:58:20 +00:00
|
|
|
tokenPreProcessor.inputType = inputType;
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
return new CachedTokenPipeline(
|
2012-04-12 13:42:09 +00:00
|
|
|
this.cachePipeline.bind( this, 'text/wiki', pipelinePart ),
|
2012-01-11 00:05:51 +00:00
|
|
|
wikiTokenizer,
|
2012-04-12 08:18:42 +00:00
|
|
|
tokenExpander,
|
|
|
|
isInclude
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-10 01:09:50 +00:00
|
|
|
|
|
|
|
default:
|
2012-01-11 19:48:49 +00:00
|
|
|
console.trace();
|
2012-01-10 01:09:50 +00:00
|
|
|
throw "ParserPipeline.makeInputPipeline: Unsupported input type " + inputType;
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
2012-01-09 19:33:49 +00:00
|
|
|
};
|
2012-01-09 17:49:16 +00:00
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
|
2012-01-09 19:33:49 +00:00
|
|
|
/**
|
2012-01-10 01:09:50 +00:00
|
|
|
* Factory for attribute transformations, with input type implicit in the
|
|
|
|
* environment.
|
|
|
|
*/
|
2012-04-11 14:34:27 +00:00
|
|
|
ParserPipeline.prototype.makeAttributePipeline = function ( inputType, args, isInclude ) {
|
2012-04-12 13:42:09 +00:00
|
|
|
var pipelinePart = isInclude ? 'attribute-include' : 'attribute-toplevel';
|
|
|
|
//console.warn( 'makeAttributePipeline: ' + pipelinePart);
|
|
|
|
if ( this.pipelineCache[inputType][pipelinePart].length ) {
|
|
|
|
var pipe = this.pipelineCache[inputType][pipelinePart].pop();
|
2012-01-17 23:18:33 +00:00
|
|
|
pipe.last.args = args;
|
2012-04-12 13:42:09 +00:00
|
|
|
//console.warn( 'from cache' + JSON.stringify( pipe.last.transformers, null, 2 ) );
|
2012-01-17 23:18:33 +00:00
|
|
|
return pipe;
|
2012-01-11 00:05:51 +00:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Token stream transformations.
|
|
|
|
* This is where all the wiki-specific functionality is implemented.
|
|
|
|
* See https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
|
|
|
|
*/
|
2012-02-20 12:56:00 +00:00
|
|
|
var tokenPreProcessor = new TokenTransformManager
|
2012-04-11 19:55:52 +00:00
|
|
|
.SyncTokenTransformManager ( this.env, inputType, 1, isInclude );
|
2012-02-20 12:56:00 +00:00
|
|
|
|
2012-04-11 14:34:27 +00:00
|
|
|
this._addTransformers( inputType, 'sync01', tokenPreProcessor, isInclude );
|
2012-02-20 12:56:00 +00:00
|
|
|
|
2012-01-20 01:46:16 +00:00
|
|
|
new NoInclude( tokenPreProcessor );
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
var tokenExpander = new TokenTransformManager.AsyncTokenTransformManager (
|
2012-01-11 19:48:49 +00:00
|
|
|
{
|
|
|
|
'input': this.makeInputPipeline.bind( this ),
|
|
|
|
'attributes': this.makeAttributePipeline.bind( this )
|
|
|
|
},
|
2012-04-11 19:55:52 +00:00
|
|
|
args, this.env, inputType, 2, isInclude
|
2012-01-11 19:48:49 +00:00
|
|
|
);
|
2012-04-12 13:42:09 +00:00
|
|
|
// Add token transformers
|
2012-02-20 12:56:00 +00:00
|
|
|
this._addTransformers( 'text/wiki', 'async12',
|
2012-04-11 14:34:27 +00:00
|
|
|
tokenExpander, isInclude );
|
2012-04-12 13:42:09 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
tokenExpander.listenForTokensFrom ( tokenPreProcessor );
|
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
//console.warn( 'new pipe' + JSON.stringify( tokenExpander.transformers, null, 2 ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
return new CachedTokenPipeline(
|
2012-04-12 13:42:09 +00:00
|
|
|
this.cachePipeline.bind( this, inputType, pipelinePart ),
|
2012-01-11 00:05:51 +00:00
|
|
|
tokenPreProcessor,
|
2012-04-12 08:18:42 +00:00
|
|
|
tokenExpander,
|
|
|
|
isInclude
|
2012-01-11 00:05:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ParserPipeline.prototype.cachePipeline = function ( inputType, pipelinePart, pipe ) {
|
|
|
|
var cache = this.pipelineCache[inputType][pipelinePart];
|
2012-04-12 13:42:09 +00:00
|
|
|
if ( cache && cache.length < 5 ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
cache.push( pipe );
|
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/**
|
|
|
|
* Feed the parser pipeline with some input, the output is emitted in events.
|
2012-01-09 19:33:49 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Mixed} All arguments are passed through to the underlying input
|
|
|
|
* pipeline's first element's process() method. For a wikitext pipeline (the
|
2012-01-10 01:09:50 +00:00
|
|
|
* default), this would be the wikitext to tokenize:
|
|
|
|
* pipeline.parse ( wikiText );
|
2012-01-09 19:33:49 +00:00
|
|
|
*/
|
|
|
|
ParserPipeline.prototype.parse = function ( ) {
|
|
|
|
// Set the pipeline in motion by feeding the first element with the given
|
|
|
|
// arguments.
|
2012-01-11 19:48:49 +00:00
|
|
|
this.inputPipeline.process.apply( this.inputPipeline , arguments );
|
2012-01-04 11:00:54 +00:00
|
|
|
};
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
// Just bubble up the document event from the pipeline
|
|
|
|
ParserPipeline.prototype.forwardDocument = function ( document ) {
|
|
|
|
this.emit( 'document', document );
|
2012-01-04 11:06:24 +00:00
|
|
|
};
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-01-04 11:00:54 +00:00
|
|
|
// XXX: remove JSON serialization here, that should only be performed when
|
2012-01-09 19:33:49 +00:00
|
|
|
// needed (and normally without pretty-printing).
|
2012-03-26 19:40:56 +00:00
|
|
|
ParserPipeline.prototype.getWikiDom = function ( document ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
return JSON.stringify(
|
2012-03-26 19:40:56 +00:00
|
|
|
this.DOMConverter.HTMLtoWiki( document.body ),
|
2012-01-03 18:44:31 +00:00
|
|
|
null,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2012-03-26 19:40:56 +00:00
|
|
|
ParserPipeline.prototype.getLinearModel = function( document ) {
|
|
|
|
return JSON.stringify( ConvertDOMToLM( document.body ), null, 2 );
|
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
/************************ CachedTokenPipeline ********************************/
|
|
|
|
|
|
|
|
/**
|
2012-01-13 18:48:25 +00:00
|
|
|
* Wrap a part of a pipeline. The last member of the pipeline is supposed to
|
|
|
|
* emit 'end' and 'chunk' events, while the first is supposed to support a
|
|
|
|
* process() method that sets the pipeline in motion.
|
2012-01-11 00:05:51 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-01-13 18:48:25 +00:00
|
|
|
* @param {Function} returnToCacheCB: Callback to return the
|
|
|
|
* CachedTokenPipeline to a cache when processing has finished
|
|
|
|
* @param {Object} first: First stage of the pipeline
|
|
|
|
* @param {Object} last: Last stage of the pipeline
|
2012-01-11 00:05:51 +00:00
|
|
|
*/
|
2012-04-12 08:18:42 +00:00
|
|
|
function CachedTokenPipeline ( returnToCacheCB, first, last, isInclude ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
this.returnToCacheCB = returnToCacheCB;
|
|
|
|
this.first = first;
|
|
|
|
this.last = last;
|
|
|
|
this.last.addListener( 'end', this.forwardEndAndRecycleSelf.bind( this ) );
|
|
|
|
this.last.addListener( 'chunk', this.forwardChunk.bind( this ) );
|
2012-04-12 08:18:42 +00:00
|
|
|
this.isInclude = isInclude;
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from EventEmitter
|
|
|
|
CachedTokenPipeline.prototype = new events.EventEmitter();
|
|
|
|
CachedTokenPipeline.prototype.constructor = CachedTokenPipeline;
|
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed input tokens to the first pipeline stage
|
|
|
|
*/
|
|
|
|
CachedTokenPipeline.prototype.process = function ( chunk ) {
|
2012-04-12 13:42:09 +00:00
|
|
|
//console.warn( 'CachedTokenPipeline::process: ' + JSON.stringify( chunk ) );
|
2012-01-11 19:48:49 +00:00
|
|
|
this.first.process( chunk );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
/**
|
|
|
|
* Forward chunks to our listeners
|
|
|
|
*/
|
|
|
|
CachedTokenPipeline.prototype.forwardChunk = function ( chunk ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'CachedTokenPipeline.forwardChunk: ' +
|
2012-01-13 18:48:25 +00:00
|
|
|
// JSON.stringify( chunk, null, 2 )
|
|
|
|
// );
|
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
this.emit( 'chunk', chunk );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chunk and end event consumer and emitter, that removes all listeners from
|
|
|
|
* the given pipeline stage and returns it to a cache.
|
|
|
|
*/
|
|
|
|
CachedTokenPipeline.prototype.forwardEndAndRecycleSelf = function ( ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'CachedTokenPipeline.forwardEndAndRecycleSelf: ' +
|
2012-01-13 18:48:25 +00:00
|
|
|
// JSON.stringify( this.listeners( 'chunk' ), null, 2 ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
// first, forward the event
|
|
|
|
this.emit( 'end' );
|
|
|
|
// now recycle self
|
2012-04-12 13:42:09 +00:00
|
|
|
if ( ! this.atTopLevel ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
this.removeAllListeners( 'end' );
|
|
|
|
this.removeAllListeners( 'chunk' );
|
|
|
|
this.returnToCacheCB ( this );
|
|
|
|
}
|
2012-01-11 00:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
if (typeof module == "object") {
|
2012-01-04 08:39:45 +00:00
|
|
|
module.exports.ParserPipeline = ParserPipeline;
|
2011-12-28 01:37:06 +00:00
|
|
|
}
|