PageRenderTime 34ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/samples/scalate-sample-sitegen/src/highlight/languages/ruby.js

http://github.com/scalate/scalate
JavaScript | 195 lines | 189 code | 1 blank | 5 comment | 0 complexity | 3f00582de83bd4e52e0e71bc7ed0bb9b MD5 | raw file
  1/*
  2Language: Ruby
  3Author: Anton Kovalyov <anton@kovalyov.net>
  4Contributors: Peter Leonov <gojpeg@yandex.ru>, Vasily Polovnyov <vast@whiteants.net>, Loren Segal <lsegal@soen.ca>
  5*/
  6
  7hljs.LANGUAGES.ruby = function(){
  8  var RUBY_IDENT_RE = '[a-zA-Z_][a-zA-Z0-9_]*(\\!|\\?)?';
  9  var RUBY_METHOD_RE = '[a-zA-Z_]\\w*[!?=]?|[-+~]\\@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?';
 10  var RUBY_DEFAULT_CONTAINS = ['comment', 'string', 'char', 'class', 'function', 'constant', 'symbol', 'number', 'variable', 'identifier', 'regexp_container']
 11  var RUBY_KEYWORDS = {
 12    'keyword': {'and': 1, 'false': 1, 'then': 1, 'defined': 1, 'module': 1, 'in': 1, 'return': 1, 'redo': 1, 'if': 1, 'BEGIN': 1, 'retry': 1, 'end': 1, 'for': 1, 'true': 1, 'self': 1, 'when': 1, 'next': 1, 'until': 1, 'do': 1, 'begin': 1, 'unless': 1, 'END': 1, 'rescue': 1, 'nil': 1, 'else': 1, 'break': 1, 'undef': 1, 'not': 1, 'super': 1, 'class': 1, 'case': 1, 'require': 1, 'yield': 1, 'alias': 1, 'while': 1, 'ensure': 1, 'elsif': 1, 'or': 1, 'def': 1},
 13    'keymethods': {'__id__': 1, '__send__': 1, 'abort': 1, 'abs': 1, 'all?': 1, 'allocate': 1, 'ancestors': 1, 'any?': 1, 'arity': 1, 'assoc': 1, 'at': 1, 'at_exit': 1, 'autoload': 1, 'autoload?': 1, 'between?': 1, 'binding': 1, 'binmode': 1, 'block_given?': 1, 'call': 1, 'callcc': 1, 'caller': 1, 'capitalize': 1, 'capitalize!': 1, 'casecmp': 1, 'catch': 1, 'ceil': 1, 'center': 1, 'chomp': 1, 'chomp!': 1, 'chop': 1, 'chop!': 1, 'chr': 1, 'class': 1, 'class_eval': 1, 'class_variable_defined?': 1, 'class_variables': 1, 'clear': 1, 'clone': 1, 'close': 1, 'close_read': 1, 'close_write': 1, 'closed?': 1, 'coerce': 1, 'collect': 1, 'collect!': 1, 'compact': 1, 'compact!': 1, 'concat': 1, 'const_defined?': 1, 'const_get': 1, 'const_missing': 1, 'const_set': 1, 'constants': 1, 'count': 1, 'crypt': 1, 'default': 1, 'default_proc': 1, 'delete': 1, 'delete!': 1, 'delete_at': 1, 'delete_if': 1, 'detect': 1, 'display': 1, 'div': 1, 'divmod': 1, 'downcase': 1, 'downcase!': 1, 'downto': 1, 'dump': 1, 'dup': 1, 'each': 1, 'each_byte': 1, 'each_index': 1, 'each_key': 1, 'each_line': 1, 'each_pair': 1, 'each_value': 1, 'each_with_index': 1, 'empty?': 1, 'entries': 1, 'eof': 1, 'eof?': 1, 'eql?': 1, 'equal?': 1, 'eval': 1, 'exec': 1, 'exit': 1, 'exit!': 1, 'extend': 1, 'fail': 1, 'fcntl': 1, 'fetch': 1, 'fileno': 1, 'fill': 1, 'find': 1, 'find_all': 1, 'first': 1, 'flatten': 1, 'flatten!': 1, 'floor': 1, 'flush': 1, 'for_fd': 1, 'foreach': 1, 'fork': 1, 'format': 1, 'freeze': 1, 'frozen?': 1, 'fsync': 1, 'getc': 1, 'gets': 1, 'global_variables': 1, 'grep': 1, 'gsub': 1, 'gsub!': 1, 'has_key?': 1, 'has_value?': 1, 'hash': 1, 'hex': 1, 'id': 1, 'include': 1, 'include?': 1, 'included_modules': 1, 'index': 1, 'indexes': 1, 'indices': 1, 'induced_from': 1, 'inject': 1, 'insert': 1, 'inspect': 1, 'instance_eval': 1, 'instance_method': 1, 'instance_methods': 1, 'instance_of?': 1, 'instance_variable_defined?': 1, 'instance_variable_get': 1, 'instance_variable_set': 1, 'instance_variables': 1, 'integer?': 1, 'intern': 1, 'invert': 1, 'ioctl': 1, 'is_a?': 1, 'isatty': 1, 'iterator?': 1, 'join': 1, 'key?': 1, 'keys': 1, 'kind_of?': 1, 'lambda': 1, 'last': 1, 'length': 1, 'lineno': 1, 'ljust': 1, 'load': 1, 'local_variables': 1, 'loop': 1, 'lstrip': 1, 'lstrip!': 1, 'map': 1, 'map!': 1, 'match': 1, 'max': 1, 'member?': 1, 'merge': 1, 'merge!': 1, 'method': 1, 'method_defined?': 1, 'method_missing': 1, 'methods': 1, 'min': 1, 'module_eval': 1, 'modulo': 1, 'name': 1, 'nesting': 1, 'new': 1, 'next': 1, 'next!': 1, 'nil?': 1, 'nitems': 1, 'nonzero?': 1, 'object_id': 1, 'oct': 1, 'open': 1, 'pack': 1, 'partition': 1, 'pid': 1, 'pipe': 1, 'pop': 1, 'popen': 1, 'pos': 1, 'prec': 1, 'prec_f': 1, 'prec_i': 1, 'print': 1, 'printf': 1, 'private_class_method': 1, 'private_instance_methods': 1, 'private_method_defined?': 1, 'private_methods': 1, 'proc': 1, 'protected_instance_methods': 1, 'protected_method_defined?': 1, 'protected_methods': 1, 'public_class_method': 1, 'public_instance_methods': 1, 'public_method_defined?': 1, 'public_methods': 1, 'push': 1, 'putc': 1, 'puts': 1, 'quo': 1, 'raise': 1, 'rand': 1, 'rassoc': 1, 'read': 1, 'read_nonblock': 1, 'readchar': 1, 'readline': 1, 'readlines': 1, 'readpartial': 1, 'rehash': 1, 'reject': 1, 'reject!': 1, 'remainder': 1, 'reopen': 1, 'replace': 1, 'require': 1, 'respond_to?': 1, 'reverse': 1, 'reverse!': 1, 'reverse_each': 1, 'rewind': 1, 'rindex': 1, 'rjust': 1, 'round': 1, 'rstrip': 1, 'rstrip!': 1, 'scan': 1, 'seek': 1, 'select': 1, 'send': 1, 'set_trace_func': 1, 'shift': 1, 'singleton_method_added': 1, 'singleton_methods': 1, 'size': 1, 'sleep': 1, 'slice': 1, 'slice!': 1, 'sort': 1, 'sort!': 1, 'sort_by': 1, 'split': 1, 'sprintf': 1, 'squeeze': 1, 'squeeze!': 1, 'srand': 1, 'stat': 1, 'step': 1, 'store': 1, 'strip': 1, 'strip!': 1, 'sub': 1, 'sub!': 1, 'succ': 1, 'succ!': 1, 'sum': 1, 'superclass': 1, 'swapcase': 1, 'swapcase!': 1, 'sync': 1, 'syscall': 1, 'sysopen': 1, 'sysread': 1, 'sysseek': 1, 'system': 1, 'syswrite': 1, 'taint': 1, 'tainted?': 1, 'tell': 1, 'test': 1, 'throw': 1, 'times': 1, 'to_a': 1, 'to_ary': 1, 'to_f': 1, 'to_hash': 1, 'to_i': 1, 'to_int': 1, 'to_io': 1, 'to_proc': 1, 'to_s': 1, 'to_str': 1, 'to_sym': 1, 'tr': 1, 'tr!': 1, 'tr_s': 1, 'tr_s!': 1, 'trace_var': 1, 'transpose': 1, 'trap': 1, 'truncate': 1, 'tty?': 1, 'type': 1, 'ungetc': 1, 'uniq': 1, 'uniq!': 1, 'unpack': 1, 'unshift': 1, 'untaint': 1, 'untrace_var': 1, 'upcase': 1, 'upcase!': 1, 'update': 1, 'upto': 1, 'value?': 1, 'values': 1, 'values_at': 1, 'warn': 1, 'write': 1, 'write_nonblock': 1, 'zero?': 1, 'zip': 1}
 14  }
 15  return {
 16    defaultMode: {
 17      lexems: [RUBY_IDENT_RE],
 18      contains: RUBY_DEFAULT_CONTAINS,
 19      keywords: RUBY_KEYWORDS
 20    },
 21    modes: [
 22      {
 23        className: 'comment',
 24        begin: '#', end: '$',
 25        contains: ['yardoctag']
 26      },
 27      {
 28        className: 'yardoctag',
 29        begin: '@[A-Za-z]+', end: '^'
 30      },
 31      {
 32        className: 'comment',
 33        begin: '^\\=begin', end: '^\\=end',
 34        contains: ['yardoctag'],
 35        relevance: 10
 36      },
 37      {
 38        className: 'comment',
 39        begin: '^__END__', end: '\\n$'
 40      },
 41      {
 42        className: 'params',
 43        begin: '\\(', end: '\\)',
 44        lexems: [RUBY_IDENT_RE],
 45        keywords: RUBY_KEYWORDS,
 46        contains: RUBY_DEFAULT_CONTAINS
 47      },
 48      {
 49        className: 'function',
 50        begin: '\\bdef\\s+', end: ' |$|;',
 51        lexems: [RUBY_IDENT_RE],
 52        keywords: RUBY_KEYWORDS,
 53        contains: ['ftitle', 'params', 'comment']
 54      },
 55      {
 56        className: 'ftitle', displayClassName: 'title',
 57        begin: RUBY_METHOD_RE, end: '^',
 58        lexems: [RUBY_IDENT_RE],
 59        keywords: RUBY_KEYWORDS
 60      },
 61      {
 62        className: 'class',
 63        begin: '\\b(class|module)\\b', end: '$|;',
 64        lexems: [hljs.UNDERSCORE_IDENT_RE],
 65        keywords: RUBY_KEYWORDS,
 66        contains: ['title', 'inheritance', 'comment'],
 67        keywords: {'class': 1, 'module': 1}
 68      },
 69      {
 70        className: 'title',
 71        begin: '[A-Za-z_]\\w*(::\\w+)*(\\?|\\!)?', end: '^',
 72        relevance: 0
 73      },
 74      {
 75        className: 'inheritance',
 76        begin: '<\\s*', end: '^',
 77        contains: ['parent']
 78      },
 79      {
 80        className: 'parent',
 81        begin: '(' + hljs.IDENT_RE + '::)?' + hljs.IDENT_RE, end: '^'
 82      },
 83      {
 84        className: 'number',
 85        begin: '(\\b0[0-7_]+)|(\\b0x[0-9a-fA-F_]+)|(\\b[1-9][0-9_]*(\\.[0-9_]+)?)|[0_]\\b', end: '^',
 86        relevance: 0
 87      },
 88      {
 89        className: 'number',
 90        begin: '\\?\\w', end: '^'
 91      },
 92      {
 93        className: 'string',
 94        begin: '\'', end: '\'',
 95        contains: ['escape', 'subst'],
 96        relevance: 0
 97      },
 98      {
 99        className: 'string',
100        begin: '"', end: '"',
101        contains: ['escape', 'subst'],
102        relevance: 0
103      },
104      {
105        className: 'string',
106        begin: '%[qw]?\\(', end: '\\)',
107        contains: ['escape', 'subst'],
108        relevance: 10
109      },
110      {
111        className: 'string',
112        begin: '%[qw]?\\[', end: '\\]',
113        contains: ['escape', 'subst'],
114        relevance: 10
115      },
116      {
117        className: 'string',
118        begin: '%[qw]?{', end: '}',
119        contains: ['escape', 'subst'],
120        relevance: 10
121      },
122      {
123        className: 'string',
124        begin: '%[qw]?<', end: '>',
125        contains: ['escape', 'subst'],
126        relevance: 10
127      },
128      {
129        className: 'string',
130        begin: '%[qw]?/', end: '/',
131        contains: ['escape', 'subst'],
132        relevance: 10
133      },
134      {
135        className: 'string',
136        begin: '%[qw]?%', end: '%',
137        contains: ['escape', 'subst'],
138        relevance: 10
139      },
140      {
141        className: 'string',
142        begin: '%[qw]?-', end: '-',
143        contains: ['escape', 'subst'],
144        relevance: 10
145      },
146      {
147        className: 'string',
148        begin: '%[qw]?\\|', end: '\\|',
149        contains: ['escape', 'subst'],
150        relevance: 10
151      },
152      {
153        className: 'constant',
154        begin: '(::)?([A-Z]\\w*(::)?)+', end: '^',
155        relevance: 0
156      },
157      {
158        className: 'symbol',
159        begin: ':', end: '^',
160        contains: ['string', 'identifier']
161      },
162      {
163        className: 'identifier',
164        begin: RUBY_IDENT_RE, end: '^',
165        lexems: [RUBY_IDENT_RE],
166        keywords: RUBY_KEYWORDS,
167        relevance: 0
168      },
169      hljs.BACKSLASH_ESCAPE,
170      {
171        className: 'subst',
172        begin: '#\\{', end: '}',
173        lexems: [RUBY_IDENT_RE],
174        keywords: RUBY_KEYWORDS,
175        contains: RUBY_DEFAULT_CONTAINS
176      },
177      {
178        className: 'regexp_container',
179        begin: '(' + hljs.RE_STARTERS_RE + ')\\s*', end: '^', noMarkup: true,
180        contains: ['comment', 'regexp'],
181        relevance: 0
182      },
183      {
184        className: 'regexp',
185        begin: '/', end: '/[a-z]*',
186        illegal: '\\n',
187        contains: ['escape']
188      },
189      {
190        className: 'variable',
191        begin: '(\\$\\W)|((\\$|\\@\\@?)(\\w+))', end: '^'
192      }
193    ]
194  };
195}();