summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes.textile5
-rw-r--r--etc/coderay-lib.tmproj41
-rw-r--r--lib/coderay/duo.rb67
-rw-r--r--lib/coderay/encoders/filter.rb16
-rw-r--r--lib/coderay/encoders/yaml.rb32
5 files changed, 121 insertions, 40 deletions
diff --git a/Changes.textile b/Changes.textile
index 12fc6ae..90a7cda 100644
--- a/Changes.textile
+++ b/Changes.textile
@@ -16,6 +16,7 @@ Changes related to the new tokens handling include:
* *REWRITE* of all Scanners, Encoders, Filters, and Tokens.
* *RENAMED* @:open@ and @:close@ actions to @:begin_group@ and @:end_group@.
* *RENAMED* @open_token@ and @close_token@ methods to @begin_group@ and @end_group@.
+* *CHANGED* The above name changes also affect the JSON, XML, and YAML encoders. CodeRay 1.0 output will be incompatible with earlier versions.
* *REMOVED* @TokenStream@ and the @Streamable@ API and all related features like @NotStreamableError@ are now obsolete and have been removed.
h3. General changes
@@ -73,6 +74,10 @@ h3. @Encoders::Statistic@
* *CHANGED*: Tokens actions are counted separately.
+h3. @Encoders::YAML@
+
+* *NEW* method @call@ for allowing code like @CodeRay::Duo[:python => :yaml].(code)@ in Ruby 1.9.
+
h3. @Scanners::Scanner@
* *REMOVED* helper method @String#to_unix@.
diff --git a/etc/coderay-lib.tmproj b/etc/coderay-lib.tmproj
index 354386a..4dfdda3 100644
--- a/etc/coderay-lib.tmproj
+++ b/etc/coderay-lib.tmproj
@@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>currentDocument</key>
- <string>../diff</string>
+ <string>../bench/bench.rb</string>
<key>documents</key>
<array>
<dict>
@@ -28,15 +28,13 @@
<key>filename</key>
<string>../diff</string>
<key>lastUsed</key>
- <date>2010-04-28T14:08:29Z</date>
- <key>selected</key>
- <true/>
+ <date>2010-05-01T03:27:36Z</date>
</dict>
<dict>
<key>filename</key>
<string>../Changes.textile</string>
<key>lastUsed</key>
- <date>2010-04-28T14:06:25Z</date>
+ <date>2010-05-01T03:02:21Z</date>
</dict>
<dict>
<key>filename</key>
@@ -89,6 +87,8 @@
<string>../rake_helpers</string>
</dict>
<dict>
+ <key>expanded</key>
+ <true/>
<key>name</key>
<string>rake_tasks</string>
<key>regexFolderFilter</key>
@@ -103,6 +103,8 @@
<date>2010-03-31T21:53:39Z</date>
</dict>
<dict>
+ <key>expanded</key>
+ <true/>
<key>name</key>
<string>functional</string>
<key>regexFolderFilter</key>
@@ -114,7 +116,7 @@
<key>filename</key>
<string>../test/scanners/coderay_suite.rb</string>
<key>lastUsed</key>
- <date>2010-04-28T12:38:44Z</date>
+ <date>2010-05-01T03:02:07Z</date>
</dict>
<dict>
<key>filename</key>
@@ -126,46 +128,31 @@
<key>filename</key>
<string>../bench/bench.rb</string>
<key>lastUsed</key>
- <date>2010-04-28T14:04:10Z</date>
+ <date>2010-05-05T11:41:28Z</date>
</dict>
</array>
<key>fileHierarchyDrawerWidth</key>
<integer>213</integer>
<key>metaData</key>
<dict>
- <key>../Changes.textile</key>
- <dict>
- <key>caret</key>
- <dict>
- <key>column</key>
- <integer>70</integer>
- <key>line</key>
- <integer>81</integer>
- </dict>
- <key>firstVisibleColumn</key>
- <integer>0</integer>
- <key>firstVisibleLine</key>
- <integer>5</integer>
- </dict>
- <key>../diff</key>
+ <key>../bench/bench.rb</key>
<dict>
<key>caret</key>
<dict>
<key>column</key>
- <integer>44</integer>
+ <integer>23</integer>
<key>line</key>
- <integer>663</integer>
+ <integer>94</integer>
</dict>
<key>firstVisibleColumn</key>
<integer>0</integer>
<key>firstVisibleLine</key>
- <integer>642</integer>
+ <integer>74</integer>
</dict>
</dict>
<key>openDocuments</key>
<array>
- <string>../diff</string>
- <string>../Changes.textile</string>
+ <string>../bench/bench.rb</string>
</array>
<key>showFileHierarchyDrawer</key>
<true/>
diff --git a/lib/coderay/duo.rb b/lib/coderay/duo.rb
index c11bd3f..d8b064d 100644
--- a/lib/coderay/duo.rb
+++ b/lib/coderay/duo.rb
@@ -1,3 +1,4 @@
+($:.unshift '..'; require 'coderay') unless defined? CodeRay
module CodeRay
# = Duo
@@ -44,12 +45,12 @@ module CodeRay
end
@options = options
end
-
+
class << self
# To allow calls like Duo[:ruby, :html].highlight.
alias [] new
end
-
+
# The scanner of the duo. Only created once.
def scanner
@scanner ||= CodeRay.scanner @lang, CodeRay.get_scanner_options(@options)
@@ -66,8 +67,68 @@ module CodeRay
encoder.encode(code, @lang, options)
end
alias highlight encode
-
+
+ # Allows to use Duo like a proc object:
+ #
+ # CodeRay::Duo[:python => :yaml].call(code)
+ #
+ # or, in Ruby 1.9 and later:
+ #
+ # CodeRay::Duo[:python => :yaml].(code)
+ alias call encode
+
end
+
+end
+if $0 == __FILE__
+ $VERBOSE = true
+ $: << File.join(File.dirname(__FILE__), '..')
+ eval DATA.read, nil, $0, __LINE__ + 4
end
+__END__
+require 'test/unit'
+
+class DuoTest < Test::Unit::TestCase
+
+ def test_two_arguments
+ duo = CodeRay::Duo[:ruby, :html]
+ assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
+ assert_kind_of CodeRay::Encoders[:html], duo.encoder
+ end
+
+ def test_two_hash
+ duo = CodeRay::Duo[:ruby => :html]
+ assert_kind_of CodeRay::Scanners[:ruby], duo.scanner
+ assert_kind_of CodeRay::Encoders[:html], duo.encoder
+ end
+
+ def test_call
+ duo = CodeRay::Duo[:python => :yaml]
+ assert_equal <<-'YAML', duo.call('def test: "pass"')
+---
+- - def
+ - :keyword
+- - " "
+ - :space
+- - test
+ - :method
+- - ":"
+ - :operator
+- - " "
+ - :space
+- - :begin_group
+ - :string
+- - "\""
+ - :delimiter
+- - pass
+ - :content
+- - "\""
+ - :delimiter
+- - :end_group
+ - :string
+ YAML
+ end
+
+end
diff --git a/lib/coderay/encoders/filter.rb b/lib/coderay/encoders/filter.rb
index 6b78ad3..13621ff 100644
--- a/lib/coderay/encoders/filter.rb
+++ b/lib/coderay/encoders/filter.rb
@@ -15,14 +15,20 @@ module Encoders
@out = Tokens.new
end
- def text_token text, kind
- @out.text_token text, kind if include_text_token? text, kind
+ def include_text_token? text, kind
+ true
end
- def include_text_token? text, kind
+ def include_block_token? action, kind
true
end
+ public
+
+ def text_token text, kind
+ @out.text_token text, kind if include_text_token? text, kind
+ end
+
def begin_group kind
@out.begin_group kind if include_block_token? :begin_group, kind
end
@@ -39,10 +45,6 @@ module Encoders
@out.end_line kind if include_block_token? :end_line, kind
end
- def include_block_token? action, kind
- true
- end
-
end
end
diff --git a/lib/coderay/encoders/yaml.rb b/lib/coderay/encoders/yaml.rb
index 5564e58..49cf86c 100644
--- a/lib/coderay/encoders/yaml.rb
+++ b/lib/coderay/encoders/yaml.rb
@@ -11,11 +11,37 @@ module Encoders
FILE_EXTENSION = 'yaml'
protected
- def compile tokens, options
+ def setup options
require 'yaml'
- @out = tokens.to_a.to_yaml
+ @out = []
end
-
+
+ def finish options
+ @out.to_a.to_yaml
+ end
+
+ public
+
+ def text_token text, kind
+ @out << [text, kind]
+ end
+
+ def begin_group kind
+ @out << [:begin_group, kind]
+ end
+
+ def end_group kind
+ @out << [:end_group, kind]
+ end
+
+ def begin_line kind
+ @out << [:begin_line, kind]
+ end
+
+ def end_line kind
+ @out << [:end_line, kind]
+ end
+
end
end