Page MenuHomePhabricator

parseFileSyntax.js

Authored By
bzimport
Nov 22 2014, 1:11 AM
Size
1 KB
Referenced Files
None
Subscribers
None

parseFileSyntax.js

function parseFileSyntax ( wikitext ) {
var escapedPipe = '\u0001',
result = {},
magicWords = mw.config.get( 'wgWikiEditorMagicWords' ),
match, params, file, i, param;
if ( wikitext.indexOf( escapedPipe ) !== -1 ) {
return false;
}
match = /^(\s*)\[\[(.*)\]\](\s*)$/.exec( wikitext );
if ( !match ) {
return false;
}
result.pre = match[1];
result.post = match[3];
/* escape pipes inside links and templates,
then split the parameters at the remaining pipes */
params = match[2].replace( /\[\[[^\[\]]*\]\]|\{\{[^\{\}]\}\}/g, function ( link ) {
return link.replace( /\|/g, escapedPipe );
} ).split( '|' );
file = new mw.Title( params[0] );
if ( file.getNamespaceId() !== 6 ) {
return false;
}
result.fileName = file.getMainText();
for ( i = 1; i < params.length; i++ ) {
param = params[i];
if ( param === 'right' || param === magicWords.img_right ) {
result.fileFloat = 'right';
} else if ( param === 'left' || param === magicWords.img_left ) {
result.fileFloat = 'left';
} else if ( param === 'none' || param === magicWords.img_none ) {
result.fileFloat = 'none';
} else if ( param === 'center' || param === 'centre' || param === magicWords.img_center ) {
result.fileFloat = 'center';
} else if ( param === 'thumbnail' || param === 'thumb' || param === magicWords.img_thumbnail ) {
result.fileFormat = 'thumbnail';
} else if ( param === 'framed' || param === 'enframed' || param === 'frame' || param === magicWords.img_framed ) {
result.fileFormat = 'framed';
} else if ( param === 'frameless' || param === magicWords.img_frameless ) {
result.fileFormat = 'frameless';
} else if ( /.+px$/.test( param ) ) {
result.fileSize = param.replace( /px$/, '' );
} else if ( param === '' ) { //empty, ignore it
} else if ( i === params.length - 1 ) { //last param -> caption
result.caption = param.replace( new RegExp ( $.escapeRE( escapedPipe ), 'g' ), '|' );
} else { //unknown param
return false;
}
}
return result;
}

File Metadata

Mime Type
text/plain
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9677
Default Alt Text
parseFileSyntax.js (1 KB)

Event Timeline