r108012 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108011‎ | r108012 | r108013 >
Date:11:00, 4 January 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Hook up the DOMPostProcessor using events as well, and rename the subscription
methods to tell a story. Also document idea on how to dynamically configure
the pipeline depending on event registrations in comment.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMPostProcessor.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.HTML5TreeBuilder.node.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformDispatcher.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformDispatcher.js
@@ -53,12 +53,12 @@
5454
5555 /**
5656 * Register to a token source, normally the tokenizer.
57 - * The event emitter emits an 'tokens' event which contains a chunk of tokens,
 57+ * The event emitter emits a 'chunk' event with a chunk of tokens,
5858 * and signals the end of tokens by triggering the 'end' event.
5959 *
6060 * @param {Object} EventEmitter token even emitter.
6161 */
62 -TokenTransformDispatcher.prototype.subscribeToTokenEmitter = function ( tokenEmitter ) {
 62+TokenTransformDispatcher.prototype.listenForTokensFrom = function ( tokenEmitter ) {
6363 tokenEmitter.addListener('chunk', this.transformTokens.bind( this ) );
6464 tokenEmitter.addListener('end', this.onEndEvent.bind( this ) );
6565 };
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.HTML5TreeBuilder.node.js
@@ -22,7 +22,11 @@
2323
2424 FauxHTML5.TreeBuilder.prototype = new events.EventEmitter();
2525
26 -FauxHTML5.TreeBuilder.prototype.subscribeToTokenEmitter = function ( emitter ) {
 26+/**
 27+ * Register for (token) 'chunk' and 'end' events from a token emitter,
 28+ * normally the TokenTransformDispatcher.
 29+ */
 30+FauxHTML5.TreeBuilder.prototype.listenForTokensFrom = function ( emitter ) {
2731 emitter.addListener('chunk', this.onChunk.bind( this ) );
2832 emitter.addListener('end', this.onEnd.bind( this ) );
2933 };
@@ -41,6 +45,8 @@
4246 this.document.body = this.parser
4347 .document.getElementsByTagName('body')[0];
4448
 49+ this.emit( 'document', this.document );
 50+
4551 // XXX: more clean up to allow reuse.
4652 this.parser.setup();
4753 this.processToken({type: 'TAG', name: 'body'});
@@ -97,6 +103,8 @@
98104 // HACK: This should not be needed really.
99105 this.document.body = this.parser.document.getElementsByTagName('body')[0];
100106 }
 107+ // Emit the document to consumers
 108+ this.emit('document', this.document);
101109 break;
102110 case "NEWLINE":
103111 //this.emit('end');
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMPostProcessor.js
@@ -1,5 +1,7 @@
22 /* Perform post-processing steps on an already-built HTML DOM. */
33
 4+var events = require('events');
 5+
46 var isBlock = function isBlock (name) {
57 switch (name.toLowerCase()) {
68 case 'div':
@@ -79,13 +81,25 @@
8082 this.processors = [process_inlines_in_p];
8183 }
8284
 85+// Inherit from EventEmitter
 86+DOMPostProcessor.prototype = new events.EventEmitter();
 87+
8388 DOMPostProcessor.prototype.doPostProcess = function ( document ) {
8489 for(var i = 0; i < this.processors.length; i++) {
8590 this.processors[i](document);
8691 }
 92+ this.emit( 'document', document );
8793 };
8894
8995
 96+/**
 97+ * Register for the 'document' event, normally emitted form the HTML5 tree
 98+ * builder.
 99+ */
 100+DOMPostProcessor.prototype.listenForDocumentFrom = function ( emitter ) {
 101+ emitter.addListener( 'document', this.doPostProcess.bind( this ) );
 102+}
 103+
90104 if (typeof module == "object") {
91105 module.exports.DOMPostProcessor = DOMPostProcessor;
92106 }
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js
@@ -29,52 +29,71 @@
3030
3131 this.wikiTokenizer = new PegTokenizer();
3232
33 - this.tokenDispatcher = new TokenTransformDispatcher ();
 33+ /**
 34+ * Token stream transformations.
 35+ * This is where all the wiki functionality is implemented.
 36+ * See https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
 37+ */
 38+ this.tokenTransformer = new TokenTransformDispatcher ();
3439
3540 // Add token transformations..
3641 var qt = new QuoteTransformer();
37 - qt.register(this.tokenDispatcher);
 42+ qt.register(this.tokenTransformer);
3843
3944 //var citeExtension = new Cite();
4045 //citeExtension.register(this.tokenDispatcher);
4146
42 - this.tokenDispatcher.subscribeToTokenEmitter( this.wikiTokenizer );
 47+ this.tokenTransformer.listenForTokensFrom( this.wikiTokenizer );
4348
44 - // Create a new tree builder, which also creates a new document.
45 - // XXX: implicitly clean up old state after processing end token, so
46 - // that we can reuse the tree builder.
47 - // XXX: convert to event listener listening for token chunks from the
48 - // token transformer and and emitting an additional 'done' event after
49 - // processing the 'end' token.
 49+ /**
 50+ * The tree builder creates a DOM tree from the token soup emitted from
 51+ * the TokenTransformDispatcher.
 52+ */
5053 this.treeBuilder = new FauxHTML5.TreeBuilder();
51 - this.treeBuilder.subscribeToTokenEmitter( this.tokenDispatcher );
 54+ this.treeBuilder.listenForTokensFrom( this.tokenTransformer );
5255
53 - // Prepare these two, but only call them from parse and getWikiDom for
54 - // now. These will be called in a callback later, when the full pipeline
55 - // is used asynchronously.
 56+ /**
 57+ * Final processing on the HTML DOM.
 58+ */
 59+
 60+ // Generic DOM transformer.
 61+ // This currently performs minor tree-dependent clean up like wrapping
 62+ // plain text in paragraphs. For HTML output, it would also be configured
 63+ // to perform more aggressive nesting cleanup.
5664 this.postProcessor = new DOMPostProcessor();
 65+ this.postProcessor.listenForDocumentFrom( this.treeBuilder );
5766
 67+
 68+ /**
 69+ * Conversion from HTML DOM to WikiDOM. This is not needed if plain HTML
 70+ * DOM output is needed, so it should only be registered to the
 71+ * DOMPostProcessor 'document' event if WikiDom output is requested. We
 72+ * could emit events for 'dom', 'wikidom', 'html' and so on, but only
 73+ * actually set up the needed pipeline stages if a listener is registered.
 74+ * Overriding the addListener method should make this possible.
 75+ */
5876 this.DOMConverter = new DOMConverter();
 77+
 78+
 79+ // Lame hack for now, see above for an idea for the external async
 80+ // interface and pipeline setup
 81+ this.postProcessor.addListener( 'document', this.setDocumentProperty.bind( this ) );
5982 }
6083
6184 ParserPipeline.prototype.parse = function ( text ) {
6285 // Set the pipeline in motion by feeding the tokenizer
6386 this.wikiTokenizer.tokenize( text );
 87+};
6488
65 - // XXX: Convert parse to an async pipeline as well!
66 - // The remaining processing below will have to happen in a callback,
67 - // triggered on the treeBuilder 'end' event, followed by an event emission
68 - // or callback calling instead of returning.
69 - this.document = this.treeBuilder.document;
 89+// XXX: Lame hack: set document property. Instead, emit events
 90+// and convert parser tests etc to listen on it! See comments above for ideas.
 91+ParserPipeline.prototype.setDocumentProperty = function ( document ) {
 92+ this.document = document;
 93+}
7094
71 - //console.log(this.document.body.innerHTML);
7295
73 - // Perform synchronous post-processing on DOM.
74 - // XXX: convert to event listener (listening on treeBuilder 'end'
75 - // event)
76 - this.postProcessor.doPostProcess( this.document );
77 -};
78 -
 96+// XXX: remove JSON serialization here, that should only be performed when
 97+// needed.
7998 ParserPipeline.prototype.getWikiDom = function () {
8099 return JSON.stringify(
81100 this.DOMConverter.HTMLtoWiki( this.document.body ),

Follow-up revisions

RevisionCommit summaryAuthorDate
r108014Fix JSHint warnings (mostly about comment indentation) from r108012.gwicke11:06, 4 January 2012

Status & tagging log