docx-builder 0.3.1 → 0.4.0

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: 04a3f8e6aceab4a926778161073b38cbed31c533e52ac924e83bf70fddc2a61c
4
+ data.tar.gz: 7746ba4203d23e18182a803082927bb50ec14d9dfed43b52c5bee557c86ba94b
5
5
  SHA512:
6
- metadata.gz: 67c44931b227c87c3c8234a969ff60c1722e00711d922e7d1a824653d461b8c8f26cf66016df92e652435ef91643d39fe584d52f54f0835a64dbbadd6f33fb1a
7
- data.tar.gz: 1a36660a25365cbf978f54369daabb734a22096cfbd7bfc0364e605f37fc4e49003e2ed1ac8600608607ecbb7f2035bf3f6714b171dbe06475c330067f378209
6
+ metadata.gz: 03e659b8e2c8f9ce9d8468f7752d2aa29bc0e55c63687ee97fa16fd1e5e029107773bf6ccbc4d5d4837de69b92a7e34b886f5fbfa8efb3587811e293cb6527f3
7
+ data.tar.gz: 144b48158964a4410792d43bf69386de53f291b329f34b66bca1fed8e909182dfd4284f162986601e6771ef0ba979d13c35b262a77fb99ba2cef863f39f7c718
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.0)
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.6)
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.2.0)
22
22
  ast (~> 2.4.1)
23
23
  racc (1.6.0)
24
- rainbow (3.0.0)
25
- rake (13.0.3)
26
- regexp_parser (2.1.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.5.0)
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.11.0)
29
+ rspec-core (~> 3.11.0)
30
+ rspec-expectations (~> 3.11.0)
31
+ rspec-mocks (~> 3.11.0)
32
+ rspec-core (3.11.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-expectations (3.11.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.11.0)
37
+ rspec-mocks (3.11.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.11.0)
40
+ rspec-support (3.11.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.18.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
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.0'
6
6
  end
7
7
  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.0
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-06-22 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: