docx 0.2.03 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/LICENSE.md +21 -21
- data/README.md +97 -11
- data/lib/docx/containers.rb +4 -3
- data/lib/docx/containers/container.rb +5 -1
- data/lib/docx/containers/paragraph.rb +46 -3
- data/lib/docx/containers/table.rb +51 -0
- data/lib/docx/containers/table_cell.rb +39 -0
- data/lib/docx/containers/table_column.rb +29 -0
- data/lib/docx/containers/table_row.rb +28 -0
- data/lib/docx/containers/text_run.rb +47 -3
- data/lib/docx/core_ext/module.rb +171 -171
- data/lib/docx/document.rb +202 -87
- data/lib/docx/elements.rb +2 -2
- data/lib/docx/elements/bookmark.rb +9 -7
- data/lib/docx/elements/element.rb +34 -1
- data/lib/docx/elements/text.rb +16 -12
- data/lib/docx/version.rb +1 -1
- metadata +81 -56
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/docx/parser.rb +0 -46
- metadata.gz.sig +0 -3
data/lib/docx/elements.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require 'docx/elements/bookmark'
|
2
|
-
require 'docx/elements/element'
|
1
|
+
require 'docx/elements/bookmark'
|
2
|
+
require 'docx/elements/element'
|
3
3
|
require 'docx/elements/text'
|
@@ -5,8 +5,10 @@ module Docx
|
|
5
5
|
class Bookmark
|
6
6
|
include Element
|
7
7
|
attr_accessor :name
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
def self.tag
|
10
|
+
'bookmarkStart'
|
11
|
+
end
|
10
12
|
|
11
13
|
def initialize(node)
|
12
14
|
@node = node
|
@@ -15,14 +17,14 @@ module Docx
|
|
15
17
|
|
16
18
|
# Insert text before bookmarkStart node
|
17
19
|
def insert_text_before(text)
|
18
|
-
text_run =
|
19
|
-
text_run.text = "#{text}#{
|
20
|
+
text_run = get_run_before
|
21
|
+
text_run.text = "#{text_run.text}#{text}"
|
20
22
|
end
|
21
23
|
|
22
24
|
# Insert text after bookmarkStart node
|
23
25
|
def insert_text_after(text)
|
24
|
-
text_run =
|
25
|
-
text_run.text = "#{
|
26
|
+
text_run = get_run_after
|
27
|
+
text_run.text = "#{text}#{text_run.text}"
|
26
28
|
end
|
27
29
|
|
28
30
|
# insert multiple lines starting with paragraph containing bookmark node.
|
@@ -49,7 +51,7 @@ module Docx
|
|
49
51
|
|
50
52
|
# Get text run immediately prior to bookmark node
|
51
53
|
def get_run_before
|
52
|
-
# at_xpath returns the first match found and preceding-sibling returns siblings in the
|
54
|
+
# at_xpath returns the first match found and preceding-sibling returns siblings in the
|
53
55
|
# order they appear in the document not the order as they appear when moving out from
|
54
56
|
# the starting node
|
55
57
|
if not (r_nodes = @node.xpath("./preceding-sibling::w:r")).empty?
|
@@ -55,10 +55,43 @@ module Docx
|
|
55
55
|
self.class.new(@node.dup)
|
56
56
|
end
|
57
57
|
|
58
|
+
# A method to wrap content in an HTML tag.
|
59
|
+
# Currently used in paragraph and text_run for the to_html methods
|
60
|
+
#
|
61
|
+
# content:: The base text content for the tag.
|
62
|
+
# styles:: Hash of the inline CSS styles to be applied. e.g.
|
63
|
+
# { 'font-size' => '12pt', 'text-decoration' => 'underline' }
|
64
|
+
#
|
65
|
+
def html_tag(name, options = {})
|
66
|
+
content = options[:content]
|
67
|
+
styles = options[:styles]
|
68
|
+
attributes = options[:attributes]
|
69
|
+
|
70
|
+
html = "<#{name.to_s}"
|
71
|
+
|
72
|
+
unless styles.nil? || styles.empty?
|
73
|
+
styles_array = []
|
74
|
+
styles.each do |property, value|
|
75
|
+
styles_array << "#{property.to_s}:#{value};"
|
76
|
+
end
|
77
|
+
html << " style=\"#{styles_array.join('')}\""
|
78
|
+
end
|
79
|
+
|
80
|
+
unless attributes.nil? || attributes.empty?
|
81
|
+
attributes.each do |attr_name, attr_value|
|
82
|
+
html << " #{attr_name}=\"#{attr_value}\""
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
html << ">"
|
87
|
+
html << content if content
|
88
|
+
html << "</#{name.to_s}>"
|
89
|
+
end
|
90
|
+
|
58
91
|
module ClassMethods
|
59
92
|
def create_with(element)
|
60
93
|
# Need to somehow get the xml document accessible here by default, but this is alright in the interim
|
61
|
-
self.new(Nokogiri::XML::Node.new("w:#{self.
|
94
|
+
self.new(Nokogiri::XML::Node.new("w:#{self.tag}", element.node))
|
62
95
|
end
|
63
96
|
|
64
97
|
def create_within(element)
|
data/lib/docx/elements/text.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
-
module Docx
|
2
|
-
module Elements
|
3
|
-
class Text
|
4
|
-
include Element
|
5
|
-
delegate :content, :content=, :to => :@node
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Docx
|
2
|
+
module Elements
|
3
|
+
class Text
|
4
|
+
include Element
|
5
|
+
delegate :content, :content=, :to => :@node
|
6
|
+
|
7
|
+
def self.tag
|
8
|
+
't'
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def initialize(node)
|
13
|
+
@node = node
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
13
17
|
end
|
data/lib/docx/version.rb
CHANGED
metadata
CHANGED
@@ -1,117 +1,142 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Hunt
|
8
8
|
- Marcus Ortiz
|
9
|
-
|
9
|
+
- Higgins Dragon
|
10
|
+
- Toms Mikoss
|
11
|
+
- Sebastian Wittenkamp
|
12
|
+
autorequire:
|
10
13
|
bindir: bin
|
11
|
-
cert_chain:
|
12
|
-
-
|
13
|
-
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURjRENDQWxpZ0F3SUJB
|
14
|
-
Z0lCQVRBTkJna3Foa2lHOXcwQkFRVUZBREEvTVJFd0R3WURWUVFERE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
|
-
clVNQjBHQTFVZEVRUVdNQlNCRW1Ob2NtRm9kVzUwUUdkdFlXbHNMbU52CmJU
|
31
|
-
QWRCZ05WSFJJRUZqQVVnUkpqYUhKaGFIVnVkRUJuYldGcGJDNWpiMjB3RFFZ
|
32
|
-
SktvWklodmNOQVFFRkJRQUQKZ2dFQkFMSzl1V25oMWliTUZzT2c3WGlKWWdV
|
33
|
-
dlVoNUt4aWxhTHdMSHRHTDFHd3U2ZlRBMU9DSU5CeDJiMUxMbgpRNTdYQWhv
|
34
|
-
dkxlOGN3Qko0RnV6RXV1aUpMTlhlOU5FTDU2L1ZpbjloMTFlVktpOHA2YTEv
|
35
|
-
MC9XeTlsWEVVSHFBCkFUR0JMTHM0MXFXN2JuV3dSK09TZ2dySitJQkYzTGYr
|
36
|
-
VjNzSHhiNkxHV0h4ekNNMHpIdU5OWEJsQTdnRWQyNFAKQ0llcEtTdnlwQUdP
|
37
|
-
ckhRbGdpOTNPbkxEWjdKa3pCMk1wcDA0em5NSllQOGZIdGJCTkE2bFUxL2tK
|
38
|
-
NEV0UFhmRQpLdFE1SGpuNFVDNTg0R0pyR01haFVnbXhMbmZleXVZWXVZTlFF
|
39
|
-
M1VzVlVQYXl6eGtQVW9DOFZzT0orV1ZmMnMrCkcxL1VkNGRVYkVtOGdRL2J4
|
40
|
-
bDM1TzBBdkhCcz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
|
41
|
-
date: 2013-06-08 00:00:00.000000000 Z
|
14
|
+
cert_chain: []
|
15
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
42
16
|
dependencies:
|
43
17
|
- !ruby/object:Gem::Dependency
|
44
18
|
name: nokogiri
|
45
19
|
requirement: !ruby/object:Gem::Requirement
|
46
20
|
requirements:
|
47
|
-
- - ~>
|
21
|
+
- - "~>"
|
48
22
|
- !ruby/object:Gem::Version
|
49
|
-
version: '1.
|
23
|
+
version: '1.10'
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.10.4
|
50
27
|
type: :runtime
|
51
28
|
prerelease: false
|
52
29
|
version_requirements: !ruby/object:Gem::Requirement
|
53
30
|
requirements:
|
54
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
- - ">="
|
55
35
|
- !ruby/object:Gem::Version
|
56
|
-
version:
|
36
|
+
version: 1.10.4
|
57
37
|
- !ruby/object:Gem::Dependency
|
58
38
|
name: rubyzip
|
59
39
|
requirement: !ruby/object:Gem::Requirement
|
60
40
|
requirements:
|
61
|
-
- - ~>
|
41
|
+
- - "~>"
|
62
42
|
- !ruby/object:Gem::Version
|
63
|
-
version: '0
|
43
|
+
version: '2.0'
|
64
44
|
type: :runtime
|
65
45
|
prerelease: false
|
66
46
|
version_requirements: !ruby/object:Gem::Requirement
|
67
47
|
requirements:
|
68
|
-
- - ~>
|
48
|
+
- - "~>"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '2.0'
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: coveralls
|
53
|
+
requirement: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0.8'
|
58
|
+
type: :development
|
59
|
+
prerelease: false
|
60
|
+
version_requirements: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - "~>"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0.8'
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rake
|
67
|
+
requirement: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '13.0'
|
72
|
+
type: :development
|
73
|
+
prerelease: false
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - "~>"
|
69
77
|
- !ruby/object:Gem::Version
|
70
|
-
version: '0
|
71
|
-
|
78
|
+
version: '13.0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: rspec
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.7'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - "~>"
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '3.7'
|
93
|
+
description: thin wrapper around rubyzip and nokogiri as a way to get started with
|
94
|
+
docx files
|
72
95
|
email:
|
73
96
|
- chrahunt@gmail.com
|
74
97
|
executables: []
|
75
98
|
extensions: []
|
76
99
|
extra_rdoc_files: []
|
77
100
|
files:
|
78
|
-
- README.md
|
79
101
|
- LICENSE.md
|
102
|
+
- README.md
|
103
|
+
- lib/docx.rb
|
104
|
+
- lib/docx/containers.rb
|
80
105
|
- lib/docx/containers/container.rb
|
81
106
|
- lib/docx/containers/paragraph.rb
|
107
|
+
- lib/docx/containers/table.rb
|
108
|
+
- lib/docx/containers/table_cell.rb
|
109
|
+
- lib/docx/containers/table_column.rb
|
110
|
+
- lib/docx/containers/table_row.rb
|
82
111
|
- lib/docx/containers/text_run.rb
|
83
|
-
- lib/docx/containers.rb
|
84
112
|
- lib/docx/core_ext/module.rb
|
85
113
|
- lib/docx/document.rb
|
114
|
+
- lib/docx/elements.rb
|
86
115
|
- lib/docx/elements/bookmark.rb
|
87
116
|
- lib/docx/elements/element.rb
|
88
117
|
- lib/docx/elements/text.rb
|
89
|
-
- lib/docx/elements.rb
|
90
|
-
- lib/docx/parser.rb
|
91
118
|
- lib/docx/version.rb
|
92
|
-
- lib/docx.rb
|
93
119
|
homepage: https://github.com/chrahunt/docx
|
94
|
-
licenses:
|
120
|
+
licenses:
|
121
|
+
- MIT
|
95
122
|
metadata: {}
|
96
|
-
post_install_message:
|
123
|
+
post_install_message:
|
97
124
|
rdoc_options: []
|
98
125
|
require_paths:
|
99
126
|
- lib
|
100
127
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
128
|
requirements:
|
102
|
-
- -
|
129
|
+
- - ">="
|
103
130
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
131
|
+
version: 2.5.0
|
105
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
133
|
requirements:
|
107
|
-
- -
|
134
|
+
- - ">="
|
108
135
|
- !ruby/object:Gem::Version
|
109
136
|
version: '0'
|
110
137
|
requirements: []
|
111
|
-
|
112
|
-
|
113
|
-
signing_key:
|
138
|
+
rubygems_version: 3.1.2
|
139
|
+
signing_key:
|
114
140
|
specification_version: 4
|
115
141
|
summary: a ruby library/gem for interacting with .docx files
|
116
142
|
test_files: []
|
117
|
-
has_rdoc:
|
checksums.yaml.gz.sig
DELETED
Binary file
|
data.tar.gz.sig
DELETED
Binary file
|
data/lib/docx/parser.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'docx/containers'
|
2
|
-
require 'docx/elements'
|
3
|
-
require 'nokogiri'
|
4
|
-
require 'zip/zip'
|
5
|
-
|
6
|
-
module Docx
|
7
|
-
class Parser
|
8
|
-
attr_reader :xml, :doc, :zip
|
9
|
-
|
10
|
-
def initialize(path)
|
11
|
-
@zip = Zip::ZipFile.open(path)
|
12
|
-
@xml = @zip.read('word/document.xml')
|
13
|
-
@doc = Nokogiri::XML(@xml)
|
14
|
-
if block_given?
|
15
|
-
yield self
|
16
|
-
@zip.close
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def paragraphs
|
21
|
-
@doc.xpath('//w:document//w:body//w:p').map { |p_node| parse_paragraph_from p_node }
|
22
|
-
end
|
23
|
-
|
24
|
-
# Returns hash of bookmarks
|
25
|
-
def bookmarks
|
26
|
-
bkmrks_hsh = Hash.new
|
27
|
-
bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
|
28
|
-
# auto-generated by office 2010
|
29
|
-
bkmrks_ary.reject! {|b| b.name == "_GoBack" }
|
30
|
-
bkmrks_ary.each {|b| bkmrks_hsh[b.name] = b }
|
31
|
-
bkmrks_hsh
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
# generate Elements::Containers::Paragraph from paragraph XML node
|
37
|
-
def parse_paragraph_from(p_node)
|
38
|
-
Elements::Containers::Paragraph.new(p_node)
|
39
|
-
end
|
40
|
-
|
41
|
-
# generate Elements::Bookmark from bookmark XML node
|
42
|
-
def parse_bookmark_from(b_node)
|
43
|
-
Elements::Bookmark.new(b_node)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
metadata.gz.sig
DELETED