2017-03-21 10:13:18 +00:00
|
|
|
/* eslint-disable no-use-before-define */
|
2015-11-24 16:03:00 +00:00
|
|
|
( function ( CodeMirror ) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function eatMnemonic( stream, style, mnemonicStyle ) {
|
|
|
|
var ok;
|
|
|
|
if ( stream.eat( '#' ) ) {
|
|
|
|
if ( stream.eat( 'x' ) ) {
|
|
|
|
ok = stream.eatWhile( /[a-fA-F\d]/ ) && stream.eat( ';' );
|
|
|
|
} else {
|
|
|
|
ok = stream.eatWhile( /[\d]/ ) && stream.eat( ';' );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
} else {
|
2015-11-24 16:03:00 +00:00
|
|
|
ok = stream.eatWhile( /[\w\.\-:]/ ) && stream.eat( ';' );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( ok ) {
|
|
|
|
mnemonicStyle += ' mw-mnemonic';
|
|
|
|
return mnemonicStyle;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2017-03-21 10:13:18 +00:00
|
|
|
CodeMirror.defineMode( 'mediawiki', function ( config /* , parserConfig */ ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
var urlProtocols = new RegExp( config.mwextUrlProtocols, 'i' ),
|
|
|
|
permittedHtmlTags = { b: true, bdi: true, del: true, i: true, ins: true,
|
|
|
|
u: true, font: true, big: true, small: true, sub: true, sup: true,
|
|
|
|
h1: true, h2: true, h3: true, h4: true, h5: true, h6: true, cite: true,
|
|
|
|
code: true, em: true, s: true, strike: true, strong: true, tt: true,
|
|
|
|
'var': true, div: true, center: true, blockquote: true, ol: true, ul: true,
|
|
|
|
dl: true, table: true, caption: true, pre: true, ruby: true, rb: true,
|
|
|
|
rp: true, rt: true, rtc: true, p: true, span: true, abbr: true, dfn: true,
|
|
|
|
kbd: true, samp: true, data: true, time: true, mark: true, br: true,
|
|
|
|
wbr: true, hr: true, li: true, dt: true, dd: true, td: true, th: true,
|
|
|
|
tr: true, noinclude: true, includeonly: true, onlyinclude: true },
|
|
|
|
isBold, isItalic, firstsingleletterword, firstmultiletterword, firstspace, mBold, mItalic, mTokens = [],
|
|
|
|
mStyle;
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function makeStyle( style, state, endGround ) {
|
|
|
|
if ( isBold ) {
|
|
|
|
style += ' strong';
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( isItalic ) {
|
|
|
|
style += ' em';
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( style, state, endGround );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function makeLocalStyle( style, state, endGround ) {
|
|
|
|
var ground = '';
|
|
|
|
switch ( state.nTemplate ) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ground += '-template';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ground += '-template2';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ground += '-template3';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch ( state.nExt ) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ground += '-ext';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ground += '-ext2';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ground += '-ext3';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( state.nLink > 0 ) {
|
|
|
|
ground += '-link';
|
|
|
|
}
|
|
|
|
if ( ground !== '' ) {
|
|
|
|
style = 'mw' + ground + '-ground ' + style;
|
|
|
|
}
|
|
|
|
if ( endGround ) {
|
|
|
|
state[ endGround ]--;
|
|
|
|
}
|
|
|
|
return style;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatBlock( style, terminator ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
while ( !stream.eol() ) {
|
|
|
|
if ( stream.match( terminator ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stream.next();
|
|
|
|
}
|
|
|
|
return makeLocalStyle( style, state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
|
|
|
|
function eatEnd( style ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
stream.skipToEnd();
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( style, state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
|
|
|
|
function eatChar( char, style ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
if ( stream.eat( char ) ) {
|
|
|
|
return makeLocalStyle( style, state );
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'error', state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatSectionHeader( count ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( stream.match( /[^&<\[\{~]+/ ) ) {
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
stream.backUp( count );
|
|
|
|
state.tokenize = eatEnd( 'mw-section-header' );
|
|
|
|
}
|
|
|
|
return null; // style is null
|
|
|
|
}
|
|
|
|
return eatWikiText( '', '' )( stream, state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inVariable( stream, state ) {
|
|
|
|
if ( stream.match( /[^\{\}\|]+/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-templatevariable-name', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eat( '|' ) ) {
|
|
|
|
state.tokenize = inVariableDefault;
|
|
|
|
return makeLocalStyle( 'mw-templatevariable-delimiter', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( '}}}' ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-templatevariable-bracket', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( '{{{' ) ) {
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
return makeLocalStyle( 'mw-templatevariable-bracket', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
stream.next();
|
|
|
|
return makeLocalStyle( 'mw-templatevariable-name', state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inVariableDefault( stream, state ) {
|
|
|
|
if ( stream.match( /[^\{\}\[<\&~]+/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-templatevariable', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '}}}' ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-templatevariable-bracket', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-templatevariable', '' )( stream, state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inParserFunctionName( stream, state ) {
|
|
|
|
if ( stream.match( /#?[^\:\}\{~]+/ ) ) { // FIXME: {{#name}} and {{uc}} are wrong, must have ':'
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-name', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eat( ':' ) ) {
|
|
|
|
state.tokenize = inParserFunctionArguments;
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-delimiter', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '}}' ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-parserfunction-bracket', state, 'nExt' );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-parserfunction', '' )( stream, state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
|
|
|
|
function inParserFunctionArguments( stream, state ) {
|
|
|
|
if ( stream.match( /[^\|\}\{\[<\&~]+/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-parserfunction', state );
|
|
|
|
} else if ( stream.eat( '|' ) ) {
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-delimiter', state );
|
|
|
|
} else if ( stream.match( '}}' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-bracket', state, 'nExt' );
|
2015-08-14 07:22:47 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-parserfunction', '' )( stream, state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatTemplatePageName( haveAte ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( stream.match( /[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
|
|
|
|
state.tokenize = eatTemplateArgument( true );
|
|
|
|
return makeLocalStyle( 'mw-template-delimiter', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( /[\s\u00a0]*\}\}/ ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-template-bracket', state, 'nTemplate' );
|
|
|
|
}
|
|
|
|
if ( haveAte && stream.sol() ) {
|
|
|
|
// @todo error message
|
|
|
|
state.nTemplate--;
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( stream.match( /[\s\u00a0]*[^\s\u00a0\|\}<\{\&~]+/ ) ) {
|
|
|
|
state.tokenize = eatTemplatePageName( true );
|
|
|
|
return makeLocalStyle( 'mw-template-name mw-pagename', state );
|
|
|
|
} else if ( stream.eatSpace() ) {
|
|
|
|
if ( stream.eol() === true ) {
|
|
|
|
return makeLocalStyle( 'mw-template-name', state );
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'mw-template-name mw-pagename', state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-template-name mw-pagename', 'mw-template-name-mnemonic mw-pagename' )( stream, state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatTemplateArgument( expectArgName ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( expectArgName && stream.eatWhile( /[^=\|\}\{\[<\&~]/ ) ) {
|
|
|
|
if ( stream.eat( '=' ) ) {
|
|
|
|
state.tokenize = eatTemplateArgument( false );
|
|
|
|
return makeLocalStyle( 'mw-template-argument-name', state );
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'mw-template', state );
|
|
|
|
} else if ( stream.eatWhile( /[^\|\}\{\[<\&~]/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-template', state );
|
|
|
|
} else if ( stream.eat( '|' ) ) {
|
|
|
|
state.tokenize = eatTemplateArgument( true );
|
|
|
|
return makeLocalStyle( 'mw-template-delimiter', state );
|
|
|
|
} else if ( stream.match( '}}' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-template-bracket', state, 'nTemplate' );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-template', '' )( stream, state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatExternalLinkProtocol( chars ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
while ( chars > 0 ) {
|
|
|
|
chars--;
|
|
|
|
stream.next();
|
|
|
|
}
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
state.nLink--;
|
|
|
|
// @todo error message
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
} else {
|
|
|
|
state.tokenize = inExternalLink;
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'mw-extlink-protocol', state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inExternalLink( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
state.nLink--;
|
|
|
|
// @todo error message
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[\s\u00a0]*\]/ ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-extlink-bracket', state, 'nLink' );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eatSpace() ) {
|
|
|
|
state.tokenize = inExternalLinkText;
|
|
|
|
return makeStyle( '', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[^\s\u00a0\]\{\&~']+/ ) || stream.eatSpace() ) {
|
|
|
|
if ( stream.peek() === '\'' ) {
|
|
|
|
if ( stream.match( '\'\'', false ) ) {
|
|
|
|
state.tokenize = inExternalLinkText;
|
|
|
|
} else {
|
|
|
|
stream.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return makeStyle( 'mw-extlink', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-extlink', '' )( stream, state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inExternalLinkText( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
state.nLink--;
|
2014-10-23 06:09:10 +00:00
|
|
|
// @todo error message
|
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eat( ']' ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-extlink-bracket', state, 'nLink' );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[^'\]\{\&~]+/ ) ) {
|
|
|
|
return makeStyle( 'mw-extlink-text', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-extlink-text', '' )( stream, state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inLink( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
state.nLink--;
|
|
|
|
// @todo error message
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[\s\u00a0]*#[\s\u00a0]*/ ) ) {
|
|
|
|
state.tokenize = inLinkToSection;
|
|
|
|
return makeLocalStyle( 'mw-link', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( /[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
|
|
|
|
state.tokenize = eatLinkText();
|
|
|
|
return makeLocalStyle( 'mw-link-delimiter', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( /[\s\u00a0]*\]\]/ ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
|
|
|
|
// if ( !stream.eatSpace() ) {
|
|
|
|
// state.ImInBlock.push( 'LinkTrail' );
|
|
|
|
// }
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[\s\u00a0]*[^\s\u00a0#\|\]\&~\{]+/ ) || stream.eatSpace() ) { // FIXME '{{' brokes Link, sample [[z{{page]]
|
|
|
|
return makeStyle( 'mw-link-pagename mw-pagename', state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-link-pagename mw-pagename', 'mw-pagename' )( stream, state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function inLinkToSection( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
// @todo error message
|
|
|
|
state.nLink--;
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[^\|\]\&~\{\}]+/ ) ) { // FIXME '{{' brokes Link, sample [[z{{page]]
|
|
|
|
return makeLocalStyle( 'mw-link-tosection', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '|' ) ) {
|
|
|
|
state.tokenize = eatLinkText();
|
|
|
|
return makeLocalStyle( 'mw-link-delimiter', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( ']]' ) ) {
|
2014-10-23 06:09:10 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
|
|
|
|
// if ( !stream.eatSpace() ) {
|
|
|
|
// state.ImInBlock.push( 'LinkTrail' );
|
|
|
|
// }
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( 'mw-link-tosection', '' )( stream, state );
|
|
|
|
}
|
|
|
|
|
|
|
|
function eatLinkText() {
|
|
|
|
var linkIsBold, linkIsItalic;
|
|
|
|
return function ( stream, state ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
var tmpstyle;
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( ']]' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-link-bracket', state, 'nLink' );
|
|
|
|
}
|
|
|
|
if ( stream.match( '\'\'\'' ) ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
linkIsBold = !linkIsBold;
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-link-text mw-apostrophes', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '\'\'' ) ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
linkIsItalic = !linkIsItalic;
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-link-text mw-apostrophes', state );
|
|
|
|
}
|
2017-03-21 10:13:18 +00:00
|
|
|
tmpstyle = 'mw-link-text';
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( linkIsBold ) {
|
|
|
|
tmpstyle += ' strong';
|
|
|
|
}
|
|
|
|
if ( linkIsItalic ) {
|
|
|
|
tmpstyle += ' em';
|
|
|
|
}
|
|
|
|
if ( stream.match( /[^'\]\{\&~]+/ ) ) {
|
|
|
|
return makeStyle( tmpstyle, state );
|
|
|
|
}
|
|
|
|
return eatWikiText( tmpstyle, '' )( stream, state );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function eatTagName( chars, isCloseTag, isHtmlTag ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
var name = '';
|
|
|
|
while ( chars > 0 ) {
|
|
|
|
chars--;
|
|
|
|
name = name + stream.next();
|
|
|
|
}
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
// @todo error message
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( ( isHtmlTag ? 'mw-htmltag-name' : 'mw-exttag-name' ), state );
|
|
|
|
}
|
|
|
|
stream.eatSpace();
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
// @todo error message
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( ( isHtmlTag ? 'mw-htmltag-name' : 'mw-exttag-name' ), state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( isHtmlTag ) {
|
|
|
|
if ( isCloseTag ) {
|
|
|
|
state.tokenize = eatChar( '>', 'mw-htmltag-bracket' );
|
|
|
|
} else {
|
|
|
|
state.tokenize = eatHtmlTagAttribute( name );
|
2014-09-03 05:04:56 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-htmltag-name', state );
|
|
|
|
} // it is the extension tag
|
|
|
|
if ( isCloseTag ) {
|
|
|
|
state.tokenize = eatChar( '>', 'mw-exttag-bracket' );
|
|
|
|
} else {
|
|
|
|
state.tokenize = eatExtTagAttribute( name );
|
2014-08-25 10:56:52 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-name', state );
|
|
|
|
};
|
|
|
|
}
|
2015-08-19 15:10:31 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatHtmlTagAttribute( name ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( stream.match( /[^>\/<\{\&~]+/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-htmltag-attribute', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '>' ) ) {
|
|
|
|
state.InHtmlTag.push( name );
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-htmltag-bracket', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '/>' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-htmltag-bracket', state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-htmltag-attribute', '' )( stream, state );
|
|
|
|
};
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatExtTagAttribute( name ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( stream.match( /[^>\/<\{\&~]+/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-exttag-attribute', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '>' ) ) {
|
|
|
|
state.extName = name;
|
2017-03-21 10:13:18 +00:00
|
|
|
if ( name in config.mwextModes.tag ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.extMode = CodeMirror.getMode( config, config.mwextModes.tag[ name ] );
|
|
|
|
state.extState = CodeMirror.startState( state.extMode );
|
|
|
|
}
|
|
|
|
state.tokenize = eatExtTagArea( name );
|
|
|
|
return makeLocalStyle( 'mw-exttag-bracket', state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '/>' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-exttag-bracket', state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-exttag-attribute', '' )( stream, state );
|
|
|
|
};
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatExtTagArea( name ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
var origString = false,
|
|
|
|
from = stream.pos,
|
|
|
|
to,
|
|
|
|
pattern = new RegExp( '</' + name + '\\s*>' ),
|
|
|
|
m = pattern.exec( from ? stream.string.slice( from ) : stream.string );
|
|
|
|
|
|
|
|
if ( m ) {
|
|
|
|
if ( m.index === 0 ) {
|
|
|
|
state.tokenize = eatExtCloseTag( name );
|
|
|
|
state.extName = false;
|
|
|
|
if ( state.extMode !== false ) {
|
|
|
|
state.extMode = false;
|
|
|
|
state.extState = false;
|
|
|
|
}
|
|
|
|
return state.tokenize( stream, state );
|
|
|
|
}
|
|
|
|
to = m.index + from;
|
|
|
|
origString = stream.string;
|
|
|
|
stream.string = origString.slice( 0, to );
|
2014-08-25 10:56:52 +00:00
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatExtTokens( origString );
|
|
|
|
return state.tokenize( stream, state );
|
|
|
|
};
|
|
|
|
}
|
2015-08-10 07:48:48 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatExtCloseTag( name ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
stream.next(); // eat <
|
|
|
|
stream.next(); // eat /
|
|
|
|
state.tokenize = eatTagName( name.length, true, false );
|
|
|
|
return makeLocalStyle( 'mw-exttag-bracket', state );
|
|
|
|
};
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatExtTokens( origString ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
var ret;
|
|
|
|
if ( state.extMode === false ) {
|
|
|
|
ret = ( origString === false && stream.sol() ? 'line-cm-mw-exttag' : 'mw-exttag' );
|
|
|
|
stream.skipToEnd();
|
|
|
|
} else {
|
|
|
|
ret = ( origString === false && stream.sol() ? 'line-cm-mw-tag-' : 'mw-tag-' ) + state.extName;
|
|
|
|
ret += ' ' + state.extMode.token( stream, state.extState, origString === false );
|
|
|
|
}
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
if ( origString !== false ) {
|
|
|
|
stream.string = origString;
|
|
|
|
}
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
}
|
|
|
|
return makeLocalStyle( ret, state );
|
|
|
|
};
|
2015-08-10 04:34:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatStartTable( stream, state ) {
|
|
|
|
stream.match( '{|' );
|
2015-08-10 05:07:55 +00:00
|
|
|
stream.eatSpace();
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = inTableDefinition;
|
|
|
|
return 'mw-table-bracket';
|
|
|
|
}
|
|
|
|
|
|
|
|
function inTableDefinition( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
state.tokenize = inTable;
|
|
|
|
return inTable( stream, state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-table-definition', '' )( stream, state );
|
|
|
|
}
|
|
|
|
|
|
|
|
function inTableCaption( stream, state ) {
|
|
|
|
if ( stream.sol() && stream.match( /[\s\u00a0]*[\|!]/, false ) ) {
|
|
|
|
state.tokenize = inTable;
|
|
|
|
return inTable( stream, state );
|
|
|
|
}
|
|
|
|
return eatWikiText( 'mw-table-caption', '' )( stream, state );
|
|
|
|
}
|
|
|
|
|
|
|
|
function inTable( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
stream.eatSpace();
|
|
|
|
if ( stream.eat( '|' ) ) {
|
|
|
|
if ( stream.eat( '-' ) ) {
|
|
|
|
stream.eatSpace();
|
|
|
|
state.tokenize = inTableDefinition;
|
|
|
|
return makeLocalStyle( 'mw-table-delimiter', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '+' ) ) {
|
|
|
|
stream.eatSpace();
|
|
|
|
state.tokenize = inTableCaption;
|
|
|
|
return makeLocalStyle( 'mw-table-delimiter', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '}' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-table-bracket', state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
stream.eatSpace();
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = eatTableRow( true, false );
|
2014-10-23 06:09:10 +00:00
|
|
|
return makeLocalStyle( 'mw-table-delimiter', state );
|
2014-08-25 10:56:52 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eat( '!' ) ) {
|
2015-08-10 04:34:11 +00:00
|
|
|
stream.eatSpace();
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = eatTableRow( true, true );
|
2015-08-10 04:34:11 +00:00
|
|
|
return makeLocalStyle( 'mw-table-delimiter', state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( '', '' )( stream, state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatTableRow( isStart, isHead ) {
|
|
|
|
return function ( stream, state ) {
|
|
|
|
if ( stream.sol() ) {
|
|
|
|
if ( stream.match( /[\s\u00a0]*[\|!]/, false ) ) {
|
|
|
|
state.tokenize = inTable;
|
|
|
|
return inTable( stream, state );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( stream.match( /[^'\|\{\[<\&~]+/ ) ) {
|
|
|
|
return makeStyle( ( isHead ? 'strong' : '' ), state );
|
|
|
|
}
|
|
|
|
if ( stream.match( '||' ) || isHead && stream.match( '!!' ) || ( isStart && stream.eat( '|' ) ) ) {
|
|
|
|
isBold = false;
|
|
|
|
isItalic = false;
|
|
|
|
if ( isStart ) {
|
|
|
|
state.tokenize = eatTableRow( false, isHead );
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'mw-table-delimiter', state );
|
2014-09-10 06:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return eatWikiText( ( isHead ? 'strong' : '' ), ( isHead ? 'strong' : '' ) )( stream, state );
|
|
|
|
};
|
|
|
|
}
|
2014-08-21 14:36:09 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatFreeExternalLinkProtocol( stream, state ) {
|
|
|
|
stream.match( urlProtocols );
|
|
|
|
state.tokenize = eatFreeExternalLink;
|
|
|
|
return makeLocalStyle( 'mw-free-extlink-protocol', state );
|
|
|
|
}
|
2015-08-10 17:14:18 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatFreeExternalLink( stream, state ) {
|
|
|
|
if ( stream.eol() ) {
|
|
|
|
// @todo error message
|
|
|
|
} else if ( stream.match( /[^\s\u00a0\{\[\]<>~\)\.,']*/ ) ) {
|
|
|
|
if ( stream.peek() === '~' ) {
|
|
|
|
if ( !stream.match( /~{3,}/, false ) ) {
|
|
|
|
stream.match( /~*/ );
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
|
|
|
} else if ( stream.peek() === '{' ) {
|
|
|
|
if ( !stream.match( /\{\{/, false ) ) {
|
|
|
|
stream.next();
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
|
|
|
} else if ( stream.peek() === '\'' ) {
|
|
|
|
if ( !stream.match( '\'\'', false ) ) {
|
|
|
|
stream.next();
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
|
|
|
} else if ( stream.match( /[\)\.,]+(?=[^\s\u00a0\{\[\]<>~\)\.,])/ ) ) {
|
2015-08-10 17:30:05 +00:00
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
2015-08-10 17:14:18 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
2015-08-10 17:14:18 +00:00
|
|
|
}
|
2014-08-26 07:00:52 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatWikiText( style, mnemonicStyle ) {
|
|
|
|
return function ( stream, state ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
var ch, tmp, mt, name, isCloseTag, tagname,
|
|
|
|
sol = stream.sol();
|
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function chain( parser ) {
|
2015-08-10 17:14:18 +00:00
|
|
|
state.stack.push( state.tokenize );
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = parser;
|
|
|
|
return parser( stream, state );
|
2015-08-10 17:14:18 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
|
|
|
|
if ( sol ) {
|
|
|
|
if ( stream.match( urlProtocols ) ) { // highlight free external links, bug T108448
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatFreeExternalLink;
|
|
|
|
return makeLocalStyle( 'mw-free-extlink-protocol', state );
|
|
|
|
}
|
|
|
|
ch = stream.next();
|
|
|
|
switch ( ch ) {
|
|
|
|
case '-':
|
|
|
|
if ( stream.match( /----*/ ) ) {
|
|
|
|
return 'mw-hr';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '=':
|
2017-03-21 10:13:18 +00:00
|
|
|
tmp = stream.match( /(={0,5})(.+?(=\1\s*))$/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( tmp ) { // Title
|
|
|
|
stream.backUp( tmp[ 2 ].length );
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatSectionHeader( tmp[ 3 ].length );
|
|
|
|
return 'mw-section-header line-cm-mw-section-' + ( tmp[ 1 ].length + 1 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
case '#':
|
|
|
|
if ( stream.match( /[\*#]*:*/ ) ) {
|
|
|
|
return 'mw-list';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
if ( stream.match( /:*{\|/, false ) ) { // Highlight indented tables :{|, bug T108454
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatStartTable;
|
|
|
|
}
|
|
|
|
if ( stream.match( /:*[\*#]*/ ) ) {
|
|
|
|
return 'mw-indenting';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
if ( stream.match( /[\s\u00a0]*:*{\|/, false ) ) { // Leading spaces is the correct syntax for a table, bug T108454
|
|
|
|
stream.eatSpace();
|
|
|
|
if ( stream.match( /:+/ ) ) { // ::{|
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatStartTable;
|
|
|
|
return 'mw-indenting';
|
|
|
|
}
|
|
|
|
stream.eat( '{' );
|
|
|
|
} else {
|
|
|
|
return 'mw-skipformatting';
|
|
|
|
}
|
|
|
|
// break is not necessary here
|
2017-03-21 10:13:18 +00:00
|
|
|
// falls through
|
2015-11-24 16:03:00 +00:00
|
|
|
case '{':
|
|
|
|
if ( stream.eat( '|' ) ) {
|
|
|
|
stream.eatSpace();
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = inTableDefinition;
|
|
|
|
return 'mw-table-bracket';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ch = stream.next();
|
|
|
|
}
|
|
|
|
|
2014-10-23 06:09:10 +00:00
|
|
|
switch ( ch ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
case '&':
|
|
|
|
return makeStyle( eatMnemonic( stream, style, mnemonicStyle ), state );
|
|
|
|
case '\'':
|
|
|
|
if ( stream.match( /'*(?=''''')/ ) || stream.match( /'''(?!')/, false ) ) { // skip the irrelevant apostrophes ( >5 or =4 )
|
|
|
|
break;
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( '\'\'' ) ) { // bold
|
|
|
|
if ( !( firstsingleletterword || stream.match( '\'\'', false ) ) ) {
|
|
|
|
prepareItalicForCorrection( stream );
|
|
|
|
}
|
2017-03-21 10:13:18 +00:00
|
|
|
isBold = !isBold;
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-apostrophes-bold', state );
|
|
|
|
} else if ( stream.eat( '\'' ) ) { // italic
|
2017-03-21 10:13:18 +00:00
|
|
|
isItalic = !isItalic;
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-apostrophes-italic', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-11-24 16:03:00 +00:00
|
|
|
case '[':
|
|
|
|
if ( stream.eat( '[' ) ) { // Link Example: [[ Foo | Bar ]]
|
2015-08-10 07:48:48 +00:00
|
|
|
stream.eatSpace();
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( /[^\]\|\[]/.test( stream.peek() ) ) {
|
|
|
|
state.nLink++;
|
2015-08-10 07:48:48 +00:00
|
|
|
state.stack.push( state.tokenize );
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = inLink;
|
|
|
|
return makeLocalStyle( 'mw-link-bracket', state );
|
2015-08-10 07:48:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-21 10:13:18 +00:00
|
|
|
mt = stream.match( urlProtocols );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( mt ) {
|
|
|
|
state.nLink++;
|
|
|
|
stream.backUp( mt[ 0 ].length );
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatExternalLinkProtocol( mt[ 0 ].length );
|
|
|
|
return makeLocalStyle( 'mw-extlink-bracket', state );
|
|
|
|
}
|
2015-08-10 07:48:48 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
break;
|
2014-10-23 06:09:10 +00:00
|
|
|
case '{':
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( !stream.match( '{{{{', false ) && stream.match( '{{' ) ) { // Template parameter (skip parameters inside a template transclusion, Bug: T108450)
|
2014-10-23 06:09:10 +00:00
|
|
|
stream.eatSpace();
|
|
|
|
state.stack.push( state.tokenize );
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = inVariable;
|
|
|
|
return makeLocalStyle( 'mw-templatevariable-bracket', state );
|
|
|
|
} else if ( stream.match( /\{[\s\u00a0]*/ ) ) {
|
|
|
|
if ( stream.peek() === '#' ) { // Parser function
|
2014-10-23 06:09:10 +00:00
|
|
|
state.nExt++;
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = inParserFunctionName;
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-bracket', state );
|
2014-08-29 04:05:50 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
// Check for parser function without '#'
|
2017-03-21 10:13:18 +00:00
|
|
|
name = stream.match( /([^\s\u00a0\}\[\]<\{\'\|\&\:]+)(\:|[\s\u00a0]*)(\}\}?)?(.)?/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( name ) {
|
|
|
|
stream.backUp( name[ 0 ].length );
|
|
|
|
if ( ( name[ 2 ] === ':' || name[ 4 ] === undefined || name[ 3 ] === '}}' ) && ( name[ 1 ].toLowerCase() in config.mwextFunctionSynonyms[ 0 ] || name[ 1 ] in config.mwextFunctionSynonyms[ 1 ] ) ) {
|
|
|
|
state.nExt++;
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = inParserFunctionName;
|
|
|
|
return makeLocalStyle( 'mw-parserfunction-bracket', state );
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
// Template
|
|
|
|
state.nTemplate++;
|
2014-10-23 06:09:10 +00:00
|
|
|
state.stack.push( state.tokenize );
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = eatTemplatePageName( false );
|
|
|
|
return makeLocalStyle( 'mw-template-bracket', state );
|
2014-09-09 14:45:04 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case '<':
|
2017-03-21 10:13:18 +00:00
|
|
|
isCloseTag = !!stream.eat( '/' );
|
|
|
|
tagname = stream.match( /[^>\/\s\u00a0\.\*\,\[\]\{\}\$\^\+\?\|\/\\'`~<=!@#%&\(\)-]+/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( '!--' ) ) { // coment
|
|
|
|
return chain( eatBlock( 'mw-comment', '-->' ) );
|
|
|
|
}
|
|
|
|
if ( tagname ) {
|
|
|
|
tagname = tagname[ 0 ].toLowerCase();
|
|
|
|
if ( tagname in config.mwextTags ) { // Parser function
|
|
|
|
if ( isCloseTag === true ) {
|
|
|
|
// @todo message
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
stream.backUp( tagname.length );
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatTagName( tagname.length, isCloseTag, false );
|
|
|
|
return makeLocalStyle( 'mw-exttag-bracket', state );
|
|
|
|
}
|
|
|
|
if ( tagname in permittedHtmlTags ) { // Html tag
|
|
|
|
if ( isCloseTag === true && tagname !== state.InHtmlTag.pop() ) {
|
|
|
|
// @todo message
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
stream.backUp( tagname.length );
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatTagName( tagname.length, isCloseTag, true );
|
|
|
|
return makeLocalStyle( 'mw-htmltag-bracket', state );
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
stream.backUp( tagname.length );
|
2014-08-25 16:59:36 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
if ( stream.match( /~{2,4}/ ) ) {
|
|
|
|
return 'mw-signature';
|
2015-08-10 17:14:18 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( /[\s\u00a0]/.test( ch ) ) {
|
|
|
|
stream.eatSpace();
|
|
|
|
if ( stream.match( urlProtocols, false ) ) { // highlight free external links, bug T108448
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatFreeExternalLinkProtocol;
|
|
|
|
return makeStyle( style, state );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stream.match( /[^\s\u00a0_>\}\[\]<\{\'\|\&\:~]+/ );
|
|
|
|
return makeStyle( style, state );
|
|
|
|
};
|
|
|
|
}
|
2014-08-22 07:21:32 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
/**
|
|
|
|
* Remembers position and status for rollbacking.
|
|
|
|
* It needed for change bold to italic with apostrophe before it if required
|
|
|
|
*
|
|
|
|
* see https://phabricator.wikimedia.org/T108455
|
|
|
|
*
|
|
|
|
* @param {Object} stream CodeMirror.StringStream
|
|
|
|
*/
|
|
|
|
function prepareItalicForCorrection( stream ) {
|
|
|
|
// see Parser::doQuotes() in MediaWiki core, it works similar
|
|
|
|
// firstsingleletterword has maximum priority
|
|
|
|
// firstmultiletterword has medium priority
|
|
|
|
// firstspace has low priority
|
|
|
|
var end = stream.pos,
|
|
|
|
str = stream.string.substr( 0, end - 3 ),
|
|
|
|
x1 = str.substr( -1, 1 ),
|
|
|
|
x2 = str.substr( -2, 1 );
|
|
|
|
|
|
|
|
// firstsingleletterword olways is undefined here
|
|
|
|
if ( x1 === ' ' ) {
|
|
|
|
if ( firstmultiletterword || firstspace ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
firstspace = end;
|
|
|
|
} else if ( x2 === ' ' ) {
|
|
|
|
firstsingleletterword = end;
|
|
|
|
} else if ( firstmultiletterword ) {
|
2015-08-19 15:10:31 +00:00
|
|
|
return;
|
2015-11-24 16:03:00 +00:00
|
|
|
} else {
|
|
|
|
firstmultiletterword = end;
|
|
|
|
}
|
|
|
|
// remember bold and italic state for restore
|
|
|
|
mBold = isBold;
|
|
|
|
mItalic = isItalic;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
startState: function () {
|
|
|
|
return { tokenize: eatWikiText( '', '' ), stack: [], InHtmlTag: [], extName: false, extMode: false, extState: false, nTemplate: 0, nLink: 0, nExt: 0 };
|
|
|
|
},
|
|
|
|
copyState: function ( state ) {
|
|
|
|
return {
|
|
|
|
tokenize: state.tokenize,
|
|
|
|
stack: state.stack.concat( [] ),
|
|
|
|
InHtmlTag: state.InHtmlTag.concat( [] ),
|
|
|
|
extName: state.extName,
|
|
|
|
extMode: state.extMode,
|
|
|
|
extState: state.extMode !== false && CodeMirror.copyState( state.extMode, state.extState ),
|
|
|
|
nTemplate: state.nTemplate,
|
|
|
|
nLink: state.nLink,
|
|
|
|
nExt: state.nExt
|
|
|
|
};
|
|
|
|
},
|
|
|
|
token: function ( stream, state ) {
|
|
|
|
var style, p, t, f,
|
|
|
|
readyTokens = [],
|
|
|
|
tmpTokens = [];
|
|
|
|
|
|
|
|
if ( mTokens.length > 0 ) { // just send saved tokens till they exists
|
|
|
|
t = mTokens.shift();
|
|
|
|
stream.pos = t.pos;
|
|
|
|
state = t.state;
|
|
|
|
return t.style;
|
|
|
|
}
|
2015-08-19 15:10:31 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.sol() ) { // reset bold and italic status in every new line
|
|
|
|
isBold = false;
|
|
|
|
isItalic = false;
|
|
|
|
firstsingleletterword = undefined;
|
|
|
|
firstmultiletterword = undefined;
|
|
|
|
firstspace = undefined;
|
|
|
|
}
|
2015-08-19 15:10:31 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
do {
|
|
|
|
style = state.tokenize( stream, state ); // get token style
|
|
|
|
f = firstsingleletterword || firstmultiletterword || firstspace;
|
|
|
|
if ( f ) { // rollback point exists
|
|
|
|
if ( f !== p ) { // new rollbak point
|
|
|
|
p = f;
|
|
|
|
if ( tmpTokens.length > 0 ) { // it's not first rollbak point
|
|
|
|
readyTokens = readyTokens.concat( tmpTokens ); // save tokens
|
|
|
|
tmpTokens = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmpTokens.push( { // save token
|
|
|
|
pos: stream.pos,
|
|
|
|
style: style,
|
|
|
|
state: CodeMirror.copyState( state.extMode ? state.extMode : 'mediawiki', state )
|
|
|
|
} );
|
|
|
|
} else { // rollback point not exists
|
|
|
|
mStyle = style; // remember style before possible rollback point
|
|
|
|
return style; // just return token style
|
|
|
|
}
|
|
|
|
} while ( !stream.eol() );
|
|
|
|
|
|
|
|
if ( isBold && isItalic ) { // needs to rollback
|
|
|
|
isItalic = mItalic; // restore status
|
|
|
|
isBold = mBold;
|
|
|
|
firstsingleletterword = undefined;
|
|
|
|
firstmultiletterword = undefined;
|
|
|
|
firstspace = undefined;
|
|
|
|
if ( readyTokens.length > 0 ) { // it contains tickets before the point of rollback
|
|
|
|
readyTokens[ readyTokens.length ].pos++; // add one apostrophe, next token will be italic (two apostrophes)
|
|
|
|
mTokens = readyTokens; // for sending tokens till the point of rollback
|
|
|
|
} else { // there are no tikets before the point of rollback
|
|
|
|
stream.pos = tmpTokens[ 0 ].pos - 2; // eat( '\'')
|
|
|
|
return mStyle; // send saved Style
|
|
|
|
}
|
|
|
|
} else { // not needs to rollback
|
|
|
|
mTokens = readyTokens.concat( tmpTokens ); // send all saved tokens
|
|
|
|
}
|
|
|
|
// return first saved token
|
2015-08-19 15:10:31 +00:00
|
|
|
t = mTokens.shift();
|
|
|
|
stream.pos = t.pos;
|
|
|
|
state = t.state;
|
|
|
|
return t.style;
|
2015-11-24 16:03:00 +00:00
|
|
|
},
|
|
|
|
blankLine: function ( state ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
var ret;
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( state.extName ) {
|
|
|
|
if ( state.extMode ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
ret = '';
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( state.extMode.blankLine ) {
|
|
|
|
ret = ' ' + state.extMode.blankLine( state.extState );
|
2015-08-19 15:10:31 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'line-cm-mw-tag-' + state.extName + ret;
|
2015-08-19 15:10:31 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'line-cm-mw-exttag';
|
2014-10-23 06:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
};
|
|
|
|
} );
|
2014-08-20 17:03:46 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
CodeMirror.defineMIME( 'text/mediawiki', 'mediawiki' );
|
2014-08-20 17:03:46 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatNowiki( style, lineStyle ) {
|
|
|
|
return function ( stream, state, ownLine ) {
|
2017-03-21 10:13:18 +00:00
|
|
|
var s;
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( ownLine && stream.sol() ) {
|
|
|
|
state.ownLine = true;
|
|
|
|
} else if ( ownLine === false && state.ownLine ) {
|
|
|
|
state.ownLine = false;
|
|
|
|
}
|
2017-03-21 10:13:18 +00:00
|
|
|
s = ( state.ownLine ? lineStyle : style );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.match( /[^&]+/ ) ) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
stream.next(); // eat &
|
|
|
|
return eatMnemonic( stream, s, s );
|
|
|
|
};
|
|
|
|
}
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2017-03-21 10:13:18 +00:00
|
|
|
CodeMirror.defineMode( 'mw-tag-pre', function ( /* config, parserConfig */ ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return {
|
|
|
|
startState: function () { return {}; },
|
|
|
|
token: eatNowiki( 'mw-tag-pre', 'line-cm-mw-tag-pre' )
|
|
|
|
};
|
|
|
|
} );
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2017-03-21 10:13:18 +00:00
|
|
|
CodeMirror.defineMode( 'mw-tag-nowiki', function ( /* config, parserConfig */ ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return {
|
|
|
|
startState: function () { return {}; },
|
|
|
|
token: eatNowiki( 'mw-tag-nowiki', 'line-cm-mw-tag-nowiki' )
|
|
|
|
};
|
|
|
|
} );
|
2014-10-23 06:09:10 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
}( CodeMirror ) );
|