Skip to content

Commit

Permalink
Merge pull request #138 from miko53/styles-dev
Browse files Browse the repository at this point in the history
add basic style definition
  • Loading branch information
satoryu authored Oct 15, 2023
2 parents 564a364 + 8245e15 commit b928484
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/docx/containers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
20 changes: 17 additions & 3 deletions lib/docx/containers/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added spec/fixtures/test_with_style.docx
Binary file not shown.

0 comments on commit b928484

Please sign in to comment.