r112799 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112798‎ | r112799 | r112800 >
Date:12:47, 1 March 2012
Author:gwicke
Status:deferred
Tags:
Comment:
More work on wiki link rendering and general wiki title / namespace
functionality.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.Title.js (added) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.Title.js
@@ -0,0 +1,57 @@
 2+function Title ( key, ns, nskey, env ) {
 3+ this.key = key;
 4+ // Namespace index
 5+ this.ns = new Namespace( ns );
 6+ // the original ns string
 7+ this.nskey = nskey;
 8+ this.env = env;
 9+}
 10+
 11+Title.prototype.makeLink = function () {
 12+ if ( this.nskey ) {
 13+ return this.env.wgScriptPath + this.nskey + ':' + this.key;
 14+ } else {
 15+ return this.env.wgScriptPath + [this.ns.getDefaultName(), this.name].join(':');
 16+ }
 17+};
 18+
 19+
 20+function Namespace ( id ) {
 21+ this.id = id;
 22+}
 23+
 24+Namespace.prototype._defaultNamespaceIDs = {
 25+ file: -2,
 26+ image: -2,
 27+ special: -1,
 28+ main: 0,
 29+ category: 14
 30+};
 31+
 32+Namespace.prototype._defaultNamespaceNames = {
 33+ '-2': 'File',
 34+ '-1': 'Special',
 35+ '0': '',
 36+ '14': 'Category'
 37+};
 38+
 39+Namespace.prototype.isFile = function ( ) {
 40+ return this.id === this._defaultNamespaceIDs.file;
 41+};
 42+Namespace.prototype.isCategory = function ( ) {
 43+ return this.id === this._defaultNamespaceIDs.category;
 44+};
 45+
 46+Namespace.prototype.getDefaultName = function ( ) {
 47+ if ( this.id == this._defaultNamespaceIDs.main ) {
 48+ return '';
 49+ } else {
 50+ return this._defaultNamespaceNames[this.id];
 51+ }
 52+};
 53+
 54+
 55+if (typeof module == "object") {
 56+ module.exports.Title = Title;
 57+ module.exports.Namespace = Namespace;
 58+}
Property changes on: trunk/extensions/VisualEditor/modules/parser/mediawiki.Title.js
___________________________________________________________________
Added: svn:eol-style
159 + native
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
@@ -1,3 +1,7 @@
 2+var title = require('./mediawiki.Title.js'),
 3+ Title = title.Title,
 4+ Namespace = title.Namespace;
 5+
26 var MWParserEnvironment = function(opts) {
37 var options = {
48 tagHooks: {},
@@ -10,6 +14,7 @@
1115 fetchTemplates: false,
1216 maxDepth: 40
1317 };
 18+ // XXX: this should be namespaced
1419 $.extend(options, opts);
1520 $.extend(this, options);
1621 };
@@ -65,7 +70,7 @@
6671 }
6772 //console.warn( 'KVtoHash: ' + JSON.stringify( res ));
6873 return res;
69 -}
 74+};
7075
7176 // Does this need separate UI/content inputs?
7277 MWParserEnvironment.prototype.formatNum = function( num ) {
@@ -98,10 +103,38 @@
99104 }
100105 };
101106
 107+
 108+MWParserEnvironment.prototype.makeTitleFromPrefixedText = function ( text ) {
 109+ text = this.normalizeTitle( text );
 110+ var nsText = text.split( ':', 1 )[0];
 111+ if ( nsText && nsText !== text ) {
 112+ var _ns = new Namespace(0);
 113+ var ns = _ns._defaultNamespaceIDs[ nsText.toLowerCase() ];
 114+ console.warn( JSON.stringify( [ nsText, ns ] ) );
 115+ if ( ns !== undefined ) {
 116+ return new Title( text.substr( nsText.length + 1 ), ns, nsText, this );
 117+ } else {
 118+ return new Title( text, 0, '', this );
 119+ }
 120+ } else {
 121+ return new Title( text, 0, this );
 122+ }
 123+};
 124+
 125+
 126+// XXX: move to Title!
