docx-builder 0.4.1 → 0.5.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: a1c29b476ddb4b801b85285265394883fb2464c0a5bb1cb816ecfcfe184e9f4b
4
- data.tar.gz: d492f70245fa1043bbdbe864c9c6a2e227d4d0900610de8dbe990dcfc42af2b7
3
+ metadata.gz: a68fa018e7eff7d96adbc7a22694d5956be07291ce4c10f37ac08466ae570e7b
4
+ data.tar.gz: 408c92d9eb92649a0abca37d76a95ffa211b16b648c281403c45abffd1df6e1a
5
5
  SHA512:
6
- metadata.gz: 11dca49bb7b0d8ce52b0fc7345af69d695cd9fc7a9165cf79e62c5979a7b3e82bd0d63a9370bea9ae3eed1a2ca9b4779975d2f9e560dc815a0aeacc84d886697
7
- data.tar.gz: 95a102b5f72f34fc32e6be1cc37738681cb075a6e5237a7e79432b7e465fc5032721c026a0004fba1bd2324ddc907d0b6bacb02bb8ed1831f668b42adb3f27f7
6
+ metadata.gz: f14b6e7a229950875201cfc3ddccc2445b633dff0612c4945e7354ece5d02c976156fa5d73a1bdbe26327d7d6d1903d21f42b5f2bfc6b15923065b24f80a7225
7
+ data.tar.gz: 497ccc0ebc2f1fab14ca859c95f97f2889540db9f0ac448e1b4069d8db026f7716d8c13196878af9027356d9ab4fda3889dd8347a1bacb86e4b01fa0aacb0d60
data/.rubocop.yml CHANGED
@@ -1,6 +1,9 @@
1
1
  Gemspec/RequiredRubyVersion:
2
2
  Enabled: false
3
3
 
4
+ Layout/EndOfLine:
5
+ Enabled: false
6
+
4
7
  Layout/LineLength:
5
8
  Max: 160
6
9
 
@@ -9,6 +12,11 @@ Metrics/BlockLength:
9
12
  CountComments: false
10
13
  Max: 50
11
14
 
15
+ Metrics/MethodLength:
16
+ CountAsOne: ['array', 'hash']
17
+ CountComments: false
18
+ Max: 10
19
+
12
20
  Style/StringLiterals:
13
21
  Enabled: true
14
22
  EnforcedStyle: single_quotes
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in docx-builder.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'i18n'
8
9
  gem 'nokogiri', '~> 1.13.2'
9
10
  gem 'rake', '~> 13.0'
10
11
  gem 'rubyzip', require: 'zip'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- docx-builder (0.4.1)
