MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary Tag: Reverted |
||
| Line 82: | Line 82: | ||
let code = pre.innerHTML; | let code = pre.innerHTML; | ||
// | // First, highlight comments without affecting keywords or other parts of the code | ||
const regexSingleLineComments = /(\/\/.*)/g; | const regexSingleLineComments = /(\/\/.*)/g; | ||
code = code.replace(regexSingleLineComments, match => { | code = code.replace(regexSingleLineComments, match => { | ||
// | // Wrap the entire comment in a span | ||
return `<span class="csharp-comment">${match}</span>`; | |||
return `<span class="csharp-comment">${ | |||
}); | }); | ||
const regexMultiLineComments = /(\/\*[\s\S]*?\*\/)/g; | const regexMultiLineComments = /(\/\*[\s\S]*?\*\/)/g; | ||
code = code.replace(regexMultiLineComments, match => { | code = code.replace(regexMultiLineComments, match => { | ||
// | // Wrap the entire comment in a span | ||
return `<span class="csharp-comment">${match}</span>`; | |||
return `<span class="csharp- | }); | ||
// Now that the comments are safely highlighted, we process the rest of the code | |||
const regexKeywords = /\b(const|class|public|private|protected|for|while|if|else|switch|case|break|return)\b/g; | |||
code = code.replace(regexKeywords, match => { | |||
// Only highlight non-comment code | |||
return `<span class="csharp-keyword">${match}</span>`; | |||
}); | |||
// Highlight C# types (e.g., int, string, etc.) | |||
const regexTypes = /\b(int|string|bool|double|float|void|char|var)\b/g; | |||
code = code.replace(regexTypes, match => { | |||
return `<span class="csharp-type">${match}</span>`; | |||
}); | |||
// Highlight class and method names | |||
const regexClasses = /\b([A-Z][a-zA-Z0-9]*)\b/g; | |||
code = code.replace(regexClasses, match => { | |||
return `<span class="csharp-class">${match}</span>`; | |||
}); | |||
// Highlight properties | |||
const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*\{)/g; | |||
code = code.replace(regexProperties, match => { | |||
return `<span class="csharp-property">${match}</span>`; | |||
}); | }); | ||
pre.innerHTML = code; | pre.innerHTML = code; | ||
}); | }); | ||
Revision as of 22:01, 18 January 2025
// Add 'csharp' class to all <pre> tags for easier targeting
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"
];
// Highlight C# keywords
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
const regex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
code = code.replace(regex, '<span class="csharp-keyword">$1</span>');
pre.innerHTML = code;
});
// Define C# types
const csharpTypes = [
"int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object",
"void", "var", "dynamic", "sbyte", "ushort", "uint", "ulong"
];
// Highlight C# types
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');
pre.innerHTML = code;
});
// Highlight C# method names
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
// Define the regex pattern for method names (excluding C# keywords and types)
const regexMethods = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\()/g;
// Apply the method name highlighting
code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');
pre.innerHTML = code;
});
// Function to check if a word is a keyword
function isKeyword(word) {
return csharpKeywords.includes(word);
}
// Highlight C# class names and property names
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
// Define the regex pattern for class names (PascalCase)
const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
code = code.replace(regexClasses, (match, p1) => {
return isKeyword(p1) ? p1 : `<span class="csharp-class">${p1}</span>`;
});
// Define the regex pattern for property names (PascalCase, followed by getter/setter)
const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*{)/g;
code = code.replace(regexProperties, (match, p1) => {
return isKeyword(p1) ? p1 : `<span class="csharp-property">${p1}</span>`;
});
pre.innerHTML = code;
});
// Add rainbow curly braces highlighting with matched colors for each pair
const rainbowColors = [
"red", "green", "blue", "purple", "orange", "yellow", "pink", "cyan", "lime", "brown"
];
// Highlight C# comments (single-line and multi-line)
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
// First, highlight comments without affecting keywords or other parts of the code
const regexSingleLineComments = /(\/\/.*)/g;
code = code.replace(regexSingleLineComments, match => {
// Wrap the entire comment in a span
return `<span class="csharp-comment">${match}</span>`;
});
const regexMultiLineComments = /(\/\*[\s\S]*?\*\/)/g;
code = code.replace(regexMultiLineComments, match => {
// Wrap the entire comment in a span
return `<span class="csharp-comment">${match}</span>`;
});
// Now that the comments are safely highlighted, we process the rest of the code
const regexKeywords = /\b(const|class|public|private|protected|for|while|if|else|switch|case|break|return)\b/g;
code = code.replace(regexKeywords, match => {
// Only highlight non-comment code
return `<span class="csharp-keyword">${match}</span>`;
});
// Highlight C# types (e.g., int, string, etc.)
const regexTypes = /\b(int|string|bool|double|float|void|char|var)\b/g;
code = code.replace(regexTypes, match => {
return `<span class="csharp-type">${match}</span>`;
});
// Highlight class and method names
const regexClasses = /\b([A-Z][a-zA-Z0-9]*)\b/g;
code = code.replace(regexClasses, match => {
return `<span class="csharp-class">${match}</span>`;
});
// Highlight properties
const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*\{)/g;
code = code.replace(regexProperties, match => {
return `<span class="csharp-property">${match}</span>`;
});
pre.innerHTML = code;
});