docx 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 236ded16324395b50579ba21fdc84891251ffc75b9bd1f5c65c4d125c6689c2f
4
- data.tar.gz: d994c54de22f1829928d4fe60087d8f4667905d4e13e88e9b607a9ae3ac34077
3
+ metadata.gz: d639e88e5950b94157fc4f81b14f0f8bf355abb4fdedd24b7f40dc00eafb72d7
4
+ data.tar.gz: e64400d25d919b4cd51f4b876d7f35c3433841523055866dff2c300f7c15aca7
5
5
  SHA512:
6
- metadata.gz: a5a969c0011ac31aba461a485474015b230a76d2c449275f90a5267f4ee9b38ba698090561fea0cade728502be7db6e306c8d9a29cc662400122ed138a209e18
7
- data.tar.gz: f0e12812fd5d488162a4d263a41d095e31ab0500824ae99cc58f4fafe82ef4468b44fa68bcf0db86b3ecd305cd263602334dfbea81fa14622d7303060e461779
6
+ metadata.gz: d2ad2931111374349dd03ca04bef655a9eaa797b84d688198ebeac84cdf93af147d4f8fd5a561c4e506d7f823c9b22884c698a315d7e2bb505357c7c6da9316a
7
+ data.tar.gz: 25cb0bc5f3b67b4af1874b902fbdfe3061205cee0c8b288187924b80cb50a6bd5a7076ea497be702a07241841da146055b3be0dbffeb90c20922f851ad31588c
@@ -40,6 +40,7 @@ module Docx
40
40
  elsif @text_nodes.empty?
41
41
  new_t = Elements::Text.create_within(self)
42
42
  new_t.content = content
43
+ @text_nodes << new_t
43
44
  end
44
45
  reset_text
45
46
  end
data/lib/docx/document.rb CHANGED
@@ -113,6 +113,9 @@ module Docx
113
113
  end
114
114
 
115
115
  def hyperlink_relationships
116
+ # @rels is nil when the document has no relationships part (#192)
117
+ return Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new) unless @rels
118
+
116
119
  @rels.xpath("//xmlns:Relationship[contains(@Type,'hyperlink')]")
117
120
  end
118
121
 
@@ -51,28 +51,92 @@ module Docx
51
51
 
52
52
  # Get text run immediately prior to bookmark node
53
53
  def get_run_before
54
- # at_xpath returns the first match found and preceding-sibling returns siblings in the
55
- # order they appear in the document not the order as they appear when moving out from
56
- # the starting node
57
- if not (r_nodes = @node.xpath("./preceding-sibling::w:r")).empty?
58
- r_node = r_nodes.last
59
- Containers::TextRun.new(r_node)
60
- else
54
+ if enclosing_paragraph
55
+ # at_xpath returns the first match found and preceding-sibling returns siblings in the
56
+ # order they appear in the document not the order as they appear when moving out from
57
+ # the starting node
58
+ unless (r_nodes = @node.xpath("./preceding-sibling::w:r")).empty?
59
+ return Containers::TextRun.new(r_nodes.last)
60
+ end
61
+
61
62
  new_r = Containers::TextRun.create_with(self)
62
63
  new_r.insert_before(self)
63
- new_r
64
+ return new_r
64
65
  end
66
+
67
+ # Block-level bookmark (e.g. Google Docs places bookmarkStart/End directly
68
+ # under w:body). Add a run to the preceding paragraph, or to a new
69
+ # paragraph before the bookmark, so the run lives inside a w:p.
70
+ run_at_paragraph_end(@node.xpath("./preceding-sibling::w:p").last) ||
71
+ run_in_new_paragraph { |paragraph| @node.add_previous_sibling(paragraph) }
65
72
  end
66
73
 
67
74
  # Get text run immediately after bookmark node
68
75
  def get_run_after
