ruby.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. module.exports = function(hljs) {
  2. var RUBY_METHOD_RE = '[a-zA-Z_]\\w*[!?=]?|[-+~]\\@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?';
  3. var RUBY_KEYWORDS = {
  4. keyword:
  5. 'and then defined module in return redo if BEGIN retry end for self when ' +
  6. 'next until do begin unless END rescue else break undef not super class case ' +
  7. 'require yield alias while ensure elsif or include attr_reader attr_writer attr_accessor',
  8. literal:
  9. 'true false nil'
  10. };
  11. var YARDOCTAG = {
  12. className: 'doctag',
  13. begin: '@[A-Za-z]+'
  14. };
  15. var IRB_OBJECT = {
  16. begin: '#<', end: '>'
  17. };
  18. var COMMENT_MODES = [
  19. hljs.COMMENT(
  20. '#',
  21. '$',
  22. {
  23. contains: [YARDOCTAG]
  24. }
  25. ),
  26. hljs.COMMENT(
  27. '^\\=begin',
  28. '^\\=end',
  29. {
  30. contains: [YARDOCTAG],
  31. relevance: 10
  32. }
  33. ),
  34. hljs.COMMENT('^__END__', '\\n$')
  35. ];
  36. var SUBST = {
  37. className: 'subst',
  38. begin: '#\\{', end: '}',
  39. keywords: RUBY_KEYWORDS
  40. };
  41. var STRING = {
  42. className: 'string',
  43. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  44. variants: [
  45. {begin: /'/, end: /'/},
  46. {begin: /"/, end: /"/},
  47. {begin: /`/, end: /`/},
  48. {begin: '%[qQwWx]?\\(', end: '\\)'},
  49. {begin: '%[qQwWx]?\\[', end: '\\]'},
  50. {begin: '%[qQwWx]?{', end: '}'},
  51. {begin: '%[qQwWx]?<', end: '>'},
  52. {begin: '%[qQwWx]?/', end: '/'},
  53. {begin: '%[qQwWx]?%', end: '%'},
  54. {begin: '%[qQwWx]?-', end: '-'},
  55. {begin: '%[qQwWx]?\\|', end: '\\|'},
  56. {
  57. // \B in the beginning suppresses recognition of ?-sequences where ?
  58. // is the last character of a preceding identifier, as in: `func?4`
  59. begin: /\B\?(\\\d{1,3}|\\x[A-Fa-f0-9]{1,2}|\\u[A-Fa-f0-9]{4}|\\?\S)\b/
  60. },
  61. { // heredocs
  62. begin: /<<[-~]?'?(\w+)(?:.|\n)*?\n\s*\1\b/,
  63. returnBegin: true,
  64. contains: [
  65. { begin: /<<[-~]?'?/ },
  66. { begin: /\w+/,
  67. endSameAsBegin: true,
  68. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  69. }
  70. ]
  71. }
  72. ]
  73. };
  74. var PARAMS = {
  75. className: 'params',
  76. begin: '\\(', end: '\\)', endsParent: true,
  77. keywords: RUBY_KEYWORDS
  78. };
  79. var RUBY_DEFAULT_CONTAINS = [
  80. STRING,
  81. IRB_OBJECT,
  82. {
  83. className: 'class',
  84. beginKeywords: 'class module', end: '$|;',
  85. illegal: /=/,
  86. contains: [
  87. hljs.inherit(hljs.TITLE_MODE, {begin: '[A-Za-z_]\\w*(::\\w+)*(\\?|\\!)?'}),
  88. {
  89. begin: '<\\s*',
  90. contains: [{
  91. begin: '(' + hljs.IDENT_RE + '::)?' + hljs.IDENT_RE
  92. }]
  93. }
  94. ].concat(COMMENT_MODES)
  95. },
  96. {
  97. className: 'function',
  98. beginKeywords: 'def', end: '$|;',
  99. contains: [
  100. hljs.inherit(hljs.TITLE_MODE, {begin: RUBY_METHOD_RE}),
  101. PARAMS
  102. ].concat(COMMENT_MODES)
  103. },
  104. {
  105. // swallow namespace qualifiers before symbols
  106. begin: hljs.IDENT_RE + '::'
  107. },
  108. {
  109. className: 'symbol',
  110. begin: hljs.UNDERSCORE_IDENT_RE + '(\\!|\\?)?:',
  111. relevance: 0
  112. },
  113. {
  114. className: 'symbol',
  115. begin: ':(?!\\s)',
  116. contains: [STRING, {begin: RUBY_METHOD_RE}],
  117. relevance: 0
  118. },
  119. {
  120. className: 'number',
  121. begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b',
  122. relevance: 0
  123. },
  124. {
  125. begin: '(\\$\\W)|((\\$|\\@\\@?)(\\w+))' // variables
  126. },
  127. {
  128. className: 'params',
  129. begin: /\|/, end: /\|/,
  130. keywords: RUBY_KEYWORDS
  131. },
  132. { // regexp container
  133. begin: '(' + hljs.RE_STARTERS_RE + '|unless)\\s*',
  134. keywords: 'unless',
  135. contains: [
  136. IRB_OBJECT,
  137. {
  138. className: 'regexp',
  139. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  140. illegal: /\n/,
  141. variants: [
  142. {begin: '/', end: '/[a-z]*'},
  143. {begin: '%r{', end: '}[a-z]*'},
  144. {begin: '%r\\(', end: '\\)[a-z]*'},
  145. {begin: '%r!', end: '![a-z]*'},
  146. {begin: '%r\\[', end: '\\][a-z]*'}
  147. ]
  148. }
  149. ].concat(COMMENT_MODES),
  150. relevance: 0
  151. }
  152. ].concat(COMMENT_MODES);
  153. SUBST.contains = RUBY_DEFAULT_CONTAINS;
  154. PARAMS.contains = RUBY_DEFAULT_CONTAINS;
  155. var SIMPLE_PROMPT = "[>?]>";
  156. var DEFAULT_PROMPT = "[\\w#]+\\(\\w+\\):\\d+:\\d+>";
  157. var RVM_PROMPT = "(\\w+-)?\\d+\\.\\d+\\.\\d(p\\d+)?[^>]+>";
  158. var IRB_DEFAULT = [
  159. {
  160. begin: /^\s*=>/,
  161. starts: {
  162. end: '$', contains: RUBY_DEFAULT_CONTAINS
  163. }
  164. },
  165. {
  166. className: 'meta',
  167. begin: '^('+SIMPLE_PROMPT+"|"+DEFAULT_PROMPT+'|'+RVM_PROMPT+')',
  168. starts: {
  169. end: '$', contains: RUBY_DEFAULT_CONTAINS
  170. }
  171. }
  172. ];
  173. return {
  174. aliases: ['rb', 'gemspec', 'podspec', 'thor', 'irb'],
  175. keywords: RUBY_KEYWORDS,
  176. illegal: /\/\*/,
  177. contains: COMMENT_MODES.concat(IRB_DEFAULT).concat(RUBY_DEFAULT_CONTAINS)
  178. };
  179. };