Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support color and strike tag in to_html #144

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/docx/containers/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions lib/docx/containers/text_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class TextRun
DEFAULT_FORMATTING = {
italic: false,
bold: false,
underline: false
underline: false,
strike: false
}

def self.tag
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -90,6 +93,10 @@ def bolded?
@formatting[:bold]
end

def striked?
@formatting[:strike]
end

def underlined?
@formatting[:underline]
end
Expand Down
15 changes: 14 additions & 1 deletion spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -382,6 +382,7 @@
@span_regex = /(\<span).+((?<=\>)\w+)(<\/span>)/
@em_regex = /(\<em).+((?<=\>)\w+)(\<\/em\>)/
@strong_regex = /(\<strong).+((?<=\>)\w+)(\<\/strong\>)/
@strike_regex = /(\<s).+((?<=\>)\w+)(\<\/s\>)/
@anchor_tag_regex = /\<a href="(.+)" target="_blank"\>(.+)\<\/a>/
end

Expand Down Expand Up @@ -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 '<s'
expect(scan.last).to eq '</s>'
expect(scan[1]).to eq 'Strike'
end

it 'should color the text' do
scan = @doc.paragraphs[14].to_html.scan(/\<p\s+([^\>]+)/).flatten
expect(scan.first).to eq 'style="font-size:11pt;color:#FF0000;"'
end

it 'should justify paragraphs' do
regex = /^<p[^\"]+.(?<=\")([^\"]+)/
expect(@doc.paragraphs[6].to_html.scan(regex).flatten.first.split(';').include?('text-align:center')).to eq(true)
Expand Down
Binary file added spec/fixtures/.DS_Store
Binary file not shown.
Binary file modified spec/fixtures/formatting.docx
Binary file not shown.
Loading