docx 0.6.2 → 0.8.0

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: 2a33dcd9e31c60144261a15670cd1c01b37877044aaf33f7091b46cc85ab3412
4
- data.tar.gz: 1911db027b3e2fbf8eb9363eaa99b95231f875afe8de2a0c060b38fee86c7238
3
+ metadata.gz: b777dc986b688750a284502115839f0c1fa58b50d562f1c098155e4386afe85b
4
+ data.tar.gz: 6de4259ddd408787823b60557535906aacc3357d54794941df3ef7e58105c713
5
5
  SHA512:
6
- metadata.gz: 0a823fedf1b0bfc542c88533c787aefa5e9cee12ae4422a01f106985a2f03e8b636aa70820a179f5501ba8f996672f8f75e24356282cd7d81505cbb1984fa967
7
- data.tar.gz: c06b4078536bd8b12b5c5e4f61fe0ef5f2f9e03e9cbc7c4017f2d01d3e8a0bc9200fa7457fac54c0b110a57ddc4bff8ad74c28d3e873512900266ac00137c6e6
6
+ metadata.gz: 8dd7ac5d3396372c3c5e21c1a8f73e6090854397874b5f69fe26f8e289c7417dd963fd1767434c8b6af920de96a1ea7a14d165e4ea9cb027f087f2b5c5248e02
7
+ data.tar.gz: '084b2be2ebf7072a472802ca449806063930723d244cda22ff6e73bcee24d9a6689d0daaf531bb2429372f639714ed8f8d18f0925c8800cbd8ff1bb313391610'
data/README.md CHANGED
@@ -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.5 or later
14
+ - Ruby 2.6 or later
15
15
 
16
16
  ### Install
17
17
 
@@ -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
@@ -70,6 +70,10 @@ module Docx
70
70
  bkmrks_hsh
71
71
  end
72
72
 
73
+ def to_xml
74
+ Nokogiri::XML(@document_xml)
75
+ end
76
+
73
77
  def tables
74
78
  @doc.xpath('//w:document//w:body//w:tbl').map { |t_node| parse_table_from t_node }
75
79
  end
@@ -14,13 +14,20 @@ module Docx
14
14
  end
15
15
 
16
16
  attr_accessor :node
17
- delegate :at_xpath, :xpath, :to => :@node
18
17
 
19
18
  # TODO: Should create a docx object from this
20
19
  def parent(type = '*')
21
20
  @node.at_xpath("./parent::#{type}")
22
21
  end
23
22
 
23
+ def at_xpath(*args)
24
+ @node.at_xpath(*args)
25
+ end
26
+
27
+ def xpath(*args)
28
+ @node.xpath(*args)
29
+ end
30
+
24
31
  # Get parent paragraph of element
25
32
  def parent_paragraph
26
33
  Elements::Containers::Paragraph.new(parent('w:p'))
@@ -91,7 +98,7 @@ module Docx
91
98
  module ClassMethods
92
99
  def create_with(element)
93
100
  # 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))
101
+ self.new(Nokogiri::XML::Node.new("w:#{self.tag}", element.node.document))
95
102
  end
96
103
 
97
104
  def create_within(element)
@@ -102,4 +109,4 @@ module Docx
102
109
  end
103
110
  end
104
111
  end
105
- end
112
+ end
@@ -2,16 +2,22 @@ module Docx
2
2
  module Elements
3
3
  class Text
4
4
  include Element
5
- delegate :content, :content=, :to => :@node
6
5
 
7
6
  def self.tag
8
7
  't'
9
8
  end
10
9
 
10
+ def content
11
+ @node.content
12
+ end
13
+
14
+ def content=(args)
15
+ @node.content = args
16
+ end
11
17
 
12
18
  def initialize(node)
13
19
  @node = node
14
20
  end
15
21
  end
16
22
  end
17
- end
23
+ end
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.6.2'
4
+ VERSION = '0.8.0'
5
5
  end
data/lib/docx.rb CHANGED
@@ -4,4 +4,3 @@ module Docx #:nodoc:
4
4
  autoload :Document, 'docx/document'
5
5
  end
6
6
 
7
- require 'docx/core_ext/module'
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.2
4
+ version: 0.8.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: 2021-07-21 00:00:00.000000000 Z
15
+ date: 2023-05-20 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
@@ -109,7 +109,6 @@ files:
109
109
  - lib/docx/containers/table_column.rb
110
110
  - lib/docx/containers/table_row.rb
111
111
  - lib/docx/containers/text_run.rb
112
- - lib/docx/core_ext/module.rb
113
112
  - lib/docx/document.rb
114
113
  - lib/docx/elements.rb
115
114
  - lib/docx/elements/bookmark.rb
@@ -128,14 +127,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
127
  requirements:
129
128
  - - ">="
130
129
  - !ruby/object:Gem::Version
131
- version: 2.5.0
130
+ version: 2.6.0
132
131
  required_rubygems_version: !ruby/object:Gem::Requirement
133
132
  requirements:
134
133
  - - ">="
135
134
  - !ruby/object:Gem::Version
136
135
  version: '0'
137
136
  requirements: []
138
- rubygems_version: 3.1.6
137
+ rubygems_version: 3.4.1
139
138
  signing_key:
140
139
  specification_version: 4
141
140
  summary: a ruby library/gem for interacting with .docx files
