rodf 0.3.7 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +77 -0
  3. data/LICENSE +7 -0
  4. data/README.md +244 -64
  5. data/Rakefile +9 -17
  6. data/lib/rodf/cell.rb +171 -0
  7. data/lib/rodf/column.rb +21 -0
  8. data/lib/rodf/container.rb +18 -0
  9. data/lib/rodf/data_style.rb +46 -0
  10. data/lib/rodf/document.rb +133 -0
  11. data/lib/rodf/hyperlink.rb +33 -0
  12. data/lib/rodf/master_page.rb +11 -0
  13. data/lib/rodf/page_layout.rb +41 -0
  14. data/lib/rodf/paragraph.rb +28 -0
  15. data/lib/rodf/paragraph_container.rb +30 -0
  16. data/lib/rodf/property.rb +138 -0
  17. data/lib/rodf/row.rb +56 -0
  18. data/lib/{odf → rodf}/skeleton/manifest.xml.erb +0 -0
  19. data/lib/{odf → rodf}/skeleton/styles.pxml +0 -0
  20. data/lib/rodf/skeleton.rb +17 -0
  21. data/lib/rodf/span.rb +45 -0
  22. data/lib/rodf/spreadsheet.rb +113 -0
  23. data/lib/rodf/style.rb +77 -0
  24. data/lib/rodf/style_section.rb +33 -0
  25. data/lib/rodf/tab.rb +17 -0
  26. data/lib/rodf/table.rb +95 -0
  27. data/lib/rodf/text.rb +126 -0
  28. data/lib/rodf/version.rb +3 -0
  29. data/lib/rodf.rb +31 -0
  30. metadata +76 -115
  31. data/CHANGELOG +0 -17
  32. data/Gemfile +0 -9
  33. data/LICENSE.LGPL +0 -166
  34. data/Manifest +0 -48
  35. data/lib/odf/cell.rb +0 -104
  36. data/lib/odf/column.rb +0 -33
  37. data/lib/odf/compatibility.rb +0 -25
  38. data/lib/odf/container.rb +0 -57
  39. data/lib/odf/data_style.rb +0 -45
  40. data/lib/odf/document.rb +0 -67
  41. data/lib/odf/hyperlink.rb +0 -51
  42. data/lib/odf/master_page.rb +0 -33
  43. data/lib/odf/page_layout.rb +0 -39
  44. data/lib/odf/paragraph.rb +0 -46
  45. data/lib/odf/paragraph_container.rb +0 -35
  46. data/lib/odf/property.rb +0 -131
  47. data/lib/odf/row.rb +0 -44
  48. data/lib/odf/skeleton.rb +0 -34
  49. data/lib/odf/span.rb +0 -70
  50. data/lib/odf/spreadsheet.rb +0 -64
  51. data/lib/odf/style.rb +0 -65
  52. data/lib/odf/style_section.rb +0 -42
  53. data/lib/odf/tab.rb +0 -38
  54. data/lib/odf/table.rb +0 -50
  55. data/lib/odf/text.rb +0 -66
  56. data/rodf.gemspec +0 -48
  57. data/spec/cell_spec.rb +0 -189
  58. data/spec/data_style_spec.rb +0 -61
  59. data/spec/file_storage_spec.rb +0 -47
  60. data/spec/hyperlink_spec.rb +0 -62
  61. data/spec/master_page_spec.rb +0 -35
  62. data/spec/page_layout_spec.rb +0 -45
  63. data/spec/paragraph_spec.rb +0 -75
  64. data/spec/property_spec.rb +0 -270
  65. data/spec/row_spec.rb +0 -59
  66. data/spec/skeleton_spec.rb +0 -33
  67. data/spec/span_spec.rb +0 -65
  68. data/spec/spec_helper.rb +0 -23
  69. data/spec/spreadsheet_spec.rb +0 -123
  70. data/spec/style_section_spec.rb +0 -42
  71. data/spec/style_spec.rb +0 -109
  72. data/spec/tab_spec.rb +0 -34
  73. data/spec/table_spec.rb +0 -90
  74. data/spec/text_spec.rb +0 -79
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b6df39b5334145e202ec1e0994066867281ff199
4
- data.tar.gz: b3aa43af22b6ff114d617ac829ca8b6f666e2d1d
2
+ SHA256:
3
+ metadata.gz: 97ad9b10231e7a1a3b553c83880873a7543cfad68b622c8a5884c177f997d208
4
+ data.tar.gz: 968d57b5f72cfe03350cd7a2f929224b34e857c41758d7b30cd7a4ae47367661
5
5
  SHA512:
