Jump to content

MediaWiki:Common.js: Difference between revisions

From Knowledge Base
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 2: Line 2:
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));


// Highlight C# comments (single-line and multi-line) FIRST
// Highlight comments FIRST
document.querySelectorAll("pre.csharp").forEach(pre => {
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;
     let code = pre.innerHTML;
Line 9: Line 9:
     const regexMultiLineComments = /\/\*[\s\S]*?\*\//g;
     const regexMultiLineComments = /\/\*[\s\S]*?\*\//g;
     code = code.replace(regexMultiLineComments, match => {
     code = code.replace(regexMultiLineComments, match => {
         return `<span class="csharp-comment">${match}</span>`;
         return `<span class="csharp-comment" data-protected="true">${match}</span>`;
     });
     });


Line 15: Line 15:
     const regexSingleLineComments = /\/\/[^\n]*/g;
     const regexSingleLineComments = /\/\/[^\n]*/g;
     code = code.replace(regexSingleLineComments, match => {
     code = code.replace(regexSingleLineComments, match => {
         return `<span class="csharp-comment">${match}</span>`;
         return `<span class="csharp-comment" data-protected="true">${match}</span>`;
     });
     });


Line 21: Line 21:
});
});


// Define C# keywords
// Highlight keywords, types, and other patterns AFTER comments are protected
const csharpKeywords = [
const csharpKeywords = [
     "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
     "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
Line 33: Line 33:
];
];


// Highlight C# keywords
document.querySelectorAll("pre.csharp").forEach(pre => {
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;
     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
    // Highlight keywords
const csharpTypes = [
    const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    "int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object",
     code = code.replace(regexKeywords, '<span class="csharp-keyword">$1</span>');
     "void", "var", "dynamic", "sbyte", "ushort", "uint", "ulong"
];


// Highlight C# types
    // Highlight types
document.querySelectorAll("pre.csharp").forEach(pre => {
    const csharpTypes = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
    let code = pre.innerHTML;
     const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
     const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
     code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');
     code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');
    pre.innerHTML = code;
});


// Highlight C# method names
    // Highlight 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;
     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>');
     code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');
   
    pre.innerHTML = code;
});


// Function to check if a word is a keyword
     // Highlight class and property names
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;
     const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
     code = code.replace(regexClasses, (match, p1) => {
     code = code.replace(regexClasses, '<span class="csharp-class">$1</span>');
        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;
     pre.innerHTML = code;
});
});


// Add rainbow curly braces highlighting with matched colors for each pair
// Ensure protected spans (comments) are skipped in all transformations
const rainbowColors = [
document.querySelectorAll('[data-protected="true"]').forEach(element => {
    "red", "green", "blue", "purple", "orange", "yellow", "pink", "cyan", "lime", "brown"
     element.removeAttribute('data-protected');
];
 
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;
    let braceStack = [];
    let braceCount = 0;
 
    // Regex for matching curly braces and adding colors
    code = code.replace(/{|}/g, (brace, offset) => {
        const color = rainbowColors[braceCount % rainbowColors.length];
 
        // Check if this is an opening brace
        if (brace === "{") {
            braceStack.push(color);  // Push color for opening brace
        } else {
            const pairColor = braceStack.pop();  // Pop the last opened brace's color for closing brace
            braceCount++;
            return `<span class="csharp-brace" style="color: ${pairColor};">${brace}</span>`;
        }
 
        // Apply color to opening brace
        braceCount++;
        return `<span class="csharp-brace" style="color: ${color};">${brace}</span>`;
    });
 
    pre.innerHTML = code;
});
});

Revision as of 13:39, 18 January 2025

// Add 'csharp' class to all <pre> tags for easier targeting
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));

// Highlight comments FIRST
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

    // Regex for matching multi-line comments (/* */)
    const regexMultiLineComments = /\/\*[\s\S]*?\*\//g;
    code = code.replace(regexMultiLineComments, match => {
        return `<span class="csharp-comment" data-protected="true">${match}</span>`;
    });

    // Regex for matching single-line comments (//)
    const regexSingleLineComments = /\/\/[^\n]*/g;
    code = code.replace(regexSingleLineComments, match => {
        return `<span class="csharp-comment" data-protected="true">${match}</span>`;
    });

    pre.innerHTML = code;
});

// Highlight keywords, types, and other patterns AFTER comments are protected
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"
];

document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

    // Highlight keywords
    const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(regexKeywords, '<span class="csharp-keyword">$1</span>');

    // Highlight types
    const csharpTypes = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
    const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
    code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');

    // Highlight method names
    const regexMethods = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\()/g;
    code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');

    // Highlight class and property names
    const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
    code = code.replace(regexClasses, '<span class="csharp-class">$1</span>');

    pre.innerHTML = code;
});

// Ensure protected spans (comments) are skipped in all transformations
document.querySelectorAll('[data-protected="true"]').forEach(element => {
    element.removeAttribute('data-protected');
});