File tree Expand file tree Collapse file tree 5 files changed +29
-3
lines changed Expand file tree Collapse file tree 5 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ def to_html
49
49
html << text_run . to_html
50
50
end
51
51
styles = { 'font-size' => "#{ font_size } pt" }
52
+ styles [ 'color' ] = "##{ font_color } " if font_color
52
53
styles [ 'text-align' ] = alignment if alignment
53
54
html_tag ( :p , content : html , styles : styles )
54
55
end
@@ -81,6 +82,11 @@ def font_size
81
82
size_tag ? size_tag . attributes [ 'val' ] . value . to_i / 2 : @font_size
82
83
end
83
84
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
+
84
90
def style
85
91
return nil unless @document
86
92
Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ class TextRun
10
10
DEFAULT_FORMATTING = {
11
11
italic : false ,
12
12
bold : false ,
13
- underline : false
13
+ underline : false ,
14
+ strike : false
14
15
}
15
16
16
17
def self . tag
@@ -60,7 +61,8 @@ def parse_formatting
60
61
{
61
62
italic : !@node . xpath ( './/w:i' ) . empty? ,
62
63
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?
64
66
}
65
67
end
66
68
@@ -73,6 +75,7 @@ def to_html
73
75
html = @text
74
76
html = html_tag ( :em , content : html ) if italicized?
75
77
html = html_tag ( :strong , content : html ) if bolded?
78
+ html = html_tag ( :s , content : html ) if striked?
76
79
styles = { }
77
80
styles [ 'text-decoration' ] = 'underline' if underlined?
78
81
# No need to be granular with font size down to the span level if it doesn't vary.
@@ -90,6 +93,10 @@ def bolded?
90
93
@formatting [ :bold ]
91
94
end
92
95
96
+ def striked?
97
+ @formatting [ :strike ]
98
+ end
99
+
93
100
def underlined?
94
101
@formatting [ :underline ]
95
102
end
Original file line number Diff line number Diff line change 7
7
describe Docx ::Document do
8
8
before ( :all ) do
9
9
@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
11
11
end
12
12
13
13
describe '#open' do
382
382
@span_regex = /(\< span).+((?<=\> )\w +)(<\/ span>)/
383
383
@em_regex = /(\< em).+((?<=\> )\w +)(\< \/ em\> )/
384
384
@strong_regex = /(\< strong).+((?<=\> )\w +)(\< \/ strong\> )/
385
+ @strike_regex = /(\< s).+((?<=\> )\w +)(\< \/ s\> )/
385
386
@anchor_tag_regex = /\< a href="(.+)" target="_blank"\> (.+)\< \/ a>/
386
387
end
387
388
411
412
expect ( scan . first ) . to eq 'style="text-decoration:underline;"'
412
413
end
413
414
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
+
414
427
it 'should justify paragraphs' do
415
428
regex = /^<p[^\" ]+.(?<=\" )([^\" ]+)/
416
429
expect ( @doc . paragraphs [ 6 ] . to_html . scan ( regex ) . flatten . first . split ( ';' ) . include? ( 'text-align:center' ) ) . to eq ( true )
You can’t perform that action at this time.
0 commit comments