docx-builder 0.3.1 → 0.4.1.pre.alpha

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: 19c51c03274fab75ebaa533376a03bc2027f7c6c451885422fd324f986cdc654
4
- data.tar.gz: 9e59f2367312b70e2f62c22da3117b36712641ee5c467fcf8650280f9fd5d00b
3
+ metadata.gz: 20a1abe01ca637f473541e829c94c92678cef288baa893ab8855f97c303df4ee
4
+ data.tar.gz: 58436a829e7c49b5c755e82c2135650530c7d34b9c3328dd075577b01333967b
5
5
  SHA512:
6
- metadata.gz: 67c44931b227c87c3c8234a969ff60c1722e00711d922e7d1a824653d461b8c8f26cf66016df92e652435ef91643d39fe584d52f54f0835a64dbbadd6f33fb1a
7
- data.tar.gz: 1a36660a25365cbf978f54369daabb734a22096cfbd7bfc0364e605f37fc4e49003e2ed1ac8600608607ecbb7f2035bf3f6714b171dbe06475c330067f378209
6
+ metadata.gz: d91b84fb0f0d4a6bcd47544a73795c6f901b3c1f67510fad9c6926ddfbc68da7bde8a70de1c36a8874eeec9f7d8c0fba961cdb9e82f57a8aeba68efac1a57e85
7
+ data.tar.gz: 7e1e292b5303d4df440110b8e40732d1e385f31ec45f9d40c5e1e8095131f5130059e0b97ca28f58c95ef416c7d2d2536bccc0c623551ec5e95f3a1bf1624ad7
data/Gemfile.lock CHANGED
@@ -1,43 +1,43 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- docx-builder (0.3.1)
4
+ docx-builder (0.4.1.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
10
  bump (0.10.0)
11
- bundler-audit (0.9.0.1)
11
+ bundler-audit (0.9.1)
12
12
  bundler (>= 1.2.0, < 3)
13
13
  thor (~> 1.0)
14
14
  byebug (11.1.3)
15
- diff-lcs (1.4.4)
15
+ diff-lcs (1.5.0)
16
16
  mini_portile2 (2.8.0)
17
- nokogiri (1.13.4)
17
+ nokogiri (1.13.10)
18
18
  mini_portile2 (~> 2.8.0)
19
19
  racc (~> 1.4)
20
- parallel (1.20.1)
21
- parser (3.0.1.0)
20
+ parallel (1.22.1)
21
+ parser (3.1.3.0)
22
22
  ast (~> 2.4.1)
23
- racc (1.6.0)
24
- rainbow (3.0.0)
25
- rake (13.0.3)
26
- regexp_parser (2.1.1)
23
+ racc (1.6.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.6.1)
27
27
  rexml (3.2.5)
28
- rspec (3.10.0)
29
- rspec-core (~> 3.10.0)
30
- rspec-expectations (~> 3.10.0)
31
- rspec-mocks (~> 3.10.0)
32
- rspec-core (3.10.1)
33
- rspec-support (~> 3.10.0)
34
- rspec-expectations (3.10.1)
28
+ rspec (3.12.0)
29
+ rspec-core (~> 3.12.0)
30
+ rspec-expectations (~> 3.12.0)
31
+ rspec-mocks (~> 3.12.0)
32
+ rspec-core (3.12.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-expectations (3.12.0)
35
35
  diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.10.0)
37
- rspec-mocks (3.10.2)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-mocks (3.12.1)
38
38
  diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.10.0)
40
- rspec-support (3.10.2)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-support (3.12.0)
41
41
  rubocop (0.93.1)
42
42
  parallel (~> 1.10)
43
43
  parser (>= 2.7.1.5)
@@ -47,12 +47,12 @@ GEM
47
47
  rubocop-ast (>= 0.6.0)
48
48
  ruby-progressbar (~> 1.7)
49
49
  unicode-display_width (>= 1.4.0, < 2.0)
50
- rubocop-ast (1.4.1)
51
- parser (>= 2.7.1.5)
50
+ rubocop-ast (1.24.0)
51
+ parser (>= 3.1.1.0)
52
52
  ruby-progressbar (1.11.0)
53
- rubyzip (2.3.0)
53
+ rubyzip (2.3.2)
54
54
  thor (1.2.1)
55
- unicode-display_width (1.7.0)
55
+ unicode-display_width (1.8.0)
56
56
 
57
57
  PLATFORMS
58
58
  ruby
@@ -69,4 +69,4 @@ DEPENDENCIES
69
69
  rubyzip
70
70
 
71
71
  BUNDLED WITH
72
- 2.1.4
72
+ 2.3.17
data/README.md CHANGED
@@ -14,6 +14,24 @@ Insert here some text: {{% item.property %}}
14
14
  {{ end }}
