55describe Docx ::Document do
66 before ( :all ) do
77 @fixtures_path = "spec/fixtures"
8+ @formatting_line_count = 11 # number of lines the formatting.docx file has
89 end
910
1011 describe 'reading' do
166167 end
167168
168169 it 'should have the correct text' do
169- @doc . paragraphs . size . should eq 11
170+ @doc . paragraphs . size . should eq @formatting_line_count
170171 @doc . paragraphs [ 0 ] . text . should eq 'Normal'
171172 @doc . paragraphs [ 1 ] . text . should eq 'Italic'
172173 @doc . paragraphs [ 2 ] . text . should eq 'Bold'
304305 end
305306 end
306307 end
308+
309+
310+ describe 'outputting html' do
311+ before do
312+ @doc = Docx ::Document . open ( @fixtures_path + '/formatting.docx' )
313+ @formatted_line = @doc . paragraphs [ 5 ]
314+ end
315+
316+ it 'should wrap pragraphs in a p tag' do
317+ scan = @doc . paragraphs [ 0 ] . to_html . scan ( /(^\< p).+((?<=\> )\w +)(\< \/ p>$)/ ) . flatten
318+ scan . first . should eq '<p'
319+ scan . last . should eq '</p>'
320+ scan [ 1 ] . should eq 'Normal'
321+ end
322+
323+ it 'should emphasize italicized text' do
324+ scan = @doc . paragraphs [ 1 ] . to_html . scan ( /(\< em\> )(\w +)(\< \/ em\> )/ ) . flatten
325+ scan . first . should eq '<em>'
326+ scan . last . should eq '</em>'
327+ scan [ 1 ] . should eq 'Italic'
328+ end
329+
330+ it 'should strong bolded text' do
331+ scan = @doc . paragraphs [ 2 ] . to_html . scan ( /(\< strong\> )(\w +)(\< \/ strong\> )/ ) . flatten
332+ scan . first . should eq '<strong>'
333+ scan . last . should eq '</strong>'
334+ scan [ 1 ] . should eq 'Bold'
335+ end
336+
337+ it 'should underline underlined text' do
338+ scan = @doc . paragraphs [ 3 ] . to_html . scan ( /\< span\s +([^\> ]+)/ ) . flatten
339+ scan . first . should eq 'style="text-decoration:underline;"'
340+ end
341+
342+ it 'should justify paragraphs' do
343+ regex = /^<p[^\" ]+.(?<=\" )([^\" ]+)/
344+ @doc . paragraphs [ 6 ] . to_html . scan ( regex ) . flatten . first . split ( ';' ) . include? ( 'text-align:center' ) . should be_true
345+ @doc . paragraphs [ 7 ] . to_html . scan ( regex ) . flatten . first . split ( ';' ) . include? ( 'text-align:left' ) . should be_false
346+ @doc . paragraphs [ 8 ] . to_html . scan ( regex ) . flatten . first . split ( ';' ) . include? ( 'text-align:right' ) . should be_true
347+ end
348+
349+ it "should set font size on styled paragraphs" do
350+ regex = /(\< p{1})[^\> ]+style\= \" ([^\" ]+).+(<\/ p>)/
351+ scan = @doc . paragraphs [ 9 ] . to_html . scan ( regex ) . flatten
352+ scan . first . should eq '<p'
353+ scan . last . should eq '</p>'
354+ scan [ 1 ] . split ( ';' ) . include? ( 'font-size:14pt' ) . should be_true
355+ end
356+
357+ it 'should set font size on styled text runs' do
358+ regex = /(\< span)[^\> ]+style\= \" ([^\" ]+)[^\< ]+(<\/ span>)/
359+ scan = @doc . paragraphs [ 10 ] . to_html . scan ( regex ) . flatten
360+ scan . first . should eq '<span'
361+ scan . last . should eq '</span>'
362+ scan [ 1 ] . split ( ';' ) . include? ( 'font-size:16pt' ) . should be_true
363+ end
364+
365+ it 'should output an entire document as html' do
366+ @doc . to_html . scan ( /(\< p)/ ) . flatten . size . should eq @formatting_line_count
367+ end
368+
369+ it 'should output styled html' do
370+ @formatted_line . to_html . scan ( '<span style="text-decoration:underline;"><strong><em>all</em></strong></span>' ) . size . should eq 1
371+ end
372+
373+ end
307374end
0 commit comments