2015-05-27 16:15:00 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWSyntaxHighlightNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki syntax highlight node.
|
|
|
|
*
|
|
|
|
* @class
|
2015-09-16 16:23:02 +00:00
|
|
|
* @abstract
|
2015-05-27 16:15:00 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode = function VeDmMWSyntaxHighlightNode() {
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2015-09-16 16:23:02 +00:00
|
|
|
OO.initClass( ve.dm.MWSyntaxHighlightNode );
|
2015-05-27 16:15:00 +00:00
|
|
|
|
|
|
|
/* Static members */
|
|
|
|
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.name = 'mwSyntaxHighlight';
|
|
|
|
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.extensionName = 'syntaxhighlight';
|
|
|
|
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.getMatchRdfaTypes = function () {
|
|
|
|
return [ 'mw:Extension/syntaxhighlight', 'mw:Extension/source' ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
|
2015-09-16 16:23:02 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.toDataElement = function ( domElements, converter ) {
|
2017-05-19 14:06:04 +00:00
|
|
|
var isInline = this.isHybridInline( domElements, converter ),
|
2024-01-22 08:41:41 +00:00
|
|
|
type = isInline ? 'mwInlineSyntaxHighlight' : 'mwBlockSyntaxHighlight';
|
|
|
|
// Parent method
|
|
|
|
return ve.dm.MWExtensionNode.static.toDataElement.call( this, domElements, converter, type );
|
2015-09-16 16:23:02 +00:00
|
|
|
};
|
|
|
|
|
2015-06-24 23:42:12 +00:00
|
|
|
( function () {
|
2016-10-21 13:49:00 +00:00
|
|
|
var supportedLanguages = [ undefined ],
|
|
|
|
geshiToPygmentsMap,
|
|
|
|
pygmentsToAceMap;
|
2015-06-24 23:42:12 +00:00
|
|
|
|
|
|
|
/**
|
2016-10-21 13:49:00 +00:00
|
|
|
* Register supported Pygments languages.
|
2024-01-22 08:41:41 +00:00
|
|
|
* Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
|
2015-06-24 23:42:12 +00:00
|
|
|
*
|
2024-01-22 08:41:41 +00:00
|
|
|
* @param {string[]} languages List of language names, e.g. "html"
|
2015-06-24 23:42:12 +00:00
|
|
|
*/
|
2016-10-21 13:49:00 +00:00
|
|
|
ve.dm.MWSyntaxHighlightNode.static.addPygmentsLanguages = function ( languages ) {
|
2015-06-24 23:42:12 +00:00
|
|
|
ve.batchPush( supportedLanguages, languages );
|
|
|
|
};
|
|
|
|
|
2016-10-21 13:49:00 +00:00
|
|
|
/**
|
|
|
|
* Register map from Geshi to pygments lexer names.
|
2024-01-22 08:41:41 +00:00
|
|
|
* Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
|
2016-10-21 13:49:00 +00:00
|
|
|
*
|
2024-01-22 08:41:41 +00:00
|
|
|
* @param {Object.<string.string>} map Map of compatible lexers, e.g. { "html5": "html" }
|
2016-10-21 13:49:00 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.addGeshiToPygmentsMap = function ( map ) {
|
|
|
|
geshiToPygmentsMap = map;
|
|
|
|
ve.batchPush( supportedLanguages, Object.keys( geshiToPygmentsMap ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a map from pygments to Ace lexer names.
|
2024-01-22 08:41:41 +00:00
|
|
|
* Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
|
2016-10-21 13:49:00 +00:00
|
|
|
*
|
2024-01-22 08:41:41 +00:00
|
|
|
* @param {Object.<string.string>} map Map of compatible lexers, e.g. { "js": "JavaScript" }
|
2016-10-21 13:49:00 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.addPygmentsToAceMap = function ( map ) {
|
|
|
|
pygmentsToAceMap = map;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a (valid) language as recognized by the SyntaxHighlight wikicode
|
|
|
|
* to a compatible Ace lexer name (to be used by CodeEditor)
|
|
|
|
*
|
|
|
|
* @param {string} language Language name
|
|
|
|
* @return {string} The name of the ace lexer
|
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.convertLanguageToAce = function ( language ) {
|
|
|
|
language = geshiToPygmentsMap[ language ] || language;
|
|
|
|
return ( pygmentsToAceMap[ language ] || language ).toLowerCase();
|
|
|
|
};
|
|
|
|
|
2015-06-24 23:42:12 +00:00
|
|
|
/**
|
|
|
|
* Check if a language is supported
|
|
|
|
*
|
2024-01-22 08:41:41 +00:00
|
|
|
* @param {string} [language] Language name
|
|
|
|
* @return {boolean} The language is supported; always true when called with no language
|
2015-06-24 23:42:12 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.isLanguageSupported = function ( language ) {
|
2015-09-16 19:23:54 +00:00
|
|
|
return supportedLanguages.indexOf( language || undefined ) !== -1;
|
2015-06-24 23:42:12 +00:00
|
|
|
};
|
2015-06-25 21:21:54 +00:00
|
|
|
|
|
|
|
/**
|
2016-10-21 13:49:00 +00:00
|
|
|
* Get an array of all languages (both Pygments and former GeSHi names)
|
2015-06-25 21:21:54 +00:00
|
|
|
*
|
2024-01-22 08:41:41 +00:00
|
|
|
* @return {string[]} All currently supported languages, including an undefined entry for "none"
|
2015-06-25 21:21:54 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.static.getLanguages = function () {
|
|
|
|
return supportedLanguages.slice();
|
|
|
|
};
|
2015-06-24 23:42:12 +00:00
|
|
|
}() );
|
2015-05-27 16:15:00 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the node's current language is supported
|
|
|
|
*
|
|
|
|
* @return {boolean} The language is supported
|
|
|
|
*/
|
|
|
|
ve.dm.MWSyntaxHighlightNode.prototype.isLanguageSupported = function () {
|
|
|
|
return this.constructor.static.isLanguageSupported( this.getLanguage() );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWSyntaxHighlightNode.prototype.getLanguage = function () {
|
2018-10-24 15:55:02 +00:00
|
|
|
return this.getAttribute( 'mw' ).attrs.lang.toLowerCase();
|
2015-05-27 16:15:00 +00:00
|
|
|
};
|