6
- metadata.gz: c2e0fea2bb28a9b6255f2944b250a1e2e013edffdcdba0fd9626146ec9f4ad53b8b7e000657cb2eea967f4ee0bf77875535236215cf12c00671e5bba03191504
7
- data.tar.gz: d089d0a704c5339380b17827140a152ceff5c63dcdc9d00a4043e808f8b3c017cafd9fdba65f89325b43aaee2c13c95ae6d4d73c3249678b048622ca1ec3782c
6
+ metadata.gz: d44ab7bb0070d832969272e6b0f9350792a346e192a703d4c23708aecbdbf4347ad01eb0fc2a546407712394795d40f983c6162c078c084215cf1707a617f887
7
+ data.tar.gz: 13bdfa011468af90e8c475a1f02aa30a7f8c29b6e0a1c12f4849d15da0eb2d42a65eb9b853f9891ac4501901c25f7df8b9c2be6867bcba5c50d7abd40c688b6a
data/CHANGELOG.md ADDED
@@ -0,0 +1,77 @@
1
+ # CHANGELOG
2
+
3
+ ## Unreleased - [View Diff](https://github.com/westonganger/rodf/compare/v1.2.0..master)
4
+ - Nothing yet
5
+
6
+ ## v1.2.0 - [View Diff](https://github.com/westonganger/rodf/compare/v1.1.1..v1.2.0)
7
+ - Allow passing `:style` argument to `Span`
8
+ - Added `Table#set_column_widths` method
9
+ - Style `:family` option now supports String and Symbol keys. Previously `:family` the family option would only support the key name as a Symbol.
10
+ - Improve Style List Documentation
11
+ - Allow adding XML attributes directly to `Table`, `Row`, `Column`, `Cell` using the `:attributes` argument
12
+ - Remove usage of `contains` metaprogramming method `Container#contains` that was hiding major implementation details from most RODF classes. The code is now much easier to understand.
13
+
14
+ ## v1.1.1 - [View Diff](https://github.com/westonganger/rodf/compare/v1.1.0..v1.1.1)
15
+ - [Issue #36](https://github.com/westonganger/rodf/issues/36) - Improve compatibility for Float Cells
16
+ - Fix gemspec require for Ruby 1.9
17
+ - Cleanup File Commenting
18
+
19
+ ## v1.1.0 - [View Diff](https://github.com/westonganger/rodf/compare/v1.0.0..v1.1.0)
20
+ - [#34](https://github.com/westonganger/rodf/pull/34) - Add ability to add many rows or cells at once
21
+ - [#29](https://github.com/westonganger/rodf/pull/29) - Remove ActiveSupport dependency in favor of `dry-inflector`
22
+ - [#21](https://github.com/thiagoarrais/rodf/pull/21) - Allow more attributes for the `data_style` method
23
+ - [#19](https://github.com/westonganger/rodf/issues/19) - Changes to Date/Time handling
24
+
25
+ ## v1.0.0 - [View Diff](https://github.com/westonganger/rodf/compare/v0.3.7..v1.0.0)
26
+ - Rename main module ODF to RODF, Fix cell types
27
+
28
+ ## v0.3.7
29
+ - Add Rails 5 support to gemspec
30
+
31
+ ## v0.3.6
32
+ - Resulting ODF files as bitstrings
33
+
34
+ ## v0.3.5
35
+ - Allows current libraries
36
+
37
+ ## v0.3.4
38
+ - Procedural cell styling
39
+
40
+ ## v0.3.3
41
+ - Documents can now write themselves to disk
42
+
43
+ ## v0.3.2
44
+ - Fixes broken styles.xml
45
+
46
+ ## v0.3.1
47
+ - Adds support for conditional formatting
48
+
49
+ ## v0.3.0
50
+ - Allow parameterless blocks
51
+
52
+ ## v0.2.2
53
+ - Reintroduce compatibility with Ruby 1.8
54
+
55
+ ## v0.2.1
56
+ - Update to newer libraries and Ruby 1.9
57
+
58
+ ## v0.1.6
59
+ - Fix date handling
60
+
61
+ ## v0.1.5
62
+ - Bug fix
63
+
64
+ ## v0.1.4
65
+ - Allow the setting of an entire cell's content to a hyperlink (by Merul Patel)
66
+
67
+ ## v0.1.3
68
+ - Dependency fix (by Merul Patel)
69
+
70
+ ## v0.1.2
71
+ - Cell span
72
+
73
+ ## v0.1.1
74
+ - Basic spreadsheet styling support
75
+
76
+ ## v0.1.0
77
+ - First version with very basic support for spreadsheet generation
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2008 Thiago Arrais
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,100 +1,280 @@
1
- # rODF
1
+ # RODF
2
+ <a href="https://badge.fury.io/rb/rodf" target="_blank"><img height="21" style='border:0px;height:21px;' border='0' src="https://badge.fury.io/rb/rodf.svg" alt="Gem Version"></a>
3
+ <a href='https://github.com/westonganger/rodf/actions' target='_blank'><img src="https://github.com/westonganger/rodf/workflows/Tests/badge.svg" style="max-width:100%;" height='21' style='border:0px;height:21px;' border='0' alt="CI Status"></a>
4
+ <a href='https://rubygems.org/gems/rodf' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://ruby-gem-downloads-badge.herokuapp.com/rodf?label=rubygems&type=total&total_label=downloads&color=brightgreen' border='0' alt='RubyGems Downloads' /></a>
5
+ <a href='https://ko-fi.com/A5071NK' target='_blank'><img height='22' style='border:0px;height:22px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
2
6
 
3
- This is a library for writing to ODF output from Ruby. It currently only
4
- supports creating spreadsheets (ODS) and text documents (ODT). Slide shows (ODP)
5
- may be added some time in the future.
7
+ RODF is a library for writing to ODF output from Ruby. It mainly focuses creating ODS spreadsheets.
6
8
 
7
- Some knowledge of the [ODF spec](http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html) is required.
9
+ As well as writing ODS spreadsheets, this library also can write ODT text documents but it is undocumented and will require knowledge of the ODF spec. It currently does not support ODP Slide shows. Also this is NOT an ODF reading library.
8
10
 
9
- This is NOT an ODF reading library.
11
+ ## Install
10
12
 
11
- ### Installation
13
+ ```
14
+ gem install rodf
15
+ ```
12
16
 
13
- You should be able to install the latest stable version by saying something like
17
+ ## Usage
14
18
 
15
- gem install rodf
19
+ RODF works pretty much like Builder, but with ODF-aware constructs. For example:
16
20
 
17
- ### How do I use it?
21
+ ```ruby
22
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do
23
+ table 'My first table from Ruby' do
24
+ row do
25
+ cell 'Hello world!'
26
+ end
27
+ end
28
+ end
29
+ ```
18
30
 
19
- rODF works pretty much like Builder, but with ODF-aware constructs. Try this:
31
+ For access to variables and methods from outer code you can use block parameter:
20
32
 
21
- require 'odf/spreadsheet'
33
+ ```ruby
34
+ @data = 'Hello world!'
22
35
 
23
- ODF::Spreadsheet.file("my-spreadsheet.ods") do
24
- table 'My first table from Ruby' do
25
- row {cell 'Hello, rODF world!' }
26
- end
36
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
37
+ sheet.table 'My first table from Ruby' do |table|
38
+ table.row do |row|
39
+ row.cell @data
27
40
  end
41
+ end
42
+ end
43
+ ```
28
44
 
29
- Some basic formatting is also possible:
45
+ Adding many rows or cells at once is supported as well:
30
46
 
31
- require 'odf/spreadsheet'
47
+ ```ruby
48
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
49
+ sheet.table 'My first table from Ruby' do |t|
50
+ t.add_rows([
51
+ [1, 'Alice'],
52
+ [2, { value: 'Bob', color: '#ff0000'}],
53
+ [3, 'Carol']
54
+ ])
32
55
 
33
- ODF::Spreadsheet.file("my-spreadsheet.ods") do
34
- style 'red-cell', :family => :cell do
35
- property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
36
- end
37
- table 'Red text table' do
38
- row {cell 'Red', :style => 'red-cell' }
39
- end
56
+ t.row do |r|
57
+ r.add_cells ['ID', 'Name']
40
58
  end
59
+ end
60
+ end
61
+ ```
41
62
 
42
- Some basic conditional formatting is also possible:
63
+ ## Procedural style
43
64
 
44
- require 'odf/spreadsheet'
65
+ The declarative style shown above is just syntatic sugar. A more procedural style can also be used. Like so:
45
66
 
46
- ODF::Spreadsheet.file("my-spreadsheet.ods") do
67
+ ```ruby
68
+ ss = RODF::Spreadsheet.new
47
69
 
48
- office_style 'red-cell', :family => :cell do
49
- property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
50
- end
70
+ t = ss.table 'My first table from Ruby'
71
+ r = t.row
72
+ c = r.cell 'Hello world!'
51
73
 
52
- office_style 'green-cell', :family => :cell do
53
- property :text, 'font-weight' => 'bold', 'color' => '#00ff00'
54
- end
74
+ # two methods to write to file
75
+ ss.write_to 'my-spreadsheet.ods'
76
+ # or
77
+ File.write('my-spreadsheet.ods', ss.bytes) # you can send your data in Rails over HTTP using the bytes method
78
+ ```
55
79
 
56
- # conditional formating must be defined as style and the value of
57
- # apply-style-name must be an office_style
58
- style 'cond1', :family => :cell do
80
+ Both styles can be mixed and matched at will:
59
81
 
60
- property :conditional,
61
- 'condition' => 'cell-content()<0',
62
- 'apply-style-name' => 'red-cell'
82
+ ```ruby
83
+ ss = RODF::Spreadsheet.new
63
84
 
64
- property :conditional,
65
- 'condition' => 'cell-content()>0',
66
- 'apply-style-name' => 'green-cell'
67
- end
85
+ ss.table 'My first table from Ruby' do |t|
86
+ t.row do |r|
87
+ r.cell 'Hello world!'
88
+ end
89
+ end
90
+
91
+ ss.write_to 'my-spreadsheet.ods'
92
+ ```
93
+
94
+ ## Styling and Formatting
95
+
96
+ ```ruby
97
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
98
+
99
+ sheet.style 'red-cell', family: :cell do |s|
100
+ s.property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
101
+ end
68
102
 
69
- table 'Red text table' do
70
- row {cell 'Red force', :style => 'red-cell' }
71
- row {cell '-4', :type => :float, :style => 'cond1' }
72
- row {cell '0', :type => :float, :style => 'cond1' }
73
- row {cell '5', :type => :float, :style => 'cond1' }
103
+ sheet.style 'row-height', family: :row do |s|
104
+ s.property :row, 'row-height' => '18pt', 'use-optimal-row-height' => 'true'
105
+ end
106
+
107
+ sheet.table 'Red text table' do |t|
108
+ t.row style: 'row-height' do |r|
109
+ r.cell 'Red', style: 'red-cell'
110
+ end
111
+ end
112
+
113
+ sheet.table 'Text with Paragraphs' do |t|
114
+ t.row style: 'row-height' do |r|
115
+ r.cell do |cell|
116
+ cell.paragraph do |paragraph|
117
+ text_array = my_text_content.split("\n").select{|x| !x.empty? }
118
+
119
+ text_array.each do |str|
120
+ if str.start_with?("#")
121
+ paragraph.span(str, style: 'red-cell')
122
+ else
123
+ paragraph.span(str)
124
+ end
125
+ end
126
+ end
74
127
  end
75
128
  end
129
+ end
76
130
 
77
- ### Procedural style
131
+ end
132
+ ```
78
133
 
79
- The declarative style shown above is just syntatic sugar. A more procedural
80
- style can also be used. Like so:
134
+ Conditional formatting is also possible:
81
135
 
82
- require 'odf/spreadsheet'
136
+ ```ruby
137
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
83
138
 
84
- ss = ODF::Spreadsheet.new
85
- t = ss.table 'My first table from Ruby'
86
- r = t.row
87
- c = r.cell 'Hello, rODF world!'
139
+ sheet.office_style 'red-cell', family: :cell do |s|
140
+ s.property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
141
+ end
88
142
 
89
- ss.write_to 'my-spreadsheet.ods'
143
+ sheet.office_style 'green-cell', family: :cell do |s|
144
+ s.property :text, 'font-weight' => 'bold', 'color' => '#00ff00'
145
+ end
90
146
 
91
- Both styles can be mixed and matched at will:
147
+ # conditional formating must be defined as style and the value of
148
+ # apply-style-name must be an office_style
149
+ sheet.style 'cond1', family: :cell do |s|
150
+ s.property :conditional, 'condition' => 'cell-content()<0', 'apply-style-name' => 'red-cell'
92
151
 
93
- require 'odf/spreadsheet'
152
+ s.property :conditional, 'condition' => 'cell-content()>0', 'apply-style-name' => 'green-cell'
153
+ end
94
154
 
95
- ss = ODF::Spreadsheet.new
96
- ss.table 'My first table from Ruby' do
97
- row { cell 'Hello, rODF world!' }
155
+ sheet.table 'Red text table' do |t|
156
+ t.row do |r|
157
+ r.cell 'Red force', style: 'red-cell'
158
+ end
159
+ t.row do |r|
160
+ r.cell '-4', type: :float, style: 'cond1'
98
161
  end
162
+ t.row do |r|
163
+ r.cell '0', type: :float, style: 'cond1'
164
+ end
165
+ t.row do |r|
166
+ r.cell '5', type: :float, style: 'cond1'
167
+ end
168
+ end
169
+ end
170
+ ```
171
+
172
+ ## Changing Columns Widths
173
+
174
+ Adding columns or columns width to your spreadsheet can be done with the following
175
+
176
+ ```ruby
177
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
178
+ sheet.table "foo" do |t|
179
+ sheet.style('default-col-width', family: :column) do |s|
180
+ s.property(:column, 'column-width' => '1.234in')
181
+ end
182
+
183
+ col_count.times do
184
+ t.column style: 'default-col-width'
185
+ end
186
+
187
+ ### OR
188
+
189
+ ### Warning this will overwrite any existing table columns (cells remain unaffected)
190
+ sheet.set_column_widths(
191
+ table: t,
192
+ column_widths: [
193
+ {'column-width' => '1in'},
194
+ {'column-width' => '2cm'},
195
+ {'column-width' => '2.54cm'},
196
+ ],
197
+ )
198
+ end
199
+ end
200
+ ```
201
+
202
+ ## Columns Types
203
+
204
+ Available columns types are:
205
+
206
+ - :string
207
+ - :float
208
+ - :date
209
+ - :time
210
+ - :currency
211
+ - :percentage
212
+
213
+ ## Style List
214
+ ```ruby
215
+ ### family: :cell or "table-cell"
216
+ style "my-cell-style", family: :cell do
217
+ property :text,
218
+ 'font-weight' => :bold, #options are :bold, :thin
219
+ 'font-size' => 12,
220
+ 'font-name' => 'Arial',
221
+ 'font-style' => 'italic',
222
+ 'text-underline-style' => 'solid', # solid, dashed, dotted, double
223
+ 'text-underline-type' => 'single',
224
+ 'text-line-through-style' => 'solid',
225
+ align: true,
226
+ color: "#000000"
227
+
228
+ property :cell,
229
+ 'background-color' => "#DDDDDD",
230
+ 'wrap-option' => 'wrap',
231
+ 'vertical_align' => 'automatic',
232
+ 'border-top' => '0.75pt solid #999999',
233
+ 'border-bottom' => '0.75pt solid #999999',
234
+ 'border-left' => '0.75pt solid #999999',
235
+ 'border-right' => '0.75pt solid #999999',
236
+ 'writing-mode' => 'lr-tb',
237
+
238
+ end
239
+
240
+ ### family: :column or "table-column"
241
+ style('my-col-style', family: :column) do
242
+ property :column,
243
+ 'column-width' => '4.0cm'
244
+ end
245
+
246
+ ### family: :row or "table-row"
247
+ style('my-row-style', family: :row) do
248
+ property :row,
249
+ 'row-height' => '18pt',
250
+ 'use-optimal-row-height' => 'true'
251
+ end
252
+
253
+ ### family: :table
254
+ style('my-row-style', family: :table) do
255
+ property :table,
256
+ 'writing-mode' => 'lr-tb',
257
+ ```
258
+
259
+ ## Adding Arbitrary XML Attributes
260
+
261
+ ```ruby
262
+ RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
263
+ sheet.table 'My first table from Ruby', attributes: {'table-protected' => 'true'} do |table|
264
+ table.row do |row|
265
+ row.cell @data
266
+ end
267
+ end
268
+ end
269
+
270
+ ```
271
+
272
+ ## Production Usage Examples
273
+
274
+ - `spreadsheet_architect` gem - [View Relevant Source Code](https://github.com/westonganger/spreadsheet_architect/blob/master/lib/spreadsheet_architect/class_methods/ods.rb)
275
+
276
+ ## Credits
277
+
278
+ Originally Created by [@thiagoarrais](https://github.com/thiagoarrais)
99
279
 
100
- ss.write_to 'my-spreadsheet.ods'
280
+ Maintained by [@westonganger](https://github.com/westonganger) since 2016, for simplified ODS spreadsheet creation within the [spreadsheet_architect](https://github.com/westonganger/spreadsheet_architect) gem
data/Rakefile CHANGED
@@ -1,23 +1,15 @@
1
- require 'rubygems'
1
+ require File.expand_path(File.dirname(__FILE__) + '/lib/rodf/version.rb')
2
+ require 'bundler/gem_tasks'
2
3
  require 'rspec/core/rake_task'
3
-
4
+
4
5
  RSpec::Core::RakeTask.new
5
- task :default => :spec
6
+
6
7
  task :test => :spec
8
+ task :default => :test
7
9
 
8
- require 'echoe'
9
- Echoe.new('rodf') do |gem|
10
- gem.author = ["Weston Ganger", "Thiago Arrais", "Foivos Zakkak"]
11
- gem.email = "thiago.arrais@gmail.com"
12
- gem.url = "http://github.com/thiagoarrais/rodf/tree"
13
- gem.summary = "ODF generation library for Ruby"
10
+ task :console do
11
+ require 'rodf'
14
12
 
15
- gem.runtime_dependencies = [
16
- ["builder", "~> 3.0"],
17
- ["rubyzip", "~> 1.0"],
18
- ["activesupport", ">= 3.0", "< 6.0"]]
19
- gem.development_dependencies = [
20
- ["rspec", "~> 2.9"],
21
- ["rspec_hpricot_matchers" , "~> 1.0"],
22
- ["echoe" , "~> 4.6"]]
13
+ require 'irb'
14
+ binding.irb
23
15
  end
data/lib/rodf/cell.rb ADDED
@@ -0,0 +1,171 @@
1
+ module RODF
2
+ class Cell < Container
3
+
4
+ def initialize(value=nil, opts={})
5
+ super
6
+
7
+ if value.is_a?(Hash)
8
+ opts = value
9
+ value = opts[:value]
10
+ else
11
+ if value.is_a?(String)
12
+ value = value.strip
13
+ end
14
+ opts = {} unless opts.is_a?(Hash)
15
+ end
16
+
17
+ @value = value || ''
18
+
19
+ @type = opts[:type]
20
+
21
+ unless empty?(@value)
22
+ @url = opts[:url]
23
+
24
+ if !@type
25
+ if @value.is_a?(Numeric)
26
+ @type = :float
27
+ elsif @value.respond_to?(:strftime)
28
+ ### for auto type inference force :date type because :time doesnt store any date info
29
+ @type = :date
30
+ else
31
+ @type = :string
32
+ @value = @value.to_s
33
+ end
34
+ end
35
+ end
36
+
37
+ ### TODO: set default DataStyle for the Spreadsheet for Date / Time / DateTime cells formatting
38
+
39
+ @elem_attrs = make_element_attributes(@type, @value, opts)
40
+
41
+ if opts[:attributes]
42
+ @elem_attrs.merge!(opts[:attributes])
43
+ end
44
+
45
+ @multiplier = (opts[:span] || 1).to_i
46
+
47
+ make_value_paragraph
48
+ end
49
+
50
+ def paragraphs
51
+ @paragraphs ||= []
52
+ end
53
+
54
+ def paragraph(*args, &block)
55
+ x = Paragraph.new(*args, &block)
56
+
57
+ paragraphs << x
58
+
59
+ return x
60
+ end
61
+ alias p paragraph
62
+
63
+ def paragraphs_xml
64
+ paragraphs.map(&:xml).join
65
+ end
66
+
67
+ def add_paragraphs(*elements)
68
+ if elements.first.is_a?(Array)
69
+ elements = elements.first
70
+ end
71
+
72
+ elements.each do |element|
73
+ paragraph(element)
74
+ end
75
+ end
76
+
77
+ def style=(style_name)
78
+ @elem_attrs['table:style-name'] = style_name
79
+ end
80
+
81
+ def xml
82
+ markup = Builder::XmlMarkup.new
83
+
84
+ text = markup.tag! 'table:table-cell', @elem_attrs do |xml|
85
+ xml << paragraphs_xml
86
+ end
87
+
88
+ (@multiplier - 1).times do
89
+ text = markup.tag! 'table:table-cell'
90
+ end
91
+
92
+ text
93
+ end
94
+
95
+ def contains_url?
96
+ !empty?(@url)
97
+ end
98
+
99
+ private
100
+
101
+ def make_element_attributes(type, value, opts)
102
+ attrs = {}
103
+
104
+ if !empty?(value) || !opts[:formula].nil? || type == :string
105
+ attrs['office:value-type'] = type
106
+ end
107
+
108
+ if type != :string && !empty?(value)
109
+ case type
110
+ when :date
111
+ attrs['office:date-value'] = value
112
+ when :time
113
+ attrs['office:time-value'] = value
114
+ else ### :float, :percentage, :currency
115
+ attrs['office:value'] = value
116
+ end
117
+ end
118
+
119
+ unless opts[:formula].nil?
120
+ attrs['table:formula'] = opts[:formula]
121
+ end
122
+
123
+ unless opts[:style].nil?
124
+ attrs['table:style-name'] = opts[:style]
125
+ end
126
+
127
+ unless opts[:span].nil?
128
+ attrs['table:number-columns-spanned'] = opts[:span]
129
+ end
130
+
131
+ if opts[:matrix_formula]
132
+ attrs['table:number-matrix-columns-spanned'] = 1
133
+ attrs['table:number-matrix-rows-spanned'] = 1
134
+ end
135
+
136
+ return attrs
137
+ end
138
+
139
+ def make_value_paragraph
140
+ if !empty?(@value)
141
+ case @type
142
+ when :float
143
+ ### https://github.com/westonganger/rodf/issues/36
144
+ value = @value
145
+ paragraph do
146
+ self << value
147
+ end
148
+ when :string
149
+ cell, value, url = self, @value, @url
150
+
151
+ # Split out newlines to be new cells since the text has been escaped at this point
152
+ value.to_s.split("\n").each do |split_value|
153
+ paragraph do
154
+ if cell.contains_url?
155
+ link split_value, href: url
156
+ else
157
+ self << split_value
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ def empty?(value)
166
+ value.respond_to?(:empty?) ? value.empty? : value.nil?
167
+ #respond_to?(:empty?) ? (value.empty? || value =~ /\A[[:space:]]*\z/) : value.nil?
168
+ end
169
+
170
+ end
171
+ end
@@ -0,0 +1,21 @@
1
+ module RODF
2
+ class Column
3
+ def initialize(opts={})
4
+ @elem_attrs = {}
5
+
6
+ @elem_attrs['table:style-name'] = opts[:style] unless opts[:style].nil?
7
+
8
+ if opts[:attributes]
9
+ @elem_attrs.merge!(opts[:attributes])
10
+ end
11
+ end
12
+
13
+ def style=(style_name)
14
+ @elem_attrs['table:style-name'] = style_name
15
+ end
16
+
17
+ def xml
18
+ Builder::XmlMarkup.new.tag! 'table:table-column', @elem_attrs
19
+ end
20
+ end
21
+ end