Debian Codegraph @

extensions/SyntaxHighlight_GeSHi/modules/ve-syntaxhighlight/ve.dm.MWSyntaxHighlightNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWSyntaxHighlightNode class.
3 *
4 * @copyright VisualEditor Team and others
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki syntax highlight node.
10 *
11 * @class
12 * @abstract
13 *
14 * @constructor
15 */
16 ve.dm.MWSyntaxHighlightNode = function VeDmMWSyntaxHighlightNode() {
17 };
18
19 /* Inheritance */
20
21 OO.initClass( ve.dm.MWSyntaxHighlightNode );
22
23 /* Static members */
24
25 ve.dm.MWSyntaxHighlightNode.static.name = 'mwSyntaxHighlight';
26
27 ve.dm.MWSyntaxHighlightNode.static.extensionName = 'syntaxhighlight';
28
29 ve.dm.MWSyntaxHighlightNode.static.getMatchRdfaTypes = function () {
30 return [ 'mw:Extension/syntaxhighlight', 'mw:Extension/source' ];
31 };
32
33 /* Static methods */
34
35 /**
36 * @inheritdoc
37 */
38 ve.dm.MWSyntaxHighlightNode.static.toDataElement = function ( domElements, converter ) {
39 const isInline = this.isHybridInline( domElements, converter ),
40 type = isInline ? 'mwInlineSyntaxHighlight' : 'mwBlockSyntaxHighlight';
41 // Parent method
42 return ve.dm.MWExtensionNode.static.toDataElement.call( this, domElements, converter, type );
43 };
44
45 const supportedLanguages = [ undefined ];
46 let geshiToPygmentsMap,
47 pygmentsToAceMap;
48
49 /**
50 * Register supported Pygments languages.
51 * Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
52 *
53 * @param {string[]} languages List of language names, e.g. "html"
54 */
55 ve.dm.MWSyntaxHighlightNode.static.addPygmentsLanguages = function ( languages ) {
56 ve.batchPush( supportedLanguages, languages );
57 };
58
59 /**
60 * Register map from Geshi to pygments lexer names.
61 * Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
62 *
63 * @param {Object.<string.string>} map Map of compatible lexers, e.g. { "html5": "html" }
64 */
65 ve.dm.MWSyntaxHighlightNode.static.addGeshiToPygmentsMap = function ( map ) {
66 geshiToPygmentsMap = map;
67 ve.batchPush( supportedLanguages, Object.keys( geshiToPygmentsMap ) );
68 };
69
70 /**
71 * Register a map from pygments to Ace lexer names.
72 * Called from PHP's ResourceLoaderSyntaxHighlightVisualEditorModule.
73 *
74 * @param {Object.<string.string>} map Map of compatible lexers, e.g. { "js": "JavaScript" }
75 */
76 ve.dm.MWSyntaxHighlightNode.static.addPygmentsToAceMap = function ( map ) {
77 pygmentsToAceMap = map;
78 };
79
80 /**
81 * Converts a (valid) language as recognized by the SyntaxHighlight wikicode
82 * to a compatible Ace lexer name (to be used by CodeEditor)
83 *
84 * @param {string} language Language name
85 * @return {string} The name of the ace lexer
86 */
87 ve.dm.MWSyntaxHighlightNode.static.convertLanguageToAce = function ( language ) {
88 language = geshiToPygmentsMap[ language ] || language;
89 return ( pygmentsToAceMap[ language ] || language ).toLowerCase();
90 };
91
92 /**
93 * Check if a language is supported
94 *
95 * @param {string} [language] Language name
96 * @return {boolean} The language is supported; always true when called with no language
97 */
98 ve.dm.MWSyntaxHighlightNode.static.isLanguageSupported = function ( language ) {
99 return supportedLanguages.indexOf( language || undefined ) !== -1;
100 };
101
102 /**
103 * Get an array of all languages (both Pygments and former GeSHi names)
104 *
105 * @return {string[]} All currently supported languages, including an undefined entry for "none"
106 */
107 ve.dm.MWSyntaxHighlightNode.static.getLanguages = function () {
108 return supportedLanguages.slice();
109 };
110
111 /* Methods */
112
113 /**
114 * Check if the node's current language is supported
115 *
116 * @return {boolean} The language is supported
117 */
118 ve.dm.MWSyntaxHighlightNode.prototype.isLanguageSupported = function () {
119 return this.constructor.static.isLanguageSupported( this.getLanguage() );
120 };
121
122 ve.dm.MWSyntaxHighlightNode.prototype.getLanguage = function () {
123 return this.getAttribute( 'mw' ).attrs.lang.toLowerCase();
124 };