Class: Mapi::RTF::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/mapi/rtf.rb

Class Method Summary collapse

Class Method Details

.process(io) ⇒ Object



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
# File 'lib/mapi/rtf.rb', line 23

def self.process io
  while true do
    case c = io.getc
    when ?{; yield :open_group
    when ?}; yield :close_group
    when ?\\
      case c = io.getc
      when ?{, ?}, ?\\; yield :text, c.chr
      when ?'; yield :text, [io.read(2)].pack('H*')
      when ?a..?z, ?A..?Z
        # read control word
        str = c.chr
        str << c while c = io.read(1) and c =~ /[a-zA-Z]/
        neg = 1
        neg = -1 and c = io.read(1) if c == '-'
        num = if c =~ /[0-9]/
          num = c
          num << c while c = io.read(1) and c =~ /[0-9]/
          num.to_i * neg
        end
        raise "invalid rtf stream" if neg == -1 and !num # ???? \blahblah- some text
        io.seek(-1, IO::SEEK_CUR) if c != ' '
        yield :control_word, str, num
      when nil
        raise "invalid rtf stream" # \EOF
      else
        # other kind of control symbol
        yield :control_symbol, c.chr
      end
    when nil
      return
    when ?\r, ?\n
      # ignore
    else yield :text, c.chr
    end
  end
end