Html2Docx 0.2.1 → 0.3.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/lib/Html2Docx.rb +6 -3
- data/lib/Html2Docx/document.rb +2 -1
- data/lib/Html2Docx/document_objects/paragraph.rb +45 -1
- data/lib/Html2Docx/relation.rb +38 -4
- data/lib/Html2Docx/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 297e2d1451a78a7707228c4335f6e7efff04cb36
|
4
|
+
data.tar.gz: d4b43692a4402c4a157598101f946b599aaa85b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e84ec6e1b667c9791252a2aa73af2cef8cbb7a452fcdefd0761024c57f2d28bac5a13f2e7330f45c2f752a483491ab172098d836a9e0c0070fa962293e9ab19
|
7
|
+
data.tar.gz: a42213c8d94065bff115771d9b2ab29e82d1a73f2a6db5fd3939081c3bc3dea88ce95502b314a47505567590eea2ba0c9f672514d6678a62357359fe779efee9
|
data/lib/Html2Docx.rb
CHANGED
@@ -32,19 +32,22 @@ module Html2Docx
|
|
32
32
|
def self.render(options = {})
|
33
33
|
initialization = Initialization.new(options)
|
34
34
|
options[:temp] = initialization.get_temp_directory
|
35
|
+
options[:initialization] = initialization
|
35
36
|
|
36
37
|
content_types = ContentTypes.new(options)
|
38
|
+
options[:content_types] = content_types
|
37
39
|
|
38
40
|
options[:main_relation] = true
|
39
|
-
|
40
|
-
options[:main_relation] =
|
41
|
+
main_relation = Relation.new(options)
|
42
|
+
options[:main_relation] = main_relation
|
41
43
|
|
42
44
|
document = Document.new(options)
|
45
|
+
options[:document] = document
|
43
46
|
|
44
47
|
# Render
|
45
48
|
document.render
|
46
49
|
content_types.render
|
47
|
-
|
50
|
+
main_relation.render
|
48
51
|
|
49
52
|
# Create Docx File
|
50
53
|
self.create_docx(options.fetch(:output), options.fetch(:temp))
|
data/lib/Html2Docx/document.rb
CHANGED
@@ -5,6 +5,7 @@ module Html2Docx
|
|
5
5
|
@document = File.open(@document_file) { |f| Nokogiri::XML(f) }
|
6
6
|
@body = @document.at_xpath('//w:body')
|
7
7
|
@contents = []
|
8
|
+
@relation = options[:main_relation]
|
8
9
|
|
9
10
|
initial_body
|
10
11
|
add_html(options[:html])
|
@@ -25,7 +26,7 @@ module Html2Docx
|
|
25
26
|
case element.name
|
26
27
|
when 'p'
|
27
28
|
# Add paragraph
|
28
|
-
paragraph = DocumentObjects::Paragraph.new(@document,
|
29
|
+
paragraph = DocumentObjects::Paragraph.new(@document, @relation)
|
29
30
|
paragraph.add_paragraph(element)
|
30
31
|
@contents.push paragraph.render
|
31
32
|
when /h[1-9]/
|
@@ -14,8 +14,22 @@ module Html2Docx
|
|
14
14
|
def create_paragraph(paragraph_object)
|
15
15
|
@paragraph = Nokogiri::XML::Node.new('w:p', @document)
|
16
16
|
|
17
|
+
paragraph_id = paragraph_object.attr('id')
|
18
|
+
|
17
19
|
add_paragraph_style paragraph_object
|
20
|
+
add_bookmark_start_tag(paragraph_id) if paragraph_id
|
18
21
|
add_paragraph_child paragraph_object.children
|
22
|
+
add_bookmark_end_tag(paragraph_id) if paragraph_id
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_bookmark_start_tag(name)
|
26
|
+
bookmark_start_tag = @relation.create_internal_link_start_tag(name, @document)
|
27
|
+
@paragraph.add_child(bookmark_start_tag)
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_bookmark_end_tag(name)
|
31
|
+
bookmark_end_tag = @relation.create_internal_link_end_tag(name, @document)
|
32
|
+
@paragraph.add_child(bookmark_end_tag)
|
19
33
|
end
|
20
34
|
|
21
35
|
def add_paragraph_style(paragraph_object)
|
@@ -100,7 +114,6 @@ module Html2Docx
|
|
100
114
|
children.each do |child|
|
101
115
|
text_field = create_text_field
|
102
116
|
text_style = create_text_style
|
103
|
-
|
104
117
|
case child.name
|
105
118
|
when 'strong'
|
106
119
|
text_field.add_child add_strong_text(text_style)
|
@@ -113,9 +126,22 @@ module Html2Docx
|
|
113
126
|
text_field.add_child add_underline_text(text_style)
|
114
127
|
when 's'
|
115
128
|
text_field.add_child add_stroke_text(text_style)
|
129
|
+
when 'a'
|
130
|
+
href = child.attr('href')
|
131
|
+
hyperlink_tag = create_hyperlink_tag(href)
|
132
|
+
text_field.add_child(add_link_class(text_style))
|
133
|
+
hyperlink_tag.add_child(text_field)
|
134
|
+
text_field.add_child add_paragraph_text(child.text)
|
135
|
+
hyperlink_tag.add_child text_field
|
136
|
+
@paragraph.add_child hyperlink_tag
|
137
|
+
next
|
116
138
|
end
|
117
139
|
|
140
|
+
paragraph_id = child.attr('id')
|
141
|
+
|
142
|
+
add_bookmark_start_tag(paragraph_id) if paragraph_id
|
118
143
|
text_field.add_child add_paragraph_text(child.text)
|
144
|
+
add_bookmark_end_tag(paragraph_id) if paragraph_id
|
119
145
|
@paragraph.add_child text_field
|
120
146
|
end
|
121
147
|
end
|
@@ -128,6 +154,16 @@ module Html2Docx
|
|
128
154
|
Nokogiri::XML::Node.new('w:rPr', @document)
|
129
155
|
end
|
130
156
|
|
157
|
+
def create_hyperlink_tag(destination)
|
158
|
+
hyperlink_tag = Nokogiri::XML::Node.new('w:hyperlink', @document)
|
159
|
+
|
160
|
+
if destination.start_with?('#')
|
161
|
+
hyperlink_tag['w:anchor'] = destination.delete('#')
|
162
|
+
end
|
163
|
+
|
164
|
+
hyperlink_tag
|
165
|
+
end
|
166
|
+
|
131
167
|
def add_paragraph_text(value)
|
132
168
|
plain_text = Nokogiri::XML::Node.new('w:t', @document)
|
133
169
|
plain_text['xml:space'] = 'preserve'
|
@@ -136,6 +172,14 @@ module Html2Docx
|
|
136
172
|
plain_text
|
137
173
|
end
|
138
174
|
|
175
|
+
def add_link_class(text_style)
|
176
|
+
r_style_tag = Nokogiri::XML::Node.new('w:rStyle', @document)
|
177
|
+
r_style_tag['w:val'] = 'Hyperlink'
|
178
|
+
text_style.add_child(r_style_tag)
|
179
|
+
|
180
|
+
text_style
|
181
|
+
end
|
182
|
+
|
139
183
|
def add_strong_text(text_style)
|
140
184
|
strong_text = Nokogiri::XML::Node.new('w:b', @document)
|
141
185
|
text_style.add_child(strong_text)
|
data/lib/Html2Docx/relation.rb
CHANGED
@@ -5,6 +5,7 @@ module Html2Docx
|
|
5
5
|
@relation = nil
|
6
6
|
@relations = []
|
7
7
|
@last_relation_id = 1
|
8
|
+
@internal_links = {}
|
8
9
|
|
9
10
|
if options[:main_relation]
|
10
11
|
@relation_file = File.join(options.fetch(:temp), 'word', '_rels', 'document2.xml.rels')
|
@@ -36,14 +37,47 @@ module Html2Docx
|
|
36
37
|
@last_relation_id = @last_relation_id + 1
|
37
38
|
end
|
38
39
|
|
39
|
-
def render
|
40
|
-
File.open(@relation_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@relation)) }
|
41
|
-
end
|
42
|
-
|
43
40
|
def get_type(type)
|
44
41
|
end
|
45
42
|
|
46
43
|
def get_target(target)
|
47
44
|
end
|
45
|
+
|
46
|
+
def create_internal_link_start_tag(name, document)
|
47
|
+
bookmark_start_tag = Nokogiri::XML::Node.new('w:bookmarkStart', document)
|
48
|
+
bookmark_start_tag['w:id'] = create_internal_link_id(name)
|
49
|
+
bookmark_start_tag['w:name'] = name
|
50
|
+
|
51
|
+
bookmark_start_tag
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_internal_link_end_tag(name, document)
|
55
|
+
bookmark_end_tag = Nokogiri::XML::Node.new('w:bookmarkEnd', document)
|
56
|
+
bookmark_end_tag['w:id'] = find_internal_link_id(name)
|
57
|
+
|
58
|
+
bookmark_end_tag
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_internal_link_id(name)
|
62
|
+
id = find_internal_link_id(name)
|
63
|
+
if id
|
64
|
+
id = get_latest_internal_link_id + 1
|
65
|
+
@internal_links[id] = name
|
66
|
+
else
|
67
|
+
id
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_latest_internal_link_id
|
72
|
+
@internal_links.keys.max || 0
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_internal_link_id(name)
|
76
|
+
@internal_links.find{ |key, value| value == name }
|
77
|
+
end
|
78
|
+
|
79
|
+
def render
|
80
|
+
File.open(@relation_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@relation)) }
|
81
|
+
end
|
48
82
|
end
|
49
83
|
end
|
data/lib/Html2Docx/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Html2Docx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MuhammetDilmac
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|