4
+ docx-builder (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -12,7 +12,10 @@ GEM
12
12
  bundler (>= 1.2.0, < 3)
13
13
  thor (~> 1.0)
14
14
  byebug (11.1.3)
15
+ concurrent-ruby (1.1.10)
15
16
  diff-lcs (1.5.0)
17
+ i18n (1.12.0)
18
+ concurrent-ruby (~> 1.0)
16
19
  mini_portile2 (2.8.0)
17
20
  nokogiri (1.13.10)
18
21
  mini_portile2 (~> 2.8.0)
@@ -62,6 +65,7 @@ DEPENDENCIES
62
65
  bundler-audit
63
66
  byebug
64
67
  docx-builder!
68
+ i18n
65
69
  nokogiri (~> 1.13.2)
66
70
  rake (~> 13.0)
67
71
  rspec (~> 3.0)
data/README.md CHANGED
@@ -24,13 +24,18 @@ In order to render a table on docx template, we wrote:
24
24
  {{t config t}}
25
25
  ```
26
26
 
27
- where "config" is a hash that contains two properties:
27
+ where "config" is a hash that may contain five properties:
28
28
 
29
29
  * columns: an array of hashes. Each element represents a column and may have the following information:
30
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)
31
+ * width (required): specifies the value of the preferred width of the column, as a percentual of the table width;
32
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.
33
+ * rows: an array of hashes. Each element represents a row and all of its properties must appear in the columns' array.
34
+ * table_width: preferred width of the table in pixels. Default value: 670 pixels
35
+ * font_family (optional): font family of the text displayed on a table. Default: Arial
36
+ * font_size (optional): font size of the text displayed on a table. Default: 12
37
+
38
+ At this moment, table width, font family and font size are the only table customizations available (after version 0.5.0)
34
39
 
35
40
  ## Installation
36
41
 
@@ -57,20 +62,20 @@ Or install it yourself as:
57
62
  # or memory_stream = builder.save_as_buffer(new_xml)
58
63
  ```
59
64
 
60
- An exemple of table configuration should be:
65
+ An example of table configuration should be:
61
66
 
62
67
  ```ruby
63
- users_table = {
68
+ {
64
69
  columns: [
65
70
  {
66
71
  name: :name,
67
72
  description: 'Name',
68
- width: 1150
73
+ width: 50
69
74
  },
70
75
  {
71
76
  name: :email,
72
77
  description: 'Mail address',
73
- width: 1150
78
+ width: 50
74
79
  }
75
80
  ],
76
81
  rows: [
@@ -86,10 +91,32 @@ An exemple of table configuration should be:
86
91
  name: 'Felipe',
87
92
  email: 'felipe@example.com'
88
93
  }
89
- ]
94
+ ],
95
+ table_width: 400,
96
+ font_family: 'Cambria',
97
+ font_size: 14
90
98
  }
91
99
  ```
92
100
 
101
+ You can also use a helper to generate the hash presented above. For this example, you may specify the description for each column in an internationalization file (i18n), otherwise its name (symbol) will be used instead.
102
+
103
+ ```ruby
104
+ people = [
105
+ { name: 'João', email: 'joao@example.com' },
106
+ { name: 'José', email: 'jose@example.com' },
107
+ { name: 'Felipe', email: 'felipe@example.com' }
108
+ ]
109
+ Docx::Utils.build_table_config_from_array(people, table_width: 400, font_family: 'Cambria', font_size: 14)
110
+ ```
111
+
112
+ ```yaml
113
+ docx_builder:
114
+ tables:
115
+ columns:
116
+ name: Name
117
+ email: Mail Address
118
+ ```
119
+
93
120
  ## Contributing
94
121
 
95
122
  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).
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Docx
4
4
  module Builder
5
- VERSION = '0.4.1'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
data/lib/docx/builder.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'i18n'
4
+ require 'nokogiri'
3
5
  require 'rubygems'
4
6
  require 'zip'
5
- require 'nokogiri'
6
7
 
7
8
  require_relative 'logger'
9
+ require_relative 'utils'
8
10
  require_relative 'builder/version'
9
11
  require_relative 'builder/decoder'
10
12
  require_relative 'builder/encoder'
data/lib/docx/utils.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Docx
4
+ # Module containing some useful methods to help using this gem
5
+ module Utils
6
+ # Build a table config object from an array of hashes
7
+ def self.build_table_config_from_array(values, options = {})
8
+ options[:table_width] ||= 670
9
+ options[:font_family] ||= 'Arial'
10
+ options[:font_size] ||= 12
11
+
12
+ properties = values.flat_map(&:keys).uniq
13
+ average_column_width = 100.0 / properties.count
14
+ {
15
+ columns: properties.map do |p|
16
+ {
17
+ name: p,
18
+ description: I18n.t(p, scope: 'docx_builder.tables.columns', default: p.to_s),
19
+ width: average_column_width
20
+ }
21
+ end,
22
+ rows: values
23
+ }.merge(options)
24
+ end
25
+ end
26
+ end
@@ -1,6 +1,6 @@
1
1
  <w:tbl>
2
2
  <w:tblPr>
3
- <w:tblW w:w="10006" w:type="dxa"/>
3
+ <w:tblW w:w="<%= data[:table_width].to_i * 15 %>" w:type="dxa"/>
4
4
  <w:tblInd w:w="-626" w:type="dxa"/>
5
5
  <w:tblCellMar>
6
6
  <w:left w:w="70" w:type="dxa"/>
@@ -10,7 +10,7 @@
10
10
  </w:tblPr>
11
11
  <w:tblGrid>
12
12
  <% data[:columns].each do |column| %>
