diff .svn/pristine/49/49c759950bed453cc0da75c2904a201b6bf1a7c4.svn-base @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.svn/pristine/49/49c759950bed453cc0da75c2904a201b6bf1a7c4.svn-base	Fri Feb 24 19:09:32 2012 +0000
@@ -0,0 +1,83 @@
+module CodeRay
+module Encoders
+  
+  # A simple JSON Encoder.
+  # 
+  # Example:
+  #  CodeRay.scan('puts "Hello world!"', :ruby).json
+  # yields
+  #  [
+  #    {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
+  #    {"type"=>"text", "text"=>" ", "kind"=>"space"},
+  #    {"type"=>"block", "action"=>"open", "kind"=>"string"},
+  #    {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
+  #    {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
+  #    {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
+  #    {"type"=>"block", "action"=>"close", "kind"=>"string"},
+  #  ]
+  class JSON < Encoder
+    
+    begin
+      require 'json'
+    rescue LoadError
+      begin
+        require 'rubygems' unless defined? Gem
+        gem 'json'
+        require 'json'
+      rescue LoadError
+        $stderr.puts "The JSON encoder needs the JSON library.\n" \
+          "Please gem install json."
+        raise
+      end
+    end
+    
+    register_for :json
+    FILE_EXTENSION = 'json'
+    
+  protected
+    def setup options
+      super
+      
+      @first = true
+      @out << '['
+    end
+    
+    def finish options
+      @out << ']'
+    end
+    
+    def append data
+      if @first
+        @first = false
+      else
+        @out << ','
+      end
+      
+      @out << data.to_json
+    end
+    
+  public
+    def text_token text, kind
+      append :type => 'text', :text => text, :kind => kind
+    end
+    
+    def begin_group kind
+      append :type => 'block', :action => 'open', :kind => kind
+    end
+    
+    def end_group kind
+      append :type => 'block', :action => 'close', :kind => kind
+    end
+    
+    def begin_line kind
+      append :type => 'block', :action => 'begin_line', :kind => kind
+    end
+    
+    def end_line kind
+      append :type => 'block', :action => 'end_line', :kind => kind
+    end
+    
+  end
+  
+end
+end