diff --git a/Gemfile.lock b/Gemfile.lock index b729a7e..8af6f90 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,15 +6,20 @@ PATH GEM remote: http://rubygems.org/ specs: - diff-lcs (1.1.2) - rspec (2.5.0) - rspec-core (~> 2.5.0) - rspec-expectations (~> 2.5.0) - rspec-mocks (~> 2.5.0) - rspec-core (2.5.1) - rspec-expectations (2.5.0) - diff-lcs (~> 1.1.2) - rspec-mocks (2.5.0) + diff-lcs (1.3) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-mocks (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) PLATFORMS ruby diff --git a/lib/ruby-rtf/parser.rb b/lib/ruby-rtf/parser.rb index 54a375c..f0e51e7 100644 --- a/lib/ruby-rtf/parser.rb +++ b/lib/ruby-rtf/parser.rb @@ -10,12 +10,14 @@ class Parser attr_reader :doc - def initialize + # @param unknown_control_warning_enabled [Boolean] Whether to write unknown control directive warnings to STDERR + def initialize(unknown_control_warning_enabled: true) # default_mods needs to be the same has in the formatting stack and in # the current_section modifiers or the first stack ends up getting lost. default_mods = {} @formatting_stack = [default_mods] @current_section = {:text => '', :modifiers => default_mods} + @unknown_control_warning_enabled = unknown_control_warning_enabled @seen = {} @@ -292,7 +294,9 @@ def handle_control(name, val, src, current_pos) else unless @seen[name] @seen[name] = true - STDERR.puts "Unknown control #{name.inspect} with #{val} at #{current_pos}" + if @unknown_control_warning_enabled + warn "Unknown control #{name.inspect} with #{val} at #{current_pos}" + end end end current_pos diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 741b178..900174f 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -6,6 +6,26 @@ let(:parser) { RubyRTF::Parser.new } let(:doc) { parser.doc } + context 'with input containing invalid control directives' do + let(:parser) { RubyRTF::Parser.new(unknown_control_warning_enabled: unknown_control_warning_enabled) } + let(:doc) { '{\rtf1\ansi\xxxxx0}' } + + context 'with unknown_control_warning_enabled = false' do + let(:unknown_control_warning_enabled) { false } + + it 'does not write anything to stderr' do + expect { parser.parse(doc) }.not_to output.to_stderr + end + end + context 'with unknown_control_warning_enabled = true' do + let(:unknown_control_warning_enabled) { true } + + it 'writes message to stderr' do + expect { parser.parse(doc) }.to output("Unknown control :xxxxx with 0 at 18\n").to_stderr + end + end + end + it 'parses hello world' do src = '{\rtf1\ansi\deff0 {\fonttbl {\f0 Times New Roman;}}\f0 \fs60 Hello, World!}' lambda { parser.parse(src) }.should_not raise_error @@ -14,7 +34,7 @@ it 'returns a RTF::Document' do src = '{\rtf1\ansi\deff0 {\fonttbl {\f0 Times New Roman;}}\f0 \fs60 Hello, World!}' d = parser.parse(src) - d.is_a?(RubyRTF::Document).should be_true + d.is_a?(RubyRTF::Document).should == true end it 'parses a default font (\deffN)' do @@ -76,21 +96,21 @@ section = parser.parse(src).sections section[0][:modifiers][:font_size].should == 30 - section[0][:modifiers][:bold].should be_true - section[0][:modifiers].has_key?(:italic).should be_false - section[0][:modifiers].has_key?(:underline).should be_false + section[0][:modifiers][:bold].should == true + section[0][:modifiers].has_key?(:italic).should == false + section[0][:modifiers].has_key?(:underline).should == false section[0][:text].should == 'Hello ' section[1][:modifiers][:font_size].should == 15 - section[1][:modifiers][:italic].should be_true - section[1][:modifiers][:bold].should be_true - section[1][:modifiers].has_key?(:underline).should be_false + section[1][:modifiers][:italic].should == true + section[1][:modifiers][:bold].should == true + section[1][:modifiers].has_key?(:underline).should == false section[1][:text].should == 'World' section[2][:modifiers][:font_size].should == 30 - section[2][:modifiers][:bold].should be_true - section[2][:modifiers][:underline].should be_true - section[2][:modifiers].has_key?(:italic).should be_false + section[2][:modifiers][:bold].should == true + section[2][:modifiers][:underline].should == true + section[2][:modifiers].has_key?(:italic).should == false section[2][:text].should == 'Goodbye, cruel world.' end @@ -106,16 +126,16 @@ it 'should parse jpeg' do section = parser.parse(src_jpeg).sections - section[0][:modifiers][:picture].should be_true + section[0][:modifiers][:picture].should == true section[0][:modifiers][:picture_format].should == 'jpeg' end it 'should parse bmp' do section = parser.parse(src_bitmap).sections - section[0][:modifiers][:picture].should be_true + section[0][:modifiers][:picture].should == true section[0][:modifiers][:picture_format].should == 'bmp' section = parser.parse(src_bitmap).sections - section[0][:modifiers][:picture].should be_true + section[0][:modifiers][:picture].should == true section[0][:modifiers][:picture_format].should == 'bmp' end @@ -144,12 +164,12 @@ it 'clears ul with ul0' do src = '{\rtf1 \ul\b Hello\b0\ul0 World}' section = parser.parse(src).sections - section[0][:modifiers][:bold].should be_true - section[0][:modifiers][:underline].should be_true + section[0][:modifiers][:bold].should == true + section[0][:modifiers][:underline].should == true section[0][:text].should == 'Hello' - section[1][:modifiers].has_key?(:bold).should be_false - section[1][:modifiers].has_key?(:underline).should be_false + section[1][:modifiers].has_key?(:bold).should == false + section[1][:modifiers].has_key?(:underline).should == false section[1][:text].should == 'World' end end @@ -376,7 +396,7 @@ doc = parser.parse(src) clr = doc.colour_table[0] - clr.use_default?.should be_true + clr.use_default?.should == true clr = doc.colour_table[1] clr.red.should == 255 @@ -456,17 +476,17 @@ it 'sets bold' do parser.handle_control(:b, nil, nil, 0) - parser.current_section[:modifiers][:bold].should be_true + parser.current_section[:modifiers][:bold].should == true end it 'sets underline' do parser.handle_control(:ul, nil, nil, 0) - parser.current_section[:modifiers][:underline].should be_true + parser.current_section[:modifiers][:underline].should == true end it 'sets italic' do parser.handle_control(:i, nil, nil, 0) - parser.current_section[:modifiers][:italic].should be_true + parser.current_section[:modifiers][:italic].should == true end %w(rquote lquote).each do |quote| @@ -474,7 +494,7 @@ parser.current_section[:text] = 'My code' parser.handle_control(quote.to_sym, nil, nil, 0) doc.sections.last[:text].should == "'" - doc.sections.last[:modifiers][quote.to_sym].should be_true + doc.sections.last[:modifiers][quote.to_sym].should == true end end @@ -483,7 +503,7 @@ parser.current_section[:text] = 'My code' parser.handle_control(quote.to_sym, nil, nil, 0) doc.sections.last[:text].should == '"' - doc.sections.last[:modifiers][quote.to_sym].should be_true + doc.sections.last[:modifiers][quote.to_sym].should == true end end @@ -530,7 +550,7 @@ parser.current_section[:text] = "end." parser.handle_control(:uc, 0, nil, 0) parser.handle_control(:u, 8232, nil, 0) - doc.sections.last[:modifiers][:newline].should be_true + doc.sections.last[:modifiers][:newline].should == true doc.sections.last[:text].should == "\n" end end @@ -540,7 +560,7 @@ it "sets from #{type}" do parser.current_section[:text] = "end." parser.handle_control(type.to_sym, nil, nil, 0) - doc.sections.last[:modifiers][:newline].should be_true + doc.sections.last[:modifiers][:newline].should == true doc.sections.last[:text].should == "\n" end end @@ -555,7 +575,7 @@ it 'inserts a \tab' do parser.current_section[:text] = "end." parser.handle_control(:tab, nil, nil, 0) - doc.sections.last[:modifiers][:tab].should be_true + doc.sections.last[:modifiers][:tab].should == true doc.sections.last[:text].should == "\t" end @@ -563,7 +583,7 @@ parser.current_section[:text] = "end." parser.handle_control(:super, nil, nil, 0) - parser.current_section[:modifiers][:superscript].should be_true + parser.current_section[:modifiers][:superscript].should == true parser.current_section[:text].should == "" end @@ -571,7 +591,7 @@ parser.current_section[:text] = "end." parser.handle_control(:sub, nil, nil, 0) - parser.current_section[:modifiers][:subscript].should be_true + parser.current_section[:modifiers][:subscript].should == true parser.current_section[:text].should == "" end @@ -579,7 +599,7 @@ parser.current_section[:text] = "end." parser.handle_control(:strike, nil, nil, 0) - parser.current_section[:modifiers][:strikethrough].should be_true + parser.current_section[:modifiers][:strikethrough].should == true parser.current_section[:text].should == "" end @@ -587,21 +607,21 @@ parser.current_section[:text] = "end." parser.handle_control(:scaps, nil, nil, 0) - parser.current_section[:modifiers][:smallcaps].should be_true + parser.current_section[:modifiers][:smallcaps].should == true parser.current_section[:text].should == "" end it 'inserts an \emdash' do parser.current_section[:text] = "end." parser.handle_control(:emdash, nil, nil, 0) - doc.sections.last[:modifiers][:emdash].should be_true + doc.sections.last[:modifiers][:emdash].should == true doc.sections.last[:text].should == "--" end it 'inserts an \endash' do parser.current_section[:text] = "end." parser.handle_control(:endash, nil, nil, 0) - doc.sections.last[:modifiers][:endash].should be_true + doc.sections.last[:modifiers][:endash].should == true doc.sections.last[:text].should == "-" end @@ -627,8 +647,8 @@ parser.current_section[:modifiers][:italic] = true parser.handle_control(type.to_sym, nil, nil, 0) - parser.current_section[:modifiers].has_key?(:bold).should be_false - parser.current_section[:modifiers].has_key?(:italic).should be_false + parser.current_section[:modifiers].has_key?(:bold).should == false + parser.current_section[:modifiers].has_key?(:italic).should == false end end @@ -723,7 +743,7 @@ it 'handles :~' do parser.current_section[:text] = "end." parser.handle_control(:~, nil, nil, 0) - doc.sections.last[:modifiers][:nbsp].should be_true + doc.sections.last[:modifiers][:nbsp].should == true doc.sections.last[:text].should == " " end end