Skip to content

Commit b928484

Browse files
authored
Merge pull request ruby-docx#138 from miko53/styles-dev
add basic style definition
2 parents 564a364 + 8245e15 commit b928484

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

lib/docx/containers/container.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ module Container
77
# Relation methods
88
# TODO: Create a properties object, include Element
99
def properties
10-
@node.at_xpath("./#{@properties_tag}")
10+
@node.at_xpath("./w:#{@properties_tag}")
1111
end
1212

1313
# Erase text within an element
1414
def blank!
15-
@node.xpath(".//w:t").each {|t| t.content = '' }
15+
@node.xpath('.//w:t').each { |t| t.content = '' }
1616
end
1717

1818
def remove!

lib/docx/containers/paragraph.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ def self.tag
1515

1616
# Child elements: pPr, r, fldSimple, hlink, subDoc
1717
# http://msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx
18-
def initialize(node, document_properties = {})
18+
def initialize(node, document_properties = {}, doc = nil)
1919
@node = node
2020
@properties_tag = 'pPr'
2121
@document_properties = document_properties
2222
@font_size = @document_properties[:font_size]
23+
@document = doc
2324
end
2425

2526
# Set text of paragraph
@@ -79,17 +80,30 @@ def font_size
7980
size_tag = @node.xpath('w:pPr//w:sz').first
8081
size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
8182
end
82-
83+
84+
def style
85+
return nil unless @document
86+
87+
if style_property.nil?
88+
@document.default_paragraph_style
89+
else
90+
@document.style_name(style_property.attributes['val'].value)
91+
end
92+
end
93+
8394
alias_method :text, :to_s
8495

8596
private
8697

98+
def style_property
99+
properties&.at_xpath('w:pStyle')
100+
end
101+
87102
# Returns the alignment if any, or nil if left
88103
def alignment
89104
alignment_tag = @node.xpath('.//w:jc').first
90105
alignment_tag ? alignment_tag.attributes['val'].value : nil
91106
end
92-
93107
end
94108
end
95109
end

lib/docx/document.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ def replace_entry(entry_path, file_contents)
168168
@replace[entry_path] = file_contents
169169
end
170170

171+
def default_paragraph_style
172+
s = @styles.at_xpath("w:styles/w:style[@w:type='paragraph' and @w:default='1']")
173+
s = s.at_xpath('w:name')
174+
s.attributes['val'].value
175+
end
176+
177+
def style_name(style_id)
178+
s = @styles.at_xpath("w:styles/w:style[@w:styleId='#{style_id}']")
179+
s = s.at_xpath('w:name')
180+
s.attributes['val'].value
181+
end
182+
171183
private
172184

173185
def load_styles
@@ -198,7 +210,7 @@ def update
198210

199211
# generate Elements::Containers::Paragraph from paragraph XML node
200212
def parse_paragraph_from(p_node)
201-
Elements::Containers::Paragraph.new(p_node, document_properties)
213+
Elements::Containers::Paragraph.new(p_node, document_properties, self)
202214
end
203215

204216
# generate Elements::Bookmark from bookmark XML node

spec/docx/document_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,22 @@
504504
expect(doc.to_s).to be_a(String)
505505
end
506506
end
507+
508+
describe 'reading style' do
509+
before do
510+
@doc = Docx::Document.open(@fixtures_path + '/test_with_style.docx')
511+
end
512+
513+
it 'read default style when not' do
514+
nb = @doc.paragraphs.size
515+
expect(nb).to eq 6
516+
expect(@doc.paragraphs[0].style).to eq 'Normal'
517+
expect(@doc.paragraphs[1].style).to eq 'STYLE1'
518+
expect(@doc.paragraphs[2].style).to eq 'heading 1'
519+
expect(@doc.paragraphs[3].style).to eq 'Normal'
520+
expect(@doc.paragraphs[4].style).to eq 'Normal'
521+
expect(@doc.paragraphs[5].style).to eq 'STYLE1'
522+
end
523+
end
524+
507525
end

spec/fixtures/test_with_style.docx

20.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)