docx 0.5.0 → 0.6.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 +4 -4
- data/README.md +1 -0
- data/lib/docx/containers/paragraph.rb +1 -1
- data/lib/docx/containers/text_run.rb +15 -0
- data/lib/docx/document.rb +16 -2
- data/lib/docx/elements/element.rb +9 -0
- data/lib/docx/version.rb +1 -1
- metadata +23 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dd59a023e1430fce917809931fe955651324db4fadb7a55d9a380731240466e
|
4
|
+
data.tar.gz: eb91759397bdc0782691c6ed46b37a67a031ab580d590568a7f221a223d2f80c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c722c427338ceb1f4d8c926c4e3b2a8c2b2de9889e77a9b78fec4ffe2270b633dfd8f843a31ed17a77b6b5d09d9e9d8b4c88e52569b3c410d1f8ae9183c76c6d
|
7
|
+
data.tar.gz: 41af211af034071991a920f1ed00e9ee9c8e5e739f41553c8b263081e7626183bc21e38735bfed88f01fdea0599f13a313cfc0ea1f9cc8331edff97e5cd5c40d
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/docx)
|
4
4
|
[](https://travis-ci.org/ruby-docx/docx)
|
5
|
+
[](https://coveralls.io/github/ruby-docx/docx?branch=master)
|
5
6
|
[](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
6
7
|
|
7
8
|
A ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading tables/rows/columns/cells and saving the document.
|
@@ -55,7 +55,7 @@ module Docx
|
|
55
55
|
|
56
56
|
# Array of text runs contained within paragraph
|
57
57
|
def text_runs
|
58
|
-
@node.xpath('w:r|w:hyperlink
|
58
|
+
@node.xpath('w:r|w:hyperlink').map { |r_node| Containers::TextRun.new(r_node, @document_properties) }
|
59
59
|
end
|
60
60
|
|
61
61
|
# Iterate over each text run within a paragraph
|
@@ -23,6 +23,8 @@ module Docx
|
|
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) }
|
26
|
+
@text_nodes = @node.xpath('w:t|w:r/w:t').map {|t_node| Elements::Text.new(t_node) }
|
27
|
+
|
26
28
|
@properties_tag = 'rPr'
|
27
29
|
@text = parse_text || ''
|
28
30
|
@formatting = parse_formatting || DEFAULT_FORMATTING
|
@@ -74,6 +76,7 @@ module Docx
|
|
74
76
|
# No need to be granular with font size down to the span level if it doesn't vary.
|
75
77
|
styles['font-size'] = "#{font_size}pt" if font_size != @font_size
|
76
78
|
html = html_tag(:span, content: html, styles: styles) unless styles.empty?
|
79
|
+
html = html_tag(:a, content: html, attributes: {href: href, target: "_blank"}) if hyperlink?
|
77
80
|
return html
|
78
81
|
end
|
79
82
|
|
@@ -89,6 +92,18 @@ module Docx
|
|
89
92
|
@formatting[:underline]
|
90
93
|
end
|
91
94
|
|
95
|
+
def hyperlink?
|
96
|
+
@node.name == 'hyperlink'
|
97
|
+
end
|
98
|
+
|
99
|
+
def href
|
100
|
+
@document_properties[:hyperlinks][hyperlink_id]
|
101
|
+
end
|
102
|
+
|
103
|
+
def hyperlink_id
|
104
|
+
@node.attributes['id'].value
|
105
|
+
end
|
106
|
+
|
92
107
|
def font_size
|
93
108
|
size_tag = @node.xpath('w:rPr//w:sz').first
|
94
109
|
size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
|
data/lib/docx/document.rb
CHANGED
@@ -45,7 +45,8 @@ module Docx
|
|
45
45
|
# This stores the current global document properties, for now
|
46
46
|
def document_properties
|
47
47
|
{
|
48
|
-
font_size: font_size
|
48
|
+
font_size: font_size,
|
49
|
+
hyperlinks: hyperlinks
|
49
50
|
}
|
50
51
|
end
|
51
52
|
|
@@ -83,6 +84,17 @@ module Docx
|
|
83
84
|
size_tag ? size_tag.attributes['val'].value.to_i / 2 : nil
|
84
85
|
end
|
85
86
|
|
87
|
+
# Hyperlink targets are extracted from the document.xml.rels file
|
88
|
+
def hyperlinks
|
89
|
+
hyperlink_relationships.each_with_object({}) do |rel, hash|
|
90
|
+
hash[rel.attributes['Id'].value] = rel.attributes['Target'].value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def hyperlink_relationships
|
95
|
+
@rels.xpath("//xmlns:Relationship[contains(@Type,'hyperlink')]")
|
96
|
+
end
|
97
|
+
|
86
98
|
##
|
87
99
|
# *Deprecated*
|
88
100
|
#
|
@@ -101,7 +113,7 @@ module Docx
|
|
101
113
|
|
102
114
|
# Output entire document as a String HTML fragment
|
103
115
|
def to_html
|
104
|
-
paragraphs.map(&:to_html).join(
|
116
|
+
paragraphs.map(&:to_html).join("\n")
|
105
117
|
end
|
106
118
|
|
107
119
|
# Save document to provided path
|
@@ -157,6 +169,8 @@ module Docx
|
|
157
169
|
def load_styles
|
158
170
|
@styles_xml = @zip.read('word/styles.xml')
|
159
171
|
@styles = Nokogiri::XML(@styles_xml)
|
172
|
+
@rels_xml = @zip.read('word/_rels/document.xml.rels')
|
173
|
+
@rels = Nokogiri::XML(@rels_xml)
|
160
174
|
rescue Errno::ENOENT => e
|
161
175
|
warn e.message
|
162
176
|
nil
|
@@ -65,8 +65,10 @@ module Docx
|
|
65
65
|
def html_tag(name, options = {})
|
66
66
|
content = options[:content]
|
67
67
|
styles = options[:styles]
|
68
|
+
attributes = options[:attributes]
|
68
69
|
|
69
70
|
html = "<#{name.to_s}"
|
71
|
+
|
70
72
|
unless styles.nil? || styles.empty?
|
71
73
|
styles_array = []
|
72
74
|
styles.each do |property, value|
|
@@ -74,6 +76,13 @@ module Docx
|
|
74
76
|
end
|
75
77
|
html << " style=\"#{styles_array.join('')}\""
|
76
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
|
+
|
77
86
|
html << ">"
|
78
87
|
html << content if content
|
79
88
|
html << "</#{name.to_s}>"
|
data/lib/docx/version.rb
CHANGED
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.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Hunt
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
- Higgins Dragon
|
10
10
|
- Toms Mikoss
|
11
11
|
- Sebastian Wittenkamp
|
12
|
-
autorequire:
|
12
|
+
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2020-
|
15
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: nokogiri
|
@@ -49,19 +49,19 @@ dependencies:
|
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '2.0'
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
name:
|
52
|
+
name: coveralls
|
53
53
|
requirement: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
55
|
- - "~>"
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version: '
|
57
|
+
version: '0.8'
|
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: '
|
64
|
+
version: '0.8'
|
65
65
|
- !ruby/object:Gem::Dependency
|
66
66
|
name: rake
|
67
67
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,6 +76,20 @@ dependencies:
|
|
76
76
|
- - "~>"
|
77
77
|
- !ruby/object:Gem::Version
|
78
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'
|
79
93
|
description: thin wrapper around rubyzip and nokogiri as a way to get started with
|
80
94
|
docx files
|
81
95
|
email:
|
@@ -106,7 +120,7 @@ homepage: https://github.com/chrahunt/docx
|
|
106
120
|
licenses:
|
107
121
|
- MIT
|
108
122
|
metadata: {}
|
109
|
-
post_install_message:
|
123
|
+
post_install_message:
|
110
124
|
rdoc_options: []
|
111
125
|
require_paths:
|
112
126
|
- lib
|
@@ -114,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
128
|
requirements:
|
115
129
|
- - ">="
|
116
130
|
- !ruby/object:Gem::Version
|
117
|
-
version: 2.
|
131
|
+
version: 2.5.0
|
118
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
133
|
requirements:
|
120
134
|
- - ">="
|
@@ -122,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
136
|
version: '0'
|
123
137
|
requirements: []
|
124
138
|
rubygems_version: 3.1.2
|
125
|
-
signing_key:
|
139
|
+
signing_key:
|
126
140
|
specification_version: 4
|
127
141
|
summary: a ruby library/gem for interacting with .docx files
|
128
142
|
test_files: []
|