handlebars.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('../../addon/mode/simple'), require('../../addon/mode/multiplex'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../../addon/mode/simple', '../../addon/mode/multiplex'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineSimpleMode('handlebars-tags', {
  15. start: [
  16. { regex: /\{\{\{/, push: 'handlebars_raw', token: 'tag' },
  17. { regex: /\{\{!--/, push: 'dash_comment', token: 'comment' },
  18. { regex: /\{\{!/, push: 'comment', token: 'comment' },
  19. { regex: /\{\{/, push: 'handlebars', token: 'tag' },
  20. ],
  21. handlebars_raw: [{ regex: /\}\}\}/, pop: true, token: 'tag' }],
  22. handlebars: [
  23. { regex: /\}\}/, pop: true, token: 'tag' },
  24. // Double and single quotes
  25. { regex: /"(?:[^\\"]|\\.)*"?/, token: 'string' },
  26. { regex: /'(?:[^\\']|\\.)*'?/, token: 'string' },
  27. // Handlebars keywords
  28. { regex: />|[#\/]([A-Za-z_]\w*)/, token: 'keyword' },
  29. { regex: /(?:else|this)\b/, token: 'keyword' },
  30. // Numeral
  31. { regex: /\d+/i, token: 'number' },
  32. // Atoms like = and .
  33. { regex: /=|~|@|true|false/, token: 'atom' },
  34. // Paths
  35. { regex: /(?:\.\.\/)*(?:[A-Za-z_][\w\.]*)+/, token: 'variable-2' },
  36. ],
  37. dash_comment: [
  38. { regex: /--\}\}/, pop: true, token: 'comment' },
  39. // Commented code
  40. { regex: /./, token: 'comment' },
  41. ],
  42. comment: [
  43. { regex: /\}\}/, pop: true, token: 'comment' },
  44. { regex: /./, token: 'comment' },
  45. ],
  46. meta: {
  47. blockCommentStart: '{{--',
  48. blockCommentEnd: '--}}',
  49. },
  50. })
  51. CodeMirror.defineMode('handlebars', function (config, parserConfig) {
  52. var handlebars = CodeMirror.getMode(config, 'handlebars-tags')
  53. if (!parserConfig || !parserConfig.base) return handlebars
  54. return CodeMirror.multiplexingMode(CodeMirror.getMode(config, parserConfig.base), { open: '{{', close: /\}\}\}?/, mode: handlebars, parseDelimiters: true })
  55. })
  56. CodeMirror.defineMIME('text/x-handlebars-template', 'handlebars')
  57. })