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:
// Add 'csharp' class to all <pre> tags for easier targeting
document.querySelectorAll("pre").forEach(pre => {
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));
    pre.classList.add("csharp"); // Add 'csharp' class for easier targeting


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


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


     // Regex for matching single-line comments (//)
     // Highlight multi-line comments (/* */)
     const regexSingleLineComments = /\/\/[^\n]*/g;
     code = code.replace(/(\/\*[\s\S]*?\*\/)/g, '<span class="csharp-comment">$1</span>');
    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
     // Highlight keywords
     const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
     const keywords = [
     code = code.replace(regexKeywords, '<span class="csharp-keyword">$1</span>');
        "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 types
     // Highlight types
     const csharpTypes = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
     const types = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
     const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
     const typeRegex = new RegExp(`\\b(${types.join("|")})\\b`, "g");
     code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');
     code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');


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


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


    // Assign the modified HTML back to the <pre> tag
     pre.innerHTML = code;
     pre.innerHTML = code;
});
// Ensure protected spans (comments) are skipped in all transformations
document.querySelectorAll('[data-protected="true"]').forEach(element => {
    element.removeAttribute('data-protected');
});
});

Revision as of 13:40, 18 January 2025

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

    // Get the original code
    let code = pre.innerHTML;

    // Highlight single-line comments (//)
    code = code.replace(/(\/\/.*?$)/gm, '<span class="csharp-comment">$1</span>');

    // Highlight multi-line comments (/* */)
    code = code.replace(/(\/\*[\s\S]*?\*\/)/g, '<span class="csharp-comment">$1</span>');

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

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

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

    // Assign the modified HTML back to the <pre> tag
    pre.innerHTML = code;
});