l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

python.js (3582B)


      1 /*
      2 Language: Python
      3 Description: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
      4 Website: https://www.python.org
      5 Category: common
      6 */
      7 
      8 function python(hljs) {
      9   var KEYWORDS = {
     10     keyword:
     11       'and elif is global as in if from raise for except finally print import pass return ' +
     12       'exec else break not with class assert yield try while continue del or def lambda ' +
     13       'async await nonlocal|10',
     14     built_in:
     15       'Ellipsis NotImplemented',
     16     literal: 'False None True'
     17   };
     18   var PROMPT = {
     19     className: 'meta',  begin: /^(>>>|\.\.\.) /
     20   };
     21   var SUBST = {
     22     className: 'subst',
     23     begin: /\{/, end: /\}/,
     24     keywords: KEYWORDS,
     25     illegal: /#/
     26   };
     27   var LITERAL_BRACKET = {
     28     begin: /\{\{/,
     29     relevance: 0
     30   };
     31   var STRING = {
     32     className: 'string',
     33     contains: [hljs.BACKSLASH_ESCAPE],
     34     variants: [
     35       {
     36         begin: /(u|b)?r?'''/, end: /'''/,
     37         contains: [hljs.BACKSLASH_ESCAPE, PROMPT],
     38         relevance: 10
     39       },
     40       {
     41         begin: /(u|b)?r?"""/, end: /"""/,
     42         contains: [hljs.BACKSLASH_ESCAPE, PROMPT],
     43         relevance: 10
     44       },
     45       {
     46         begin: /(fr|rf|f)'''/, end: /'''/,
     47         contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST]
     48       },
     49       {
     50         begin: /(fr|rf|f)"""/, end: /"""/,
     51         contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST]
     52       },
     53       {
     54         begin: /(u|r|ur)'/, end: /'/,
     55         relevance: 10
     56       },
     57       {
     58         begin: /(u|r|ur)"/, end: /"/,
     59         relevance: 10
     60       },
     61       {
     62         begin: /(b|br)'/, end: /'/
     63       },
     64       {
     65         begin: /(b|br)"/, end: /"/
     66       },
     67       {
     68         begin: /(fr|rf|f)'/, end: /'/,
     69         contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST]
     70       },
     71       {
     72         begin: /(fr|rf|f)"/, end: /"/,
     73         contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST]
     74       },
     75       hljs.APOS_STRING_MODE,
     76       hljs.QUOTE_STRING_MODE
     77     ]
     78   };
     79   var NUMBER = {
     80     className: 'number', relevance: 0,
     81     variants: [
     82       {begin: hljs.BINARY_NUMBER_RE + '[lLjJ]?'},
     83       {begin: '\\b(0o[0-7]+)[lLjJ]?'},
     84       {begin: hljs.C_NUMBER_RE + '[lLjJ]?'}
     85     ]
     86   };
     87   var PARAMS = {
     88     className: 'params',
     89     variants: [
     90       // Exclude params at functions without params
     91       {begin: /\(\s*\)/, skip: true, className: null },
     92       {
     93         begin: /\(/, end: /\)/, excludeBegin: true, excludeEnd: true,
     94         contains: ['self', PROMPT, NUMBER, STRING, hljs.HASH_COMMENT_MODE],
     95       },
     96     ],
     97   };
     98   SUBST.contains = [STRING, NUMBER, PROMPT];
     99   return {
    100     name: 'Python',
    101     aliases: ['py', 'gyp', 'ipython'],
    102     keywords: KEYWORDS,
    103     illegal: /(<\/|->|\?)|=>/,
    104     contains: [
    105       PROMPT,
    106       NUMBER,
    107       // eat "if" prior to string so that it won't accidentally be
    108       // labeled as an f-string as in:
    109       { beginKeywords: "if", relevance: 0 },
    110       STRING,
    111       hljs.HASH_COMMENT_MODE,
    112       {
    113         variants: [
    114           {className: 'function', beginKeywords: 'def'},
    115           {className: 'class', beginKeywords: 'class'}
    116         ],
    117         end: /:/,
    118         illegal: /[${=;\n,]/,
    119         contains: [
    120           hljs.UNDERSCORE_TITLE_MODE,
    121           PARAMS,
    122           {
    123             begin: /->/, endsWithParent: true,
    124             keywords: 'None'
    125           }
    126         ]
    127       },
    128       {
    129         className: 'meta',
    130         begin: /^[\t ]*@/, end: /$/
    131       },
    132       {
    133         begin: /\b(print|exec)\(/ // don’t highlight keywords-turned-functions in Python 3
    134       }
    135     ]
    136   };
    137 }
    138 
    139 module.exports = python;