prism-twig.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Prism.languages.twig = {
  2. 'comment': /^\{#[\s\S]*?#\}$/,
  3. 'tag-name': {
  4. pattern: /(^\{%-?\s*)\w+/,
  5. lookbehind: true,
  6. alias: 'keyword'
  7. },
  8. 'delimiter': {
  9. pattern: /^\{[{%]-?|-?[%}]\}$/,
  10. alias: 'punctuation'
  11. },
  12. 'string': {
  13. pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
  14. inside: {
  15. 'punctuation': /^['"]|['"]$/
  16. }
  17. },
  18. 'keyword': /\b(?:even|if|odd)\b/,
  19. 'boolean': /\b(?:false|null|true)\b/,
  20. 'number': /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
  21. 'operator': [
  22. {
  23. pattern: /(\s)(?:and|b-and|b-or|b-xor|ends with|in|is|matches|not|or|same as|starts with)(?=\s)/,
  24. lookbehind: true
  25. },
  26. /[=<>]=?|!=|\*\*?|\/\/?|\?:?|[-+~%|]/
  27. ],
  28. 'punctuation': /[()\[\]{}:.,]/
  29. };
  30. Prism.hooks.add('before-tokenize', function (env) {
  31. if (env.language !== 'twig') {
  32. return;
  33. }
  34. var pattern = /\{(?:#[\s\S]*?#|%[\s\S]*?%|\{[\s\S]*?\})\}/g;
  35. Prism.languages['markup-templating'].buildPlaceholders(env, 'twig', pattern);
  36. });
  37. Prism.hooks.add('after-tokenize', function (env) {
  38. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'twig');
  39. });