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 {
|
2017-07-20 13:34:40 +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
|
|
|
|
2017-04-28 13:42:30 +00:00
|
|
|
var mwConfig = config.mwConfig,
|
2021-02-05 15:07:40 +00:00
|
|
|
urlProtocols = new RegExp( '^(?:' + mwConfig.urlProtocols + ')', 'i' ),
|
2015-11-24 16:03:00 +00:00
|
|
|
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,
|
2021-12-04 21:17:03 +00:00
|
|
|
var: true, div: true, center: true, blockquote: true, q: true, ol: true, ul: true,
|
2015-11-24 16:03:00 +00:00
|
|
|
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,
|
2018-03-05 19:59:12 +00:00
|
|
|
tr: true, noinclude: true, includeonly: true, onlyinclude: true, translate: true },
|
2018-03-06 02:23:33 +00:00
|
|
|
voidHtmlTags = { br: true, hr: true, wbr: true },
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^&<[{~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^{}|]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^{}[<&~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^#?[^:}{~]+/ ) ) { // FIXME: {{#name}} and {{uc}} are wrong, must have ':'
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^|}{[<&~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = eatTemplateArgument( true );
|
|
|
|
return makeLocalStyle( 'mw-template-delimiter', state );
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*\}\}/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return makeLocalStyle( 'mw-template-bracket', state, 'nTemplate' );
|
|
|
|
}
|
2021-04-23 13:31:06 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*<!--.*?-->/ ) ) {
|
|
|
|
return makeLocalStyle( 'mw-comment', state );
|
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( haveAte && stream.sol() ) {
|
|
|
|
// @todo error message
|
|
|
|
state.nTemplate--;
|
|
|
|
state.tokenize = state.stack.pop();
|
|
|
|
return;
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*[^\s\u00a0|}<{&~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2017-07-20 13:34:40 +00:00
|
|
|
if ( expectArgName && stream.eatWhile( /[^=|}{[<&~]/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.eat( '=' ) ) {
|
|
|
|
state.tokenize = eatTemplateArgument( false );
|
|
|
|
return makeLocalStyle( 'mw-template-argument-name', state );
|
|
|
|
}
|
|
|
|
return makeLocalStyle( 'mw-template', state );
|
2017-07-20 13:34:40 +00:00
|
|
|
} else if ( stream.eatWhile( /[^|}{[<&~]/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*\]/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^\s\u00a0\]{&~']+/ ) || stream.eatSpace() ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^'\]{&~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*#[\s\u00a0]*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = inLinkToSection;
|
|
|
|
return makeLocalStyle( 'mw-link', state );
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*\|[\s\u00a0]*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = eatLinkText();
|
|
|
|
return makeLocalStyle( 'mw-link-delimiter', state );
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*[^\s\u00a0#|\]&~{]+/ ) || stream.eatSpace() ) { // FIXME '{{' brokes Link, sample [[z{{page]]
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^|\]&~{}]+/ ) ) { // FIXME '{{' brokes Link, sample [[z{{page]]
|
2015-11-24 16:03:00 +00:00
|
|
|
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';
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^'\]{&~]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2018-03-06 02:23:33 +00:00
|
|
|
if ( isCloseTag && !( name in voidHtmlTags ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2017-04-05 06:19:56 +00:00
|
|
|
state.tokenize = eatChar( '>', 'mw-exttag-bracket mw-ext-' + name );
|
2015-11-24 16:03:00 +00:00
|
|
|
} else {
|
|
|
|
state.tokenize = eatExtTagAttribute( name );
|
2014-08-25 10:56:52 +00:00
|
|
|
}
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-name mw-ext-' + name, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
};
|
|
|
|
}
|
2015-08-19 15:10:31 +00:00
|
|
|
|
2015-11-24 16:03:00 +00:00
|
|
|
function eatHtmlTagAttribute( name ) {
|
|
|
|
return function ( stream, state ) {
|
2021-04-23 18:43:33 +00:00
|
|
|
if ( stream.match( /^(?:"[^"]*"|'[^']*'|[^>/<{&~])+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-htmltag-attribute', state );
|
|
|
|
}
|
|
|
|
if ( stream.eat( '>' ) ) {
|
2018-03-06 02:23:33 +00:00
|
|
|
if ( !( name in voidHtmlTags ) ) {
|
|
|
|
state.InHtmlTag.push( name );
|
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) {
|
2021-04-23 18:43:33 +00:00
|
|
|
if ( stream.match( /^(?:"[^"]*"|'[^']*'|[^>/<{&~])+/ ) ) {
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-attribute mw-ext-' + name, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
}
|
|
|
|
if ( stream.eat( '>' ) ) {
|
|
|
|
state.extName = name;
|
2017-04-28 13:42:30 +00:00
|
|
|
if ( name in mwConfig.tagModes ) {
|
|
|
|
state.extMode = CodeMirror.getMode( config, mwConfig.tagModes[ name ] );
|
2015-11-24 16:03:00 +00:00
|
|
|
state.extState = CodeMirror.startState( state.extMode );
|
|
|
|
}
|
|
|
|
state.tokenize = eatExtTagArea( name );
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
}
|
|
|
|
if ( stream.match( '/>' ) ) {
|
|
|
|
state.tokenize = state.stack.pop();
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
}
|
2017-04-05 06:19:56 +00:00
|
|
|
return eatWikiText( 'mw-exttag-attribute mw-ext-' + name, '' )( stream, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
};
|
|
|
|
}
|
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,
|
2019-04-13 21:44:50 +00:00
|
|
|
pattern = new RegExp( '</' + name + '\\s*>', 'i' ),
|
2015-11-24 16:03:00 +00:00
|
|
|
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 );
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + name, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
};
|
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 ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.sol() && stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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() ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*[|!]/, false ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
state.tokenize = inTable;
|
|
|
|
return inTable( stream, state );
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^'|{[<&~!]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
2021-01-12 14:13:19 +00:00
|
|
|
} else if ( stream.match( /^[^\s\u00a0{[\]<>~).,']*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( stream.peek() === '~' ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( !stream.match( /^~{3,}/, false ) ) {
|
|
|
|
stream.match( /^~*/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
|
|
|
} else if ( stream.peek() === '{' ) {
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( !stream.match( /^\{\{/, false ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
stream.next();
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
|
|
|
} else if ( stream.peek() === '\'' ) {
|
|
|
|
if ( !stream.match( '\'\'', false ) ) {
|
|
|
|
stream.next();
|
|
|
|
return makeLocalStyle( 'mw-free-extlink', state );
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
} 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 ) {
|
2018-03-14 02:51:35 +00:00
|
|
|
if ( !stream.match( '//', false ) && stream.match( urlProtocols ) ) { // highlight free external links, bug T108448
|
2015-11-24 16:03:00 +00:00
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatFreeExternalLink;
|
|
|
|
return makeLocalStyle( 'mw-free-extlink-protocol', state );
|
|
|
|
}
|
|
|
|
ch = stream.next();
|
|
|
|
switch ( ch ) {
|
|
|
|
case '-':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^----*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'mw-hr';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '=':
|
2021-01-12 14:13:19 +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 '#':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[*#]*:*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'mw-list';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ':':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^:*{\|/, false ) ) { // Highlight indented tables :{|, bug T108454
|
2015-11-24 16:03:00 +00:00
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatStartTable;
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^:*[*#]*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'mw-indenting';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[\s\u00a0]*:*{\|/, false ) ) { // Leading spaces is the correct syntax for a table, bug T108454
|
2015-11-24 16:03:00 +00:00
|
|
|
stream.eatSpace();
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^:+/ ) ) { // ::{|
|
2015-11-24 16:03:00 +00:00
|
|
|
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 '\'':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^'*(?=''''')/ ) || stream.match( /^'''(?!')/, false ) ) { // skip the irrelevant apostrophes ( >5 or =4 )
|
2015-11-24 16:03:00 +00:00
|
|
|
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();
|
2017-07-20 13:34:40 +00:00
|
|
|
if ( /[^\]|[]/.test( stream.peek() ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 );
|
2021-01-12 14:13:19 +00:00
|
|
|
} else if ( stream.match( /^\{[\s\u00a0]*/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 '#'
|
2021-01-12 14:13:19 +00:00
|
|
|
name = stream.match( /^([^\s\u00a0}[\]<{'|&:]+)(:|[\s\u00a0]*)(\}\}?)?(.)?/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( name ) {
|
|
|
|
stream.backUp( name[ 0 ].length );
|
2017-04-28 13:42:30 +00:00
|
|
|
if ( ( name[ 2 ] === ':' || name[ 4 ] === undefined || name[ 3 ] === '}}' ) && ( name[ 1 ].toLowerCase() in mwConfig.functionSynonyms[ 0 ] || name[ 1 ] in mwConfig.functionSynonyms[ 1 ] ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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( '/' );
|
2021-01-12 14:13:19 +00:00
|
|
|
tagname = stream.match( /^[^>/\s\u00a0.*,[\]{}$^+?|/\\'`~<=!@#%&()-]+/ );
|
2017-04-05 06:19:56 +00:00
|
|
|
if ( stream.match( '!--' ) ) { // comment
|
2015-11-24 16:03:00 +00:00
|
|
|
return chain( eatBlock( 'mw-comment', '-->' ) );
|
|
|
|
}
|
|
|
|
if ( tagname ) {
|
|
|
|
tagname = tagname[ 0 ].toLowerCase();
|
2017-04-28 13:42:30 +00:00
|
|
|
if ( tagname in mwConfig.tags ) { // Parser function
|
2015-11-24 16:03:00 +00:00
|
|
|
if ( isCloseTag === true ) {
|
|
|
|
// @todo message
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
stream.backUp( tagname.length );
|
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatTagName( tagname.length, isCloseTag, false );
|
2017-04-05 06:19:56 +00:00
|
|
|
return makeLocalStyle( 'mw-exttag-bracket mw-ext-' + tagname, state );
|
2015-11-24 16:03:00 +00:00
|
|
|
}
|
|
|
|
if ( tagname in permittedHtmlTags ) { // Html tag
|
|
|
|
if ( isCloseTag === true && tagname !== state.InHtmlTag.pop() ) {
|
|
|
|
// @todo message
|
|
|
|
return 'error';
|
|
|
|
}
|
2018-03-06 02:23:33 +00:00
|
|
|
if ( isCloseTag === true && tagname in voidHtmlTags ) {
|
|
|
|
// @todo message
|
|
|
|
return 'error';
|
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
stream.backUp( tagname.length );
|
|
|
|
state.stack.push( state.tokenize );
|
2018-03-06 02:23:33 +00:00
|
|
|
// || ( tagname in voidHtmlTags ) because opening void tags should also be treated as the closing tag.
|
|
|
|
state.tokenize = eatTagName( tagname.length, isCloseTag || ( tagname in voidHtmlTags ), true );
|
2015-11-24 16:03:00 +00:00
|
|
|
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 '~':
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^~{2,4}/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
return 'mw-signature';
|
2015-08-10 17:14:18 +00:00
|
|
|
}
|
2015-11-24 16:03:00 +00:00
|
|
|
break;
|
2017-09-13 03:47:24 +00:00
|
|
|
case '_': // Maybe double undescored Magic Word as __TOC__
|
|
|
|
tmp = 1;
|
|
|
|
while ( stream.eat( '_' ) ) { // Optimize processing of many underscore symbols
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
if ( tmp > 2 ) { // Many underscore symbols
|
|
|
|
if ( !stream.eol() ) {
|
|
|
|
stream.backUp( 2 ); // Leave last two underscore symbols for processing again in next iteration
|
|
|
|
}
|
|
|
|
return makeStyle( style, state ); // Optimization: skip regex function at the end for EOL and backuped symbols
|
|
|
|
} else if ( tmp === 2 ) { // Check on double underscore Magic Word
|
2021-01-12 14:13:19 +00:00
|
|
|
name = stream.match( /^([^\s\u00a0>}[\]<{'|&:~]+?)__/ ); // The same as the end of function except '_' inside and with '__' at the end of string
|
2017-09-13 03:47:24 +00:00
|
|
|
if ( name && name[ 0 ] ) {
|
|
|
|
if ( '__' + name[ 0 ].toLowerCase() in mwConfig.doubleUnderscore[ 0 ] || '__' + name[ 0 ] in mwConfig.doubleUnderscore[ 1 ] ) {
|
|
|
|
return 'mw-doubleUnderscore';
|
|
|
|
}
|
|
|
|
if ( !stream.eol() ) {
|
|
|
|
stream.backUp( 2 ); // Two underscore symbols at the end can be begining of other double undescored Magic Word
|
|
|
|
}
|
|
|
|
return makeStyle( style, state ); // Optimization: skip regex function at the end for EOL and backuped symbols
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-11-24 16:03:00 +00:00
|
|
|
default:
|
|
|
|
if ( /[\s\u00a0]/.test( ch ) ) {
|
|
|
|
stream.eatSpace();
|
2018-03-14 02:51:35 +00:00
|
|
|
if ( stream.match( urlProtocols, false ) && !stream.match( '//' ) ) { // highlight free external links, bug T108448
|
2015-11-24 16:03:00 +00:00
|
|
|
state.stack.push( state.tokenize );
|
|
|
|
state.tokenize = eatFreeExternalLinkProtocol;
|
|
|
|
return makeStyle( style, state );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-01-12 14:13:19 +00:00
|
|
|
stream.match( /^[^\s\u00a0_>}[\]<{'|&:~]+/ );
|
2015-11-24 16:03:00 +00:00
|
|
|
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
|
2017-08-25 23:05:55 +00:00
|
|
|
readyTokens[ readyTokens.length - 1 ].pos++; // add one apostrophe, next token will be italic (two apostrophes)
|
2015-11-24 16:03:00 +00:00
|
|
|
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 );
|
2021-01-12 14:13:19 +00:00
|
|
|
if ( stream.match( /^[^&]+/ ) ) {
|
2015-11-24 16:03:00 +00:00
|
|
|
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 ) );
|