docx-builder 0.1.13 → 0.2.2
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/.github/workflows/ci.yaml +4 -5
- data/.gitignore +7 -1
- data/.rspec +0 -0
- data/.rubocop.yml +0 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +5 -1
- data/Gemfile.lock +3 -1
- data/LICENSE.txt +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/docx-builder.gemspec +2 -2
- data/lib/docx/builder/cleaners.rb +41 -0
- data/lib/docx/builder/configuration.rb +11 -0
- data/lib/docx/builder/decoder.rb +14 -0
- data/lib/docx/builder/encoder.rb +18 -0
- data/lib/docx/builder/template.rb +50 -0
- data/lib/docx/builder/version.rb +1 -1
- data/lib/docx/builder/xml_processor.rb +40 -0
- data/lib/docx/builder.rb +18 -55
- data/publish.sh +1 -4
- data/tmp/.gitkeep +0 -0
- metadata +11 -9
- data/lib/docx/cleaners.rb +0 -37
- data/lib/docx/decoder.rb +0 -14
- data/lib/docx/encoder.rb +0 -18
- data/lib/docx/xml_processor.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a12d4646ce0b78cc0977eb4c1fa6a6d4082911dc1546c567b299a58f9b84e56
|
4
|
+
data.tar.gz: 13197d06bd647197e7fb0857ce7f10c3d4f9c119c3e0a28503d2ad20c73a44fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36580ca2af1ab880193203d304f8030125a53e6139e66c750d33f6f7846e33ccd0efe581d37eed5948708f8a933beef76d527922d3eadc018501028ca9521c51
|
7
|
+
data.tar.gz: cd3a2fd33771dcfc717f0e2ecf5340125ec80e9be792ed073425c2126c5684c2a66fc7ecbf269b795c5593fa08bab8f256af608861de0d62624b57ca60836bbf
|
data/.github/workflows/ci.yaml
CHANGED
@@ -22,10 +22,9 @@ jobs:
|
|
22
22
|
# ssh-key: ${{ secrets.DEPLOYMENT_KEY }}
|
23
23
|
# path: '.github/deployment'
|
24
24
|
|
25
|
-
# -
|
26
|
-
# uses: ruby/setup-ruby@v1
|
25
|
+
# - uses: ruby/setup-ruby@v1
|
27
26
|
# with:
|
28
|
-
# ruby-version:
|
27
|
+
# ruby-version: 2.7.2
|
29
28
|
|
30
29
|
# - name: Ruby gem cache
|
31
30
|
# uses: actions/cache@v1
|
@@ -59,9 +58,9 @@ jobs:
|
|
59
58
|
# ssh-key: ${{ secrets.DEPLOYMENT_KEY }}
|
60
59
|
# path: '.github/deployment'
|
61
60
|
|
62
|
-
# - uses:
|
61
|
+
# - uses: ruby/setup-ruby@v1
|
63
62
|
# with:
|
64
|
-
# ruby-version:
|
63
|
+
# ruby-version: 2.7.2
|
65
64
|
|
66
65
|
# - name: Tests
|
67
66
|
# uses: ./.github/deployment/actions/tests/rails
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
File without changes
|
data/.rubocop.yml
CHANGED
File without changes
|
data/CODE_OF_CONDUCT.md
CHANGED
File without changes
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
docx-builder (0.2.
|
4
|
+
docx-builder (0.2.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ast (2.4.2)
|
10
|
+
bump (0.10.0)
|
10
11
|
byebug (11.1.3)
|
11
12
|
diff-lcs (1.4.4)
|
12
13
|
mini_portile2 (2.5.0)
|
@@ -53,6 +54,7 @@ PLATFORMS
|
|
53
54
|
ruby
|
54
55
|
|
55
56
|
DEPENDENCIES
|
57
|
+
bump
|
56
58
|
byebug
|
57
59
|
docx-builder!
|
58
60
|
nokogiri (~> 1.11.1)
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/docx-builder.gemspec
CHANGED
@@ -5,8 +5,8 @@ require_relative "lib/docx/builder/version"
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "docx-builder"
|
7
7
|
spec.version = Docx::Builder::VERSION
|
8
|
-
spec.authors = ["
|
9
|
-
spec.email = ["
|
8
|
+
spec.authors = ["Tecnologia Cashme"]
|
9
|
+
spec.email = ["tecnologia@cashme.com.br"]
|
10
10
|
|
11
11
|
spec.summary = "Builds a new docx file using ERB template"
|
12
12
|
spec.description = "Builds a new docx file using ERB template"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Docx
|
2
|
+
module Builder
|
3
|
+
module Cleaners
|
4
|
+
def self.starting_tags_without_spaces_cleaner(str)
|
5
|
+
str && str.gsub(/\{\s*\{\s*%/, '{{%')
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.ending_tags_without_spaces_cleaner(str)
|
9
|
+
str && str.gsub(/%\s*\}\s*\}/, '%}}')
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.single_quote_cleaner(str)
|
13
|
+
str && str.gsub(/\‘/, '\'').gsub(/\’/, '\'')
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.var_without_duplicated_spaces_cleaner(str)
|
17
|
+
str && str.gsub(/\s+/, ' ')
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.var_without_spaces_inside_brackets_cleaner(str)
|
21
|
+
str && str.gsub(/\[(.*?)\]/) { |match| match.gsub(/\s/, '')}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.no_spaces_around_undeline_cleaner(str)
|
25
|
+
str && str.gsub(/\s*\_\s*/, "_")
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.join_dots_and_brackets_cleaner(str)
|
29
|
+
str && str.gsub(/\s*\.\s*/, '.').gsub(/\s*\[\s*/, '[').gsub(/\s*\]/, ']')
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.question_method_cleaner(str)
|
33
|
+
if str && str =~ /\A\{\{\s+if\s*(.*?)\s*\?\s+\}\}\Z/
|
34
|
+
"{{ if #{$1}\? }}"
|
35
|
+
else
|
36
|
+
str
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
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
|
18
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Docx
|
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
|
49
|
+
end
|
50
|
+
end
|
data/lib/docx/builder/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Docx
|
2
|
+
module Builder
|
3
|
+
class XmlProcessor
|
4
|
+
def self.clean_variables(document)
|
5
|
+
document.gsub(/\{\{.*?\}\}/m) do |match|
|
6
|
+
ret = match.include?('>') ? join_variable_tags(match) : match
|
7
|
+
ret = clean_spaces_inside_variables(ret)
|
8
|
+
ret = insert_null_placeholder(ret)
|
9
|
+
ret
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.join_variable_tags(match)
|
14
|
+
before = match.gsub(/<(.*)/m, '')
|
15
|
+
inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
|
16
|
+
after = match.gsub(/.*>([^>]+)$/m, "\\1")
|
17
|
+
[before, inside, after].join(' ')
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.clean_spaces_inside_variables(match)
|
21
|
+
ret = match.dup
|
22
|
+
cleaner_methods = Docx::Builder::Cleaners.methods.grep /_cleaner/
|
23
|
+
cleaner_methods.each do |clean_method|
|
24
|
+
ret = Docx::Builder::Cleaners.send(clean_method, ret)
|
25
|
+
end
|
26
|
+
puts "Returning cleaned variable: #{ret}"
|
27
|
+
ret
|
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
|
36
|
+
end
|
37
|
+
|
38
|
+
# {{% devedor[:pessoa_fisica][:cedula_identidade] %}},
|
39
|
+
end
|
40
|
+
end
|
data/lib/docx/builder.rb
CHANGED
@@ -1,64 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "builder/version"
|
4
|
-
require_relative "decoder"
|
5
|
-
require_relative "encoder"
|
6
|
-
require_relative "xml_processor"
|
7
|
-
|
8
3
|
require 'rubygems'
|
9
4
|
require 'zip'
|
10
5
|
require 'nokogiri'
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@document = Docx::Decoder.to_xml(path)
|
20
|
-
@sections = ["word/document.xml"]
|
21
|
-
@document.each do |file|
|
22
|
-
if file.name.include? "word/header"
|
23
|
-
@sections.push(file.name)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def render(data)
|
29
|
-
@sections.map do |section|
|
30
|
-
cleaned_document = clean(@document.read(section))
|
31
|
-
erb_template = build_erb_template(cleaned_document)
|
32
|
-
processed_document = render_erb_template(erb_template, data)
|
33
|
-
[section, processed_document]
|
34
|
-
end.to_h
|
35
|
-
end
|
36
|
-
|
37
|
-
def clean(document)
|
38
|
-
document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
|
39
|
-
XmlProcessor.clean_variables(document)
|
40
|
-
end
|
41
|
-
|
42
|
-
def build_erb_template(document)
|
43
|
-
ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>')
|
44
|
-
ret = ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>')
|
45
|
-
ret
|
46
|
-
end
|
47
|
-
|
48
|
-
def render_erb_template(document, data)
|
49
|
-
erb_template = ERB.new(document)
|
50
|
-
ret = erb_template.result_with_hash(data)
|
51
|
-
ret
|
52
|
-
end
|
7
|
+
require_relative "builder/version"
|
8
|
+
require_relative "builder/decoder"
|
9
|
+
require_relative "builder/encoder"
|
10
|
+
require_relative 'builder/cleaners'
|
11
|
+
require_relative "builder/xml_processor"
|
12
|
+
require_relative "builder/template"
|
13
|
+
require_relative "builder/configuration"
|
53
14
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
15
|
+
module Docx
|
16
|
+
module Builder
|
17
|
+
class << self
|
18
|
+
def configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure
|
23
|
+
yield(configuration)
|
24
|
+
end
|
59
25
|
end
|
60
|
-
@document.close
|
61
|
-
end
|
62
26
|
end
|
63
|
-
|
64
|
-
end
|
27
|
+
end
|
data/publish.sh
CHANGED
data/tmp/.gitkeep
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docx-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Tecnologia Cashme
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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:
|
15
|
-
-
|
15
|
+
- tecnologia@cashme.com.br
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
@@ -31,11 +31,13 @@ files:
|
|
31
31
|
- bin/setup
|
32
32
|
- docx-builder.gemspec
|
33
33
|
- lib/docx/builder.rb
|
34
|
+
- lib/docx/builder/cleaners.rb
|
35
|
+
- lib/docx/builder/configuration.rb
|
36
|
+
- lib/docx/builder/decoder.rb
|
37
|
+
- lib/docx/builder/encoder.rb
|
38
|
+
- lib/docx/builder/template.rb
|
34
39
|
- lib/docx/builder/version.rb
|
35
|
-
- lib/docx/
|
36
|
-
- lib/docx/decoder.rb
|
37
|
-
- lib/docx/encoder.rb
|
38
|
-
- lib/docx/xml_processor.rb
|
40
|
+
- lib/docx/builder/xml_processor.rb
|
39
41
|
- publish.sh
|
40
42
|
- tmp/.gitkeep
|
41
43
|
homepage: https://github.com/cash-me/docx-builder
|
@@ -60,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
62
|
- !ruby/object:Gem::Version
|
61
63
|
version: '0'
|
62
64
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
65
|
+
rubygems_version: 3.3.9
|
64
66
|
signing_key:
|
65
67
|
specification_version: 4
|
66
68
|
summary: Builds a new docx file using ERB template
|
data/lib/docx/cleaners.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
module Cleaners
|
2
|
-
def self.starting_tags_without_spaces_cleaner(str)
|
3
|
-
str && str.gsub(/\{\s*\{\s*%/, '{{%')
|
4
|
-
end
|
5
|
-
|
6
|
-
def self.ending_tags_without_spaces_cleaner(str)
|
7
|
-
str && str.gsub(/%\s*\}\s*\}/, '%}}')
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.single_quote_cleaner(str)
|
11
|
-
str && str.gsub(/\‘/, '\'').gsub(/\’/, '\'')
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.var_without_duplicated_spaces_cleaner(str)
|
15
|
-
str && str.gsub(/\s+/, ' ')
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.var_without_spaces_inside_brackets_cleaner(str)
|
19
|
-
str && str.gsub(/\[(.*?)\]/) { |match| match.gsub(/\s/, '')}
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.no_spaces_around_undeline_cleaner(str)
|
23
|
-
str && str.gsub(/\s*\_\s*/, "_")
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.join_dots_and_brackets_cleaner(str)
|
27
|
-
str && str.gsub(/\s*\.\s*/, '.').gsub(/\s*\[\s*/, '[').gsub(/\s*\]/, ']')
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.question_method_cleaner(str)
|
31
|
-
if str && str =~ /\A\{\{\s+if\s*(.*?)\s*\?\s+\}\}\Z/
|
32
|
-
"{{ if #{$1}\? }}"
|
33
|
-
else
|
34
|
-
str
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
data/lib/docx/decoder.rb
DELETED
data/lib/docx/encoder.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'zip'
|
4
|
-
|
5
|
-
module Docx
|
6
|
-
module Encoder
|
7
|
-
def self.build_new_document_xml(original_document, new_document_sections)
|
8
|
-
output = Zip::OutputStream.write_buffer(StringIO.new) do |out|
|
9
|
-
original_document.each do |entry|
|
10
|
-
entry_name = entry.name
|
11
|
-
out.put_next_entry(entry_name)
|
12
|
-
out.write(new_document_sections[entry_name] || entry.get_input_stream.read)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
output.string
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/lib/docx/xml_processor.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require_relative 'cleaners'
|
2
|
-
|
3
|
-
class XmlProcessor
|
4
|
-
BROKEN_VARIABLE_PATTERN = "<w:t>([^\#^<^>]*?)#([^\#^<^>]*?)<\/w:t>"
|
5
|
-
|
6
|
-
def self.clean_variables(document)
|
7
|
-
document.gsub(/\{\{.*?\}\}/m) do |match|
|
8
|
-
ret = match.include?('>') ? join_variable_tags(match) : match
|
9
|
-
ret = clean_spaces_inside_variables(ret)
|
10
|
-
ret
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.join_variable_tags(match)
|
15
|
-
before = match.gsub(/\<(.*)/m, '')
|
16
|
-
inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join(' ').gsub(/\s*\.\s*/, '.')
|
17
|
-
after = match.gsub(/.*>([^>]+)$/m, "\\1")
|
18
|
-
[before, inside, after].join(' ')
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.clean_spaces_inside_variables(match)
|
22
|
-
ret = match.dup
|
23
|
-
cleaner_methods = Cleaners.methods.grep /_cleaner/
|
24
|
-
cleaner_methods.each do |clean_method|
|
25
|
-
ret = Cleaners.send(clean_method, ret)
|
26
|
-
end
|
27
|
-
puts "Returning cleaned variable: #{ret}"
|
28
|
-
ret
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# {{% devedor[:pessoa_fisica][:cedula_identidade] %}},
|