yaml.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module.exports = function(hljs) {
  2. var LITERALS = 'true false yes no null';
  3. // Define keys as starting with a word character
  4. // ...containing word chars, spaces, colons, forward-slashes, hyphens and periods
  5. // ...and ending with a colon followed immediately by a space, tab or newline.
  6. // The YAML spec allows for much more than this, but this covers most use-cases.
  7. var KEY = {
  8. className: 'attr',
  9. variants: [
  10. { begin: '\\w[\\w :\\/.-]*:(?=[ \t]|$)' },
  11. { begin: '"\\w[\\w :\\/.-]*":(?=[ \t]|$)' }, //double quoted keys
  12. { begin: '\'\\w[\\w :\\/.-]*\':(?=[ \t]|$)' } //single quoted keys
  13. ]
  14. };
  15. var TEMPLATE_VARIABLES = {
  16. className: 'template-variable',
  17. variants: [
  18. { begin: '\{\{', end: '\}\}' }, // jinja templates Ansible
  19. { begin: '%\{', end: '\}' } // Ruby i18n
  20. ]
  21. };
  22. var STRING = {
  23. className: 'string',
  24. relevance: 0,
  25. variants: [
  26. {begin: /'/, end: /'/},
  27. {begin: /"/, end: /"/},
  28. {begin: /\S+/}
  29. ],
  30. contains: [
  31. hljs.BACKSLASH_ESCAPE,
  32. TEMPLATE_VARIABLES
  33. ]
  34. };
  35. return {
  36. case_insensitive: true,
  37. aliases: ['yml', 'YAML', 'yaml'],
  38. contains: [
  39. KEY,
  40. {
  41. className: 'meta',
  42. begin: '^---\s*$',
  43. relevance: 10
  44. },
  45. { // multi line string
  46. // Blocks start with a | or > followed by a newline
  47. //
  48. // Indentation of subsequent lines must be the same to
  49. // be considered part of the block
  50. className: 'string',
  51. begin: '[\\|>]([0-9]?[+-])?[ ]*\\n( *)[\\S ]+\\n(\\2[\\S ]+\\n?)*',
  52. },
  53. { // Ruby/Rails erb
  54. begin: '<%[%=-]?', end: '[%-]?%>',
  55. subLanguage: 'ruby',
  56. excludeBegin: true,
  57. excludeEnd: true,
  58. relevance: 0
  59. },
  60. { // local tags
  61. className: 'type',
  62. begin: '!' + hljs.UNDERSCORE_IDENT_RE,
  63. },
  64. { // data type
  65. className: 'type',
  66. begin: '!!' + hljs.UNDERSCORE_IDENT_RE,
  67. },
  68. { // fragment id &ref
  69. className: 'meta',
  70. begin: '&' + hljs.UNDERSCORE_IDENT_RE + '$',
  71. },
  72. { // fragment reference *ref
  73. className: 'meta',
  74. begin: '\\*' + hljs.UNDERSCORE_IDENT_RE + '$'
  75. },
  76. { // array listing
  77. className: 'bullet',
  78. // TODO: remove |$ hack when we have proper look-ahead support
  79. begin: '\\-(?=[ ]|$)',
  80. relevance: 0
  81. },
  82. hljs.HASH_COMMENT_MODE,
  83. {
  84. beginKeywords: LITERALS,
  85. keywords: {literal: LITERALS}
  86. },
  87. // numbers are any valid C-style number that
  88. // sit isolated from other words
  89. {
  90. className: 'number',
  91. begin: hljs.C_NUMBER_RE + '\\b'
  92. },
  93. STRING
  94. ]
  95. };
  96. };