Skip to content

Commit

Permalink
Do not write to stderr by default (dj2#6)
Browse files Browse the repository at this point in the history
Add a 'unknown_control_warning_enabled' flag to the parser to disable writing to stderr on a unknown control character. Upgrade rspec.
  • Loading branch information
nippysaurus authored and dj2 committed May 10, 2017
1 parent bdc5d20 commit 75d5c50
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
23 changes: 14 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/ruby-rtf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand Down
90 changes: 55 additions & 35 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -456,25 +476,25 @@

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|
it "sets a #{quote}" do
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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -555,53 +575,53 @@
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

it 'inserts a \super' do
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

it 'inserts a \sub' do
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

it 'inserts a \strike' do
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

it 'inserts a \scaps' do
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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75d5c50

Please sign in to comment.