From b1a7119a467fe5aa8aea5cd35b79bcc5127efa3a Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Thu, 1 Mar 2012 13:51:53 +0000 Subject: [PATCH] Hack up some rudimentary image rendering. Using jshashes for the md5, and a few hard-coded image image sizes ;) 262 tests passing. --- modules/parser/README.txt | 1 + modules/parser/ext.core.LinkHandler.js | 23 +++++++++++++++---- modules/parser/mediawiki.Title.js | 7 +++--- .../parser/mediawiki.parser.environment.js | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/modules/parser/README.txt b/modules/parser/README.txt index 193cd3ac64..66de2419d0 100644 --- a/modules/parser/README.txt +++ b/modules/parser/README.txt @@ -11,6 +11,7 @@ optimist pegjs querystring html5 +jshashes request (also required and automatically installed by jsdom) assert diff --git a/modules/parser/ext.core.LinkHandler.js b/modules/parser/ext.core.LinkHandler.js index 752f7bff9b..08da97f22f 100644 --- a/modules/parser/ext.core.LinkHandler.js +++ b/modules/parser/ext.core.LinkHandler.js @@ -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' )] }; }; diff --git a/modules/parser/mediawiki.Title.js b/modules/parser/mediawiki.Title.js index af220d934f..44b0a429ef 100644 --- a/modules/parser/mediawiki.Title.js +++ b/modules/parser/mediawiki.Title.js @@ -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()]; } }; diff --git a/modules/parser/mediawiki.parser.environment.js b/modules/parser/mediawiki.parser.environment.js index 4815c3c74c..e14305403c 100644 --- a/modules/parser/mediawiki.parser.environment.js +++ b/modules/parser/mediawiki.parser.environment.js @@ -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 {