diff --git a/lib/docx/containers/paragraph.rb b/lib/docx/containers/paragraph.rb index 34004de..3af2c1e 100755 --- a/lib/docx/containers/paragraph.rb +++ b/lib/docx/containers/paragraph.rb @@ -49,6 +49,7 @@ def to_html html << text_run.to_html end styles = { 'font-size' => "#{font_size}pt" } + styles['color'] = "##{font_color}" if font_color styles['text-align'] = alignment if alignment html_tag(:p, content: html, styles: styles) end @@ -81,6 +82,11 @@ def font_size size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size end + def font_color + color_tag = @node.xpath('w:r//w:rPr//w:color').first + color_tag ? color_tag.attributes['val'].value : nil + end + def style return nil unless @document diff --git a/lib/docx/containers/text_run.rb b/lib/docx/containers/text_run.rb index 1bc717f..722bea9 100755 --- a/lib/docx/containers/text_run.rb +++ b/lib/docx/containers/text_run.rb @@ -10,7 +10,8 @@ class TextRun DEFAULT_FORMATTING = { italic: false, bold: false, - underline: false + underline: false, + strike: false } def self.tag @@ -60,7 +61,8 @@ def parse_formatting { italic: !@node.xpath('.//w:i').empty?, bold: !@node.xpath('.//w:b').empty?, - underline: !@node.xpath('.//w:u').empty? + underline: !@node.xpath('.//w:u').empty?, + strike: !@node.xpath('.//w:strike').empty? } end @@ -73,6 +75,7 @@ def to_html html = @text html = html_tag(:em, content: html) if italicized? html = html_tag(:strong, content: html) if bolded? + html = html_tag(:s, content: html) if striked? styles = {} styles['text-decoration'] = 'underline' if underlined? # No need to be granular with font size down to the span level if it doesn't vary. @@ -90,6 +93,10 @@ def bolded? @formatting[:bold] end + def striked? + @formatting[:strike] + end + def underlined? @formatting[:underline] end diff --git a/spec/docx/document_spec.rb b/spec/docx/document_spec.rb index 980327a..42587dc 100755 --- a/spec/docx/document_spec.rb +++ b/spec/docx/document_spec.rb @@ -7,7 +7,7 @@ describe Docx::Document do before(:all) do @fixtures_path = 'spec/fixtures' - @formatting_line_count = 13 # number of lines the formatting.docx file has + @formatting_line_count = 15 # number of lines the formatting.docx file has end describe '#open' do @@ -382,6 +382,7 @@ @span_regex = /(\)\w+)(<\/span>)/ @em_regex = /(\)\w+)(\<\/em\>)/ @strong_regex = /(\)\w+)(\<\/strong\>)/ + @strike_regex = /(\)\w+)(\<\/s\>)/ @anchor_tag_regex = /\(.+)\<\/a>/ end @@ -411,6 +412,18 @@ expect(scan.first).to eq 'style="text-decoration:underline;"' end + it 'should strike striked text' do + scan = @doc.paragraphs[13].to_html.scan(@strike_regex).flatten + expect(scan.first).to eq '' + expect(scan[1]).to eq 'Strike' + end + + it 'should color the text' do + scan = @doc.paragraphs[14].to_html.scan(/\]+)/).flatten + expect(scan.first).to eq 'style="font-size:11pt;color:#FF0000;"' + end + it 'should justify paragraphs' do regex = /^