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
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
WikiLinkHandler.prototype.onWikiLink = function ( token, frame, cb ) {
|
2012-03-05 12:00:38 +00:00
|
|
|
var env = this.manager.env,
|
2012-06-05 15:26:27 +00:00
|
|
|
href = token.attribs[0].v;
|
2012-03-01 12:47:05 +00:00
|
|
|
var title = this.manager.env.makeTitleFromPrefixedText(
|
2012-03-05 12:00:38 +00:00
|
|
|
env.tokensToString( href )
|
|
|
|
);
|
2012-03-01 12:47:05 +00:00
|
|
|
|
|
|
|
if ( title.ns.isFile() ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( this.renderFile( token, frame, cb, title ) );
|
2012-03-01 12:47:05 +00:00
|
|
|
} else if ( title.ns.isCategory() ) {
|
|
|
|
// TODO: implement
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( { } );
|
2012-03-01 12:47:05 +00:00
|
|
|
} else {
|
|
|
|
// Check if page exists
|
|
|
|
//
|
2012-03-05 12:00:38 +00:00
|
|
|
//console.warn( 'title: ' + JSON.stringify( title ) );
|
2012-06-04 10:28:05 +00:00
|
|
|
var normalizedHref = title.makeLink(),
|
|
|
|
obj = new TagTk( 'a',
|
2012-05-17 12:32:56 +00:00
|
|
|
[
|
2012-06-04 10:28:05 +00:00
|
|
|
new KV( 'href', normalizedHref ),
|
2012-05-22 11:36:06 +00:00
|
|
|
new KV('rel', 'mw:wikiLink')
|
2012-05-17 12:32:56 +00:00
|
|
|
]
|
2012-05-22 11:36:06 +00:00
|
|
|
, token.dataAttribs
|
2012-05-17 12:32:56 +00:00
|
|
|
),
|
2012-06-05 15:26:27 +00:00
|
|
|
content = token.attribs.slice(2);
|
2012-06-04 10:28:05 +00:00
|
|
|
if ( href !== normalizedHref ) {
|
2012-06-19 22:34:57 +00:00
|
|
|
obj.dataAttribs.sHref = env.tokensToString( href );
|
2012-06-04 10:28:05 +00:00
|
|
|
}
|
2012-03-05 12:00:38 +00:00
|
|
|
//console.warn('content: ' + JSON.stringify( content, null, 2 ) );
|
|
|
|
// XXX: handle trail
|
|
|
|
if ( content.length ) {
|
2012-03-05 16:23:00 +00:00
|
|
|
var out = [];
|
2012-03-05 12:00:38 +00:00
|
|
|
for ( var i = 0, l = content.length; i < l ; i++ ) {
|
2012-03-05 15:34:27 +00:00
|
|
|
out = out.concat( content[i].v );
|
2012-03-05 12:00:38 +00:00
|
|
|
if ( i < l - 1 ) {
|
|
|
|
out.push( '|' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content = out;
|
|
|
|
} else {
|
2012-03-06 13:49:37 +00:00
|
|
|
content = [ env.decodeURI( env.tokensToString( href ) ) ];
|
2012-05-22 11:36:06 +00:00
|
|
|
obj.dataAttribs.gc = 1;
|
2012-03-05 12:00:38 +00:00
|
|
|
}
|
2012-06-05 15:26:27 +00:00
|
|
|
|
|
|
|
var tail = token.attribs[1].v;
|
2012-03-05 12:00:38 +00:00
|
|
|
if ( tail ) {
|
2012-06-02 04:29:39 +00:00
|
|
|
obj.dataAttribs.tail = tail;
|
2012-03-05 12:00:38 +00:00
|
|
|
content.push( tail );
|
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
cb ( {
|
2012-06-05 15:26:27 +00:00
|
|
|
tokens: [obj].concat( content, [ new EndTagTk( 'a' ) ] )
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
2012-03-01 12:47:05 +00:00
|
|
|
}
|
2012-02-29 13:56:29 +00:00
|
|
|
};
|
|
|
|
|
2012-03-05 12:00:38 +00:00
|
|
|
WikiLinkHandler.prototype._simpleImageOptions = {
|
|
|
|
// halign
|
|
|
|
'left': 'halign',
|
|
|
|
'right': 'halign',
|
|
|
|
'center': 'halign',
|
2012-04-17 09:02:52 +00:00
|
|
|
'float': 'halign',
|
2012-03-05 12:00:38 +00:00
|
|
|
'none': 'halign',
|
|
|
|
// valign
|
|
|
|
'baseline': 'valign',
|
|
|
|
'sub': 'valign',
|
|
|
|
'super': 'valign',
|
|
|
|
'top': 'valign',
|
|
|
|
'text-top': 'valign',
|
|
|
|
'middle': 'valign',
|
|
|
|
'bottom': 'valign',
|
|
|
|
'text-bottom': 'valign',
|
|
|
|
// format
|
|
|
|
'border': 'format',
|
|
|
|
'frameless': 'format',
|
|
|
|
'frame': 'format',
|
|
|
|
'thumbnail': 'format',
|
2012-05-03 14:15:34 +00:00
|
|
|
'thumb': 'format'
|
2012-03-05 12:00:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WikiLinkHandler.prototype._prefixImageOptions = {
|
|
|
|
'link': 'link',
|
|
|
|
'alt': 'alt',
|
|
|
|
'page': 'page',
|
|
|
|
'thumbnail': 'thumb',
|
2012-05-03 14:15:34 +00:00
|
|
|
'thumb': 'thumb',
|
|
|
|
'upright': 'aspect'
|
2012-03-05 12:00:38 +00:00
|
|
|
};
|
2012-02-29 13:56:29 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
WikiLinkHandler.prototype.renderFile = function ( token, frame, cb, title ) {
|
|
|
|
var env = this.manager.env;
|
2012-03-01 12:47:05 +00:00
|
|
|
// distinguish media types
|
|
|
|
// if image: parse options
|
2012-03-02 13:36:37 +00:00
|
|
|
|
2012-06-05 15:26:27 +00:00
|
|
|
// Slice off the target and tail
|
|
|
|
var content = token.attribs.slice(2);
|
2012-03-02 13:36:37 +00:00
|
|
|
|
2012-03-01 13:51:53 +00:00
|
|
|
|
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-06 13:49:37 +00:00
|
|
|
// TODO: Hackhack.. Move to proper test harness setup!
|
2012-03-06 18:02:35 +00:00
|
|
|
path = [ this.manager.env.wgUploadPath, hash[0],
|
|
|
|
hash.substr(0, 2), title.key ].join('/');
|
2012-03-01 13:51:53 +00:00
|
|
|
|
|
|
|
|
2012-03-05 12:00:38 +00:00
|
|
|
|
2012-03-06 13:49:37 +00:00
|
|
|
// extract options
|
2012-03-05 12:00:38 +00:00
|
|
|
var options = [],
|
2012-03-08 10:03:22 +00:00
|
|
|
oHash = {},
|
2012-04-09 20:58:55 +00:00
|
|
|
caption = [];
|
2012-03-05 12:00:38 +00:00
|
|
|
for( var i = 0, l = content.length; i<l; i++ ) {
|
|
|
|
var oContent = content[i],
|
2012-04-25 14:35:59 +00:00
|
|
|
oText = this.manager.env.tokensToString( oContent.v, true );
|
2012-03-08 10:03:22 +00:00
|
|
|
//console.log( JSON.stringify( oText, null, 2 ) );
|
2012-03-05 12:00:38 +00:00
|
|
|
if ( oText.constructor === String ) {
|
2012-03-05 16:23:00 +00:00
|
|
|
oText = oText.trim();
|
2012-04-17 08:46:20 +00:00
|
|
|
if ( this._simpleImageOptions[ oText.toLowerCase() ] ) {
|
|
|
|
options.push( new KV( this._simpleImageOptions[ oText.toLowerCase() ],
|
2012-03-05 12:00:38 +00:00
|
|
|
oText ) );
|
2012-03-08 10:03:22 +00:00
|
|
|
oHash[ this._simpleImageOptions[ oText ] ] = oText;
|
2012-03-05 12:00:38 +00:00
|
|
|
continue;
|
2012-03-08 10:03:22 +00:00
|
|
|
} else {
|
|
|
|
var maybeSize = oText.match(/^(\d*)(?:x(\d+))?px$/);
|
|
|
|
//console.log( maybeSize );
|
|
|
|
if ( maybeSize !== null ) {
|
|
|
|
var x = maybeSize[1],
|
|
|
|
y = maybeSize[2];
|
|
|
|
if ( x !== undefined ) {
|
|
|
|
options.push(new KV( 'width', x ) );
|
|
|
|
oHash.width = x;
|
|
|
|
}
|
|
|
|
if ( y !== undefined ) {
|
|
|
|
options.push(new KV( 'height', y ) );
|
|
|
|
oHash.height = y;
|
|
|
|
}
|
2012-04-09 20:58:55 +00:00
|
|
|
} else {
|
2012-04-17 08:46:20 +00:00
|
|
|
var bits = oText.split( '=', 2 ),
|
|
|
|
key = this._prefixImageOptions[ bits[0].trim().toLowerCase() ];
|
2012-05-03 14:15:34 +00:00
|
|
|
if ( bits[0] && key) {
|
2012-04-17 08:46:20 +00:00
|
|
|
oHash[key] = bits[1];
|
|
|
|
options.push( new KV( key, bits[1] ) );
|
2012-04-17 09:02:52 +00:00
|
|
|
//console.warn('handle prefix ' + bits );
|
2012-04-17 08:46:20 +00:00
|
|
|
} else {
|
|
|
|
// neither simple nor prefix option, add original
|
|
|
|
// tokens to caption.
|
|
|
|
caption = caption.concat( oContent.v );
|
|
|
|
}
|
2012-03-08 10:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-17 09:02:52 +00:00
|
|
|
} else {
|
|
|
|
caption = caption.concat( oContent.v );
|
2012-03-05 12:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//var contentPos = token.dataAttribs.contentPos;
|
|
|
|
//var optionSource = token.source.substr( contentPos[0], contentPos[1] - contentPos[0] );
|
|
|
|
//console.log( 'optionSource: ' + optionSource );
|
2012-03-02 14:19:33 +00:00
|
|
|
// XXX: The trouble with re-parsing is the need to re-expand templates.
|
2012-03-05 12:00:38 +00:00
|
|
|
// Figure out how often non-image links contain image-like parameters!
|
|
|
|
//var options = this.imageParser.processImageOptions( optionSource );
|
2012-03-02 13:36:37 +00:00
|
|
|
//console.log( JSON.stringify( options, null, 2 ) );
|
2012-03-05 12:00:38 +00:00
|
|
|
// XXX: check if the file exists, generate thumbnail, get size
|
2012-03-01 18:07:20 +00:00
|
|
|
// XXX: render according to mode (inline, thumb, framed etc)
|
2012-04-09 20:58:55 +00:00
|
|
|
|
2012-04-10 10:11:59 +00:00
|
|
|
if ( oHash.format && ( oHash.format === 'thumb' || oHash.format === 'thumbnail') ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
return this.renderThumb( token, this.manager, cb, title, path, caption, oHash, options );
|
2012-04-09 20:58:55 +00:00
|
|
|
} else {
|
|
|
|
// TODO: get /wiki from config!
|
|
|
|
var a = new TagTk( 'a', [ new KV( 'href', title.makeLink() ) ] );
|
|
|
|
a.dataAttribs = token.dataAttribs;
|
|
|
|
|
|
|
|
var width, height;
|
2012-05-03 12:49:55 +00:00
|
|
|
if ( ! oHash.height && ! oHash.width ) {
|
|
|
|
width = '200px';
|
2012-04-09 20:58:55 +00:00
|
|
|
} else {
|
|
|
|
width = oHash.width;
|
|
|
|
height = oHash.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
var img = new SelfclosingTagTk( 'img',
|
|
|
|
[
|
|
|
|
// FIXME!
|
|
|
|
new KV( 'height', height || '' ),
|
|
|
|
new KV( 'width', width || '' ),
|
|
|
|
new KV( 'src', path ),
|
|
|
|
new KV( 'alt', oHash.alt || title.key )
|
|
|
|
] );
|
|
|
|
|
|
|
|
return { tokens: [ a, img, new EndTagTk( 'a' )] };
|
|
|
|
}
|
2012-03-01 12:47:05 +00:00
|
|
|
};
|
2012-02-29 13:56:29 +00:00
|
|
|
|
2012-04-09 20:58:55 +00:00
|
|
|
WikiLinkHandler.prototype.renderThumb = function ( token, manager, cb, title, path, caption, oHash, options ) {
|
|
|
|
// TODO: get /wiki from config!
|
|
|
|
var a = new TagTk( 'a', [ new KV( 'href', title.makeLink() ) ] );
|
|
|
|
a.dataAttribs = token.dataAttribs;
|
|
|
|
a.dataAttribs.optionHash = oHash;
|
|
|
|
a.dataAttribs.optionList = options;
|
|
|
|
|
2012-05-04 09:15:35 +00:00
|
|
|
var width = 165;
|
|
|
|
|
|
|
|
// Handle upright
|
|
|
|
if ( 'aspect' in oHash ) {
|
|
|
|
if ( oHash.aspect > 0 ) {
|
|
|
|
width = width * oHash.aspect;
|
|
|
|
} else {
|
|
|
|
width *= 0.75;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var figurestyle = "width: " + (width + 5) + "px;",
|
2012-04-10 10:11:59 +00:00
|
|
|
figureclass = "thumb tright thumbinner";
|
|
|
|
|
|
|
|
// set horizontal alignment
|
|
|
|
if ( oHash.halign ) {
|
|
|
|
if ( oHash.halign === 'left' ) {
|
|
|
|
figurestyle += ' float: left;';
|
|
|
|
figureclass = "thumb tleft thumbinner";
|
|
|
|
} else if ( oHash.halign === 'center' ) {
|
|
|
|
figureclass = "thumb center thumbinner";
|
|
|
|
} else if ( oHash.halign === 'none' ) {
|
|
|
|
figureclass = "thumb thumbinner";
|
|
|
|
} else {
|
|
|
|
figurestyle += ' float: right;';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
figurestyle += ' float: right;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: set vertical alignment (valign)
|
|
|
|
// XXX: support other formats (border, frameless, frame)
|
|
|
|
// XXX: support prefixes
|
|
|
|
|
2012-04-09 20:58:55 +00:00
|
|
|
var thumb =
|
|
|
|
[
|
|
|
|
new TagTk(
|
|
|
|
'figure',
|
|
|
|
[
|
2012-04-10 10:11:59 +00:00
|
|
|
new KV('class', figureclass),
|
|
|
|
new KV('style', figurestyle),
|
2012-04-09 20:58:55 +00:00
|
|
|
new KV('typeof', 'http://mediawiki.org/rdf/Thumb'),
|
2012-05-17 12:32:56 +00:00
|
|
|
// XXX: define this globally?
|
2012-04-09 20:58:55 +00:00
|
|
|
new KV('prefix', "mw: http://mediawiki.org/rdf/terms/")
|
|
|
|
]
|
|
|
|
),
|
|
|
|
new TagTk(
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
new KV('href', title.makeLink()),
|
|
|
|
new KV('class', 'image')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
new SelfclosingTagTk(
|
|
|
|
'img',
|
|
|
|
[
|
|
|
|
new KV('src', path),
|
2012-05-04 09:15:35 +00:00
|
|
|
new KV('width', width + 'px'),
|
2012-05-03 13:36:42 +00:00
|
|
|
//new KV('height', '160px'),
|
2012-04-09 20:58:55 +00:00
|
|
|
new KV('class', 'thumbimage'),
|
|
|
|
new KV('alt', oHash.alt || title.key ),
|
2012-05-17 12:32:56 +00:00
|
|
|
// Add resource as CURIE- needs global default prefix
|
|
|
|
// definition.
|
|
|
|
new KV('resource', '[:' + title.getPrefixedText() + ']')
|
2012-04-09 20:58:55 +00:00
|
|
|
]
|
|
|
|
),
|
|
|
|
new EndTagTk( 'a' ),
|
|
|
|
new SelfclosingTagTk (
|
|
|
|
'a',
|
|
|
|
[
|
|
|
|
new KV('href', title.makeLink()),
|
|
|
|
new KV('class', 'internal sprite details magnify'),
|
|
|
|
new KV('title', 'View photo details')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
new TagTk( 'figcaption',
|
|
|
|
[
|
|
|
|
new KV('class', 'thumbcaption'),
|
|
|
|
new KV('property', 'mw:thumbcaption')
|
|
|
|
] )
|
|
|
|
].concat(
|
|
|
|
caption,
|
|
|
|
[
|
|
|
|
new EndTagTk( 'figcaption' ),
|
|
|
|
new EndTagTk( 'figure' )
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// set round-trip information on the wrapping figure token
|
|
|
|
thumb[0].dataAttribs = token.dataAttribs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wikia DOM:
|
|
|
|
<figure class="thumb tright thumbinner" style="width:270px;">
|
|
|
|
<a href="Delorean.jpg" class="image" data-image-name="DeLorean.jpg" id="DeLorean-jpg">
|
|
|
|
<img alt="" src="Delorean.jpg" width="268" height="123" class="thumbimage">
|
|
|
|
</a>
|
|
|
|
<a href="File:DeLorean.jpg" class="internal sprite details magnify" title="View photo details"></a>
|
|
|
|
<figcaption class="thumbcaption">
|
|
|
|
A DeLorean DMC-12 from the front with the gull-wing doors open
|
|
|
|
<table><tr><td>test</td></tr></table>
|
|
|
|
Continuation of the caption
|
|
|
|
</figcaption>
|
|
|
|
<div class="picture-attribution">
|
|
|
|
<img src="Christian-Avatar.png" width="16" height="16" class="avatar" alt="Christian">Added by <a href="User:Christian">Christian</a>
|
|
|
|
</div>
|
|
|
|
</figure>
|
|
|
|
*/
|
|
|
|
//console.warn( 'thumbtokens: ' + JSON.stringify( thumb, null, 2 ) );
|
|
|
|
return { tokens: thumb };
|
|
|
|
};
|
2012-03-01 18:07:20 +00:00
|
|
|
|
2012-03-05 15:34:27 +00:00
|
|
|
|
|
|
|
function ExternalLinkHandler( manager, isInclude ) {
|
|
|
|
this.manager = manager;
|
|
|
|
this.manager.addTransform( this.onUrlLink.bind( this ), this.rank, 'tag', 'urllink' );
|
|
|
|
this.manager.addTransform( this.onExtLink.bind( this ),
|
2012-03-05 16:23:00 +00:00
|
|
|
this.rank - 0.001, 'tag', 'extlink' );
|
2012-05-22 11:36:06 +00:00
|
|
|
this.manager.addTransform( this.onEnd.bind( this ),
|
|
|
|
this.rank, 'end' );
|
2012-03-05 15:34:27 +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.
|
|
|
|
ExternalLinkHandler.prototype.imageParser = new PegTokenizer();
|
|
|
|
}
|
2012-05-23 14:25:42 +00:00
|
|
|
this._reset();
|
2012-03-05 15:34:27 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 14:25:42 +00:00
|
|
|
ExternalLinkHandler.prototype._reset = function () {
|
|
|
|
this.linkCount = 1;
|
|
|
|
};
|
|
|
|
|
2012-03-05 15:34:27 +00:00
|
|
|
ExternalLinkHandler.prototype.rank = 1.15;
|
|
|
|
ExternalLinkHandler.prototype._imageExtensions = {
|
|
|
|
'jpg': true,
|
|
|
|
'png': true,
|
2012-04-27 08:44:35 +00:00
|
|
|
'gif': true,
|
|
|
|
'svg': true
|
2012-03-05 15:34:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ExternalLinkHandler.prototype._isImageLink = function ( href ) {
|
|
|
|
var bits = href.split( '.' );
|
|
|
|
return bits.length > 1 &&
|
|
|
|
this._imageExtensions[ bits[bits.length - 1] ] &&
|
2012-03-05 16:23:00 +00:00
|
|
|
href.match( /^https?:\/\// );
|
2012-03-05 15:34:27 +00:00
|
|
|
};
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
ExternalLinkHandler.prototype.onUrlLink = function ( token, frame, cb ) {
|
2012-03-06 18:02:35 +00:00
|
|
|
var env = this.manager.env,
|
|
|
|
href = env.sanitizeURI(
|
|
|
|
env.tokensToString( env.lookupKV( token.attribs, 'href' ).v )
|
2012-03-06 13:49:37 +00:00
|
|
|
);
|
2012-03-05 15:34:27 +00:00
|
|
|
if ( this._isImageLink( href ) ) {
|
2012-05-10 08:04:24 +00:00
|
|
|
cb( { tokens: [ new SelfclosingTagTk( 'img',
|
|
|
|
[
|
2012-04-16 15:09:06 +00:00
|
|
|
new KV( 'src', href ),
|
2012-05-17 12:32:56 +00:00
|
|
|
new KV( 'alt', href.split('/').last() ),
|
2012-05-22 11:36:06 +00:00
|
|
|
new KV('rel', 'mw:externalImage')
|
2012-05-17 12:32:56 +00:00
|
|
|
],
|
2012-05-22 11:36:06 +00:00
|
|
|
{ stx: 'urllink' }
|
2012-05-10 08:04:24 +00:00
|
|
|
)
|
|
|
|
]
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
2012-03-05 15:34:27 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( {
|
2012-03-05 15:34:27 +00:00
|
|
|
tokens: [
|
2012-05-17 12:32:56 +00:00
|
|
|
new TagTk( 'a',
|
|
|
|
[
|
|
|
|
new KV( 'href', href ),
|
2012-05-22 11:36:06 +00:00
|
|
|
new KV('rel', 'mw:extLink')
|
2012-05-17 12:32:56 +00:00
|
|
|
],
|
2012-05-22 11:36:06 +00:00
|
|
|
{ stx: 'urllink' } ),
|
2012-03-05 15:34:27 +00:00
|
|
|
href,
|
|
|
|
new EndTagTk( 'a' )
|
|
|
|
]
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
2012-03-05 15:34:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-09 20:58:55 +00:00
|
|
|
|
2012-03-05 15:34:27 +00:00
|
|
|
// Bracketed external link
|
|
|
|
ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) {
|
2012-03-06 18:02:35 +00:00
|
|
|
var env = this.manager.env,
|
|
|
|
href = env.tokensToString( env.lookupKV( token.attribs, 'href' ).v ),
|
2012-05-22 11:36:06 +00:00
|
|
|
content= env.lookupKV( token.attribs, 'content' ).v;
|
2012-03-06 18:02:35 +00:00
|
|
|
href = env.sanitizeURI( href );
|
2012-03-06 14:32:45 +00:00
|
|
|
//console.warn('extlink href: ' + href );
|
2012-03-05 16:23:00 +00:00
|
|
|
//console.warn( 'content: ' + JSON.stringify( content, null, 2 ) );
|
2012-03-05 15:34:27 +00:00
|
|
|
// validate the href
|
2012-05-23 14:25:42 +00:00
|
|
|
if ( ! content.length ) {
|
|
|
|
content = ['[' + this.linkCount + ']'];
|
|
|
|
this.linkCount++;
|
|
|
|
}
|
2012-03-08 09:00:45 +00:00
|
|
|
if ( this.imageParser.tokenizeURL( href ) ) {
|
2012-03-05 16:23:00 +00:00
|
|
|
if ( content.length === 1 &&
|
|
|
|
content[0].constructor === String &&
|
2012-03-08 09:00:45 +00:00
|
|
|
this.imageParser.tokenizeURL( content[0] ) &&
|
2012-03-05 16:23:00 +00:00
|
|
|
this._isImageLink( content[0] ) )
|
|
|
|
{
|
|
|
|
var src = content[0];
|
|
|
|
content = [ new SelfclosingTagTk( 'img',
|
|
|
|
[
|
2012-04-16 15:09:06 +00:00
|
|
|
new KV( 'src', src ),
|
|
|
|
new KV( 'alt', src.split('/').last() )
|
2012-05-17 12:32:56 +00:00
|
|
|
],
|
|
|
|
{ type: 'extlink' })
|
2012-03-05 16:23:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( {
|
2012-03-05 15:34:27 +00:00
|
|
|
tokens:
|
|
|
|
[
|
|
|
|
|
2012-03-13 12:32:31 +00:00
|
|
|
new TagTk ( 'a',
|
2012-05-17 12:32:56 +00:00
|
|
|
[
|
|
|
|
new KV('href', href),
|
2012-05-22 11:36:06 +00:00
|
|
|
new KV('rel', 'mw:extLink')
|
2012-05-17 12:32:56 +00:00
|
|
|
],
|
2012-03-13 12:32:31 +00:00
|
|
|
token.dataAttribs
|
|
|
|
)
|
2012-03-05 15:34:27 +00:00
|
|
|
].concat( content, [ new EndTagTk( 'a' )])
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
2012-03-05 15:34:27 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( {
|
2012-03-05 18:06:29 +00:00
|
|
|
tokens: ['[', href, ' ' ].concat( content, [']'] )
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
2012-03-05 15:34:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-22 11:36:06 +00:00
|
|
|
ExternalLinkHandler.prototype.onEnd = function ( token, manager, cb ) {
|
2012-05-23 14:25:42 +00:00
|
|
|
this._reset();
|
|
|
|
cb( { tokens: [ token ] } );
|
2012-05-22 11:36:06 +00:00
|
|
|
};
|
|
|
|
|
2012-03-05 15:34:27 +00:00
|
|
|
|
2012-02-29 13:56:29 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.WikiLinkHandler = WikiLinkHandler;
|
2012-03-05 15:34:27 +00:00
|
|
|
module.exports.ExternalLinkHandler = ExternalLinkHandler;
|
2012-02-29 13:56:29 +00:00
|
|
|
}
|