python.js 3.1 KB

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