Jump to content

MediaWiki:Common.js: Difference between revisions

From Knowledge Base
No edit summary
Tag: Reverted
No edit summary
 
(71 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Add csharp class to all <pre> tags
// Remove tools from sidebar
$(document).ready(function() {
    $('#p-tools').hide();
});
 
// Add 'csharp' class to all <pre> tags for easier targeting
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));


Line 14: Line 19:
];
];


// Highlight C# keywords and add more features (types, variables, comments, strings)
// 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 => {
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;
     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;
      
      
     // Highlight C# keywords
     // Apply the method name highlighting
    const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
     code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');
     code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');
      
      
     // Highlight types (int, double, string, etc.)
     pre.innerHTML = code;
     const typeRegex = /\b(int|double|string|bool|float|long|char|decimal)\b/g;
});
     code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');
 
   
// Function to check if a word is a keyword
     // Highlight variable names (anything that is not a keyword or type)
function isKeyword(word) {
     const variableRegex = /\b([a-zA-Z_]\w*)\b/g;
    return csharpKeywords.includes(word);
     code = code.replace(variableRegex, function(match, group1) {
}
         if (!csharpKeywords.includes(group1) && !["int", "double", "string", "bool", "float", "long", "char", "decimal"].includes(group1)) {
 
             return '<span class="csharp-variable">' + group1 + '</span>';
// 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
// Add rainbow curly braces highlighting
// 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 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>`;
         }
         }
         return match;
 
        // Apply color to opening brace
        braceCount++;
         return `<span class="csharp-brace" style="color: ${color};">${brace}</span>`;
    });
 
    pre.innerHTML = code;
});
 
// Highlight C# comments (single-line and multi-line)
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
 
    // Regex for matching single-line comments (//)
    const regexSingleLineComments = /(\/\/.*)/g;
    code = code.replace(regexSingleLineComments, match => {
        // Highlight the first word with the 'csharp-class' class
        const modifiedMatch = match.replace(/^(\w+)/, '<span class="csharp-class">$1</span>');
        return `<span class="csharp-comment">${modifiedMatch}</span>`;
    });
 
    // Regex for matching multi-line comments (/* */)
    const regexMultiLineComments = /(\/\*[\s\S]*?\*\/)/g;
    code = code.replace(regexMultiLineComments, match => {
        // Highlight the first word with the 'csharp-class' class
        const modifiedMatch = match.replace(/^(\w+)/, '<span class="csharp-class">$1</span>');
        return `<span class="csharp-comment">${modifiedMatch}</span>`;
     });
     });
   
 
    // Highlight single-line comments (//)
    const singleCommentRegex = /\/\/[^\n]*/g;
    code = code.replace(singleCommentRegex, '<span class="csharp-comment">$&</span>');
   
    // Highlight multi-line comments (/* */)
    const multiCommentRegex = /\/\*[\s\S]*?\*\//g;
    code = code.replace(multiCommentRegex, '<span class="csharp-comment">$&</span>');
   
    // Highlight strings (inside double quotes)
    const stringRegex = /"([^"]*)"/g;
    code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>');
   
     pre.innerHTML = code;
     pre.innerHTML = code;
});
});

Latest revision as of 13:59, 20 January 2025

// Remove tools from sidebar
$(document).ready(function() {
    $('#p-tools').hide();
});

// 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
// Add rainbow curly braces highlighting
// 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 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;
});

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

    // Regex for matching single-line comments (//)
    const regexSingleLineComments = /(\/\/.*)/g;
    code = code.replace(regexSingleLineComments, match => {
        // Highlight the first word with the 'csharp-class' class
        const modifiedMatch = match.replace(/^(\w+)/, '<span class="csharp-class">$1</span>');
        return `<span class="csharp-comment">${modifiedMatch}</span>`;
    });

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

    pre.innerHTML = code;
});