tornado.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"),
  6. require("../../addon/mode/overlay"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror", "../htmlmixed/htmlmixed",
  9. "../../addon/mode/overlay"], mod);
  10. else // Plain browser env
  11. mod(CodeMirror);
  12. })(function(CodeMirror) {
  13. "use strict";
  14. CodeMirror.defineMode("tornado:inner", function() {
  15. var keywords = ["and","as","assert","autoescape","block","break","class","comment","context",
  16. "continue","datetime","def","del","elif","else","end","escape","except",
  17. "exec","extends","false","finally","for","from","global","if","import","in",
  18. "include","is","json_encode","lambda","length","linkify","load","module",
  19. "none","not","or","pass","print","put","raise","raw","return","self","set",
  20. "squeeze","super","true","try","url_escape","while","with","without","xhtml_escape","yield"];
  21. keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b");
  22. function tokenBase (stream, state) {
  23. stream.eatWhile(/[^\{]/);
  24. var ch = stream.next();
  25. if (ch == "{") {
  26. if (ch = stream.eat(/\{|%|#/)) {
  27. state.tokenize = inTag(ch);
  28. return "tag";
  29. }
  30. }
  31. }
  32. function inTag (close) {
  33. if (close == "{") {
  34. close = "}";
  35. }
  36. return function (stream, state) {
  37. var ch = stream.next();
  38. if ((ch == close) && stream.eat("}")) {
  39. state.tokenize = tokenBase;
  40. return "tag";
  41. }
  42. if (stream.match(keywords)) {
  43. return "keyword";
  44. }
  45. return close == "#" ? "comment" : "string";
  46. };
  47. }
  48. return {
  49. startState: function () {
  50. return {tokenize: tokenBase};
  51. },
  52. token: function (stream, state) {
  53. return state.tokenize(stream, state);
  54. }
  55. };
  56. });
  57. CodeMirror.defineMode("tornado", function(config) {
  58. var htmlBase = CodeMirror.getMode(config, "text/html");
  59. var tornadoInner = CodeMirror.getMode(config, "tornado:inner");
  60. return CodeMirror.overlayMode(htmlBase, tornadoInner);
  61. });
  62. CodeMirror.defineMIME("text/x-tornado", "tornado");
  63. });