twig.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module.exports = function(hljs) {
  2. var PARAMS = {
  3. className: 'params',
  4. begin: '\\(', end: '\\)'
  5. };
  6. var FUNCTION_NAMES = 'attribute block constant cycle date dump include ' +
  7. 'max min parent random range source template_from_string';
  8. var FUNCTIONS = {
  9. beginKeywords: FUNCTION_NAMES,
  10. keywords: {name: FUNCTION_NAMES},
  11. relevance: 0,
  12. contains: [
  13. PARAMS
  14. ]
  15. };
  16. var FILTER = {
  17. begin: /\|[A-Za-z_]+:?/,
  18. keywords:
  19. 'abs batch capitalize column convert_encoding date date_modify default ' +
  20. 'escape filter first format inky_to_html inline_css join json_encode keys last ' +
  21. 'length lower map markdown merge nl2br number_format raw reduce replace ' +
  22. 'reverse round slice sort spaceless split striptags title trim upper url_encode',
  23. contains: [
  24. FUNCTIONS
  25. ]
  26. };
  27. var TAGS = 'apply autoescape block deprecated do embed extends filter flush for from ' +
  28. 'if import include macro sandbox set use verbatim with';
  29. TAGS = TAGS + ' ' + TAGS.split(' ').map(function(t){return 'end' + t}).join(' ');
  30. return {
  31. aliases: ['craftcms'],
  32. case_insensitive: true,
  33. subLanguage: 'xml',
  34. contains: [
  35. hljs.COMMENT(/\{#/, /#}/),
  36. {
  37. className: 'template-tag',
  38. begin: /\{%/, end: /%}/,
  39. contains: [
  40. {
  41. className: 'name',
  42. begin: /\w+/,
  43. keywords: TAGS,
  44. starts: {
  45. endsWithParent: true,
  46. contains: [FILTER, FUNCTIONS],
  47. relevance: 0
  48. }
  49. }
  50. ]
  51. },
  52. {
  53. className: 'template-variable',
  54. begin: /\{\{/, end: /}}/,
  55. contains: ['self', FILTER, FUNCTIONS]
  56. }
  57. ]
  58. };
  59. };