2012-02-29 13:56:29 +00:00
|
|
|
/**
|
|
|
|
* Simple link handler. Registers after template expansions, as an
|
|
|
|
* asynchronous transform.
|
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*
|
|
|
|
* TODO: keep round-trip information in meta tag or the like
|
|
|
|
*/
|
|
|
|
|
2012-03-01 18:07:20 +00:00
|
|
|
var jshashes = require('jshashes'),
|
|
|
|
PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer;
|
2012-03-01 13:51:53 +00:00
|
|
|
|
2012-02-29 13:56:29 +00:00
|
|
|
function WikiLinkHandler( manager, isInclude ) {
|
|
|
|
this.manager = manager;
|
|
|
|
this.manager.addTransform( this.onWikiLink.bind( this ), this.rank, 'tag', 'wikilink' );
|
2012-03-01 18:07:20 +00:00
|
|
|
// create a new peg parser for image options..
|
|
|
|
if ( !this.imageParser ) {
|
|
|
|
// Actually the regular tokenizer, but we'll call it with the
|
|
|
|
// img_options production only.
|
|
|
|
WikiLinkHandler.prototype.imageParser = new PegTokenizer();
|
|
|
|
}
|
2012-02-29 13:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WikiLinkHandler.prototype.rank = 1.15; // after AttributeExpander
|
|
|
|
|
|
|
|
WikiLinkHandler.prototype.onWikiLink = function ( token, manager, cb ) {
|
2012-03-01 12:47:05 +00:00
|
|
|
var env = this.manager.env;
|
|
|
|
var title = this.manager.env.makeTitleFromPrefixedText(
|
|
|
|
env.tokensToString(
|
|
|
|
env.lookupKV( token.attribs, 'href' ).v
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( title.ns.isFile() ) {
|
|
|
|
return this.renderFile( token, manager, cb, title );
|
|
|
|
} else if ( title.ns.isCategory() ) {
|
|
|
|
// TODO: implement
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
// 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 };
|
|
|
|
}
|
2012-02-29 13:56:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-01 12:47:05 +00:00
|
|
|
WikiLinkHandler.prototype.renderFile = function ( token, manager, cb, title ) {
|
2012-03-01 18:07:20 +00:00
|
|
|
var env = manager.env;
|
2012-03-01 12:47:05 +00:00
|
|
|
// distinguish media types
|
|
|
|
// if image: parse options
|
2012-03-01 13:51:53 +00:00
|
|
|
// XXX: get /wiki from config!
|
|
|
|
var a = new TagTk( 'a', [ new KV( 'href', '/wiki' + title.makeLink() ) ] );
|
|
|
|
|
2012-03-01 18:07:20 +00:00
|
|
|
var MD5 = new jshashes.MD5(),
|
2012-03-01 13:51:53 +00:00
|
|
|
hash = MD5.hex( title.key ),
|
2012-03-01 18:07:20 +00:00
|
|
|
// XXX: Hackhack..
|
2012-03-01 13:51:53 +00:00
|
|
|
path = 'http://example.com/images/' +
|
|
|
|
[ hash[0], hash.substr(0, 2) ].join('/') + '/' + title.key;
|
|
|
|
|
|
|
|
|
2012-03-01 18:07:20 +00:00
|
|
|
// XXX: parse options
|
|
|
|
var options = this.parseImageOptions( env.lookupKV( token.attribs, 'content' ) );
|
|
|
|
// XXX: check if the file exists, generate thumbnail
|
|
|
|
// XXX: render according to mode (inline, thumb, framed etc)
|
2012-03-01 13:51:53 +00:00
|
|
|
var img = new SelfclosingTagTk( 'img',
|
|
|
|
[
|
2012-03-01 18:07:20 +00:00
|
|
|
// FIXME!
|
2012-03-01 13:51:53 +00:00
|
|
|
new KV( 'height', '220' ),
|
|
|
|
new KV( 'width', '1941' ),
|
|
|
|
new KV( 'src', path ),
|
2012-03-01 18:07:20 +00:00
|
|
|
new KV( 'alt', title.key )
|
2012-03-01 13:51:53 +00:00
|
|
|
] );
|
2012-03-01 18:07:20 +00:00
|
|
|
|
2012-03-01 12:47:05 +00:00
|
|
|
return { tokens: [ a, img, new EndTagTk( 'a' )] };
|
|
|
|
};
|
2012-02-29 13:56:29 +00:00
|
|
|
|
2012-03-01 18:07:20 +00:00
|
|
|
WikiLinkHandler.prototype.parseImageOptions = function ( tokens ) {
|
|
|
|
var text = this.manager.env.tokensToString( tokens );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-02-29 13:56:29 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.WikiLinkHandler = WikiLinkHandler;
|
|
|
|
}
|