13
- <w:gridCol w:w="<%= column[:width] %>"/>
13
+ <w:gridCol w:w="<%= (column[:width].to_f / 100) * data[:table_width].to_i * 15 %>"/>
14
14
  <% end %>
15
15
  </w:tblGrid>
16
16
  <w:tr w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidTr="00E653AF">
@@ -20,36 +20,27 @@
20
20
  <% data[:columns].each do |column| %>
21
21
  <w:tc>
22
22
  <w:tcPr>
23
- <w:tcW w:w="<%= column[:width] %>" w:type="dxa"/>
23
+ <w:tcW w:w="<%= (column[:width].to_f / 100) * data[:table_width].to_i * 15 %>" w:type="dxa"/>
24
24
  <w:tcBorders>
25
25
  <w:top w:val="single" w:sz="4" w:space="0" w:color="000000"/>
26
26
  <w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
27
27
  <w:bottom w:val="single" w:sz="8" w:space="0" w:color="000000"/>
28
28
  <w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/>
29
29
  </w:tcBorders>
30
- <w:shd w:val="clear" w:color="000000" w:fill="FFFFFF"/>
31
- <w:noWrap/>
32
30
  <w:vAlign w:val="center"/>
33
- <w:hideMark/>
34
31
  </w:tcPr>
35
32
  <w:p w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidRDefault="00080F99" w:rsidP="00E653AF">
36
33
  <w:pPr>
37
34
  <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
35
  </w:pPr>
46
36
  <w:r w:rsidRPr="00E71341">
47
37
  <w:rPr>
48
- <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
38
+ <w:rFonts w:ascii="<%= data[:font_family] %>" w:hAnsi="<%= data[:font_family] %>" w:cs="<%= data[:font_family] %>"/>
49
39
  <w:b/>
50
40
  <w:bCs/>
51
41
  <w:color w:val="000000"/>
52
42
  <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
43
+ <w:sz w:val="<%= data[:font_size].to_i * 2 %>" />
53
44
  </w:rPr>
54
45
  <w:t><%= column[:description] %></w:t>
55
46
  </w:r>
@@ -65,32 +56,25 @@
65
56
  <% data[:columns].each do |column| %>
66
57
  <w:tc>
67
58
  <w:tcPr>
68
- <w:tcW w:w="<%= column[:width] %>" w:type="dxa"/>
59
+ <w:tcW w:w="<%= (column[:width].to_f / 100) * data[:table_width].to_i * 15 %>" w:type="dxa"/>
69
60
  <w:tcBorders>
70
61
  <w:top w:val="single" w:sz="4" w:space="0" w:color="000000"/>
71
62
  <w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
72
63
  <w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000"/>
73
64
  <w:right w:val="single" w:sz="4" w:space="0" w:color="000000"/>
74
65
  </w:tcBorders>
75
- <w:shd w:val="clear" w:color="000000" w:fill="FFFFFF"/>
76
- <w:noWrap/>
77
66
  <w:vAlign w:val="center"/>
78
- <w:hideMark/>
79
67
  </w:tcPr>
80
68
  <w:p w:rsidR="00080F99" w:rsidRPr="00E71341" w:rsidRDefault="005549E3" w:rsidP="00E653AF">
81
69
  <w:pPr>
82
70
  <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
71
  </w:pPr>
89
72
  <w:r>
90
73
  <w:rPr>
91
- <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
74
+ <w:rFonts w:ascii="<%= data[:font_family] %>" w:hAnsi="<%= data[:font_family] %>" w:cs="<%= data[:font_family] %>"/>
92
75
  <w:color w:val="000000"/>
93
76
  <w:lang w:val="pt-BR" w:eastAsia="pt-BR"/>
77
+ <w:sz w:val="<%= data[:font_size].to_i * 2 %>" />
94
78
  </w:rPr>
95
79
  <w:t><%= row[column[:name]] %></w:t>
96
80
  </w:r>
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.4.1
4
+ version: 0.5.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-12-19 00:00:00.000000000 Z
11
+ date: 2022-12-20 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
+ - lib/docx/utils.rb
43
44
  - templates/table.xml.erb
44
45
  - tmp/.gitkeep
45
46
  homepage: https://github.com/cash-me/docx-builder