xml.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. module.exports = function(hljs) {
  2. var XML_IDENT_RE = '[A-Za-z0-9\\._:-]+';
  3. var XML_ENTITIES = {
  4. className: 'symbol',
  5. begin: '&[a-z]+;|&#[0-9]+;|&#x[a-f0-9]+;'
  6. };
  7. var XML_META_KEYWORDS = {
  8. begin: '\\s',
  9. contains:[
  10. {
  11. className: 'meta-keyword',
  12. begin: '#?[a-z_][a-z1-9_-]+',
  13. illegal: '\\n',
  14. }
  15. ]
  16. };
  17. var XML_META_PAR_KEYWORDS = hljs.inherit(XML_META_KEYWORDS, {begin: '\\(', end: '\\)'});
  18. var APOS_META_STRING_MODE = hljs.inherit(hljs.APOS_STRING_MODE, {className: 'meta-string'});
  19. var QUOTE_META_STRING_MODE = hljs.inherit(hljs.QUOTE_STRING_MODE, {className: 'meta-string'});
  20. var TAG_INTERNALS = {
  21. endsWithParent: true,
  22. illegal: /</,
  23. relevance: 0,
  24. contains: [
  25. {
  26. className: 'attr',
  27. begin: XML_IDENT_RE,
  28. relevance: 0
  29. },
  30. {
  31. begin: /=\s*/,
  32. relevance: 0,
  33. contains: [
  34. {
  35. className: 'string',
  36. endsParent: true,
  37. variants: [
  38. {begin: /"/, end: /"/, contains: [XML_ENTITIES]},
  39. {begin: /'/, end: /'/, contains: [XML_ENTITIES]},
  40. {begin: /[^\s"'=<>`]+/}
  41. ]
  42. }
  43. ]
  44. }
  45. ]
  46. };
  47. return {
  48. aliases: ['html', 'xhtml', 'rss', 'atom', 'xjb', 'xsd', 'xsl', 'plist', 'wsf', 'svg'],
  49. case_insensitive: true,
  50. contains: [
  51. {
  52. className: 'meta',
  53. begin: '<![a-z]', end: '>',
  54. relevance: 10,
  55. contains: [
  56. XML_META_KEYWORDS,
  57. QUOTE_META_STRING_MODE,
  58. APOS_META_STRING_MODE,
  59. XML_META_PAR_KEYWORDS,
  60. {
  61. begin: '\\[', end: '\\]',
  62. contains:[
  63. {
  64. className: 'meta',
  65. begin: '<![a-z]', end: '>',
  66. contains: [
  67. XML_META_KEYWORDS,
  68. XML_META_PAR_KEYWORDS,
  69. QUOTE_META_STRING_MODE,
  70. APOS_META_STRING_MODE
  71. ]
  72. }
  73. ]
  74. }
  75. ]
  76. },
  77. hljs.COMMENT(
  78. '<!--',
  79. '-->',
  80. {
  81. relevance: 10
  82. }
  83. ),
  84. {
  85. begin: '<\\!\\[CDATA\\[', end: '\\]\\]>',
  86. relevance: 10
  87. },
  88. XML_ENTITIES,
  89. {
  90. className: 'meta',
  91. begin: /<\?xml/, end: /\?>/, relevance: 10
  92. },
  93. {
  94. begin: /<\?(php)?/, end: /\?>/,
  95. subLanguage: 'php',
  96. contains: [
  97. // We don't want the php closing tag ?> to close the PHP block when
  98. // inside any of the following blocks:
  99. {begin: '/\\*', end: '\\*/', skip: true},
  100. {begin: 'b"', end: '"', skip: true},
  101. {begin: 'b\'', end: '\'', skip: true},
  102. hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}),
  103. hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true})
  104. ]
  105. },
  106. {
  107. className: 'tag',
  108. /*
  109. The lookahead pattern (?=...) ensures that 'begin' only matches
  110. '<style' as a single word, followed by a whitespace or an
  111. ending braket. The '$' is needed for the lexeme to be recognized
  112. by hljs.subMode() that tests lexemes outside the stream.
  113. */
  114. begin: '<style(?=\\s|>)', end: '>',
  115. keywords: {name: 'style'},
  116. contains: [TAG_INTERNALS],
  117. starts: {
  118. end: '</style>', returnEnd: true,
  119. subLanguage: ['css', 'xml']
  120. }
  121. },
  122. {
  123. className: 'tag',
  124. // See the comment in the <style tag about the lookahead pattern
  125. begin: '<script(?=\\s|>)', end: '>',
  126. keywords: {name: 'script'},
  127. contains: [TAG_INTERNALS],
  128. starts: {
  129. end: '\<\/script\>', returnEnd: true,
  130. subLanguage: ['actionscript', 'javascript', 'handlebars', 'xml']
  131. }
  132. },
  133. {
  134. className: 'tag',
  135. begin: '</?', end: '/?>',
  136. contains: [
  137. {
  138. className: 'name', begin: /[^\/><\s]+/, relevance: 0
  139. },
  140. TAG_INTERNALS
  141. ]
  142. }
  143. ]
  144. };
  145. };