Skip to content

Instantly share code, notes, and snippets.

@mimoo
Last active October 11, 2024 22:02
Show Gist options
  • Select an option

  • Save mimoo/fd9652b0c0d64bfc9f8cae69c72030b1 to your computer and use it in GitHub Desktop.

Select an option

Save mimoo/fd9652b0c0d64bfc9f8cae69c72030b1 to your computer and use it in GitHub Desktop.

Revisions

  1. mimoo revised this gist Oct 11, 2024. 1 changed file with 396 additions and 0 deletions.
    396 changes: 396 additions & 0 deletions respec-py.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,396 @@
    /*
    Language: Python
    Description: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
    Website: https://www.python.org
    Category: common
    */
    var module = module ? module : {}; // shim for browser use
    function hljsDefinePython(hljs) {
    const regex = hljs.regex;
    const IDENT_RE = /[\p{XID_Start}_]\p{XID_Continue}*/u;
    const RESERVED_WORDS = [
    "and",
    "as",
    "assert",
    "async",
    "await",
    "break",
    "case",
    "class",
    "continue",
    "def",
    "del",
    "elif",
    "else",
    "except",
    "finally",
    "for",
    "from",
    "global",
    "if",
    "import",
    "in",
    "is",
    "lambda",
    "match",
    "nonlocal|10",
    "not",
    "or",
    "pass",
    "raise",
    "return",
    "try",
    "while",
    "with",
    "yield",
    ];

    const BUILT_INS = [
    "__import__",
    "abs",
    "all",
    "any",
    "ascii",
    "bin",
    "bool",
    "breakpoint",
    "bytearray",
    "bytes",
    "callable",
    "chr",
    "classmethod",
    "compile",
    "complex",
    "delattr",
    "dict",
    "dir",
    "divmod",
    "enumerate",
    "eval",
    "exec",
    "filter",
    "float",
    "format",
    "frozenset",
    "getattr",
    "globals",
    "hasattr",
    "hash",
    "help",
    "hex",
    "id",
    "input",
    "int",
    "isinstance",
    "issubclass",
    "iter",
    "len",
    "list",
    "locals",
    "map",
    "max",
    "memoryview",
    "min",
    "next",
    "object",
    "oct",
    "open",
    "ord",
    "pow",
    "print",
    "property",
    "range",
    "repr",
    "reversed",
    "round",
    "set",
    "setattr",
    "slice",
    "sorted",
    "staticmethod",
    "str",
    "sum",
    "super",
    "tuple",
    "type",
    "vars",
    "zip",
    ];

    const LITERALS = [
    "__debug__",
    "Ellipsis",
    "False",
    "None",
    "NotImplemented",
    "True",
    ];

    // https://docs.python.org/3/library/typing.html
    // TODO: Could these be supplemented by a CamelCase matcher in certain
    // contexts, leaving these remaining only for relevance hinting?
    const TYPES = [
    "Any",
    "Callable",
    "Coroutine",
    "Dict",
    "List",
    "Literal",
    "Generic",
    "Optional",
    "Sequence",
    "Set",
    "Tuple",
    "Type",
    "Union",
    ];

    const KEYWORDS = {
    $pattern: /[A-Za-z]\w+|__\w+__/,
    keyword: RESERVED_WORDS,
    built_in: BUILT_INS,
    literal: LITERALS,
    type: TYPES,
    };

    const PROMPT = {
    className: "meta",
    begin: /^(>>>|\.\.\.) /,
    };

    const SUBST = {
    className: "subst",
    begin: /\{/,
    end: /\}/,
    keywords: KEYWORDS,
    illegal: /#/,
    };

    const LITERAL_BRACKET = {
    begin: /\{\{/,
    relevance: 0,
    };

    const STRING = {
    className: "string",
    contains: [hljs.BACKSLASH_ESCAPE],
    variants: [
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,
    end: /'''/,
    contains: [hljs.BACKSLASH_ESCAPE, PROMPT],
    relevance: 10,
    },
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,
    end: /"""/,
    contains: [hljs.BACKSLASH_ESCAPE, PROMPT],
    relevance: 10,
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'''/,
    end: /'''/,
    contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"""/,
    end: /"""/,
    contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST],
    },
    {
    begin: /([uU]|[rR])'/,
    end: /'/,
    relevance: 10,
    },
    {
    begin: /([uU]|[rR])"/,
    end: /"/,
    relevance: 10,
    },
    {
    begin: /([bB]|[bB][rR]|[rR][bB])'/,
    end: /'/,
    },
    {
    begin: /([bB]|[bB][rR]|[rR][bB])"/,
    end: /"/,
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'/,
    end: /'/,
    contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"/,
    end: /"/,
    contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST],
    },
    hljs.APOS_STRING_MODE,
    hljs.QUOTE_STRING_MODE,
    ],
    };

    // https://docs.python.org/3.9/reference/lexical_analysis.html#numeric-literals
    const digitpart = "[0-9](_?[0-9])*";
    const pointfloat = `(\\b(${digitpart}))?\\.(${digitpart})|\\b(${digitpart})\\.`;
    // Whitespace after a number (or any lexical token) is needed only if its absence
    // would change the tokenization
    // https://docs.python.org/3.9/reference/lexical_analysis.html#whitespace-between-tokens
    // We deviate slightly, requiring a word boundary or a keyword
    // to avoid accidentally recognizing *prefixes* (e.g., `0` in `0x41` or `08` or `0__1`)
    const lookahead = `\\b|${RESERVED_WORDS.join("|")}`;
    const NUMBER = {
    className: "number",
    relevance: 0,
    variants: [
    // exponentfloat, pointfloat
    // https://docs.python.org/3.9/reference/lexical_analysis.html#floating-point-literals
    // optionally imaginary
    // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
    // Note: no leading \b because floats can start with a decimal point
    // and we don't want to mishandle e.g. `fn(.5)`,
    // no trailing \b for pointfloat because it can end with a decimal point
    // and we don't want to mishandle e.g. `0..hex()`; this should be safe
    // because both MUST contain a decimal point and so cannot be confused with
    // the interior part of an identifier
    {
    begin: `(\\b(${digitpart})|(${pointfloat}))[eE][+-]?(${digitpart})[jJ]?(?=${lookahead})`,
    },
    {
    begin: `(${pointfloat})[jJ]?`,
    },

    // decinteger, bininteger, octinteger, hexinteger
    // https://docs.python.org/3.9/reference/lexical_analysis.html#integer-literals
    // optionally "long" in Python 2
    // https://docs.python.org/2.7/reference/lexical_analysis.html#integer-and-long-integer-literals
    // decinteger is optionally imaginary
    // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
    {
    begin: `\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${lookahead})`,
    },
    {
    begin: `\\b0[bB](_?[01])+[lL]?(?=${lookahead})`,
    },
    {
    begin: `\\b0[oO](_?[0-7])+[lL]?(?=${lookahead})`,
    },
    {
    begin: `\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${lookahead})`,
    },

    // imagnumber (digitpart-based)
    // https://docs.python.org/3.9/reference/lexical_analysis.html#imaginary-literals
    {
    begin: `\\b(${digitpart})[jJ](?=${lookahead})`,
    },
    ],
    };
    const COMMENT_TYPE = {
    className: "comment",
    begin: regex.lookahead(/# type:/),
    end: /$/,
    keywords: KEYWORDS,
    contains: [
    {
    // prevent keywords from coloring `type`
    begin: /# type:/,
    },
    // comment within a datatype comment includes no keywords
    {
    begin: /#/,
    end: /\b\B/,
    endsWithParent: true,
    },
    ],
    };
    const PARAMS = {
    className: "params",
    variants: [
    // Exclude params in functions without params
    {
    className: "",
    begin: /\(\s*\)/,
    skip: true,
    },
    {
    begin: /\(/,
    end: /\)/,
    excludeBegin: true,
    excludeEnd: true,
    keywords: KEYWORDS,
    contains: ["self", PROMPT, NUMBER, STRING, hljs.HASH_COMMENT_MODE],
    },
    ],
    };
    SUBST.contains = [STRING, NUMBER, PROMPT];

    return {
    name: "Python",
    aliases: ["py", "gyp", "ipython"],
    unicodeRegex: true,
    keywords: KEYWORDS,
    illegal: /(<\/|\?)|=>/,
    contains: [
    PROMPT,
    NUMBER,
    {
    // very common convention
    scope: "variable.language",
    match: /\bself\b/,
    },
    {
    // eat "if" prior to string so that it won't accidentally be
    // labeled as an f-string
    beginKeywords: "if",
    relevance: 0,
    },
    { match: /\bor\b/, scope: "keyword" },
    STRING,
    COMMENT_TYPE,
    hljs.HASH_COMMENT_MODE,
    {
    match: [/\bdef/, /\s+/, IDENT_RE],
    scope: {
    1: "keyword",
    3: "title.function",
    },
    contains: [PARAMS],
    },
    {
    variants: [
    {
    match: [
    /\bclass/,
    /\s+/,
    IDENT_RE,
    /\s*/,
    /\(\s*/,
    IDENT_RE,
    /\s*\)/,
    ],
    },
    {
    match: [/\bclass/, /\s+/, IDENT_RE],
    },
    ],
    scope: {
    1: "keyword",
    3: "title.class",
    6: "title.class.inherited",
    },
    },
    {
    className: "meta",
    begin: /^[\t ]*@/,
    end: /(?=#)|$/,
    contains: [NUMBER, PARAMS, STRING],
    },
    ],
    };
    }
    module.exports = function (hljs) {
    hljs.registerLanguage("py", hljsDefinePython);
    };

    module.exports.definer = hljsDefinePython;
  2. mimoo revised this gist Oct 11, 2024. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions hljs-es-py.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    /*! `python` grammar compiled for Highlight.js 11.10.0 */
    var hljsGrammar=(()=>{"use strict";return e=>{
    const n=e.regex,a=/[\p{XID_Start}_]\p{XID_Continue}*/u,s=["and","as","assert","async","await","break","case","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","match","nonlocal|10","not","or","pass","raise","return","try","while","with","yield"],t={
    $pattern:/[A-Za-z]\w+|__\w+__/,keyword:s,
    built_in:["__import__","abs","all","any","ascii","bin","bool","breakpoint","bytearray","bytes","callable","chr","classmethod","compile","complex","delattr","dict","dir","divmod","enumerate","eval","exec","filter","float","format","frozenset","getattr","globals","hasattr","hash","help","hex","id","input","int","isinstance","issubclass","iter","len","list","locals","map","max","memoryview","min","next","object","oct","open","ord","pow","print","property","range","repr","reversed","round","set","setattr","slice","sorted","staticmethod","str","sum","super","tuple","type","vars","zip"],
    literal:["__debug__","Ellipsis","False","None","NotImplemented","True"],
    type:["Any","Callable","Coroutine","Dict","List","Literal","Generic","Optional","Sequence","Set","Tuple","Type","Union"]
    },i={className:"meta",begin:/^(>>>|\.\.\.) /},r={className:"subst",begin:/\{/,
    end:/\}/,keywords:t,illegal:/#/},l={begin:/\{\{/,relevance:0},o={
    className:"string",contains:[e.BACKSLASH_ESCAPE],variants:[{
    begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,end:/'''/,
    contains:[e.BACKSLASH_ESCAPE,i],relevance:10},{
    begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,end:/"""/,
    contains:[e.BACKSLASH_ESCAPE,i],relevance:10},{
    begin:/([fF][rR]|[rR][fF]|[fF])'''/,end:/'''/,
    contains:[e.BACKSLASH_ESCAPE,i,l,r]},{begin:/([fF][rR]|[rR][fF]|[fF])"""/,
    end:/"""/,contains:[e.BACKSLASH_ESCAPE,i,l,r]},{begin:/([uU]|[rR])'/,end:/'/,
    relevance:10},{begin:/([uU]|[rR])"/,end:/"/,relevance:10},{
    begin:/([bB]|[bB][rR]|[rR][bB])'/,end:/'/},{begin:/([bB]|[bB][rR]|[rR][bB])"/,
    end:/"/},{begin:/([fF][rR]|[rR][fF]|[fF])'/,end:/'/,
    contains:[e.BACKSLASH_ESCAPE,l,r]},{begin:/([fF][rR]|[rR][fF]|[fF])"/,end:/"/,
    contains:[e.BACKSLASH_ESCAPE,l,r]},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE]
    },b="[0-9](_?[0-9])*",c=`(\\b(${b}))?\\.(${b})|\\b(${b})\\.`,d="\\b|"+s.join("|"),m={
    className:"number",relevance:0,variants:[{
    begin:`(\\b(${b})|(${c}))[eE][+-]?(${b})[jJ]?(?=${d})`},{begin:`(${c})[jJ]?`},{
    begin:`\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${d})`},{
    begin:`\\b0[bB](_?[01])+[lL]?(?=${d})`},{begin:`\\b0[oO](_?[0-7])+[lL]?(?=${d})`
    },{begin:`\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${d})`},{begin:`\\b(${b})[jJ](?=${d})`
    }]},g={className:"comment",begin:n.lookahead(/# type:/),end:/$/,keywords:t,
    contains:[{begin:/# type:/},{begin:/#/,end:/\b\B/,endsWithParent:!0}]},p={
    className:"params",variants:[{className:"",begin:/\(\s*\)/,skip:!0},{begin:/\(/,
    end:/\)/,excludeBegin:!0,excludeEnd:!0,keywords:t,
    contains:["self",i,m,o,e.HASH_COMMENT_MODE]}]};return r.contains=[o,m,i],{
    name:"Python",aliases:["py","gyp","ipython"],unicodeRegex:!0,keywords:t,
    illegal:/(<\/|\?)|=>/,contains:[i,m,{scope:"variable.language",match:/\bself\b/
    },{beginKeywords:"if",relevance:0},{match:/\bor\b/,scope:"keyword"
    },o,g,e.HASH_COMMENT_MODE,{match:[/\bdef/,/\s+/,a],scope:{1:"keyword",
    3:"title.function"},contains:[p]},{variants:[{
    match:[/\bclass/,/\s+/,a,/\s*/,/\(\s*/,a,/\s*\)/]},{match:[/\bclass/,/\s+/,a]}],
    scope:{1:"keyword",3:"title.class",6:"title.class.inherited"}},{
    className:"meta",begin:/^[\t ]*@/,end:/(?=#)|$/,contains:[m,p,o]}]}}})()
    ;export default hljsGrammar;
  3. mimoo revised this gist Oct 11, 2024. 1 changed file with 42 additions and 293 deletions.
    335 changes: 42 additions & 293 deletions hljs-py.js
    Original file line number Diff line number Diff line change
    @@ -1,293 +1,42 @@
    /*! `python` grammar compiled for Highlight.js 11.8.0 */
    (() => {
    var e = (() => {
    "use strict";
    return (e) => {
    const n = e.regex,
    a = /[\p{XID_Start}_]\p{XID_Continue}*/u,
    i = [
    "and",
    "as",
    "assert",
    "async",
    "await",
    "break",
    "case",
    "class",
    "continue",
    "def",
    "del",
    "elif",
    "else",
    "except",
    "finally",
    "for",
    "from",
    "global",
    "if",
    "import",
    "in",
    "is",
    "lambda",
    "match",
    "nonlocal|10",
    "not",
    "or",
    "pass",
    "raise",
    "return",
    "try",
    "while",
    "with",
    "yield",
    ],
    s = {
    $pattern: /[A-Za-z]\w+|__\w+__/,
    keyword: i,
    built_in: [
    "__import__",
    "abs",
    "all",
    "any",
    "ascii",
    "bin",
    "bool",
    "breakpoint",
    "bytearray",
    "bytes",
    "callable",
    "chr",
    "classmethod",
    "compile",
    "complex",
    "delattr",
    "dict",
    "dir",
    "divmod",
    "enumerate",
    "eval",
    "exec",
    "filter",
    "float",
    "format",
    "frozenset",
    "getattr",
    "globals",
    "hasattr",
    "hash",
    "help",
    "hex",
    "id",
    "input",
    "int",
    "isinstance",
    "issubclass",
    "iter",
    "len",
    "list",
    "locals",
    "map",
    "max",
    "memoryview",
    "min",
    "next",
    "object",
    "oct",
    "open",
    "ord",
    "pow",
    "print",
    "property",
    "range",
    "repr",
    "reversed",
    "round",
    "set",
    "setattr",
    "slice",
    "sorted",
    "staticmethod",
    "str",
    "sum",
    "super",
    "tuple",
    "type",
    "vars",
    "zip",
    ],
    literal: [
    "__debug__",
    "Ellipsis",
    "False",
    "None",
    "NotImplemented",
    "True",
    ],
    type: [
    "Any",
    "Callable",
    "Coroutine",
    "Dict",
    "List",
    "Literal",
    "Generic",
    "Optional",
    "Sequence",
    "Set",
    "Tuple",
    "Type",
    "Union",
    ],
    },
    t = { className: "meta", begin: /^(>>>|\.\.\.) / },
    r = {
    className: "subst",
    begin: /\{/,
    end: /\}/,
    keywords: s,
    illegal: /#/,
    },
    l = { begin: /\{\{/, relevance: 0 },
    b = {
    className: "string",
    contains: [e.BACKSLASH_ESCAPE],
    variants: [
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,
    end: /'''/,
    contains: [e.BACKSLASH_ESCAPE, t],
    relevance: 10,
    },
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,
    end: /"""/,
    contains: [e.BACKSLASH_ESCAPE, t],
    relevance: 10,
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'''/,
    end: /'''/,
    contains: [e.BACKSLASH_ESCAPE, t, l, r],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"""/,
    end: /"""/,
    contains: [e.BACKSLASH_ESCAPE, t, l, r],
    },
    { begin: /([uU]|[rR])'/, end: /'/, relevance: 10 },
    { begin: /([uU]|[rR])"/, end: /"/, relevance: 10 },
    {
    begin: /([bB]|[bB][rR]|[rR][bB])'/,
    end: /'/,
    },
    { begin: /([bB]|[bB][rR]|[rR][bB])"/, end: /"/ },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'/,
    end: /'/,
    contains: [e.BACKSLASH_ESCAPE, l, r],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"/,
    end: /"/,
    contains: [e.BACKSLASH_ESCAPE, l, r],
    },
    e.APOS_STRING_MODE,
    e.QUOTE_STRING_MODE,
    ],
    },
    o = "[0-9](_?[0-9])*",
    c = `(\\b(${o}))?\\.(${o})|\\b(${o})\\.`,
    d = "\\b|" + i.join("|"),
    g = {
    className: "number",
    relevance: 0,
    variants: [
    {
    begin: `(\\b(${o})|(${c}))[eE][+-]?(${o})[jJ]?(?=${d})`,
    },
    { begin: `(${c})[jJ]?` },
    {
    begin: `\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${d})`,
    },
    {
    begin: `\\b0[bB](_?[01])+[lL]?(?=${d})`,
    },
    { begin: `\\b0[oO](_?[0-7])+[lL]?(?=${d})` },
    { begin: `\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${d})` },
    { begin: `\\b(${o})[jJ](?=${d})` },
    ],
    },
    p = {
    className: "comment",
    begin: n.lookahead(/# type:/),
    end: /$/,
    keywords: s,
    contains: [
    { begin: /# type:/ },
    { begin: /#/, end: /\b\B/, endsWithParent: !0 },
    ],
    },
    m = {
    className: "params",
    variants: [
    { className: "", begin: /\(\s*\)/, skip: !0 },
    {
    begin: /\(/,
    end: /\)/,
    excludeBegin: !0,
    excludeEnd: !0,
    keywords: s,
    contains: ["self", t, g, b, e.HASH_COMMENT_MODE],
    },
    ],
    };
    return (
    (r.contains = [b, g, t]),
    {
    name: "Python",
    aliases: ["py", "gyp", "ipython"],
    unicodeRegex: !0,
    keywords: s,
    illegal: /(<\/|\?)|=>/,
    contains: [
    t,
    g,
    { begin: /\bself\b/ },
    { beginKeywords: "if", relevance: 0 },
    b,
    p,
    e.HASH_COMMENT_MODE,
    {
    match: [/\bdef/, /\s+/, a],
    scope: {
    1: "keyword",
    3: "title.function",
    },
    contains: [m],
    },
    {
    variants: [
    {
    match: [/\bclass/, /\s+/, a, /\s*/, /\(\s*/, a, /\s*\)/],
    },
    { match: [/\bclass/, /\s+/, a] },
    ],
    scope: {
    1: "keyword",
    3: "title.class",
    6: "title.class.inherited",
    },
    },
    {
    className: "meta",
    begin: /^[\t ]*@/,
    end: /(?=#)|$/,
    contains: [g, m, b],
    },
    ],
    }
    );
    };
    })();
    hljs.registerLanguage("python", e);
    })();
    /*! `python` grammar compiled for Highlight.js 11.10.0 */
    (()=>{var e=(()=>{"use strict";return e=>{
    const n=e.regex,a=/[\p{XID_Start}_]\p{XID_Continue}*/u,s=["and","as","assert","async","await","break","case","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","match","nonlocal|10","not","or","pass","raise","return","try","while","with","yield"],t={
    $pattern:/[A-Za-z]\w+|__\w+__/,keyword:s,
    built_in:["__import__","abs","all","any","ascii","bin","bool","breakpoint","bytearray","bytes","callable","chr","classmethod","compile","complex","delattr","dict","dir","divmod","enumerate","eval","exec","filter","float","format","frozenset","getattr","globals","hasattr","hash","help","hex","id","input","int","isinstance","issubclass","iter","len","list","locals","map","max","memoryview","min","next","object","oct","open","ord","pow","print","property","range","repr","reversed","round","set","setattr","slice","sorted","staticmethod","str","sum","super","tuple","type","vars","zip"],
    literal:["__debug__","Ellipsis","False","None","NotImplemented","True"],
    type:["Any","Callable","Coroutine","Dict","List","Literal","Generic","Optional","Sequence","Set","Tuple","Type","Union"]
    },i={className:"meta",begin:/^(>>>|\.\.\.) /},r={className:"subst",begin:/\{/,
    end:/\}/,keywords:t,illegal:/#/},l={begin:/\{\{/,relevance:0},o={
    className:"string",contains:[e.BACKSLASH_ESCAPE],variants:[{
    begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,end:/'''/,
    contains:[e.BACKSLASH_ESCAPE,i],relevance:10},{
    begin:/([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,end:/"""/,
    contains:[e.BACKSLASH_ESCAPE,i],relevance:10},{
    begin:/([fF][rR]|[rR][fF]|[fF])'''/,end:/'''/,
    contains:[e.BACKSLASH_ESCAPE,i,l,r]},{begin:/([fF][rR]|[rR][fF]|[fF])"""/,
    end:/"""/,contains:[e.BACKSLASH_ESCAPE,i,l,r]},{begin:/([uU]|[rR])'/,end:/'/,
    relevance:10},{begin:/([uU]|[rR])"/,end:/"/,relevance:10},{
    begin:/([bB]|[bB][rR]|[rR][bB])'/,end:/'/},{begin:/([bB]|[bB][rR]|[rR][bB])"/,
    end:/"/},{begin:/([fF][rR]|[rR][fF]|[fF])'/,end:/'/,
    contains:[e.BACKSLASH_ESCAPE,l,r]},{begin:/([fF][rR]|[rR][fF]|[fF])"/,end:/"/,
    contains:[e.BACKSLASH_ESCAPE,l,r]},e.APOS_STRING_MODE,e.QUOTE_STRING_MODE]
    },b="[0-9](_?[0-9])*",c=`(\\b(${b}))?\\.(${b})|\\b(${b})\\.`,d="\\b|"+s.join("|"),g={
    className:"number",relevance:0,variants:[{
    begin:`(\\b(${b})|(${c}))[eE][+-]?(${b})[jJ]?(?=${d})`},{begin:`(${c})[jJ]?`},{
    begin:`\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${d})`},{
    begin:`\\b0[bB](_?[01])+[lL]?(?=${d})`},{begin:`\\b0[oO](_?[0-7])+[lL]?(?=${d})`
    },{begin:`\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${d})`},{begin:`\\b(${b})[jJ](?=${d})`
    }]},p={className:"comment",begin:n.lookahead(/# type:/),end:/$/,keywords:t,
    contains:[{begin:/# type:/},{begin:/#/,end:/\b\B/,endsWithParent:!0}]},m={
    className:"params",variants:[{className:"",begin:/\(\s*\)/,skip:!0},{begin:/\(/,
    end:/\)/,excludeBegin:!0,excludeEnd:!0,keywords:t,
    contains:["self",i,g,o,e.HASH_COMMENT_MODE]}]};return r.contains=[o,g,i],{
    name:"Python",aliases:["py","gyp","ipython"],unicodeRegex:!0,keywords:t,
    illegal:/(<\/|\?)|=>/,contains:[i,g,{scope:"variable.language",match:/\bself\b/
    },{beginKeywords:"if",relevance:0},{match:/\bor\b/,scope:"keyword"
    },o,p,e.HASH_COMMENT_MODE,{match:[/\bdef/,/\s+/,a],scope:{1:"keyword",
    3:"title.function"},contains:[m]},{variants:[{
    match:[/\bclass/,/\s+/,a,/\s*/,/\(\s*/,a,/\s*\)/]},{match:[/\bclass/,/\s+/,a]}],
    scope:{1:"keyword",3:"title.class",6:"title.class.inherited"}},{
    className:"meta",begin:/^[\t ]*@/,end:/(?=#)|$/,contains:[g,m,o]}]}}})()
    ;hljs.registerLanguage("python",e)})();
  4. mimoo created this gist Oct 11, 2024.
    293 changes: 293 additions & 0 deletions hljs-py.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,293 @@
    /*! `python` grammar compiled for Highlight.js 11.8.0 */
    (() => {
    var e = (() => {
    "use strict";
    return (e) => {
    const n = e.regex,
    a = /[\p{XID_Start}_]\p{XID_Continue}*/u,
    i = [
    "and",
    "as",
    "assert",
    "async",
    "await",
    "break",
    "case",
    "class",
    "continue",
    "def",
    "del",
    "elif",
    "else",
    "except",
    "finally",
    "for",
    "from",
    "global",
    "if",
    "import",
    "in",
    "is",
    "lambda",
    "match",
    "nonlocal|10",
    "not",
    "or",
    "pass",
    "raise",
    "return",
    "try",
    "while",
    "with",
    "yield",
    ],
    s = {
    $pattern: /[A-Za-z]\w+|__\w+__/,
    keyword: i,
    built_in: [
    "__import__",
    "abs",
    "all",
    "any",
    "ascii",
    "bin",
    "bool",
    "breakpoint",
    "bytearray",
    "bytes",
    "callable",
    "chr",
    "classmethod",
    "compile",
    "complex",
    "delattr",
    "dict",
    "dir",
    "divmod",
    "enumerate",
    "eval",
    "exec",
    "filter",
    "float",
    "format",
    "frozenset",
    "getattr",
    "globals",
    "hasattr",
    "hash",
    "help",
    "hex",
    "id",
    "input",
    "int",
    "isinstance",
    "issubclass",
    "iter",
    "len",
    "list",
    "locals",
    "map",
    "max",
    "memoryview",
    "min",
    "next",
    "object",
    "oct",
    "open",
    "ord",
    "pow",
    "print",
    "property",
    "range",
    "repr",
    "reversed",
    "round",
    "set",
    "setattr",
    "slice",
    "sorted",
    "staticmethod",
    "str",
    "sum",
    "super",
    "tuple",
    "type",
    "vars",
    "zip",
    ],
    literal: [
    "__debug__",
    "Ellipsis",
    "False",
    "None",
    "NotImplemented",
    "True",
    ],
    type: [
    "Any",
    "Callable",
    "Coroutine",
    "Dict",
    "List",
    "Literal",
    "Generic",
    "Optional",
    "Sequence",
    "Set",
    "Tuple",
    "Type",
    "Union",
    ],
    },
    t = { className: "meta", begin: /^(>>>|\.\.\.) / },
    r = {
    className: "subst",
    begin: /\{/,
    end: /\}/,
    keywords: s,
    illegal: /#/,
    },
    l = { begin: /\{\{/, relevance: 0 },
    b = {
    className: "string",
    contains: [e.BACKSLASH_ESCAPE],
    variants: [
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?'''/,
    end: /'''/,
    contains: [e.BACKSLASH_ESCAPE, t],
    relevance: 10,
    },
    {
    begin: /([uU]|[bB]|[rR]|[bB][rR]|[rR][bB])?"""/,
    end: /"""/,
    contains: [e.BACKSLASH_ESCAPE, t],
    relevance: 10,
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'''/,
    end: /'''/,
    contains: [e.BACKSLASH_ESCAPE, t, l, r],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"""/,
    end: /"""/,
    contains: [e.BACKSLASH_ESCAPE, t, l, r],
    },
    { begin: /([uU]|[rR])'/, end: /'/, relevance: 10 },
    { begin: /([uU]|[rR])"/, end: /"/, relevance: 10 },
    {
    begin: /([bB]|[bB][rR]|[rR][bB])'/,
    end: /'/,
    },
    { begin: /([bB]|[bB][rR]|[rR][bB])"/, end: /"/ },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])'/,
    end: /'/,
    contains: [e.BACKSLASH_ESCAPE, l, r],
    },
    {
    begin: /([fF][rR]|[rR][fF]|[fF])"/,
    end: /"/,
    contains: [e.BACKSLASH_ESCAPE, l, r],
    },
    e.APOS_STRING_MODE,
    e.QUOTE_STRING_MODE,
    ],
    },
    o = "[0-9](_?[0-9])*",
    c = `(\\b(${o}))?\\.(${o})|\\b(${o})\\.`,
    d = "\\b|" + i.join("|"),
    g = {
    className: "number",
    relevance: 0,
    variants: [
    {
    begin: `(\\b(${o})|(${c}))[eE][+-]?(${o})[jJ]?(?=${d})`,
    },
    { begin: `(${c})[jJ]?` },
    {
    begin: `\\b([1-9](_?[0-9])*|0+(_?0)*)[lLjJ]?(?=${d})`,
    },
    {
    begin: `\\b0[bB](_?[01])+[lL]?(?=${d})`,
    },
    { begin: `\\b0[oO](_?[0-7])+[lL]?(?=${d})` },
    { begin: `\\b0[xX](_?[0-9a-fA-F])+[lL]?(?=${d})` },
    { begin: `\\b(${o})[jJ](?=${d})` },
    ],
    },
    p = {
    className: "comment",
    begin: n.lookahead(/# type:/),
    end: /$/,
    keywords: s,
    contains: [
    { begin: /# type:/ },
    { begin: /#/, end: /\b\B/, endsWithParent: !0 },
    ],
    },
    m = {
    className: "params",
    variants: [
    { className: "", begin: /\(\s*\)/, skip: !0 },
    {
    begin: /\(/,
    end: /\)/,
    excludeBegin: !0,
    excludeEnd: !0,
    keywords: s,
    contains: ["self", t, g, b, e.HASH_COMMENT_MODE],
    },
    ],
    };
    return (
    (r.contains = [b, g, t]),
    {
    name: "Python",
    aliases: ["py", "gyp", "ipython"],
    unicodeRegex: !0,
    keywords: s,
    illegal: /(<\/|\?)|=>/,
    contains: [
    t,
    g,
    { begin: /\bself\b/ },
    { beginKeywords: "if", relevance: 0 },
    b,
    p,
    e.HASH_COMMENT_MODE,
    {
    match: [/\bdef/, /\s+/, a],
    scope: {
    1: "keyword",
    3: "title.function",
    },
    contains: [m],
    },
    {
    variants: [
    {
    match: [/\bclass/, /\s+/, a, /\s*/, /\(\s*/, a, /\s*\)/],
    },
    { match: [/\bclass/, /\s+/, a] },
    ],
    scope: {
    1: "keyword",
    3: "title.class",
    6: "title.class.inherited",
    },
    },
    {
    className: "meta",
    begin: /^[\t ]*@/,
    end: /(?=#)|$/,
    contains: [g, m, b],
    },
    ],
    }
    );
    };
    })();
    hljs.registerLanguage("python", e);
    })();