docx 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6dd59a023e1430fce917809931fe955651324db4fadb7a55d9a380731240466e
4
- data.tar.gz: eb91759397bdc0782691c6ed46b37a67a031ab580d590568a7f221a223d2f80c
3
+ metadata.gz: 96b871b0d07fecde4e6c93c67463da727565f3acca519b08b9c554d987570e58
4
+ data.tar.gz: 2f3b9362b91c33d02e6c1b4acf0b23fb723f71d26f9b2e800b930c91c3687eee
5
5
  SHA512:
6
- metadata.gz: c722c427338ceb1f4d8c926c4e3b2a8c2b2de9889e77a9b78fec4ffe2270b633dfd8f843a31ed17a77b6b5d09d9e9d8b4c88e52569b3c410d1f8ae9183c76c6d
7
- data.tar.gz: 41af211af034071991a920f1ed00e9ee9c8e5e739f41553c8b263081e7626183bc21e38735bfed88f01fdea0599f13a313cfc0ea1f9cc8331edff97e5cd5c40d
6
+ metadata.gz: 5e198cb74eb7a06b62bb63a3a0ff065b0c95cf26aafb53320bb5b14339dc386d975a1f57178316d1d5d7753081e07989b4498bcdfa3f0092f2fc14b1e4086b84
7
+ data.tar.gz: d0626b7332fd3a3d95b6358a3fb6ea09b0faf58545db9bd948c9f97baa5457eacb43267cc40914df2fa54872b42cca5c1963f66111a8b0b4be33a3e6f99c4b38
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # docx
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/docx.svg)](https://badge.fury.io/rb/docx)
4
- [![Build Status](https://travis-ci.org/ruby-docx/docx.svg?branch=master)](https://travis-ci.org/ruby-docx/docx)
4
+ [![Ruby](https://github.com/ruby-docx/docx/workflows/Ruby/badge.svg)](https://github.com/ruby-docx/docx/actions?query=workflow%3ARuby)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/ruby-docx/docx/badge.svg?branch=master)](https://coveralls.io/github/ruby-docx/docx?branch=master)
6
6
  [![Gitter](https://badges.gitter.im/ruby-docx/community.svg)](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
7
7
 
@@ -11,7 +11,7 @@ A ruby library/gem for interacting with `.docx` files. currently capabilities in
11
11
 
12
12
  ### Prerequisites
13
13
 
14
- - Ruby 2.4 or later
14
+ - Ruby 2.5 or later
15
15
 
16
16
  ### Install
17
17
 
@@ -134,6 +134,35 @@ end
134
134
  doc.save('example-edited.docx')
135
135
  ```
136
136
 
137
+ ### Writing to tables
138
+
139
+ ``` ruby
140
+ require 'docx'
141
+
142
+ # Create a Docx::Document object for our existing docx file
143
+ doc = Docx::Document.open('tables.docx')
144
+
145
+ # Iterate over each table
146
+ doc.tables.each do |table|
147
+ last_row = table.rows.last
148
+
149
+ # Copy last row and insert a new one before last row
150
+ new_row = last_row.copy
151
+ new_row.insert_before(last_row)
152
+
153
+ # Substitute text in each cell of this new row
154
+ new_row.cells.each do |cell|
155
+ cell.paragraphs.each do |paragraph|
156
+ paragraph.each_text_run do |text|
157
+ text.substitute('_placeholder_', 'replacement value')
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ doc.save('tables-edited.docx')
164
+ ```
165
+
137
166
  ### Advanced
138
167
 
139
168
  ``` ruby
@@ -12,14 +12,14 @@ module Docx
12
12
  bold: false,
13
13
  underline: false
14
14
  }
15
-
15
+
16
16
  def self.tag
17
17
  'r'
18
18
  end
19
19
 
20
20
  attr_reader :text
21
21
  attr_reader :formatting
22
-
22
+
23
23
  def initialize(node, document_properties = {})
24
24
  @node = node
25
25
  @text_nodes = @node.xpath('w:t').map {|t_node| Elements::Text.new(t_node) }
@@ -40,6 +40,7 @@ module Docx
40
40
  new_t = Elements::Text.create_within(self)
41
41
  new_t.content = content
42
42
  end
43
+ reset_text
43
44
  end
44
45
 
45
46
  # Returns text contained within text run
@@ -52,6 +53,7 @@ module Docx
52
53
  @text_nodes.each do |text_node|
53
54
  text_node.content = text_node.content.gsub(match, replacement)
54
55
  end
56
+ reset_text
55
57
  end
56
58
 
57
59
  def parse_formatting
@@ -74,7 +76,7 @@ module Docx
74
76
  styles = {}
75
77
  styles['text-decoration'] = 'underline' if underlined?
76
78
  # No need to be granular with font size down to the span level if it doesn't vary.
77
- styles['font-size'] = "#{font_size}pt" if font_size != @font_size
79
+ styles['font-size'] = "#{font_size}pt" if font_size != @font_size
78
80
  html = html_tag(:span, content: html, styles: styles) unless styles.empty?
79
81
  html = html_tag(:a, content: html, attributes: {href: href, target: "_blank"}) if hyperlink?
80
82
  return html
@@ -83,11 +85,11 @@ module Docx
83
85
  def italicized?
84
86
  @formatting[:italic]
85
87
  end
86
-
88
+
87
89
  def bolded?
88
90
  @formatting[:bold]
89
91
  end
90
-
92
+
91
93
  def underlined?
92
94
  @formatting[:underline]
93
95
  end
@@ -102,12 +104,18 @@ module Docx
102
104
 
103
105
  def hyperlink_id
104
106
  @node.attributes['id'].value
105
- end
107
+ end
106
108
 
107
109
  def font_size
108
110
  size_tag = @node.xpath('w:rPr//w:sz').first
109
111
  size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
110
112
  end
113
+
114
+ private
115
+
116
+ def reset_text
117
+ @text = parse_text
118
+ end
111
119
  end
112
120
  end
113
121
  end
data/lib/docx/document.rb CHANGED
@@ -30,8 +30,7 @@ module Docx
30
30
  @zip = Zip::File.open_buffer(path_or_io)
31
31
  end
32
32
 
33
- document = @zip.find_entry('word/document.xml')
34
- document ||= @zip.find_entry('word/document2.xml')
33
+ document = @zip.glob('word/document*.xml').first
35
34
  raise Errno::ENOENT if document.nil?
36
35
 
37
36
  @document_xml = document.get_input_stream.read
@@ -71,6 +70,10 @@ module Docx
71
70
  bkmrks_hsh
72
71
  end
73
72
 
73
+ def to_xml
74
+ Nokogiri::XML(@document_xml)
75
+ end
76
+
74
77
  def tables
75
78
  @doc.xpath('//w:document//w:body//w:tbl').map { |t_node| parse_table_from t_node }
76
79
  end
@@ -88,12 +91,12 @@ module Docx
88
91
  def hyperlinks
89
92
  hyperlink_relationships.each_with_object({}) do |rel, hash|
90
93
  hash[rel.attributes['Id'].value] = rel.attributes['Target'].value
91
- end
94
+ end
92
95
  end
93
96
 
94
97
  def hyperlink_relationships
95
98
  @rels.xpath("//xmlns:Relationship[contains(@Type,'hyperlink')]")
96
- end
99
+ end
97
100
 
98
101
  ##
99
102
  # *Deprecated*
@@ -169,13 +172,20 @@ module Docx
169
172
  def load_styles
170
173
  @styles_xml = @zip.read('word/styles.xml')
171
174
  @styles = Nokogiri::XML(@styles_xml)
172
- @rels_xml = @zip.read('word/_rels/document.xml.rels')
173
- @rels = Nokogiri::XML(@rels_xml)
175
+ load_rels
174
176
  rescue Errno::ENOENT => e
175
177
  warn e.message
176
178
  nil
177
179
  end
178
180
 
181
+ def load_rels
182
+ rels_entry = @zip.glob('word/_rels/document*.xml.rels').first
183
+ raise Errno::ENOENT unless rels_entry
184
+
185
+ @rels_xml = rels_entry.get_input_stream.read
186
+ @rels = Nokogiri::XML(@rels_xml)
187
+ end
188
+
179
189
  #--
180
190
  # TODO: Flesh this out to be compatible with other files
181
191
  # TODO: Method to set flag on files that have been edited, probably by inserting something at the
@@ -91,7 +91,7 @@ module Docx
91
91
  module ClassMethods
92
92
  def create_with(element)
93
93
  # Need to somehow get the xml document accessible here by default, but this is alright in the interim
94
- self.new(Nokogiri::XML::Node.new("w:#{self.tag}", element.node))
94
+ self.new(Nokogiri::XML::Node.new("w:#{self.tag}", element.node.document))
95
95
  end
96
96
 
97
97
  def create_within(element)
data/lib/docx/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Docx #:nodoc:
2
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
3
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2020-10-14 00:00:00.000000000 Z
15
+ date: 2022-03-13 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: nokogiri
@@ -20,20 +20,20 @@ dependencies:
20
20
  requirements:
21
21
  - - "~>"
22
22
  - !ruby/object:Gem::Version
23
- version: '1.10'
23
+ version: '1.13'
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.10.4
26
+ version: 1.13.0
27
27
  type: :runtime
28
28
  prerelease: false
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.10'
33
+ version: '1.13'
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 1.10.4
36
+ version: 1.13.0
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rubyzip
39
39
  requirement: !ruby/object:Gem::Requirement
@@ -49,19 +49,19 @@ dependencies:
49
49
  - !ruby/object:Gem::Version
50
50
  version: '2.0'
51
51
  - !ruby/object:Gem::Dependency
52
- name: coveralls
52
+ name: coveralls_reborn
53
53
  requirement: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - "~>"
56
56
  - !ruby/object:Gem::Version
57
- version: '0.8'
57
+ version: '0.21'
58
58
  type: :development
59
59
  prerelease: false
60
60
  version_requirements: !ruby/object:Gem::Requirement
61
61
  requirements:
62
62
  - - "~>"
63
63
  - !ruby/object:Gem::Version
64
- version: '0.8'
64
+ version: '0.21'
65
65
  - !ruby/object:Gem::Dependency
66
66
  name: rake
67
67
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +128,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 2.5.0
131
+ version: 2.6.0
132
132
  required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  requirements:
134
134
  - - ">="
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  requirements: []
138
- rubygems_version: 3.1.2
138
+ rubygems_version: 3.3.3
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: a ruby library/gem for interacting with .docx files