prism-ruby.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Original by Samuel Flores
  3. *
  4. * Adds the following new token classes:
  5. * constant, builtin, variable, symbol, regex
  6. */
  7. (function (Prism) {
  8. Prism.languages.ruby = Prism.languages.extend('clike', {
  9. 'comment': {
  10. pattern: /#.*|^=begin\s[\s\S]*?^=end/m,
  11. greedy: true
  12. },
  13. 'class-name': {
  14. pattern: /(\b(?:class|module)\s+|\bcatch\s+\()[\w.\\]+|\b[A-Z_]\w*(?=\s*\.\s*new\b)/,
  15. lookbehind: true,
  16. inside: {
  17. 'punctuation': /[.\\]/
  18. }
  19. },
  20. 'keyword': /\b(?:BEGIN|END|alias|and|begin|break|case|class|def|define_method|defined|do|each|else|elsif|end|ensure|extend|for|if|in|include|module|new|next|nil|not|or|prepend|private|protected|public|raise|redo|require|rescue|retry|return|self|super|then|throw|undef|unless|until|when|while|yield)\b/,
  21. 'operator': /\.{2,3}|&\.|===|<?=>|[!=]?~|(?:&&|\|\||<<|>>|\*\*|[+\-*/%<>!^&|=])=?|[?:]/,
  22. 'punctuation': /[(){}[\].,;]/,
  23. });
  24. Prism.languages.insertBefore('ruby', 'operator', {
  25. 'double-colon': {
  26. pattern: /::/,
  27. alias: 'punctuation'
  28. },
  29. });
  30. var interpolation = {
  31. pattern: /((?:^|[^\\])(?:\\{2})*)#\{(?:[^{}]|\{[^{}]*\})*\}/,
  32. lookbehind: true,
  33. inside: {
  34. 'content': {
  35. pattern: /^(#\{)[\s\S]+(?=\}$)/,
  36. lookbehind: true,
  37. inside: Prism.languages.ruby
  38. },
  39. 'delimiter': {
  40. pattern: /^#\{|\}$/,
  41. alias: 'punctuation'
  42. }
  43. }
  44. };
  45. delete Prism.languages.ruby.function;
  46. var percentExpression = '(?:' + [
  47. /([^a-zA-Z0-9\s{(\[<=])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
  48. /\((?:[^()\\]|\\[\s\S]|\((?:[^()\\]|\\[\s\S])*\))*\)/.source,
  49. /\{(?:[^{}\\]|\\[\s\S]|\{(?:[^{}\\]|\\[\s\S])*\})*\}/.source,
  50. /\[(?:[^\[\]\\]|\\[\s\S]|\[(?:[^\[\]\\]|\\[\s\S])*\])*\]/.source,
  51. /<(?:[^<>\\]|\\[\s\S]|<(?:[^<>\\]|\\[\s\S])*>)*>/.source
  52. ].join('|') + ')';
  53. var symbolName = /(?:"(?:\\.|[^"\\\r\n])*"|(?:\b[a-zA-Z_]\w*|[^\s\0-\x7F]+)[?!]?|\$.)/.source;
  54. Prism.languages.insertBefore('ruby', 'keyword', {
  55. 'regex-literal': [
  56. {
  57. pattern: RegExp(/%r/.source + percentExpression + /[egimnosux]{0,6}/.source),
  58. greedy: true,
  59. inside: {
  60. 'interpolation': interpolation,
  61. 'regex': /[\s\S]+/
  62. }
  63. },
  64. {
  65. pattern: /(^|[^/])\/(?!\/)(?:\[[^\r\n\]]+\]|\\.|[^[/\\\r\n])+\/[egimnosux]{0,6}(?=\s*(?:$|[\r\n,.;})#]))/,
  66. lookbehind: true,
  67. greedy: true,
  68. inside: {
  69. 'interpolation': interpolation,
  70. 'regex': /[\s\S]+/
  71. }
  72. }
  73. ],
  74. 'variable': /[@$]+[a-zA-Z_]\w*(?:[?!]|\b)/,
  75. 'symbol': [
  76. {
  77. pattern: RegExp(/(^|[^:]):/.source + symbolName),
  78. lookbehind: true,
  79. greedy: true
  80. },
  81. {
  82. pattern: RegExp(/([\r\n{(,][ \t]*)/.source + symbolName + /(?=:(?!:))/.source),
  83. lookbehind: true,
  84. greedy: true
  85. },
  86. ],
  87. 'method-definition': {
  88. pattern: /(\bdef\s+)\w+(?:\s*\.\s*\w+)?/,
  89. lookbehind: true,
  90. inside: {
  91. 'function': /\b\w+$/,
  92. 'keyword': /^self\b/,
  93. 'class-name': /^\w+/,
  94. 'punctuation': /\./
  95. }
  96. }
  97. });
  98. Prism.languages.insertBefore('ruby', 'string', {
  99. 'string-literal': [
  100. {
  101. pattern: RegExp(/%[qQiIwWs]?/.source + percentExpression),
  102. greedy: true,
  103. inside: {
  104. 'interpolation': interpolation,
  105. 'string': /[\s\S]+/
  106. }
  107. },
  108. {
  109. pattern: /("|')(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|(?!\1)[^\\#\r\n])*\1/,
  110. greedy: true,
  111. inside: {
  112. 'interpolation': interpolation,
  113. 'string': /[\s\S]+/
  114. }
  115. },
  116. {
  117. pattern: /<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
  118. alias: 'heredoc-string',
  119. greedy: true,
  120. inside: {
  121. 'delimiter': {
  122. pattern: /^<<[-~]?[a-z_]\w*|\b[a-z_]\w*$/i,
  123. inside: {
  124. 'symbol': /\b\w+/,
  125. 'punctuation': /^<<[-~]?/
  126. }
  127. },
  128. 'interpolation': interpolation,
  129. 'string': /[\s\S]+/
  130. }
  131. },
  132. {
  133. pattern: /<<[-~]?'([a-z_]\w*)'[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
  134. alias: 'heredoc-string',
  135. greedy: true,
  136. inside: {
  137. 'delimiter': {
  138. pattern: /^<<[-~]?'[a-z_]\w*'|\b[a-z_]\w*$/i,
  139. inside: {
  140. 'symbol': /\b\w+/,
  141. 'punctuation': /^<<[-~]?'|'$/,
  142. }
  143. },
  144. 'string': /[\s\S]+/
  145. }
  146. }
  147. ],
  148. 'command-literal': [
  149. {
  150. pattern: RegExp(/%x/.source + percentExpression),
  151. greedy: true,
  152. inside: {
  153. 'interpolation': interpolation,
  154. 'command': {
  155. pattern: /[\s\S]+/,
  156. alias: 'string'
  157. }
  158. }
  159. },
  160. {
  161. pattern: /`(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|[^\\`#\r\n])*`/,
  162. greedy: true,
  163. inside: {
  164. 'interpolation': interpolation,
  165. 'command': {
  166. pattern: /[\s\S]+/,
  167. alias: 'string'
  168. }
  169. }
  170. }
  171. ]
  172. });
  173. delete Prism.languages.ruby.string;
  174. Prism.languages.insertBefore('ruby', 'number', {
  175. 'builtin': /\b(?:Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Fixnum|Float|Hash|IO|Integer|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|Stat|String|Struct|Symbol|TMS|Thread|ThreadGroup|Time|TrueClass)\b/,
  176. 'constant': /\b[A-Z][A-Z0-9_]*(?:[?!]|\b)/
  177. });
  178. Prism.languages.rb = Prism.languages.ruby;
  179. }(Prism));