docx 0.3.0 → 0.4.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
- SHA1:
3
- metadata.gz: 436e2d77b50265c75cd9be63a2957a5f71da61e5
4
- data.tar.gz: 6b14b8f1a8ec88a2f7286777f4238da6c95748e0
2
+ SHA256:
3
+ metadata.gz: 9b2cc086dcc2bb5043cc9f20b311f1e4b02f7c9730f459c8b91e48097d8bacb5
4
+ data.tar.gz: 8536619dd3cebe92112b7328cf64cdca40be898411d974037d5e9ffc02af1a08
5
5
  SHA512:
6
- metadata.gz: aa87a1a97fab2a49e023bae4dfe1000477ed20f7cf09df9d5f63f46caf8a8e238533f2df680684f0da3530b1031a827f7d4c54fa2520a72c153214db547b0e26
7
- data.tar.gz: 856503643daa3f057cd4625fc8f63660748e476f71712005a841721bf392d9322430dd6d5988a3c6549ef1d7668c12de49faeacb032ffe19b2e47a5d2504292b
6
+ metadata.gz: 77a0ee6f32422ad64e916b98d52d685958867e574582252d4ce424fac721cee4f73b400f8960ad88c762608024bfa7a5d966bea0cd65e1daecdb4d58967da307
7
+ data.tar.gz: e87937fb28f2c6f7d7777b63df9b483fde5670e98261253b91ed55fee4d737f72cc2596542ced45a80ba525a18b9ca7a1760f76440ad71ed30facdfe17fd0cc1
data/README.md CHANGED
@@ -4,11 +4,29 @@ A ruby library/gem for interacting with `.docx` files. currently capabilities in
4
4
 
5
5
  ## Usage
6
6
 
7
+ ### Prerequisites
8
+
9
+ - Ruby 2.4 or later
10
+
7
11
  ### Install
8
12
 
9
- Requires ruby (tested with 2.1.1)
13
+ Add the following line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'docx'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```shell
22
+ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
10
26
 
11
- gem 'docx', '~> 0.2.07', :require => ["docx"]
27
+ ```shell
28
+ gem install docx
29
+ ```
12
30
 
13
31
  ### Reading
14
32
 
@@ -61,7 +79,7 @@ doc.tables.each do |table|
61
79
  puts cell.text
62
80
  end
63
81
  end
64
-
82
+
65
83
  table.columns.each do |column| # Column-based iteration
66
84
  column.cells.each do |cell|
67
85
  puts cell.text
@@ -89,6 +107,13 @@ doc.paragraphs.each do |p|
89
107
  p.remove! if p.to_s =~ /TODO/
90
108
  end
91
109
 
110
+ # Substitute text, preserving formatting
111
+ doc.paragraphs.each do |p|
112
+ p.each_text_run do |tr|
113
+ tr.substitute('_placeholder_', 'replacement value')
114
+ end
115
+ end
116
+
92
117
  # Save document to specified path
93
118
  doc.save('example-edited.docx')
94
119
  ```
@@ -45,6 +45,13 @@ module Docx
45
45
  @text_nodes.map(&:content).join('')
46
46
  end
47
47
 
48
+ # Substitute text in text @text_nodes
49
+ def substitute(match, replacement)
50
+ @text_nodes.each do |text_node|
51
+ text_node.content = text_node.content.gsub(match, replacement)
52
+ end
53
+ end
54
+
48
55
  def parse_formatting
49
56
  {
50
57
  italic: !@node.xpath('.//w:i').empty?,
@@ -19,7 +19,7 @@ module Docx
19
19
  # end
20
20
  class Document
21
21
  attr_reader :xml, :doc, :zip, :styles
22
-
22
+
23
23
  def initialize(path, &block)
24
24
  @replace = {}
25
25
  @zip = Zip::File.open(path)
@@ -51,7 +51,7 @@ module Docx
51
51
  end
52
52
 
53
53
  def paragraphs
54
- @doc.xpath('//w:document//w:body//w:p').map { |p_node| parse_paragraph_from p_node }
54
+ @doc.xpath('//w:document//w:body/w:p').map { |p_node| parse_paragraph_from p_node }
55
55
  end
56
56
 
57
57
  def bookmarks
@@ -102,6 +102,7 @@ module Docx
102
102
  update
103
103
  Zip::OutputStream.open(path) do |out|
104
104
  zip.each do |entry|
105
+ next unless entry.file?
105
106
  out.put_next_entry(entry.name)
106
107
 
107
108
  if @replace[entry.name]
@@ -1,3 +1,3 @@
1
1
  module Docx #:nodoc:
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  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.3.0
4
+ version: 0.4.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: 2018-04-02 00:00:00.000000000 Z
15
+ date: 2020-02-09 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: nokogiri
@@ -20,40 +20,34 @@ dependencies:
20
20
  requirements:
21
21
  - - "~>"
22
22
  - !ruby/object:Gem::Version
23
- version: '1.8'
23
+ version: '1.10'
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.8.1
26
+ version: 1.10.4
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.8'
33
+ version: '1.10'
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 1.8.1
36
+ version: 1.10.4
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rubyzip
39
39
  requirement: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - "~>"
42
42
  - !ruby/object:Gem::Version
43
- version: '1.2'
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 1.2.1
43
+ version: '2.0'
47
44
  type: :runtime
48
45
  prerelease: false
49
46
  version_requirements: !ruby/object:Gem::Requirement
50
47
  requirements:
51
48
  - - "~>"
52
49
  - !ruby/object:Gem::Version
53
- version: '1.2'
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 1.2.1
50
+ version: '2.0'
57
51
  - !ruby/object:Gem::Dependency
58
52
  name: rspec
59
53
  requirement: !ruby/object:Gem::Requirement
@@ -74,14 +68,14 @@ dependencies:
74
68
  requirements:
75
69
  - - "~>"
76
70
  - !ruby/object:Gem::Version
77
- version: '12.3'
71
+ version: '13.0'
78
72
  type: :development
79
73
  prerelease: false
80
74
  version_requirements: !ruby/object:Gem::Requirement
81
75
  requirements:
82
76
  - - "~>"
83
77
  - !ruby/object:Gem::Version
84
- version: '12.3'
78
+ version: '13.0'
85
79
  description: thin wrapper around rubyzip and nokogiri as a way to get started with
86
80
  docx files
87
81
  email:
@@ -120,15 +114,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
114
  requirements:
121
115
  - - ">="
122
116
  - !ruby/object:Gem::Version
123
- version: '0'
117
+ version: 2.4.0
124
118
  required_rubygems_version: !ruby/object:Gem::Requirement
125
119
  requirements:
126
120
  - - ">="
127
121
  - !ruby/object:Gem::Version
128
122
  version: '0'
129
123
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.6.14
124
+ rubygems_version: 3.1.2
132
125
  signing_key:
133
126
  specification_version: 4
134
127
  summary: a ruby library/gem for interacting with .docx files