Hack up some rudimentary image rendering. Using jshashes for the md5, and

a few hard-coded image image sizes ;) 262 tests passing.
This commit is contained in:
Gabriel Wicke 2012-03-01 13:51:53 +00:00
parent d4faf9eaf4
commit b1a7119a46
Notes: Gabriel Wicke 2012-03-01 13:51:53 +00:00
4 changed files with 25 additions and 8 deletions

View file

@ -11,6 +11,7 @@ optimist
pegjs
querystring
html5
jshashes
request (also required and automatically installed by jsdom)
assert

View file

@ -19,6 +19,8 @@
* + noinclude etc handled automatically by having all tokens on content level
*/
var jshashes = require('jshashes');
function WikiLinkHandler( manager, isInclude ) {
this.manager = manager;
this.manager.addTransform( this.onWikiLink.bind( this ), this.rank, 'tag', 'wikilink' );
@ -55,10 +57,23 @@ WikiLinkHandler.prototype.onWikiLink = function ( token, manager, cb ) {
WikiLinkHandler.prototype.renderFile = function ( token, manager, cb, title ) {
// distinguish media types
// if image: parse options
var a = new TagTk( 'a', [ new KV( 'href', title.makeLink() ) ] );
a.attribs.push( new KV('data-mw-type', 'internal') );
var img = new SelfclosingTagTk( 'img', [ new KV( 'src',
title.makeLink() ) ] );
// XXX: get /wiki from config!
var a = new TagTk( 'a', [ new KV( 'href', '/wiki' + title.makeLink() ) ] );
var MD5 = new jshashes.MD5,
hash = MD5.hex( title.key ),
path = 'http://example.com/images/' +
[ hash[0], hash.substr(0, 2) ].join('/') + '/' + title.key;
var img = new SelfclosingTagTk( 'img',
[
new KV( 'height', '220' ),
new KV( 'width', '1941' ),
new KV( 'src', path ),
new KV( 'alt', title.key ),
] );
return { tokens: [ a, img, new EndTagTk( 'a' )] };
};

View file

@ -8,10 +8,11 @@ function Title ( key, ns, nskey, env ) {
}
Title.prototype.makeLink = function () {
if ( this.nskey ) {
// XXX: links always point to the canonical namespace name.
if ( false && this.nskey ) {
return this.env.wgScriptPath + this.nskey + ':' + this.key;
} else {
return this.env.wgScriptPath + [this.ns.getDefaultName(), this.name].join(':');
return this.env.wgScriptPath + [this.ns.getDefaultName(), this.key].join(':');
}
};
@ -46,7 +47,7 @@ Namespace.prototype.getDefaultName = function ( ) {
if ( this.id == this._defaultNamespaceIDs.main ) {
return '';
} else {
return this._defaultNamespaceNames[this.id];
return this._defaultNamespaceNames[this.id.toString()];
}
};

View file

@ -110,7 +110,7 @@ MWParserEnvironment.prototype.makeTitleFromPrefixedText = function ( text ) {
if ( nsText && nsText !== text ) {
var _ns = new Namespace(0);
var ns = _ns._defaultNamespaceIDs[ nsText.toLowerCase() ];
console.warn( JSON.stringify( [ nsText, ns ] ) );
//console.warn( JSON.stringify( [ nsText, ns ] ) );
if ( ns !== undefined ) {
return new Title( text.substr( nsText.length + 1 ), ns, nsText, this );
} else {