MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 49: | Line 49: | ||
pre.innerHTML = code; | pre.innerHTML = code; | ||
}); | }); | ||
// Define an array of keywords already identified earlier | |||
const csharpKeywords = ['public', 'private', 'protected', 'class', 'void', 'int', 'string', 'for', 'if', 'else']; | |||
// Function to check if a word is a keyword | // Function to check if a word is a keyword | ||
| Line 55: | Line 60: | ||
} | } | ||
// Highlight C# | // Function to check if a word looks like a variable (e.g., camelCase) | ||
function isVariable(word) { | |||
return /^[a-z][a-zA-Z0-9]*$/.test(word); | |||
} | |||
// Highlight C# code | |||
document.querySelectorAll("pre.csharp").forEach(pre => { | document.querySelectorAll("pre.csharp").forEach(pre => { | ||
let code = pre.innerHTML; | let code = pre.innerHTML; | ||
// | // 1. First pass: Highlight keywords (to avoid interfering with class names or properties) | ||
const regexKeywords = /\b([a-zA-Z_][a-zA-Z0-9_]*)\b/g; | |||
code = code.replace(regexKeywords, (match, p1) => { | |||
return isKeyword(p1) ? `<span class="csharp-keyword">${p1}</span>` : p1; | |||
}); | |||
// 2. Second pass: Highlight class names (PascalCase) | |||
const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g; | const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g; | ||
code = code.replace(regexClasses, (match, p1) => { | code = code.replace(regexClasses, (match, p1) => { | ||
| Line 65: | Line 81: | ||
}); | }); | ||
// | // 3. Third pass: Highlight properties (PascalCase followed by { for C# properties) | ||
const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*{)/g; | const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*{)/g; | ||
code = code.replace(regexProperties, (match, p1) => { | code = code.replace(regexProperties, (match, p1) => { | ||
return isKeyword(p1) ? p1 : `<span class="csharp-property">${p1}</span>`; | return isKeyword(p1) || isVariable(p1) ? p1 : `<span class="csharp-property">${p1}</span>`; | ||
}); | }); | ||
pre.innerHTML = code; | pre.innerHTML = code; | ||
}); | }); | ||
Revision as of 11:29, 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"
];
// 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;
});
// Define an array of keywords already identified earlier
const csharpKeywords = ['public', 'private', 'protected', 'class', 'void', 'int', 'string', 'for', 'if', 'else'];
// Function to check if a word is a keyword
function isKeyword(word) {
return csharpKeywords.includes(word);
}
// Function to check if a word looks like a variable (e.g., camelCase)
function isVariable(word) {
return /^[a-z][a-zA-Z0-9]*$/.test(word);
}
// Highlight C# code
document.querySelectorAll("pre.csharp").forEach(pre => {
let code = pre.innerHTML;
// 1. First pass: Highlight keywords (to avoid interfering with class names or properties)
const regexKeywords = /\b([a-zA-Z_][a-zA-Z0-9_]*)\b/g;
code = code.replace(regexKeywords, (match, p1) => {
return isKeyword(p1) ? `<span class="csharp-keyword">${p1}</span>` : p1;
});
// 2. Second pass: Highlight 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>`;
});
// 3. Third pass: Highlight properties (PascalCase followed by { for C# properties)
const regexProperties = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\s*{)/g;
code = code.replace(regexProperties, (match, p1) => {
return isKeyword(p1) || isVariable(p1) ? p1 : `<span class="csharp-property">${p1}</span>`;
});
pre.innerHTML = code;
});