102127 MWParserEnvironment.prototype.normalizeTitle = function( name ) {
103128 if (typeof name !== 'string') {
104129 throw new Error('nooooooooo not a string');
105130 }
 131+ var forceNS;
 132+ if ( name.substr( 0, 1 ) === ':' ) {
 133+ forceNS = ':';
 134+ name = name.substr(1);
 135+ } else {
 136+ forceNS = '';
 137+ }
 138+
106139 name = name.trim().replace(/[\s_]+/g, '_');
107140
108141 // Implement int: as alias for MediaWiki:
@@ -115,11 +148,18 @@
116149 }
117150
118151 function upperFirst( s ) { return s.substr(0, 1).toUpperCase() + s.substr(1); }
119 - name = name.split(':').map( upperFirst ).join(':');
 152+ // XXX: Do not uppercase all bits!
 153+ var ns = name.split(':', 1)[0];
 154+ if( ns !== '' && ns !== name ) {
 155+ name = upperFirst( ns ) + ':' + upperFirst( name.substr( ns.length + 1 ) );
 156+ } else {
 157+ name = upperFirst( name );
 158+ }
 159+ //name = name.split(':').map( upperFirst ).join(':');
120160 //if (name === '') {
121161 // throw new Error('Invalid/empty title');
122162 //}
123 - return name;
 163+ return forceNS + name;
124164 };
125165
126166 /**
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
@@ -27,21 +27,40 @@
2828 WikiLinkHandler.prototype.rank = 1.15; // after AttributeExpander
2929
3030 WikiLinkHandler.prototype.onWikiLink = function ( token, manager, cb ) {
31 - // Split off and normalize namespace
32 - // Compare with image/media namespaces
33 - // handle image
34 - // handle
35 - // Check if page exists
36 - //
37 - var obj = new TagTk( 'a', [ this.manager.env.lookupKV( token.attribs, 'href' ) ] );
38 - obj.attribs.push( new KV('data-mw-type', 'internal') );
39 - var out = [obj].concat( this.manager.env.lookupKV( token.attribs, 'content' ).v,
40 - new EndTagTk( 'a' ) );
41 - //console.warn( JSON.stringify( out, null, 2 ) );
42 - return { tokens: out };
 31+ var env = this.manager.env;
 32+ var title = this.manager.env.makeTitleFromPrefixedText(
 33+ env.tokensToString(
 34+ env.lookupKV( token.attribs, 'href' ).v
 35+ )
 36+ );
 37+
 38+ if ( title.ns.isFile() ) {
 39+ return this.renderFile( token, manager, cb, title );
 40+ } else if ( title.ns.isCategory() ) {
 41+ // TODO: implement
 42+ return [];
 43+ } else {
 44+ // Check if page exists
 45+ //
 46+ var obj = new TagTk( 'a', [ this.manager.env.lookupKV( token.attribs, 'href' ) ] );
 47+ obj.attribs.push( new KV('data-mw-type', 'internal') );
 48+ var out = [obj].concat( this.manager.env.lookupKV( token.attribs, 'content' ).v,
 49+ new EndTagTk( 'a' ) );
 50+ //console.warn( JSON.stringify( out, null, 2 ) );
 51+ return { tokens: out };
 52+ }
4353 };
4454
4555
 56+WikiLinkHandler.prototype.renderFile = function ( token, manager, cb, title ) {
 57+ // distinguish media types
 58+ // if image: parse options
 59+ var a = new TagTk( 'a', [ new KV( 'href', title.makeLink() ) ] );
 60+ a.attribs.push( new KV('data-mw-type', 'internal') );
 61+ var img = new SelfclosingTagTk( 'img', [ new KV( 'src',
 62+ title.makeLink() ) ] );
 63+ return { tokens: [ a, img, new EndTagTk( 'a' )] };
 64+};
4665
4766 if (typeof module == "object") {
4867 module.exports.WikiLinkHandler = WikiLinkHandler;

Status & tagging log