prism-django.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Django/Jinja2 syntax definition for Prism.js <http://prismjs.com> syntax highlighter.
  2. // Mostly it works OK but can paint code incorrectly on complex html/template tag combinations.
  3. (function (Prism) {
  4. Prism.languages.django = {
  5. 'comment': /^\{#[\s\S]*?#\}$/,
  6. 'tag': {
  7. pattern: /(^\{%[+-]?\s*)\w+/,
  8. lookbehind: true,
  9. alias: 'keyword'
  10. },
  11. 'delimiter': {
  12. pattern: /^\{[{%][+-]?|[+-]?[}%]\}$/,
  13. alias: 'punctuation'
  14. },
  15. 'string': {
  16. pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
  17. greedy: true
  18. },
  19. 'filter': {
  20. pattern: /(\|)\w+/,
  21. lookbehind: true,
  22. alias: 'function'
  23. },
  24. 'test': {
  25. pattern: /(\bis\s+(?:not\s+)?)(?!not\b)\w+/,
  26. lookbehind: true,
  27. alias: 'function'
  28. },
  29. 'function': /\b[a-z_]\w+(?=\s*\()/i,
  30. 'keyword': /\b(?:and|as|by|else|for|if|import|in|is|loop|not|or|recursive|with|without)\b/,
  31. 'operator': /[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,
  32. 'number': /\b\d+(?:\.\d+)?\b/,
  33. 'boolean': /[Ff]alse|[Nn]one|[Tt]rue/,
  34. 'variable': /\b\w+\b/,
  35. 'punctuation': /[{}[\](),.:;]/
  36. };
  37. var pattern = /\{\{[\s\S]*?\}\}|\{%[\s\S]*?%\}|\{#[\s\S]*?#\}/g;
  38. var markupTemplating = Prism.languages['markup-templating'];
  39. Prism.hooks.add('before-tokenize', function (env) {
  40. markupTemplating.buildPlaceholders(env, 'django', pattern);
  41. });
  42. Prism.hooks.add('after-tokenize', function (env) {
  43. markupTemplating.tokenizePlaceholders(env, 'django');
  44. });
  45. // Add an Jinja2 alias
  46. Prism.languages.jinja2 = Prism.languages.django;
  47. Prism.hooks.add('before-tokenize', function (env) {
  48. markupTemplating.buildPlaceholders(env, 'jinja2', pattern);
  49. });
  50. Prism.hooks.add('after-tokenize', function (env) {
  51. markupTemplating.tokenizePlaceholders(env, 'jinja2');
  52. });
  53. }(Prism));