69
- if (r_node = @node.at_xpath("./following-sibling::w:r"))
70
- Containers::TextRun.new(r_node)
71
- else
76
+ if enclosing_paragraph
77
+ if (r_node = @node.at_xpath("./following-sibling::w:r"))
78
+ return Containers::TextRun.new(r_node)
79
+ end
80
+
72
81
  new_r = Containers::TextRun.create_with(self)
73
82
  new_r.insert_after(self)
74
- new_r
83
+ return new_r
84
+ end
85
+
86
+ # Block-level bookmark: add a run to the following paragraph, or to a new
87
+ # paragraph after the bookmark.
88
+ run_at_paragraph_start(@node.at_xpath("./following-sibling::w:p")) ||
89
+ run_in_new_paragraph { |paragraph| @node.add_next_sibling(paragraph) }
90
+ end
91
+
92
+ # Override Element#parent_paragraph so insert_multiple_lines also works for
93
+ # block-level bookmarks. For those we fill a fresh paragraph inserted at the
94
+ # bookmark, rather than an adjacent existing paragraph, so we neither crash
95
+ # (no enclosing paragraph) nor overwrite unrelated content.
96
+ def parent_paragraph
97
+ return Containers::Paragraph.new(enclosing_paragraph) if enclosing_paragraph
98
+
99
+ paragraph = Nokogiri::XML::Node.new("w:p", @node.document)
100
+ @node.add_next_sibling(paragraph)
101
+ Containers::Paragraph.new(paragraph)
102
+ end
103
+
104
+ private
105
+
106
+ # The w:p the bookmark sits inside, or nil when it is block-level.
107
+ def enclosing_paragraph
108
+ @node.at_xpath("./parent::w:p")
109
+ end
110
+
111
+ # A new run inserted at the start of paragraph_node (after w:pPr if present).
112
+ # Returns nil when paragraph_node is nil.
113
+ def run_at_paragraph_start(paragraph_node)
114
+ return nil unless paragraph_node
115
+
116
+ new_r = Containers::TextRun.create_with(self)
117
+ if (props = paragraph_node.at_xpath("w:pPr"))
118
+ props.add_next_sibling(new_r.node)
119
+ else
120
+ paragraph_node.prepend_child(new_r.node)
75
121
  end
122
+ new_r
123
+ end
124
+
125
+ # A new run appended to the end of paragraph_node. nil when it is nil.
126
+ def run_at_paragraph_end(paragraph_node)
127
+ return nil unless paragraph_node
128
+
129
+ Containers::TextRun.create_within(Containers::Paragraph.new(paragraph_node))
130
+ end
131
+
132
+ # Create a run wrapped in a fresh w:p; the block positions that paragraph
133
+ # relative to the bookmark.
134
+ def run_in_new_paragraph
135
+ new_r = Containers::TextRun.create_with(self)
136
+ paragraph = Nokogiri::XML::Node.new("w:p", @node.document)
137
+ paragraph.add_child(new_r.node)
138
+ yield paragraph
139
+ new_r
76
140
  end
77
141
  end
78
142
  end
@@ -42,7 +42,8 @@ module Docx
42
42
 
43
43
  # Insert node as first child (after properties)
44
44
  def prepend_to(element)
45
- @node = element.node.properties.add_next_sibling(@node)
45
+ properties = element.properties if element.respond_to?(:properties)
46
+ @node = properties ? properties.add_next_sibling(@node) : element.node.prepend_child(@node)
46
47
  self
47
48
  end
48
49
 
data/lib/docx/helpers.rb CHANGED
@@ -4,7 +4,7 @@ module Docx
4
4
  # than the default inspect method. But you can still get the default inspect method by passing
5
5
  # true as the first argument.
6
6
  def inspect(full = false)
7
- return(super) if full
7
+ return super() if full
8
8
 
9
9
  variable_values =
10
10
  instance_variables.map do |var|
data/lib/docx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Docx #:nodoc:
4
- VERSION = '0.13.0'
4
+ VERSION = '0.13.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt