add highlighting internal link (v 1.7.0)

Change-Id: I9609c62e67ff61650d7e4efed802246a99f0703c
This commit is contained in:
Pavel Astakhov 2014-08-26 13:00:52 +06:00
parent 3627671730
commit 9b4faf39b1
3 changed files with 92 additions and 12 deletions

View file

@ -15,7 +15,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is an extension to MediaWiki and thus not a valid entry point.' ); die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
} }
const EXT_CODEMIRROR_VERSION = '1.6.1'; const EXT_CODEMIRROR_VERSION = '1.7.0';
// Register this extension on Special:Version // Register this extension on Special:Version
$wgExtensionCredits['parserhook'][] = array( $wgExtensionCredits['parserhook'][] = array(
@ -53,5 +53,8 @@ $wgResourceModules['ext.CodeMirror.lib'] = array(
//'lib/codemirror/edit/closebrackets.js', //'lib/codemirror/edit/closebrackets.js',
'mode/mediawiki/mediawiki.js', 'mode/mediawiki/mediawiki.js',
), ),
'styles' => 'lib/codemirror/lib/codemirror.css', 'styles' => array(
'lib/codemirror/lib/codemirror.css',
'mode/mediawiki/mediawiki.css',
),
) + $tpl; ) + $tpl;

View file

@ -0,0 +1,4 @@
.cm-mw-underline {
text-decoration: underline;
}

View file

@ -13,6 +13,7 @@
CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) { CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
var tagName = false; var tagName = false;
var mustEat = true;
function inWikitext( stream, state ) { function inWikitext( stream, state ) {
function chain( parser ) { function chain( parser ) {
@ -28,6 +29,63 @@ CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
} }
switch ( blockType ) { switch ( blockType ) {
case 'Link':
if ( sol ) {
state.ImInBlock.pop(); //FIXME: it is wrong Link
return null;
} else if ( stream.eatWhile( /[^#\s\u00a0\|\]]/ ) ) { //FIXME '{{' brokes Link, sample [[z{{page]]
return 'attribute mw-underline strong';
} else if ( stream.eat( '#' ) ) {
state.ImInBlock.push( 'LinkToSection' );
return 'attribute strong';
} else if ( stream.eat( '|' ) ) {
stream.eatSpace();
state.ImInBlock.pop();
state.ImInBlock.push( 'LinkText' );
return 'tag strong';
} else if ( stream.eatSpace() ) {
if ( /[^#\|\]]/.test( stream.peek() ) ) {
return 'attribute mw-underline strong';
}
return null;
} else if ( stream.eat( ']' ) ) {
if ( stream.eat( ']' ) ) {
state.ImInBlock.pop();
if ( !stream.eatSpace() ) {
state.ImInBlock.push( 'LinkTrail' );
}
return 'tag bracket';
}
}
break;
case 'LinkToSection':
state.ImInBlock.pop();
if ( sol ) {
state.ImInBlock.pop(); //FIXME: it is wrong Link
return null;
}
stream.eatWhile( /[^\|\]]/ ); //FIXME '{{' brokes Link, sample [[z{{page]]
return 'attribute';
case 'LinkText':
stream.eatSpace();
if ( stream.match( /[\s\u00a0]*\]\]/ ) ) {
state.ImInBlock.pop();
if ( !stream.eatSpace() ) {
state.ImInBlock.push( 'LinkTrail' );
}
return 'tag bracket';
}
mustEat = false;
stream.eatWhile( /[^\]\s\u00a0]/ );
style.push( 'mw-underline' );
break;
case 'LinkTrail': // FIXME with Language::linkTrail()
state.ImInBlock.pop();
if ( !stream.sol && stream.eatWhile( /[^\s\u00a0>\}\[\]<\{\']/ ) ) { // &
mustEat = false;
style.push( 'mw-underline' );
}
break;
case 'TemplatePageName': case 'TemplatePageName':
state.ImInBlock.pop(); state.ImInBlock.pop();
if ( stream.eat( '#' ) ) { if ( stream.eat( '#' ) ) {
@ -36,14 +94,14 @@ CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
} else { } else {
if ( stream.eatWhile( /[^\s\u00a0\}\|<\{\&]/ ) ) { if ( stream.eatWhile( /[^\s\u00a0\}\|<\{\&]/ ) ) {
state.ImInBlock.push( 'TemplatePageNameContinue' ); state.ImInBlock.push( 'TemplatePageNameContinue' );
return 'link'; return 'attribute mw-underline';
} }
} }
break; break;
case 'TemplatePageNameContinue': case 'TemplatePageNameContinue':
stream.eatSpace(); stream.eatSpace();
if ( stream.match( /[\s\u00a0]*[^\s\u00a0\}\|<\{\&]/ ) ) { if ( stream.match( /[\s\u00a0]*[^\s\u00a0\}\|<\{\&]/ ) ) {
return 'link'; return 'attribute mw-underline';
} }
if ( stream.eat( '|' ) ) { if ( stream.eat( '|' ) ) {
state.ImInBlock.pop(); state.ImInBlock.pop();
@ -162,15 +220,15 @@ CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
return null; return null;
} }
} }
if ( state.isBold ) {
style.push( 'strong' );
}
if ( state.isItalic ) {
style.push( 'em' );
}
} }
var ch = stream.next(); var ch = null;
if ( mustEat ) {
ch = stream.next();
} else {
mustEat = true;
}
if ( ch === '&' ) { if ( ch === '&' ) {
// this code was copied from mode/xml/xml.js // this code was copied from mode/xml/xml.js
var ok; var ok;
@ -196,6 +254,15 @@ CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
return 'tag bracket'; return 'tag bracket';
} }
break; break;
case '[':
if ( stream.eat( '[' ) ) { // Link Example: [[ Foo | Bar ]]
stream.eatSpace();
if ( /[^\]\|\[\{]/.test( stream.peek() ) ) {
state.ImInBlock.push( 'Link' );
return 'tag bracket';
}
}
break;
case '<': case '<':
if ( stream.match( '!--' ) ) { if ( stream.match( '!--' ) ) {
return chain( inBlock( 'comment', '-->' ) ); return chain( inBlock( 'comment', '-->' ) );
@ -215,7 +282,13 @@ CodeMirror.defineMode('mediawiki', function( /*config, parserConfig*/ ) {
} }
break; break;
} }
stream.eatWhile( /[^>\}<\{\'\&]/ ); stream.eatWhile( /[^\s\u00a0>\}\[\]<\{\'\&]/ );
if ( state.isBold ) {
style.push( 'strong' );
}
if ( state.isItalic ) {
style.push( 'em' );
}
if ( !state.allowWikiformatting ) { if ( !state.allowWikiformatting ) {
style.push( 'qualifier' ); style.push( 'qualifier' );
} }