docx-builder 0.2.0 → 0.2.3

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: 208b51187c9e384db0e9d585552b8696e0a868709d2ee7348ae57e3d96566e1e
4
+ data.tar.gz: 54cdcbe11f62f7dcb3ab6fdcd4575f8d2ca09f876da8c0a8e7c8a57209313b42
5
5
  SHA512:
6
- metadata.gz: b9eda257bc7525ba96125c994b7b52ea63ba48469d56d2deef80fd46405274b5ebd48cbe47431ab5c0bdac131aee31d18e3027b200eac0ddad8a1978a34cebce
7
- data.tar.gz: 8920365c82374b20cc2f411a49964731d53edb0042783713103a666c72324d5cf58ff9b4bb42bcf5d107f53e1ca10ed48c69f1f3c887a779da0820a31233cf13
6
+ metadata.gz: 43f1e1c08dd0f604ba920ad88c139e4797e4b8a6ed7b9d2edf2f5d363c92b19bb9e1b889e32f3a6f4b4deb9c5920e8d197a2815320b167b6049ec8a0636506ae
7
+ data.tar.gz: 3b71f7b11e68f1c8e19616265401c4f03d392fbd503ab34aef98f8d237ff5a08545108593c4f53daf3e4d1e5e195905326bd9e005b91ec422eea87b2e014f24f
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.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Docx
4
+ module Builder
5
+ class Configuration
6
+ attr_accessor :null_placeholder
7
+ attr_accessor :logger
8
+
9
+ def initialize
10
+ @null_placeholder = ''
11
+ @logger = Docx::Logger.new(STDOUT)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -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,54 @@
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
+ Docx::Builder.logger.debug("Cleaning template variables for section #{section}")
17
+ cleaned_document = clean(@document.read(section))
18
+ Docx::Builder.logger.debug("Replacing template tags ('{{%' -> '<%') for section #{section}")
19
+ erb_template = build_erb_template(cleaned_document)
20
+ Docx::Builder.logger.debug("Rendering template for section #{section} with data")
21
+ processed_document = render_erb_template(erb_template, data)
22
+ [section, processed_document]
23
+ end.to_h
24
+ end
25
+
26
+ def clean(document)
27
+ document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
28
+ Docx::Builder::XmlProcessor.clean_variables(document)
29
+ end
30
+
31
+ def build_erb_template(document)
32
+ ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>')
33
+ ret = ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>')
34
+ ret
35
+ end
36
+
37
+ def render_erb_template(document, data)
38
+ erb_template = ERB.new(document)
39
+ ret = erb_template.result_with_hash(data)
40
+ ret
41
+ end
42
+
43
+ # creates xml template keys in input docx (template file)
44
+ def save(path, new_document)
45
+ Docx::Builder.logger.info("Saving output file into path #{path.to_s}")
46
+ File.open(path, 'wb') do |f|
47
+ new_document_xml_string = Docx::Builder::Encoder.build_new_document_xml(@document, new_document)
48
+ f.write(new_document_xml_string)
49
+ end
50
+ @document.close
51
+ end
52
+ end
49
53
  end
50
- end
54
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Docx
4
4
  module Builder
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.3"
6
6
  end
7
7
  end
@@ -1,19 +1,18 @@
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
9
7
  ret = clean_spaces_inside_variables(ret)
8
+ ret = insert_null_placeholder(ret)
10
9
  ret
11
10
  end
12
11
  end
13
12
 
14
13
  def self.join_variable_tags(match)
15
- before = match.gsub(/\<(.*)/m, '')
16
- inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join(' ').gsub(/\s*\.\s*/, '.')
14
+ before = match.gsub(/<(.*)/m, '')
15
+ inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
17
16
  after = match.gsub(/.*>([^>]+)$/m, "\\1")
18
17
  [before, inside, after].join(' ')
19
18
  end
@@ -24,9 +23,16 @@ module Docx
24
23
  cleaner_methods.each do |clean_method|
25
24
  ret = Docx::Builder::Cleaners.send(clean_method, ret)
26
25
  end
27
- puts "Returning cleaned variable: #{ret}"
26
+ Docx::Builder.logger.debug("Returning cleaned variable: #{ret}")
28
27
  ret
29
28
  end
29
+
30
+ def self.insert_null_placeholder(expression)
31
+ placeholder = Docx::Builder.configuration.null_placeholder
32
+ expression.gsub(/\{\{%\s(.+)\s%\}\}/) do |m|
33
+ "{{% (#{$1} || '#{placeholder}') %}}"
34
+ end
35
+ end
30
36
  end
31
37
 
32
38
  # {{% devedor[:pessoa_fisica][:cedula_identidade] %}},
data/lib/docx/builder.rb CHANGED
@@ -4,9 +4,29 @@ require 'rubygems'
4
4
  require 'zip'
5
5
  require 'nokogiri'
6
6
 
7
+ require_relative "logger"
7
8
  require_relative "builder/version"
8
9
  require_relative "builder/decoder"
9
10
  require_relative "builder/encoder"
10
11
  require_relative 'builder/cleaners'
11
12
  require_relative "builder/xml_processor"
12
- require_relative "builder/template"
13
+ require_relative "builder/template"
14
+ require_relative "builder/configuration"
15
+
16
+ module Docx
17
+ module Builder
18
+ class << self
19
+ def configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def logger
24
+ configuration.logger
25
+ end
26
+
27
+ def configure
28
+ yield(configuration)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'logger'
2
+
3
+ module Docx
4
+ class Logger < ::Logger
5
+ def initialize(*args, **kwargs)
6
+ super
7
+ @formatter = SimpleFormatter.new
8
+ @level = ENV['LOG_LEVEL'].to_i if ENV.keys.include?('LOG_LEVEL')
9
+ end
10
+
11
+ # Simple formatter which only displays the message
12
+ class SimpleFormatter < ::Logger::Formatter
13
+ # This method is invoked when a log event occurs
14
+ def call(severity, timestamp, progname, msg)
15
+ "#{String === msg ? msg : msg.inspect}\n"
16
+ end
17
+ end
18
+ end
19
+ end
data/publish.sh CHANGED
@@ -1,4 +1 @@
1
- gem bump
2
- gem build
3
- gem push $(ls docx-builder-*.gem)
4
- rm docx-builder-*.gem
1
+ bundle exec rake release
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.3
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-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Builds a new docx file using ERB template
14
14
  email:
@@ -32,11 +32,13 @@ files:
32
32
  - docx-builder.gemspec
33
33
  - lib/docx/builder.rb
34
34
  - lib/docx/builder/cleaners.rb
35
+ - lib/docx/builder/configuration.rb
35
36
  - lib/docx/builder/decoder.rb
36
37
  - lib/docx/builder/encoder.rb
37
38
  - lib/docx/builder/template.rb
38
39
  - lib/docx/builder/version.rb
39
40
  - lib/docx/builder/xml_processor.rb
41
+ - lib/docx/logger.rb
40
42
  - publish.sh
41
43
  - tmp/.gitkeep
42
44
  homepage: https://github.com/cash-me/docx-builder