docx-builder 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/Gemfile.lock +1 -1
- data/lib/docx/builder/decoder.rb +10 -10
- data/lib/docx/builder/encoder.rb +14 -14
- data/lib/docx/builder/template.rb +47 -47
- data/lib/docx/builder/version.rb +1 -1
- data/lib/docx/builder/xml_processor.rb +2 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a834e7eb99b4459de40d6942008c8487626ff25ccf75d19a2c4d5af4978402b
|
4
|
+
data.tar.gz: 1fa0be4e4a3c0d9c7e15804d90b1dbcedc9ca3b42f059593daeef50f08ca5b47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b80c298d29268dc7b5aae2ddc90533be3668fc8fa34c2f0da64451c47a58a44e9eee63b269092cb4af1e86b7ecdcba0ee63d23498f7c24935fcfc524019c5abd
|
7
|
+
data.tar.gz: 8f85311bcfd8a02207a2f673d0b81be605b49ede108508b9a3da514ce3a725c0085f9dbbbefdcf567a8abb46ee614e974e5d5ac0eda7f4456315684c0c60edd9
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/docx/builder/decoder.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Docx
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
module Builder
|
5
|
+
module Decoder
|
6
|
+
def self.to_xml(path)
|
7
|
+
Zip::File.open(path) do |file|
|
8
|
+
@zipfile = file
|
9
|
+
end
|
10
|
+
@zipfile
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
14
|
end
|
data/lib/docx/builder/encoder.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Docx
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
4
|
+
module Builder
|
5
|
+
module Encoder
|
6
|
+
def self.build_new_document_xml(original_document, new_document_sections)
|
7
|
+
output = Zip::OutputStream.write_buffer(StringIO.new) do |out|
|
8
|
+
original_document.each do |entry|
|
9
|
+
entry_name = entry.name
|
10
|
+
out.put_next_entry(entry_name)
|
11
|
+
out.write(new_document_sections[entry_name] || entry.get_input_stream.read)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
output.string
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
18
|
end
|
@@ -1,50 +1,50 @@
|
|
1
1
|
module Docx
|
2
2
|
module Builder
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
3
|
+
class Template
|
4
|
+
def initialize(path)
|
5
|
+
@document = Docx::Builder::Decoder.to_xml(path)
|
6
|
+
@sections = ["word/document.xml"]
|
7
|
+
@document.each do |file|
|
8
|
+
if file.name.include? "word/header"
|
9
|
+
@sections.push(file.name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(data)
|
15
|
+
@sections.map do |section|
|
16
|
+
cleaned_document = clean(@document.read(section))
|
17
|
+
erb_template = build_erb_template(cleaned_document)
|
18
|
+
processed_document = render_erb_template(erb_template, data)
|
19
|
+
[section, processed_document]
|
20
|
+
end.to_h
|
21
|
+
end
|
22
|
+
|
23
|
+
def clean(document)
|
24
|
+
document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
|
25
|
+
Docx::Builder::XmlProcessor.clean_variables(document)
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_erb_template(document)
|
29
|
+
ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>')
|
30
|
+
ret = ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>')
|
31
|
+
ret
|
32
|
+
end
|
33
|
+
|
34
|
+
def render_erb_template(document, data)
|
35
|
+
erb_template = ERB.new(document)
|
36
|
+
ret = erb_template.result_with_hash(data)
|
37
|
+
ret
|
38
|
+
end
|
39
|
+
|
40
|
+
# creates xml template keys in input docx (template file)
|
41
|
+
def save(path, new_document)
|
42
|
+
File.open(path, 'wb') do |f|
|
43
|
+
new_document_xml_string = Docx::Builder::Encoder.build_new_document_xml(@document, new_document)
|
44
|
+
f.write(new_document_xml_string)
|
45
|
+
end
|
46
|
+
@document.close
|
47
|
+
end
|
48
|
+
end
|
49
49
|
end
|
50
|
-
end
|
50
|
+
end
|
data/lib/docx/builder/version.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Docx
|
2
2
|
module Builder
|
3
3
|
class XmlProcessor
|
4
|
-
BROKEN_VARIABLE_PATTERN = "<w:t>([^\#^<^>]*?)#([^\#^<^>]*?)<\/w:t>"
|
5
|
-
|
6
4
|
def self.clean_variables(document)
|
7
5
|
document.gsub(/\{\{.*?\}\}/m) do |match|
|
8
6
|
ret = match.include?('>') ? join_variable_tags(match) : match
|
@@ -12,8 +10,8 @@ module Docx
|
|
12
10
|
end
|
13
11
|
|
14
12
|
def self.join_variable_tags(match)
|
15
|
-
before = match.gsub(
|
16
|
-
inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
|
13
|
+
before = match.gsub(/<(.*)/m, '')
|
14
|
+
inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
|
17
15
|
after = match.gsub(/.*>([^>]+)$/m, "\\1")
|
18
16
|
[before, inside, after].join(' ')
|
19
17
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tecnologia Cashme
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Builds a new docx file using ERB template
|
14
14
|
email:
|