summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/text.rb
blob: c1460384f2b35c0e38d0fe527d28cf37792e17b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
($:.unshift '../..'; require 'coderay') unless defined? CodeRay
module CodeRay
module Encoders
  
  # Concats the tokens into a single string, resulting in the original
  # code string if no tokens were removed.
  # 
  # Alias: +plain+
  # 
  # == Options
  # 
  # === :separator
  # A separator string to join the tokens.
  # 
  # Default: empty String
  class Text < Encoder

    register_for :text

    FILE_EXTENSION = 'txt'

    DEFAULT_OPTIONS = {
      :separator => nil
    }

    def text_token text, kind
      @out << text
      @out << @sep if @sep
    end

  protected
    def setup options
      super
      @sep = options[:separator]
    end

    def finish options
      super.chomp @sep
    end

  end

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 CountTest < Test::Unit::TestCase
  
  def test_count
    ruby = <<-RUBY
puts "Hello world!"
    RUBY
    tokens = CodeRay.scan ruby, :ruby
    assert_equal ruby, tokens.encode_with(:text)
  end
  
end