15
15
  ```
16
16
 
17
+ ## Helpers
18
+
19
+ ### Table
20
+
21
+ In order to render a table on docx template, we wrote:
22
+
23
+ ```
24
+ {{t config t}}
25
+ ```
26
+
27
+ where "config" is a hash that contains two properties:
28
+
29
+ * columns: an array of hashes. Each element represents a column and may have the following information:
30
+ * name (required): attribute name;
31
+ * width (required): specifies the value of the preferred width of the column (according to the officeopenxml documentation, it's in twentieths of a point or 1/1440 of an inch)
32
+ * description (optional): attribute description used on table's header; if not specified, its name will be used as a description
33
+ * rows: an array of hashes. Each element represents a row and all of its properties must appear in the columns' array.
34
+
17
35
  ## Installation
18
36
 
19
37
  Add this line to your application's Gemfile:
@@ -39,6 +57,39 @@ Or install it yourself as:
39
57
  # or memory_stream = builder.save_as_buffer(new_xml)
40
58
  ```
41
59
 
60
+ An exemple of table configuration should be:
61
+
62
+ ```ruby
63
+ users_table = {
64
+ columns: [
65
+ {
66
+ name: :name,
67
+ description: 'Name',
68
+ width: 1150
69
+ },
70
+ {
71
+ name: :email,
72
+ description: 'Mail address',
73
+ width: 1150
74
+ }
75
+ ],
76
+ rows: [
77
+ {
78
+ name: 'João',
79
+ email: 'joao@example.com'
80
+ },
81
+ {
82
+ name: 'José',
83
+ email: 'jose@example.com'
84
+ },
85
+ {
86
+ name: 'Felipe',
87
+ email: 'felipe@example.com'
88
+ }
89
+ ]
90
+ }
91
+ ```
92
+
42
93
  ## Contributing
43
94
 
44
95
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/docx-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/docx-builder/blob/master/CODE_OF_CONDUCT.md).
@@ -4,6 +4,8 @@ module Docx
4
4
  module Builder
5
5
  # Represents a docx erb template
6
6
  class Template
7
+ attr_reader :document, :sections
8
+
7
9
  def initialize(path)
8
10
  @document = Docx::Builder::Decoder.to_xml(path)
9
11
  @sections = ['word/document.xml']
@@ -34,6 +36,7 @@ module Docx
34
36
  # Replaces the docx-builder's tags for those ones currently used in erb files
35
37
  def build_erb_template(document)
36
38
  ret = document.gsub(/\{\{%/, '<%=').gsub(/%\}\}/, '%>')
39
+ ret = ret.gsub(/\{\{t\s(.*)\st\}\}/) { |_| build_dynamic_table(Regexp.last_match(1)) }
37
40
  ret.gsub(/\{\{/, '<%').gsub(/\}\}/, '%>')
38
41
  end
39
42
 
@@ -59,6 +62,18 @@ module Docx
59
62
  @document.close
60
63
  buffer
61
64
  end
65
+
66
+ private
67
+
68
+ def build_dynamic_table(variable)
69
+ template = read_xml_template(:table)
70
+ template.gsub('data', variable)
71
+ end
72
+
73
+ def read_xml_template(filename)
74
+ path = File.join(File.dirname(__FILE__), "../../../templates/#{filename}.xml.erb")
75
+ File.readlines(path).map(&:strip).join('')
76
+ end
62
77
  end
63
78
  end
64
79
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Docx
4
4
  module Builder
5
- VERSION = '0.3.1'
5
+ VERSION = '0.4.1-alpha'
6
6
  end
7
7
  end
@@ -8,7 +8,7 @@ module Docx
8
8
  document.gsub(/\{\{.*?\}\}/m) do |match|
9
9
  ret = match.include?('>') ? join_variable_tags(match) : match
10
10
  Docx::Builder.logger.debug("Before cleaning: #{ret}")
11
- ret = clean_spaces_inside_variables(ret)
11
+ ret = Docx::Builder::Cleaners.single_quote_cleaner(ret)
12
12
  ret = insert_null_placeholder(ret)
13
13
  Docx::Builder.logger.debug("After cleaning: #{ret}")
14
14
  ret
@@ -19,7 +19,7 @@ module Docx
19
19
  before = match.gsub(/<(.*)/m, '')
20
20
  inside = match.scan(/<w:t.*?>([^>]*?)<.w:t>/m).join
21
21
  after = match.gsub(/.*>([^>]+)$/m, '\\1')
22
- [before, inside, after].join(' ')
22
+ [before, inside, after].join
23
23
  end
24
24
 
25
25
  def self.clean_spaces_inside_variables(match)
@@ -33,7 +33,7 @@ module Docx
33
33
 
34
34
  def self.insert_null_placeholder(expression)
35
35
  placeholder = Docx::Builder.configuration.null_placeholder
36
- expression.gsub(/\{\{%\s(.+)\s%\}\}/) do |_m|
36
+ expression.gsub(/\{\{%\s(.+?)\s%\}\}/) do |_m|
37
37
  "{{% (#{Regexp.last_match(1)} || '#{placeholder}') %}}"
38
38
  end
39
39
  end
