docx-builder 0.1.11 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5847fb8238c628a1907025033216d203c41d4dcb6798a8d3d4d9538d05af4a2e
4
- data.tar.gz: 7ce08edfa70098ce12c5c436d6fed7a73a7de59539f35ea6dd61734e3d452d33
3
+ metadata.gz: 4a834e7eb99b4459de40d6942008c8487626ff25ccf75d19a2c4d5af4978402b
4
+ data.tar.gz: 1fa0be4e4a3c0d9c7e15804d90b1dbcedc9ca3b42f059593daeef50f08ca5b47
5
5
  SHA512:
6
- metadata.gz: e4f6190d0f1bb16a313740edbc5dca7feab7ec812b8748905a7329bd67dc3bc1be48166e62d95629e0a69654d058e57830767b8e7be395dc1a373e47612baf15
7
- data.tar.gz: 27993bea930a31617d205684c66bca679da3e2b1d02e5be983a14facac29609a60b83858666283d0e00dcefb26d126612f146a71fdeb68766dd169c3d5de9acf
6
+ metadata.gz: b80c298d29268dc7b5aae2ddc90533be3668fc8fa34c2f0da64451c47a58a44e9eee63b269092cb4af1e86b7ecdcba0ee63d23498f7c24935fcfc524019c5abd
7
+ data.tar.gz: 8f85311bcfd8a02207a2f673d0b81be605b49ede108508b9a3da514ce3a725c0085f9dbbbefdcf567a8abb46ee614e974e5d5ac0eda7f4456315684c0c60edd9
@@ -0,0 +1,74 @@
1
+ name: CI - Checks
2
+ on:
3
+ push:
4
+ branches-ignore:
5
+ - master
6
+
7
+ jobs:
8
+
9
+ # lint:
10
+ # name: Linters verification
11
+ # runs-on: ubuntu-18.04
12
+
13
+ # steps:
14
+
15
+ # - name: Checkout code
16
+ # uses: actions/checkout@v2
17
+
18
+ # - name: Checkout deployment repo
19
+ # uses: actions/checkout@v2
20
+ # with:
21
+ # repository: cash-me/deployment
22
+ # ssh-key: ${{ secrets.DEPLOYMENT_KEY }}
23
+ # path: '.github/deployment'
24
+
25
+ # - uses: ruby/setup-ruby@v1
26
+ # with:
27
+ # ruby-version: 2.7.2
28
+
29
+ # - name: Ruby gem cache
30
+ # uses: actions/cache@v1
31
+ # with:
32
+ # path: vendor/bundle
33
+ # key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
34
+ # restore-keys: |
35
+ # ${{ runner.os }}-gems-
36
+
37
+ # - name: Linters
38
+ # uses: ./.github/deployment/actions/linters/rails
39
+
40
+ # test:
41
+ # name: Run tests
42
+ # needs: lint
43
+ # runs-on: ubuntu-18.04
44
+
45
+ # services:
46
+ # postgres:
47
+ # image: postgres:11.6
48
+ # ports: ["5432:5432"]
49
+ # options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
50
+
51
+ # steps:
52
+ # - uses: actions/checkout@v2
53
+
54
+ # - name: Checkout deployment repo
55
+ # uses: actions/checkout@v2
56
+ # with:
57
+ # repository: cash-me/deployment
58
+ # ssh-key: ${{ secrets.DEPLOYMENT_KEY }}
59
+ # path: '.github/deployment'
60
+
61
+ # - uses: ruby/setup-ruby@v1
62
+ # with:
63
+ # ruby-version: 2.7.2
64
+
65
+ # - name: Tests
66
+ # uses: ./.github/deployment/actions/tests/rails
67
+ # with:
68
+ # cc_reporter_id: ${{ secrets.CC_TEST_REPORTER_ID }}
69
+
70
+ ci-workflow:
71
+ # needs: test
72
+ runs-on: ubuntu-18.04
73
+ steps:
74
+ - run: exit 0
data/.gitignore CHANGED
@@ -5,8 +5,14 @@
5
5
  /doc/
