MediaWiki:Common.js: Difference between revisions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 14: | Line 14: | ||
]; | ]; | ||
// Highlight C# keywords | // Define C# data types | ||
const csharpDataTypes = [ | |||
"int", "string", "bool", "float", "double", "char", "long", "short", "byte", "decimal", "object" | |||
]; | |||
// Define C# operators | |||
const csharpOperators = [ | |||
"=", "==", "+", "-", "*", "/", "%", "++", "--", "&&", "||", "!", "?", ":", "<", ">", "<=", ">=" | |||
]; | |||
// Highlight C# keywords, data types, operators, comments, and strings | |||
document.querySelectorAll("pre.csharp").forEach(pre => { | document.querySelectorAll("pre.csharp").forEach(pre => { | ||
let code = pre.innerHTML; | let code = pre.innerHTML; | ||
// Highlight | // Highlight keywords | ||
const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g"); | const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g"); | ||
code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>'); | code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>'); | ||
// Highlight types | // Highlight data types | ||
const | const dataTypeRegex = new RegExp(`\\b(${csharpDataTypes.join("|")})\\b`, "g"); | ||
code = code.replace( | code = code.replace(dataTypeRegex, '<span class="csharp-data-type">$1</span>'); | ||
// Highlight | // Highlight operators | ||
const | const operatorRegex = new RegExp(`(${csharpOperators.join("|")})`, "g"); | ||
code = code.replace( | code = code.replace(operatorRegex, '<span class="csharp-operator">$1</span>'); | ||
// Highlight single-line comments | |||
const singleLineCommentRegex = /\/\/.*$/gm; | |||
code = code.replace(singleLineCommentRegex, '<span class="csharp-comment">$&</span>'); | |||
// Highlight multi-line comments | |||
// Highlight single-line comments | const multiLineCommentRegex = /\/\*[\s\S]*?\*\//gm; | ||
const | code = code.replace(multiLineCommentRegex, '<span class="csharp-comment">$&</span>'); | ||
code = code.replace( | |||
// Highlight strings | |||
// Highlight multi-line comments | |||
const | |||
code = code.replace( | |||
// Highlight strings | |||
const stringRegex = /"([^"]*)"/g; | const stringRegex = /"([^"]*)"/g; | ||
code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>'); | code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>'); | ||
pre.innerHTML = code; | pre.innerHTML = code; | ||
}); | }); | ||
Revision as of 11:07, 18 January 2025
// Add csharp class to all <pre> tags
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));
// Define C# keywords
const csharpKeywords = [
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
"class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum",
"event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach",
"goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace",
"new", "null", "object", "operator", "out", "override", "params", "private", "protected",
"public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc",
"static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint",
"ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while"
];
// Define C# data types
const csharpDataTypes = [
"int", "string", "bool", "float", "double", "char", "long", "short", "byte", "decimal", "object"
];
// Define C# operators
const csharpOperators = [
"=", "==", "+", "-", "*", "/", "%", "++", "--", "&&", "||", "!", "?", ":", "<", ">", "<=", ">="
];
// Highlight C# keywords, data types, operators, comments, and strings
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
// Highlight keywords
const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');
// Highlight data types
const dataTypeRegex = new RegExp(`\\b(${csharpDataTypes.join("|")})\\b`, "g");
code = code.replace(dataTypeRegex, '<span class="csharp-data-type">$1</span>');
// Highlight operators
const operatorRegex = new RegExp(`(${csharpOperators.join("|")})`, "g");
code = code.replace(operatorRegex, '<span class="csharp-operator">$1</span>');
// Highlight single-line comments
const singleLineCommentRegex = /\/\/.*$/gm;
code = code.replace(singleLineCommentRegex, '<span class="csharp-comment">$&</span>');
// Highlight multi-line comments
const multiLineCommentRegex = /\/\*[\s\S]*?\*\//gm;
code = code.replace(multiLineCommentRegex, '<span class="csharp-comment">$&</span>');
// Highlight strings
const stringRegex = /"([^"]*)"/g;
code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>');
pre.innerHTML = code;
});