Skip to content

Commit 9ea9595

Browse files
authored
Merge pull request #144 from supraja-trd/supraja_to_html_color_strike_support
Support color and strike tag in to_html
2 parents 24288dc + 4826fc5 commit 9ea9595

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

lib/docx/containers/paragraph.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def to_html
4949
html << text_run.to_html
5050
end
5151
styles = { 'font-size' => "#{font_size}pt" }
52+
styles['color'] = "##{font_color}" if font_color
5253
styles['text-align'] = alignment if alignment
5354
html_tag(:p, content: html, styles: styles)
5455
end
@@ -81,6 +82,11 @@ def font_size
8182
size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
8283
end
8384

85+
def font_color
86+
color_tag = @node.xpath('w:r//w:rPr//w:color').first
87+
color_tag ? color_tag.attributes['val'].value : nil
88+
end
89+
8490
def style
8591
return nil unless @document
8692

lib/docx/containers/text_run.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class TextRun
1010
DEFAULT_FORMATTING = {
1111
italic: false,
1212
bold: false,
13-
underline: false
13+
underline: false,
14+
strike: false
1415
}
1516

1617
def self.tag
@@ -60,7 +61,8 @@ def parse_formatting
6061
{
6162
italic: !@node.xpath('.//w:i').empty?,
6263
bold: !@node.xpath('.//w:b').empty?,
63-
underline: !@node.xpath('.//w:u').empty?
64+
underline: !@node.xpath('.//w:u').empty?,
65+
strike: !@node.xpath('.//w:strike').empty?
6466
}
6567
end
6668

@@ -73,6 +75,7 @@ def to_html
7375
html = @text
7476
html = html_tag(:em, content: html) if italicized?
7577
html = html_tag(:strong, content: html) if bolded?
78+
html = html_tag(:s, content: html) if striked?
7679
styles = {}
7780
styles['text-decoration'] = 'underline' if underlined?
7881
# No need to be granular with font size down to the span level if it doesn't vary.
@@ -90,6 +93,10 @@ def bolded?
9093
@formatting[:bold]
9194
end
9295

96+
def striked?
97+
@formatting[:strike]
98+
end
99+
93100
def underlined?
94101
@formatting[:underline]
95102
end

spec/docx/document_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
describe Docx::Document do
88
before(:all) do
99
@fixtures_path = 'spec/fixtures'
10-
@formatting_line_count = 13 # number of lines the formatting.docx file has
10+
@formatting_line_count = 15 # number of lines the formatting.docx file has
1111
end
1212

1313
describe '#open' do
@@ -382,6 +382,7 @@
382382
@span_regex = /(\<span).+((?<=\>)\w+)(<\/span>)/
383383
@em_regex = /(\<em).+((?<=\>)\w+)(\<\/em\>)/
384384
@strong_regex = /(\<strong).+((?<=\>)\w+)(\<\/strong\>)/
385+
@strike_regex = /(\<s).+((?<=\>)\w+)(\<\/s\>)/
385386
@anchor_tag_regex = /\<a href="(.+)" target="_blank"\>(.+)\<\/a>/
386387
end
387388

@@ -411,6 +412,18 @@
411412
expect(scan.first).to eq 'style="text-decoration:underline;"'
412413
end
413414

415+
it 'should strike striked text' do
416+
scan = @doc.paragraphs[13].to_html.scan(@strike_regex).flatten
417+
expect(scan.first).to eq '<s'
418+
expect(scan.last).to eq '</s>'
419+
expect(scan[1]).to eq 'Strike'
420+
end
421+
422+
it 'should color the text' do
423+
scan = @doc.paragraphs[14].to_html.scan(/\<p\s+([^\>]+)/).flatten
424+
expect(scan.first).to eq 'style="font-size:11pt;color:#FF0000;"'
425+
end
426+
414427
it 'should justify paragraphs' do
415428
regex = /^<p[^\"]+.(?<=\")([^\"]+)/
416429
expect(@doc.paragraphs[6].to_html.scan(regex).flatten.first.split(';').include?('text-align:center')).to eq(true)

spec/fixtures/.DS_Store

6 KB
Binary file not shown.

spec/fixtures/formatting.docx

-1.27 KB
Binary file not shown.

0 commit comments

Comments
 (0)