Start to move wikilink expansion to a separate async token transformer.

This commit is contained in:
Gabriel Wicke 2012-02-29 13:56:29 +00:00
parent e0838db315
commit 4b9bd45b82
Notes: Gabriel Wicke 2012-02-29 13:56:29 +00:00
4 changed files with 59 additions and 6 deletions

View file

@ -65,6 +65,7 @@ AttributeExpander.prototype.onToken = function ( token, frame, cb ) {
return { async: true };
}
} else {
token.rank = this.rank;
return { token: token };
}
};
@ -80,6 +81,7 @@ AttributeExpander.prototype._returnAttributes = function ( expandData,
// Remove the target from the attributes
expandData.token.attribs = attributes;
if ( expandData.async ) {
expandData.token.rank = this.rank;
expandData.cb( [expandData.token], false );
}
};

View file

@ -0,0 +1,48 @@
/**
* Simple link handler. Registers after template expansions, as an
* asynchronous transform.
*
* @author Gabriel Wicke <gwicke@wikimedia.org>
*
* * Collect description/parameter tokens between 'a' tags
* * Extract image options and add image html if target is media/image
* namespace
* *
*
*
* TODO: keep round-trip information in meta tag or the like
*
*
*
* Pro/Contra of single token vs. tags and tokens
* - Need to collect description tokens between a and /a
* + noinclude etc handled automatically by having all tokens on content level
*/
function WikiLinkHandler( manager, isInclude ) {
this.manager = manager;
this.manager.addTransform( this.onWikiLink.bind( this ), this.rank, 'tag', 'wikilink' );
}
WikiLinkHandler.prototype.rank = 1.15; // after AttributeExpander
WikiLinkHandler.prototype.onWikiLink = function ( token, manager, cb ) {
// Split off and normalize namespace
// Compare with image/media namespaces
// handle image
// handle
// Check if page exists
//
var obj = new TagTk( 'a', [ this.manager.env.lookupKV( token.attribs, 'href' ) ] );
obj.attribs.push( new KV('data-mw-type', 'internal') );
var out = [obj].concat( this.manager.env.lookupKV( token.attribs, 'content' ).v,
new EndTagTk( 'a' ) );
//console.warn( JSON.stringify( out, null, 2 ) );
return { tokens: out };
};
if (typeof module == "object") {
module.exports.WikiLinkHandler = WikiLinkHandler;
}

View file

@ -26,6 +26,7 @@ var fs = require('fs'),
Sanitizer = require('./ext.core.Sanitizer.js').Sanitizer,
TemplateHandler = require('./ext.core.TemplateHandler.js').TemplateHandler,
AttributeExpander = require('./ext.core.AttributeExpander.js').AttributeExpander,
WikiLinkHandler = require('./ext.core.LinkHandler.js').WikiLinkHandler,
Cite = require('./ext.Cite.js').Cite,
FauxHTML5 = require('./mediawiki.HTML5TreeBuilder.node.js').FauxHTML5,
DOMPostProcessor = require('./mediawiki.DOMPostProcessor.js').DOMPostProcessor,
@ -165,7 +166,8 @@ ParserPipeline.prototype._transformers = {
// Expand attributes after templates to avoid expanding unused branches
// XXX: Should we support further processing after attribute
// expansion?
AttributeExpander
AttributeExpander,
WikiLinkHandler
/* ExtensionHandler1, */
/* ExtensionHandler2, */
],

View file

@ -746,12 +746,11 @@ wikilink
// class. Can we work out a static negative class instead?
// XXX: Exclude uppercase chars from non-latin languages too!
trail:(! [A-Z \t(),.:-] tc:text_char { return tc })* {
var obj = new TagTk( 'a',
[
new KV('data-mw-type', 'internal')
] ),
var obj = new SelfclosingTagTk( 'wikilink' ),
textTokens = [];
obj.attribs.push( new KV('href', target) );
// Deal with content. XXX: Properly support pipe-trick etc
if (lcontent && lcontent.length) {
textTokens = lcontent;
if (trail) {
@ -765,8 +764,10 @@ wikilink
textTokens = $.extend(true, [], target);
}
}
obj.attribs.push( new KV( 'content', flatten( textTokens ) ) );
//console.warn( "XXX:" + pp([obj].concat(textTokens, [new EndTagTk( 'a' )])) );
return [obj].concat(textTokens, [new EndTagTk( 'a' )]);
return [obj];
}
link_text