Class: RTF::Tokenizer

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

Class Method Summary collapse

Class Method Details

.process(io) ⇒ Object



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

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