diff options
author | murphy <murphy@rubychan.de> | 2010-05-05 13:08:42 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2010-05-05 13:08:42 +0000 |
commit | 7b4acdd55492c8cb7db2fba4739b45d5955698de (patch) | |
tree | f8a1e7d50dbd5753909b98c3b3e516385537cdb8 /lib/coderay/duo.rb | |
parent | a97d39b3ca84484e45f3ff44a1ace6bdfb337c4c (diff) | |
download | coderay-7b4acdd55492c8cb7db2fba4739b45d5955698de.tar.gz |
Fixes for YAML encoder, Filter, and tests and API enhancements for Duo.
Diffstat (limited to 'lib/coderay/duo.rb')
-rw-r--r-- | lib/coderay/duo.rb | 67 |
1 files changed, 64 insertions, 3 deletions
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 |