Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic style definition #138

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 19 additions & 2 deletions lib/docx/containers/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def initialize(node, document_properties = {})
@font_size = @document_properties[:font_size]
end

def document=(doc)
@document = doc
end

# Set text of paragraph
def text=(content)
if text_runs.size == 1
Expand Down Expand Up @@ -79,17 +83,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
16 changes: 15 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,9 @@ def update

# generate Elements::Containers::Paragraph from paragraph XML node
def parse_paragraph_from(p_node)
Elements::Containers::Paragraph.new(p_node, document_properties)
p = Elements::Containers::Paragraph.new(p_node, document_properties)
p.document = self
p
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#document= looks much open: anybody can replace another Document instance with this accessor of the paragraph returned.
how about giving Document object to Paragraph's constructor like Element::Containers::Paragraph.new(p_node, document_properties, self) ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it's ok, my initial idea was to avoid modifying previous API.
I modify branch to take into account.

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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add one indent

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done also

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.