jenkins-bot has submitted this change and it was merged. Change subject: Code cleanup: Pipeline utilities now take env and parent frame. ......................................................................
Code cleanup: Pipeline utilities now take env and parent frame. * Removed manager and passed in env and parent-frame to all utilities that process content in new pipelines. * Added more documentation to mediawiki.Util.js. * Renamed processAttributeToDOM to a more appropriate name. * Added pipelineFactory property to env and used that to construct parsing pipelines everywhere. Change-Id: Ic612e5630d19d4e3f5d6388bc5cd117d337fd799 --- M api/ParsoidService.js M lib/ext.Cite.js M lib/ext.core.AttributeExpander.js M lib/ext.core.DOMFragmentBuilder.js M lib/ext.core.LinkHandler.js M lib/ext.core.TemplateHandler.js M lib/mediawiki.Util.js M lib/mediawiki.parser.environment.js M tests/parse.js M tests/parserTests.js M tests/roundtrip-test.js 11 files changed, 104 insertions(+), 64 deletions(-) Approvals: Arlolra: Looks good to me, approved Marcoil: Looks good to me, but someone else must approve GWicke: Looks good to me, but someone else must approve jenkins-bot: Verified diff --git a/api/ParsoidService.js b/api/ParsoidService.js index 4ac1bf2..8c63e8e 100644 --- a/api/ParsoidService.js +++ b/api/ParsoidService.js @@ -647,7 +647,7 @@ env.page.name = ''; } - var parser = Util.getParserPipeline( env, 'text/x-mediawiki/full' ); + var parser = env.pipelineFactory.getPipeline('text/x-mediawiki/full'); parser.once( 'document', function ( document ) { // Don't cache requests when wt is set in case somebody uses // GET for wikitext parsing diff --git a/lib/ext.Cite.js b/lib/ext.Cite.js index f0e539f..2b7c560 100644 --- a/lib/ext.Cite.js +++ b/lib/ext.Cite.js @@ -50,7 +50,7 @@ opts.srcOffsets = [ tsr[0]+tagWidths[0]+leadingWS.length, tsr[1]-tagWidths[1] ]; // Process ref content - Util.processContentInPipeline( manager, content, opts ); + Util.processContentInPipeline(manager.env, manager.frame, content, opts); } } diff --git a/lib/ext.core.AttributeExpander.js b/lib/ext.core.AttributeExpander.js index 61539f2..cc1bf4e 100644 --- a/lib/ext.core.AttributeExpander.js +++ b/lib/ext.core.AttributeExpander.js @@ -249,7 +249,7 @@ var manager = this.manager; // Async-expand all token arrays to DOM. - Util.expandValuesToDOM(manager, vals, function(err, eVals) { + Util.expandValuesToDOM(manager.env, manager.frame, vals, function(err, eVals) { // Rebuild flattened k-v pairs. var expandedAttrs = []; for (var i = 0; i < eVals.length; i += 2) { diff --git a/lib/ext.core.DOMFragmentBuilder.js b/lib/ext.core.DOMFragmentBuilder.js index 00e215e..a1f13d1 100644 --- a/lib/ext.core.DOMFragmentBuilder.js +++ b/lib/ext.core.DOMFragmentBuilder.js @@ -70,7 +70,8 @@ // Process tokens Util.processContentInPipeline( - this.manager, + this.manager.env, + this.manager.frame, // Append EOF content.concat([new EOFTk()]), { diff --git a/lib/ext.core.LinkHandler.js b/lib/ext.core.LinkHandler.js index 8c82e17..8dcd63a 100644 --- a/lib/ext.core.LinkHandler.js +++ b/lib/ext.core.LinkHandler.js @@ -397,8 +397,9 @@ } else { // Deal with sort keys that come from generated content (transclusions, etc.) cb( { async: true } ); - Util.processAttributeToDOM( - this.manager, + Util.processTokensToDOM( + this.manager.env, + this.manager.frame, content, function(dom) { var sortKeyInfo = [ {"txt": "mw:sortKey"}, {"html": dom.body.innerHTML} ], diff --git a/lib/ext.core.TemplateHandler.js b/lib/ext.core.TemplateHandler.js index f0c336d..852b131 100644 --- a/lib/ext.core.TemplateHandler.js +++ b/lib/ext.core.TemplateHandler.js @@ -487,21 +487,26 @@ } this.manager.env.dp( 'TemplateHandler._startDocumentPipeline', tplArgs.name, tplArgs.attribs ); - Util.processContentInPipeline(this.manager, src, { - // Full pipeline all the way to DOM - pipelineType: 'text/x-mediawiki/full', - pipelineOpts: { - isInclude: true, - // we *might* be able to get away without this if we transfer - // more than just the about when unwrapping - wrapTemplates: false, - // suppress paragraphs - // Should this be the default in all cases? - inBlockToken: true - }, - tplArgs: tplArgs, - documentCB: this._onDocument.bind(this, state, cb) - }); + Util.processContentInPipeline( + this.manager.env, + this.manager.frame, + src, + { + // Full pipeline all the way to DOM + pipelineType: 'text/x-mediawiki/full', + pipelineOpts: { + isInclude: true, + // we *might* be able to get away without this if we transfer + // more than just the about when unwrapping + wrapTemplates: false, + // suppress paragraphs + // Should this be the default in all cases? + inBlockToken: true + }, + tplArgs: tplArgs, + documentCB: this._onDocument.bind(this, state, cb) + } + ); }; /** @@ -537,19 +542,24 @@ // Get a nested transformation pipeline for the input type. The input // pipeline includes the tokenizer, synchronous stage-1 transforms for // 'text/wiki' input and asynchronous stage-2 transforms). - Util.processContentInPipeline(this.manager, src, { - pipelineType: type || 'text/x-mediawiki', - pipelineOpts: { - inTemplate: true, - isInclude: true, - // NOTE: No template wrapping required for nested templates. - wrapTemplates: false, - extTag: this.options.extTag - }, - tplArgs: tplArgs, - chunkCB: this._onChunk.bind ( this, state, cb ), - endCB: this._onEnd.bind ( this, state, cb ) - }); + Util.processContentInPipeline( + this.manager.env, + this.manager.frame, + src, + { + pipelineType: type || 'text/x-mediawiki', + pipelineOpts: { + inTemplate: true, + isInclude: true, + // NOTE: No template wrapping required for nested templates. + wrapTemplates: false, + extTag: this.options.extTag + }, + tplArgs: tplArgs, + chunkCB: this._onChunk.bind ( this, state, cb ), + endCB: this._onEnd.bind ( this, state, cb ) + } + ); }; TemplateHandler.prototype.addAboutToTableElements = function ( state, tokens ) { diff --git a/lib/mediawiki.Util.js b/lib/mediawiki.Util.js index f6ec1d0..34cd020 100644 --- a/lib/mediawiki.Util.js +++ b/lib/mediawiki.Util.js @@ -1038,27 +1038,32 @@ * Processes content (wikitext, array of tokens, whatever) in its own pipeline * based on options. * - * @param {TokenTransformManager} manager - * The manager to use for building new pipelines and set parent frames + * @param {Object} env + * The environment/context for the expansion. + * + * @param {Object} frame + * The parent frame within which the expansion is taking place. + * This param is mostly defunct now that we are not doing native + * expansion anymore. * * @param {Object} content * This could be wikitext or single token or an array of tokens. - * How this content is processed depends on what kind of pipeline is - * constructed specified by opts. + * How this content is processed depends on what kind of pipeline + * is constructed specified by opts. * * @param {Object} opts * Processing options that specify pipeline-type, opts, and callbacks. */ - processContentInPipeline: function(manager, content, opts) { + processContentInPipeline: function(env, frame, content, opts) { // Build a pipeline - var pipeline = manager.pipeFactory.getPipeline( + var pipeline = env.pipelineFactory.getPipeline( opts.pipelineType, opts.pipelineOpts ); // Set frame if necessary if (opts.tplArgs) { - pipeline.setFrame(manager.frame, opts.tplArgs.name, opts.tplArgs.attribs); + pipeline.setFrame(frame, opts.tplArgs.name, opts.tplArgs.attribs); } // Set source offsets for this pipeline's content @@ -1081,10 +1086,33 @@ pipeline.process(content, opts.tplArgs ? opts.tplArgs.cacheKey : undefined); }, - processAttributeToDOM: function(manager, content, cb) { + /** + * Processes an array of tokens all the way to DOM + * + * @param {Object} env + * The environment/context for the expansion. + * + * @param {Object} frame + * The parent frame within which the expansion is taking place. + * This param is mostly defunct now that we are not doing native + * expansion anymore. + * + * @param {Object} tokens + * The array of tokens to process + * + * @param {Object} cb + * The callback to pass back the DOM to. + */ + processTokensToDOM: function(env, frame, tokens, cb) { + if (!Array.isArray(tokens)) { + cb(tokens); + return; + } + this.processContentInPipeline( - manager, - content.concat([new pd.EOFTk()]), { + env, + frame, + tokens.concat([new pd.EOFTk()]), { pipelineType: "tokens/x-mediawiki/expanded", pipelineOpts: { attrExpansion: true, @@ -1100,11 +1128,16 @@ /** * Expands values all the way to DOM and passes them back to a callback * - * @param {TokenTransformManager} manager - * The manager to use for building new pipelines and set parent frames + * @param {Object} env + * The environment/context for the expansion. + * + * @param {Object} frame + * The parent frame within which the expansion is taking place. + * This param is mostly defunct now that we are not doing native + * expansion anymore. * * @param {Object[]} vals - * The array of tokens to process. + * The array of values to process. * Each value of this array is expected to be an object with a "html" property. * The html property is expanded to DOM only if it is an array (of tokens). * Non-arrays are passed back unexpanded. @@ -1114,14 +1147,15 @@ * * FIXME: This could be generified a bit more. */ - expandValuesToDOM: function(manager, vals, finalCB) { + expandValuesToDOM: function(env, frame, vals, finalCB) { var self = this; async.map( vals, function(v, cb) { if (Array.isArray(v.html)) { - self.processAttributeToDOM( - manager, + self.processTokensToDOM( + env, + frame, v.html, function(dom) { v.html = dom.body.innerHTML; @@ -1320,15 +1354,6 @@ Util.diff = diff; // XXX gwicke: move to a Parser object? -var ParserPipelineFactory; -Util.getParserPipeline = function ( env, type ) { - if (!ParserPipelineFactory) { - ParserPipelineFactory = require( './mediawiki.parser.js' ).ParserPipelineFactory; - } - return ( new ParserPipelineFactory( env ) ).getPipeline( type ); -}; - -// XXX gwicke: move to a Parser object? Util.parse = function ( env, cb, err, src, expansions ) { if ( err !== null ) { cb( null, err ); @@ -1341,7 +1366,7 @@ } // Now go ahead with the actual parsing - var parser = Util.getParserPipeline( env, 'text/x-mediawiki/full' ); + var parser = env.pipelineFactory.getPipeline( 'text/x-mediawiki/full' ); parser.once( 'document', cb.bind( null, env, null ) ); try { parser.processToplevelDoc( src ); diff --git a/lib/mediawiki.parser.environment.js b/lib/mediawiki.parser.environment.js index 72fbfa8..a3108da 100644 --- a/lib/mediawiki.parser.environment.js +++ b/lib/mediawiki.parser.environment.js @@ -4,7 +4,8 @@ ParsoidConfig = require( './mediawiki.ParsoidConfig.js' ).ParsoidConfig, ConfigRequest = require( './mediawiki.ApiRequest.js' ).ConfigRequest, Util = require( './mediawiki.Util.js').Util, - Title = require('./mediawiki.Title.js').Title; + Title = require('./mediawiki.Title.js').Title, + ParserPipelineFactory = require( './mediawiki.parser.js' ).ParserPipelineFactory; var util = require('util'); @@ -68,6 +69,8 @@ this.reset( this.page.name ); + this.pipelineFactory = new ParserPipelineFactory(this); + // Outstanding page requests (for templates etc) this.requestQueue = {}; diff --git a/tests/parse.js b/tests/parse.js index bc7ba88..491b3f5 100755 --- a/tests/parse.js +++ b/tests/parse.js @@ -149,7 +149,7 @@ serializer; if ( !argv.html2wt ) { - parserPipeline = Util.getParserPipeline(env, 'text/x-mediawiki/full'); + parserPipeline = env.pipelineFactory.getPipeline( 'text/x-mediawiki/full'); } if ( !argv.wt2html ) { diff --git a/tests/parserTests.js b/tests/parserTests.js index 35d90af..9ca31d7 100755 --- a/tests/parserTests.js +++ b/tests/parserTests.js @@ -1521,7 +1521,7 @@ // Create parsers, serializers, .. if ( options.html2html || options.wt2wt || options.wt2html || options.selser ) { - this.parserPipeline = Util.getParserPipeline(this.env, 'text/x-mediawiki/full'); + this.parserPipeline = this.env.pipelineFactory.getPipeline('text/x-mediawiki/full'); } options.reportStart(); diff --git a/tests/roundtrip-test.js b/tests/roundtrip-test.js index 0debb3b..c375601 100755 --- a/tests/roundtrip-test.js +++ b/tests/roundtrip-test.js @@ -390,7 +390,7 @@ process.exit( 1 ); }; - var parserPipeline = Util.getParserPipeline( env, 'text/x-mediawiki/full' ); + var parserPipeline = env.pipelineFactory.getPipeline('text/x-mediawiki/full'); parserPipeline.once( 'document', checkIfSignificant.bind( null, env, offsets, src, body, out, cb ) ); parserPipeline.processToplevelDoc( out ); -- To view, visit https://gerrit.wikimedia.org/r/112821 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic612e5630d19d4e3f5d6388bc5cd117d337fd799 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Subramanya Sastry <ssas...@wikimedia.org> Gerrit-Reviewer: Arlolra <abrea...@wikimedia.org> Gerrit-Reviewer: Cscott <canan...@wikimedia.org> Gerrit-Reviewer: GWicke <gwi...@wikimedia.org> Gerrit-Reviewer: Marcoil <marc...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits