docx-builder 0.2.0 → 0.2.1

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: 193655c8d43727fc462cdafe92e546f2c3e2ae974e57ed7d5b846499bf03e4bb
4
- data.tar.gz: b8981a3a583f8bcfb81258d8cd233c4cc30a685682fd2a4d729cdb027e97c63e
3
+ metadata.gz: 4a834e7eb99b4459de40d6942008c8487626ff25ccf75d19a2c4d5af4978402b
4
+ data.tar.gz: 1fa0be4e4a3c0d9c7e15804d90b1dbcedc9ca3b42f059593daeef50f08ca5b47
5
5
  SHA512:
6
- metadata.gz: b9eda257bc7525ba96125c994b7b52ea63ba48469d56d2deef80fd46405274b5ebd48cbe47431ab5c0bdac131aee31d18e3027b200eac0ddad8a1978a34cebce
7
- data.tar.gz: 8920365c82374b20cc2f411a49964731d53edb0042783713103a666c72324d5cf58ff9b4bb42bcf5d107f53e1ca10ed48c69f1f3c887a779da0820a31233cf13
6
+ metadata.gz: b80c298d29268dc7b5aae2ddc90533be3668fc8fa34c2f0da64451c47a58a44e9eee63b269092cb4af1e86b7ecdcba0ee63d23498f7c24935fcfc524019c5abd
7
+ data.tar.gz: 8f85311bcfd8a02207a2f673d0b81be605b49ede108508b9a3da514ce3a725c0085f9dbbbefdcf567a8abb46ee614e974e5d5ac0eda7f4456315684c0c60edd9
data/.gitignore CHANGED
@@ -13,3 +13,6 @@
13
13
 
14
14
  # Generated gem
15
15
  docx-builder-*.gem
16
+
17
+ # Ignore files created by Word
18
+ ~$*.docx
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- docx-builder (0.2.0)
4
+ docx-builder (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Docx
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
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
@@ -1,18 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Docx
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
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
- 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
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Docx
4
4
  module Builder
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -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(/\<(.*)/m, '')
16
- inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join(' ').gsub(/\s*\.\s*/, '.')
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.0
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-01 00:00:00.000000000 Z
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: