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 |
1 | 59 | + 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 | + |
2 | 6 | var MWParserEnvironment = function(opts) { |
3 | 7 | var options = { |
4 | 8 | tagHooks: {}, |
— | — | @@ -10,6 +14,7 @@ |
11 | 15 | fetchTemplates: false, |
12 | 16 | maxDepth: 40 |
13 | 17 | }; |
| 18 | + // XXX: this should be namespaced |
14 | 19 | $.extend(options, opts); |
15 | 20 | $.extend(this, options); |
16 | 21 | }; |
— | — | @@ -65,7 +70,7 @@ |
66 | 71 | } |
67 | 72 | //console.warn( 'KVtoHash: ' + JSON.stringify( res )); |
68 | 73 | return res; |
69 | | -} |
| 74 | +}; |
70 | 75 | |
71 | 76 | // Does this need separate UI/content inputs? |
72 | 77 | MWParserEnvironment.prototype.formatNum = function( num ) { |
— | — | @@ -98,10 +103,38 @@ |
99 | 104 | } |
100 | 105 | }; |
101 | 106 | |
| 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! |
102 | 127 | MWParserEnvironment.prototype.normalizeTitle = function( name ) { |
103 | 128 | if (typeof name !== 'string') { |
104 | 129 | throw new Error('nooooooooo not a string'); |
105 | 130 | } |
| 131 | + var forceNS; |
| 132 | + if ( name.substr( 0, 1 ) === ':' ) { |
| 133 | + forceNS = ':'; |
| 134 | + name = name.substr(1); |
| 135 | + } else { |
| 136 | + forceNS = ''; |
| 137 | + } |
| 138 | + |
106 | 139 | name = name.trim().replace(/[\s_]+/g, '_'); |
107 | 140 | |
108 | 141 | // Implement int: as alias for MediaWiki: |
— | — | @@ -115,11 +148,18 @@ |
116 | 149 | } |
117 | 150 | |
118 | 151 | 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(':'); |
120 | 160 | //if (name === '') { |
121 | 161 | // throw new Error('Invalid/empty title'); |
122 | 162 | //} |
123 | | - return name; |
| 163 | + return forceNS + name; |
124 | 164 | }; |
125 | 165 | |
126 | 166 | /** |
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js |
— | — | @@ -27,21 +27,40 @@ |
28 | 28 | WikiLinkHandler.prototype.rank = 1.15; // after AttributeExpander |
29 | 29 | |
30 | 30 | 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 | + } |
43 | 53 | }; |
44 | 54 | |
45 | 55 | |
| 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 | +}; |
46 | 65 | |
47 | 66 | if (typeof module == "object") { |
48 | 67 | module.exports.WikiLinkHandler = WikiLinkHandler; |