6
6
  /pkg/
7
7
  /spec/reports/
8
- /tmp/
8
+ /tmp/output.docx
9
+ .byebug_history
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
12
- docx-builder-0.1.0.gem
13
+
14
+ # Generated gem
15
+ docx-builder-*.gem
16
+
17
+ # Ignore files created by Word
18
+ ~$*.docx
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
@@ -7,10 +7,14 @@ gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
9
  gem "rspec", "~> 3.0"
10
- gem "zip", "~> 2.0.2"
10
+ gem 'rubyzip', require: 'zip'
11
11
  gem "rubocop", "~> 0.80"
12
12
  gem "nokogiri", "~> 1.11.1"
13
13
 
14
14
  group :test do
15
15
  gem "byebug"
16
- end
16
+ end
17
+
18
+ group :development do
19
+ gem "bump"
20
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ docx-builder (0.2.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ bump (0.10.0)
11
+ byebug (11.1.3)
12
+ diff-lcs (1.4.4)
13
+ mini_portile2 (2.5.0)
14
+ nokogiri (1.11.3)
15
+ mini_portile2 (~> 2.5.0)
16
+ racc (~> 1.4)
17
+ parallel (1.20.1)
18
+ parser (3.0.1.0)
19
+ ast (~> 2.4.1)
20
+ racc (1.5.2)
21
+ rainbow (3.0.0)
22
+ rake (13.0.3)
23
+ regexp_parser (2.1.1)
24
+ rexml (3.2.5)
25
+ rspec (3.10.0)
26
+ rspec-core (~> 3.10.0)
27
+ rspec-expectations (~> 3.10.0)
28
+ rspec-mocks (~> 3.10.0)
29
+ rspec-core (3.10.1)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-expectations (3.10.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-mocks (3.10.2)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-support (3.10.2)
38
+ rubocop (0.93.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.7.1.5)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
43
+ rexml
44
+ rubocop-ast (>= 0.6.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-ast (1.4.1)
48
+ parser (>= 2.7.1.5)
49
+ ruby-progressbar (1.11.0)
50
+ rubyzip (2.3.0)
51
+ unicode-display_width (1.7.0)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ bump
58
+ byebug
59
+ docx-builder!
60
+ nokogiri (~> 1.11.1)
61
+ rake (~> 13.0)
62
+ rspec (~> 3.0)
63
+ rubocop (~> 0.80)
64
+ rubyzip
65
+
66
+ BUNDLED WITH
67
+ 2.1.4
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 = ["Paulo Soares"]
9
- spec.email = ["paulo.soares@cashme.com.br"]
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,14 @@
1
+ # frozen_string_literal: true
2
+
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
14
+ 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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Docx
4
4
  module Builder
5
- VERSION = "0.1.11"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -0,0 +1,32 @@
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
9
+ end
10
+ end
11
+
12
+ def self.join_variable_tags(match)
13
+ before = match.gsub(/<(.*)/m, '')
14
+ inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
15
+ after = match.gsub(/.*>([^>]+)$/m, "\\1")
16
+ [before, inside, after].join(' ')
17
+ end
18
+
19
+ def self.clean_spaces_inside_variables(match)
20
+ ret = match.dup
21
+ cleaner_methods = Docx::Builder::Cleaners.methods.grep /_cleaner/
22
+ cleaner_methods.each do |clean_method|
23
+ ret = Docx::Builder::Cleaners.send(clean_method, ret)
24
+ end
25
+ puts "Returning cleaned variable: #{ret}"
26
+ ret
27
+ end
28
+ end
29
+
30
+ # {{% devedor[:pessoa_fisica][:cedula_identidade] %}},
31
+ end
32
+ end
data/lib/docx/builder.rb CHANGED
@@ -1,59 +1,12 @@
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
- module Docx
13
- module Builder
14
- class Error < StandardError; end
15
-
16
- class Template
17
- SECTIONS = %w( word/header1.xml word/document.xml )
18
-
19
- def initialize(path)
20
- @document = Docx::Decoder.to_xml(path)
21
- end
22
-
23
- def render(data)
24
- SECTIONS.map do |section|
25
- cleaned_document = clean(@document.read(section))
26
- erb_template = build_erb_template(cleaned_document)
27
- processed_document = render_erb_template(erb_template, data)
28
- [section, processed_document]
29
- end.to_h
30
- end
31
-
32
- def clean(document)
33
- document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
34
- XmlProcessor.clean_variables(document)
35
- end
36
-
37
- def build_erb_template(document)
38
- ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>')
39
- ret = ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>')
40
- ret
41
- end
42
-
43
- def render_erb_template(document, data)
44
- erb_template = ERB.new(document)
45
- ret = erb_template.result_with_hash(data)
46
- ret
47
- end
48
-
49
- # creates xml template keys in input docx (template file)
50
- def save(path, new_document)
51
- File.open(path, 'wb') do |f|
52
- new_document_xml_string = Docx::Encoder.build_new_document_xml(@document, new_document)
53
- f.write(new_document_xml_string)
54
- end
55
- @document.close
56
- end
57
- end
58
- end
59
- 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"
data/tmp/.gitkeep ADDED
File without changes
metadata CHANGED
@@ -1,28 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docx-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - Paulo Soares
7
+ - Tecnologia Cashme
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-23 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:
15
- - paulo.soares@cashme.com.br
15
+ - tecnologia@cashme.com.br
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".github/workflows/main.yml"
20
+ - ".github/workflows/ci.yaml"
21
21
  - ".gitignore"
22
22
  - ".rspec"
23
23
  - ".rubocop.yml"
24
24
  - CODE_OF_CONDUCT.md
25
25
  - Gemfile
26
+ - Gemfile.lock
26
27
  - LICENSE.txt
27
28
  - README.md
28
29
  - Rakefile
@@ -30,12 +31,14 @@ files:
30
31
  - bin/setup
31
32
  - docx-builder.gemspec
32
33
  - lib/docx/builder.rb
34
+ - lib/docx/builder/cleaners.rb
35
+ - lib/docx/builder/decoder.rb
36
+ - lib/docx/builder/encoder.rb
37
+ - lib/docx/builder/template.rb
33
38
  - lib/docx/builder/version.rb
34
- - lib/docx/cleaners.rb
35
- - lib/docx/decoder.rb
36
- - lib/docx/encoder.rb
37
- - lib/docx/xml_processor.rb
39
+ - lib/docx/builder/xml_processor.rb
38
40
  - publish.sh
41
+ - tmp/.gitkeep
39
42
  homepage: https://github.com/cash-me/docx-builder
40
43
  licenses:
41
44
  - MIT
@@ -58,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
61
  - !ruby/object:Gem::Version
59
62
  version: '0'
60
63
  requirements: []
61
- rubygems_version: 3.1.4
64
+ rubygems_version: 3.3.9
62
65
  signing_key:
63
66
  specification_version: 4
64
67
  summary: Builds a new docx file using ERB template
@@ -1,18 +0,0 @@
1
- name: Ruby
2
-
3
- on: [push,pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
- - name: Set up Ruby
11
- uses: ruby/setup-ruby@v1
12
- with:
13
- ruby-version: 2.7.2
14
- - name: Run the default task
15
- run: |
16
- gem install bundler -v 2.2.2
17
- bundle install
18
- bundle exec rake
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
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'zip'
4
-
5
- module Docx
6
- module Decoder
7
- def self.to_xml(path)
8
- Zip::File.open(path)
9
- end
10
- end
11
- end
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
@@ -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] %}},