tornado.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'), require('../htmlmixed/htmlmixed'), require('../../addon/mode/overlay'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../htmlmixed/htmlmixed', '../../addon/mode/overlay'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineMode('tornado:inner', function () {
  15. var keywords = [
  16. 'and',
  17. 'as',
  18. 'assert',
  19. 'autoescape',
  20. 'block',
  21. 'break',
  22. 'class',
  23. 'comment',
  24. 'context',
  25. 'continue',
  26. 'datetime',
  27. 'def',
  28. 'del',
  29. 'elif',
  30. 'else',
  31. 'end',
  32. 'escape',
  33. 'except',
  34. 'exec',
  35. 'extends',
  36. 'false',
  37. 'finally',
  38. 'for',
  39. 'from',
  40. 'global',
  41. 'if',
  42. 'import',
  43. 'in',
  44. 'include',
  45. 'is',
  46. 'json_encode',
  47. 'lambda',
  48. 'length',
  49. 'linkify',
  50. 'load',
  51. 'module',
  52. 'none',
  53. 'not',
  54. 'or',
  55. 'pass',
  56. 'print',
  57. 'put',
  58. 'raise',
  59. 'raw',
  60. 'return',
  61. 'self',
  62. 'set',
  63. 'squeeze',
  64. 'super',
  65. 'true',
  66. 'try',
  67. 'url_escape',
  68. 'while',
  69. 'with',
  70. 'without',
  71. 'xhtml_escape',
  72. 'yield',
  73. ]
  74. keywords = new RegExp('^((' + keywords.join(')|(') + '))\\b')
  75. function tokenBase(stream, state) {
  76. stream.eatWhile(/[^\{]/)
  77. var ch = stream.next()
  78. if (ch == '{') {
  79. if ((ch = stream.eat(/\{|%|#/))) {
  80. state.tokenize = inTag(ch)
  81. return 'tag'
  82. }
  83. }
  84. }
  85. function inTag(close) {
  86. if (close == '{') {
  87. close = '}'
  88. }
  89. return function (stream, state) {
  90. var ch = stream.next()
  91. if (ch == close && stream.eat('}')) {
  92. state.tokenize = tokenBase
  93. return 'tag'
  94. }
  95. if (stream.match(keywords)) {
  96. return 'keyword'
  97. }
  98. return close == '#' ? 'comment' : 'string'
  99. }
  100. }
  101. return {
  102. startState: function () {
  103. return { tokenize: tokenBase }
  104. },
  105. token: function (stream, state) {
  106. return state.tokenize(stream, state)
  107. },
  108. }
  109. })
  110. CodeMirror.defineMode('tornado', function (config) {
  111. var htmlBase = CodeMirror.getMode(config, 'text/html')
  112. var tornadoInner = CodeMirror.getMode(config, 'tornado:inner')
  113. return CodeMirror.overlayMode(htmlBase, tornadoInner)
  114. })
  115. CodeMirror.defineMIME('text/x-tornado', 'tornado')
  116. })