diff --git a/lib/docx/containers/container.rb b/lib/docx/containers/container.rb index 8764d70..510bb9b 100644 --- a/lib/docx/containers/container.rb +++ b/lib/docx/containers/container.rb @@ -7,12 +7,12 @@ module Container # Relation methods # TODO: Create a properties object, include Element def properties - @node.at_xpath("./#{@properties_tag}") + @node.at_xpath("./w:#{@properties_tag}") end # Erase text within an element def blank! - @node.xpath(".//w:t").each {|t| t.content = '' } + @node.xpath('.//w:t').each { |t| t.content = '' } end def remove! diff --git a/lib/docx/containers/paragraph.rb b/lib/docx/containers/paragraph.rb index 4e0338e..34004de 100755 --- a/lib/docx/containers/paragraph.rb +++ b/lib/docx/containers/paragraph.rb @@ -15,11 +15,12 @@ def self.tag # Child elements: pPr, r, fldSimple, hlink, subDoc # http://msdn.microsoft.com/en-us/library/office/ee364458(v=office.11).aspx - def initialize(node, document_properties = {}) + def initialize(node, document_properties = {}, doc = nil) @node = node @properties_tag = 'pPr' @document_properties = document_properties @font_size = @document_properties[:font_size] + @document = doc end # Set text of paragraph @@ -79,17 +80,30 @@ def font_size size_tag = @node.xpath('w:pPr//w:sz').first size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size end - + + def style + return nil unless @document + + if style_property.nil? + @document.default_paragraph_style + else + @document.style_name(style_property.attributes['val'].value) + end + end + alias_method :text, :to_s private + def style_property + properties&.at_xpath('w:pStyle') + end + # Returns the alignment if any, or nil if left def alignment alignment_tag = @node.xpath('.//w:jc').first alignment_tag ? alignment_tag.attributes['val'].value : nil end - end end end diff --git a/lib/docx/document.rb b/lib/docx/document.rb index f4ae3f0..a3e10de 100755 --- a/lib/docx/document.rb +++ b/lib/docx/document.rb @@ -168,6 +168,18 @@ def replace_entry(entry_path, file_contents) @replace[entry_path] = file_contents end + def default_paragraph_style + s = @styles.at_xpath("w:styles/w:style[@w:type='paragraph' and @w:default='1']") + s = s.at_xpath('w:name') + s.attributes['val'].value + end + + def style_name(style_id) + s = @styles.at_xpath("w:styles/w:style[@w:styleId='#{style_id}']") + s = s.at_xpath('w:name') + s.attributes['val'].value + end + private def load_styles @@ -198,7 +210,7 @@ def update # generate Elements::Containers::Paragraph from paragraph XML node def parse_paragraph_from(p_node) - Elements::Containers::Paragraph.new(p_node, document_properties) + Elements::Containers::Paragraph.new(p_node, document_properties, self) end # generate Elements::Bookmark from bookmark XML node diff --git a/spec/docx/document_spec.rb b/spec/docx/document_spec.rb index 00bd63f..28db70f 100755 --- a/spec/docx/document_spec.rb +++ b/spec/docx/document_spec.rb @@ -504,4 +504,22 @@ expect(doc.to_s).to be_a(String) end end + + describe 'reading style' do + before do + @doc = Docx::Document.open(@fixtures_path + '/test_with_style.docx') + end + + it 'read default style when not' do + nb = @doc.paragraphs.size + expect(nb).to eq 6 + expect(@doc.paragraphs[0].style).to eq 'Normal' + expect(@doc.paragraphs[1].style).to eq 'STYLE1' + expect(@doc.paragraphs[2].style).to eq 'heading 1' + expect(@doc.paragraphs[3].style).to eq 'Normal' + expect(@doc.paragraphs[4].style).to eq 'Normal' + expect(@doc.paragraphs[5].style).to eq 'STYLE1' + end + end + end diff --git a/spec/fixtures/test_with_style.docx b/spec/fixtures/test_with_style.docx new file mode 100755 index 0000000..02edfc0 Binary files /dev/null and b/spec/fixtures/test_with_style.docx differ