@@ -1,172 +0,0 @@
1
- unless Object.const_defined?("ActiveSupport")
2
- class Module
3
- # Provides a delegate class method to easily expose contained objects' public methods
4
- # as your own. Pass one or more methods (specified as symbols or strings)
5
- # and the name of the target object via the <tt>:to</tt> option (also a symbol
6
- # or string). At least one method and the <tt>:to</tt> option are required.
7
- #
8
- # Delegation is particularly useful with Active Record associations:
9
- #
10
- # class Greeter < ActiveRecord::Base
11
- # def hello
12
- # 'hello'
13
- # end
14
- #
15
- # def goodbye
16
- # 'goodbye'
17
- # end
18
- # end
19
- #
20
- # class Foo < ActiveRecord::Base
21
- # belongs_to :greeter
22
- # delegate :hello, to: :greeter
23
- # end
24
- #
25
- # Foo.new.hello # => "hello"
26
- # Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
27
- #
28
- # Multiple delegates to the same target are allowed:
29
- #
30
- # class Foo < ActiveRecord::Base
31
- # belongs_to :greeter
32
- # delegate :hello, :goodbye, to: :greeter
33
- # end
34
- #
35
- # Foo.new.goodbye # => "goodbye"
36
- #
37
- # Methods can be delegated to instance variables, class variables, or constants
38
- # by providing them as a symbols:
39
- #
40
- # class Foo
41
- # CONSTANT_ARRAY = [0,1,2,3]
42
- # @@class_array = [4,5,6,7]
43
- #
44
- # def initialize
45
- # @instance_array = [8,9,10,11]
46
- # end
47
- # delegate :sum, to: :CONSTANT_ARRAY
48
- # delegate :min, to: :@@class_array
49
- # delegate :max, to: :@instance_array
50
- # end
51
- #
52
- # Foo.new.sum # => 6
53
- # Foo.new.min # => 4
54
- # Foo.new.max # => 11
55
- #
56
- # It's also possible to delegate a method to the class by using +:class+:
57
- #
58
- # class Foo
59
- # def self.hello
60
- # "world"
61
- # end
62
- #
63
- # delegate :hello, to: :class
64
- # end
65
- #
66
- # Foo.new.hello # => "world"
67
- #
68
- # Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
69
- # is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
70
- # delegated to.
71
- #
72
- # Person = Struct.new(:name, :address)
73
- #
74
- # class Invoice < Struct.new(:client)
75
- # delegate :name, :address, to: :client, prefix: true
76
- # end
77
- #
78
- # john_doe = Person.new('John Doe', 'Vimmersvej 13')
79
- # invoice = Invoice.new(john_doe)
80
- # invoice.client_name # => "John Doe"
81
- # invoice.client_address # => "Vimmersvej 13"
82
- #
83
- # It is also possible to supply a custom prefix.
84
- #
85
- # class Invoice < Struct.new(:client)
86
- # delegate :name, :address, to: :client, prefix: :customer
87
- # end
88
- #
89
- # invoice = Invoice.new(john_doe)
90
- # invoice.customer_name # => 'John Doe'
91
- # invoice.customer_address # => 'Vimmersvej 13'
92
- #
93
- # If the delegate object is +nil+ an exception is raised, and that happens
94
- # no matter whether +nil+ responds to the delegated method. You can get a
95
- # +nil+ instead with the +:allow_nil+ option.
96
- #
97
- # class Foo
98
- # attr_accessor :bar
99
- # def initialize(bar = nil)
100
- # @bar = bar
101
- # end
102
- # delegate :zoo, to: :bar
103
- # end
104
- #
105
- # Foo.new.zoo # raises NoMethodError exception (you called nil.zoo)
106
- #
107
- # class Foo
108
- # attr_accessor :bar
109
- # def initialize(bar = nil)
110
- # @bar = bar
111
- # end
112
- # delegate :zoo, to: :bar, allow_nil: true
113
- # end
114
- #
115
- # Foo.new.zoo # returns nil
116
- def delegate(*methods)
117
- options = methods.pop
118
- unless options.is_a?(Hash) && to = options[:to]
119
- raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
120
- end
121
-
122
- prefix, allow_nil = options.values_at(:prefix, :allow_nil)
123
-
124
- if prefix == true && to =~ /^[^a-z_]/
125
- raise ArgumentError, 'Can only automatically set the delegation prefix when delegating to a method.'
126
- end
127
-
128
- method_prefix = \
129
- if prefix
130
- "#{prefix == true ? to : prefix}_"
131
- else
132
- ''
133
- end
134
-
135
- file, line = caller.first.split(':', 2)
136
- line = line.to_i
137
-
138
- to = to.to_s
139
- to = 'self.class' if to == 'class'
140
-
141
- methods.each do |method|
142
- # Attribute writer methods only accept one argument. Makes sure []=
143
- # methods still accept two arguments.
144
- definition = (method =~ /[^\]]=$/) ? 'arg' : '*args, &block'
145
-
146
- if allow_nil
147
- module_eval(<<-EOS, file, line - 2)
148
- def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block)
149
- if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name)
150
- #{to}.#{method}(#{definition}) # client.name(*args, &block)
151
- end # end
152
- end # end
153
- EOS
154
- else
155
- exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
156
-
157
- module_eval(<<-EOS, file, line - 1)
158
- def #{method_prefix}#{method}(#{definition}) # def customer_name(*args, &block)
159
- #{to}.#{method}(#{definition}) # client.name(*args, &block)
160
- rescue NoMethodError # rescue NoMethodError
161
- if #{to}.nil? # if client.nil?
162
- #{exception} # # add helpful message to the exception
163
- else # else
164
- raise # raise
165
- end # end
166
- end # end
167
- EOS
168
- end
169
- end
170
- end
171
- end
172
- end