From 27d6da9457433b6e0a1a2e3e29a2cc79e903fc39 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 7 Sep 2007 21:30:46 +0000 Subject: [PATCH] Added javascript highlightment support (http://pastie.textmate.org/50774/) git-svn-id: http://redmine.rubyforge.org/svn/trunk@716 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- .../lib/coderay/helpers/file_type.rb | 1 + .../lib/coderay/scanners/javascript.rb | 176 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 vendor/plugins/coderay-0.7.6.227/lib/coderay/scanners/javascript.rb diff --git a/vendor/plugins/coderay-0.7.6.227/lib/coderay/helpers/file_type.rb b/vendor/plugins/coderay-0.7.6.227/lib/coderay/helpers/file_type.rb index 3d29d7833..41b6c2ef2 100644 --- a/vendor/plugins/coderay-0.7.6.227/lib/coderay/helpers/file_type.rb +++ b/vendor/plugins/coderay-0.7.6.227/lib/coderay/helpers/file_type.rb @@ -84,6 +84,7 @@ module FileType 'cpp' => :c, 'c' => :c, 'h' => :c, + 'js' => :javascript, 'xml' => :xml, 'htm' => :html, 'html' => :html, diff --git a/vendor/plugins/coderay-0.7.6.227/lib/coderay/scanners/javascript.rb b/vendor/plugins/coderay-0.7.6.227/lib/coderay/scanners/javascript.rb new file mode 100644 index 000000000..419a5255b --- /dev/null +++ b/vendor/plugins/coderay-0.7.6.227/lib/coderay/scanners/javascript.rb @@ -0,0 +1,176 @@ +# http://pastie.textmate.org/50774/ +module CodeRay module Scanners + + class JavaScript < Scanner + + register_for :javascript + + RESERVED_WORDS = [ + 'asm', 'break', 'case', 'continue', 'default', 'do', 'else', + 'for', 'goto', 'if', 'return', 'switch', 'while', +# 'struct', 'union', 'enum', 'typedef', +# 'static', 'register', 'auto', 'extern', +# 'sizeof', + 'typeof', +# 'volatile', 'const', # C89 +# 'inline', 'restrict', # C99 + 'var', 'function','try','new','in', + 'instanceof','throw','catch' + ] + + PREDEFINED_CONSTANTS = [ + 'void', 'null', 'this', + 'true', 'false','undefined', + ] + + IDENT_KIND = WordList.new(:ident). + add(RESERVED_WORDS, :reserved). + add(PREDEFINED_CONSTANTS, :pre_constant) + + ESCAPE = / [rbfnrtv\n\\\/'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x + UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x + + def scan_tokens tokens, options + + state = :initial + string_type = nil + regexp_allowed = true + + until eos? + + kind = :error + match = nil + + if state == :initial + + if scan(/ \s+ | \\\n /x) + kind = :space + + elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx) + kind = :comment + regexp_allowed = false + + elsif match = scan(/ \# \s* if \s* 0 /x) + match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos? + kind = :comment + regexp_allowed = false + + elsif regexp_allowed and scan(/\//) + tokens << [:open, :regexp] + state = :regex + kind = :delimiter + + elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%] | \.(?!\d) /x) + kind = :operator + regexp_allowed=true + + elsif match = scan(/ [$A-Za-z_][A-Za-z_0-9]* /x) + kind = IDENT_KIND[match] +# if kind == :ident and check(/:(?!:)/) +# match << scan(/:/) +# kind = :label +# end + regexp_allowed=false + + elsif match = scan(/["']/) + tokens << [:open, :string] + string_type = matched + state = :string + kind = :delimiter + +# elsif scan(/#\s*(\w*)/) +# kind = :preprocessor # FIXME multiline preprocs +# state = :include_expected if self[1] == 'include' +# +# elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox) +# kind = :char + + elsif scan(/0[xX][0-9A-Fa-f]+/) + kind = :hex + regexp_allowed=false + + elsif scan(/(?:0[0-7]+)(?![89.eEfF])/) + kind = :oct + regexp_allowed=false + + elsif scan(/(?:\d+)(?![.eEfF])/) + kind = :integer + regexp_allowed=false + + elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/) + kind = :float + regexp_allowed=false + + else + getch + end + + elsif state == :regex + if scan(/[^\\\/]+/) + kind = :content + elsif scan(/\\\/|\\\\/) + kind = :content + elsif scan(/\//) + tokens << [matched, :delimiter] + tokens << [:close, :regexp] + state = :initial + next + else + getch + kind = :content + end + + elsif state == :string + if scan(/[^\\"']+/) + kind = :content + elsif scan(/["']/) + if string_type==matched + tokens << [matched, :delimiter] + tokens << [:close, :string] + state = :initial + string_type=nil + next + else + kind = :content + end + elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox) + kind = :char + elsif scan(/ \\ | $ /x) + kind = :error + state = :initial + else + raise "else case \" reached; %p not handled." % peek(1), tokens + end + +# elsif state == :include_expected +# if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/) +# kind = :include +# state = :initial +# +# elsif match = scan(/\s+/) +# kind = :space +# state = :initial if match.index ?\n +# +# else +# getch +# +# end +# + else + raise 'else-case reached', tokens + + end + + match ||= matched +# raise [match, kind], tokens if kind == :error + + tokens << [match, kind] + + end + tokens + + end + + end + +end end \ No newline at end of file -- 2.39.5