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 | |
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')
-rw-r--r-- | lib/coderay/duo.rb | 67 | ||||
-rw-r--r-- | lib/coderay/encoders/filter.rb | 16 | ||||
-rw-r--r-- | lib/coderay/encoders/yaml.rb | 32 |
3 files changed, 102 insertions, 13 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 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 |