twig.js 1.9 KB

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