Jump to content

MediaWiki:Common.js: Difference between revisions

From Knowledge Base
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
document.querySelectorAll("pre").forEach(pre => {
// Add 'csharp' class to all <pre> tags for easier targeting
    pre.classList.add("csharp"); // Add the 'csharp' class for targeting
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));


    // Get the raw code content
// Highlight C# comments (single-line and multi-line FIRST to override other syntax)
     let code = pre.textContent;
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;


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


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


     // Highlight keywords
     pre.innerHTML = code;
    const keywords = [
});
        "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"
    ];
    const keywordRegex = new RegExp(`\\b(${keywords.join("|")})\\b`, "g");
    code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');


    // Highlight data types
// Define C# keywords
    const types = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
const csharpKeywords = [
     const typeRegex = new RegExp(`\\b(${types.join("|")})\\b`, "g");
    "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
     code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');
    "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 method names (e.g., "MyMethod()")
// Highlight C# keywords
     code = code.replace(/(\b[a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/g, '<span class="csharp-method">$1</span>');
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;


     // Highlight class names and properties (start with uppercase letters)
     // Regex for matching C# keywords
     code = code.replace(/\b([A-Z][a-zA-Z0-9_]*)\b/g, '<span class="csharp-class">$1</span>');
    const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(regexKeywords, '<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;
 
    // Regex for matching C# types
    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;
 
    // Regex for method names (avoid highlighting keywords or types)
    const regexMethods = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\()/g;
     code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');
 
    pre.innerHTML = code;
});
 
// Highlight C# class names and property names
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
 
    // Regex for class names (PascalCase)
    const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
    code = code.replace(regexClasses, '<span class="csharp-class">$1</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"
];
 
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
    let braceStack = [];
    let braceCount = 0;
 
    // Regex for matching curly braces
    code = code.replace(/{|}/g, (brace) => {
        const color = rainbowColors[braceCount % rainbowColors.length];
 
        if (brace === "{") {
            braceStack.push(color); // Push color for opening brace
        } else {
            const pairColor = braceStack.pop(); // Pop color for closing brace
        }
 
        braceCount++;
        return `<span class="csharp-brace" style="color: ${color};">${brace}</span>`;
    });


    // Safely apply the modified HTML back to the <pre> element
     pre.innerHTML = code;
     pre.innerHTML = code;
});
});

Revision as of 13:44, 18 January 2025

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

// Highlight C# comments (single-line and multi-line FIRST to override other syntax)
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

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

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

    pre.innerHTML = code;
});

// 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;

    // Regex for matching C# keywords
    const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(regexKeywords, '<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;

    // Regex for matching C# types
    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;

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

    pre.innerHTML = code;
});

// Highlight C# class names and property names
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

    // Regex for class names (PascalCase)
    const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
    code = code.replace(regexClasses, '<span class="csharp-class">$1</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"
];

document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
    let braceStack = [];
    let braceCount = 0;

    // Regex for matching curly braces
    code = code.replace(/{|}/g, (brace) => {
        const color = rainbowColors[braceCount % rainbowColors.length];

        if (brace === "{") {
            braceStack.push(color); // Push color for opening brace
        } else {
            const pairColor = braceStack.pop(); // Pop color for closing brace
        }

        braceCount++;
        return `<span class="csharp-brace" style="color: ${color};">${brace}</span>`;
    });

    pre.innerHTML = code;
});