lib/rouge/lexers/javascript.rb in rouge-0.0.6 vs lib/rouge/lexers/javascript.rb in rouge-0.0.7

- old
+ new

@@ -1,12 +1,20 @@ module Rouge module Lexers - class JavascriptLexer < RegexLexer + class Javascript < RegexLexer tag 'javascript' aliases 'js' - extensions 'js' + filenames '*.js' + mimetypes 'application/javascript', 'application/x-javascript', + 'text/javascript', 'text/x-javascript' + def self.analyze_text(text) + return 1 if text.shebang?('node') + return 1 if text.shebang?('jsc') + # TODO: rhino, spidermonkey, etc + end + state :comments_and_whitespace do rule /\s+/, 'Text' rule /<!--/, 'Comment' # really...? rule %r(//.*?\n), 'Comment.Single' rule %r(/\*.*?\*/), 'Comment.Multiline' @@ -59,10 +67,11 @@ Error eval isFinite isNaN parseFloat parseInt document this window ).join('|') state :root do + rule /\A\s*#!.*?\n/m, 'Comment.Preproc' rule %r(^(?=\s|/|<!--)), 'Text', :slash_starts_regex mixin :comments_and_whitespace rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | === | !== | \? | : )x, 'Operator', :slash_starts_regex @@ -79,9 +88,64 @@ rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Literal.Number.Float' rule /0x[0-9a-fA-F]+/, 'Literal.Number.Hex' rule /[0-9]+/, 'Literal.Number.Integer' rule /"(\\\\|\\"|[^"])*"/, 'Literal.String.Double' rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Single' + end + end + + class JSON < RegexLexer + tag 'json' + filenames '*.json' + mimetypes 'application/json' + + # TODO: is this too much of a performance hit? JSON is quite simple, + # so I'd think this wouldn't be too bad, but for large documents this + # could mean doing two full lexes. + def self.analyze_text(text) + text.lexes_cleanly?(self) ? 0.8 : 0 + end + + state :root do + mixin :whitespace + # special case for empty objects + rule /(\{)(\s*)(\})/ do + group 'Punctuation' + group 'Text.Whitespace' + group 'Punctuation' + end + rule /{/, 'Punctuation', :object_key + rule /\[/, 'Punctuation', :array + rule /-?(?:0|[1-9]\d*)\.\d+(?:e[+-]\d+)?/i, 'Literal.Number.Float' + rule /-?(?:0|[1-9]\d*)(?:e[+-]\d+)?/i, 'Literal.Number.Integer' + mixin :has_string + end + + state :whitespace do + rule /\s+/m, 'Text.Whitespace' + end + + state :has_string do + rule /"(\\.|[^"])*"/, 'Literal.String.Double' + end + + state :object_key do + mixin :whitespace + rule /:/, 'Punctuation', :object_val + rule /}/, 'Error', :pop! + mixin :has_string + end + + state :object_val do + rule /,/, 'Punctuation', :pop! + rule(/}/) { token 'Punctuation'; pop!; pop! } + mixin :root + end + + state :array do + rule /\]/, 'Punctuation', :pop! + rule /,/, 'Punctuation' + mixin :root end end end end