@@ -0,0 +1,102 @@
1
+ <w:tbl>
2
+ <w:tblPr>
3
+ <w:tblW w:w="10006" w:type="dxa"/>
4
+ <w:tblInd w:w="-626" w:type="dxa"/>
5
+ <w:tblCellMar>
6
+ <w:left w:w="70" w:type="dxa"/>
7
+ <w:right w:w="70" w:type="dxa"/>
8
+ </w:tblCellMar>
9
+ <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
10
+ </w:tblPr>
11
+ <w:tblGrid>
12
+ <% data[:columns].each do |column| %>
13
+ <w:gridCol w:w="<%= column[:width] %>"/>
14
+ <% end %>
15
+ </w:tblGrid>
16
+ <w:tr w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidTr="00E653AF">
17
+ <w:trPr>
18
+ <w:trHeight w:val="315"/>
19
+ </w:trPr>
20
+ <% data[:columns].each do |column| %>
21
+ <w:tc>
22
+ <w:tcPr>
23
+ <w:tcW w:w="<%= column[:width] %>" w:type="dxa"/>
24
+ <w:tcBorders>
25
+ <w:top w:val="single" w:sz="4" w:space="0" w:color="000000"/>
26
+ <w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
27
+ <w:bottom w:val="single" w:sz="8" w:space="0" w:color="000000"/>
28
+ <w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/>
29
+ </w:tcBorders>
30
+ <w:shd w:val="clear" w:color="000000" w:fill="FFFFFF"/>
31
+ <w:noWrap/>
32
+ <w:vAlign w:val="center"/>
33
+ <w:hideMark/>
34
+ </w:tcPr>
35
+ <w:p w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidRDefault="00080F99" w:rsidP="00E653AF">
36
+ <w:pPr>
37
+ <w:jc w:val="center"/>
38
+ <w:rPr>
39
+ <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
40
+ <w:b/>
41
+ <w:bCs/>
42
+ <w:color w:val="000000"/>
43
+ <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
44
+ </w:rPr>
45
+ </w:pPr>
46
+ <w:r w:rsidRPr="00E71341">
47
+ <w:rPr>
48
+ <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
49
+ <w:b/>
50
+ <w:bCs/>
51
+ <w:color w:val="000000"/>
52
+ <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
53
+ </w:rPr>
54
+ <w:t><%= column[:description] %></w:t>
55
+ </w:r>
56
+ </w:p>
57
+ </w:tc>
58
+ <% end %>
59
+ </w:tr>
60
+ <% data[:rows].each do |row| %>
61
+ <w:tr w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidTr="00E653AF">
62
+ <w:trPr>
63
+ <w:trHeight w:val="315"/>
64
+ </w:trPr>
65
+ <% data[:columns].each do |column| %>
66
+ <w:tc>
67
+ <w:tcPr>
68
+ <w:tcW w:w="<%= column[:width] %>" w:type="dxa"/>
69
+ <w:tcBorders>
70
+ <w:top w:val="single" w:sz="4" w:space="0" w:color="000000"/>
71
+ <w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
72
+ <w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000"/>
73
+ <w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/>
74
+ </w:tcBorders>
75
+ <w:shd w:val="clear" w:color="000000" w:fill="FFFFFF"/>
76
+ <w:noWrap/>
77
+ <w:vAlign w:val="center"/>
78
+ <w:hideMark/>
79
+ </w:tcPr>
80
+ <w:p w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidRDefault="005549E3" w:rsidP="00E653AF">
81
+ <w:pPr>
82
+ <w:jc w:val="center"/>
83
+ <w:rPr>
84
+ <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
85
+ <w:color w:val="000000"/>
86
+ <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
87
+ </w:rPr>
88
+ </w:pPr>
89
+ <w:r>
90
+ <w:rPr>
91
+ <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
92
+ <w:color w:val="000000"/>
93
+ <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
94
+ </w:rPr>
95
+ <w:t><%= row[column[:name]] %></w:t>
96
+ </w:r>
97
+ </w:p>
98
+ </w:tc>
99
+ <% end %>
100
+ </w:tr>
101
+ <% end %>
102
+ </w:tbl>
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.3.1
4
+ version: 0.4.1.pre.alpha
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-14 00:00:00.000000000 Z
11
+ date: 2022-12-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Builds a new docx file using ERB template
14
14
  email:
@@ -40,6 +40,7 @@ files:
40
40
  - lib/docx/builder/version.rb
41
41
  - lib/docx/builder/xml_processor.rb
42
42
  - lib/docx/logger.rb
43
+ - templates/table.xml.erb
43
44
  - tmp/.gitkeep
44
45
  homepage: https://github.com/cash-me/docx-builder
45
46
  licenses:
@@ -59,9 +60,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
60
  version: 2.3.0
60
61
  required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  requirements:
62
- - - ">="
63
+ - - ">"
63
64
  - !ruby/object:Gem::Version
64
- version: '0'
65
+ version: 1.3.1
65
66
  requirements: []
66
67
  rubygems_version: 3.1.6
67
68
  signing_key: