Spreadsheet-HTML 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f87b9dc34a54719ccd396dd5e6c45234f920f4b
4
+ data.tar.gz: 376181b18aedfd03c3f19adf58217bb775c867c8
5
+ SHA512:
6
+ metadata.gz: 13c66aed213cabad46d6db74c3bedffc8cf64d5a47cd93ff03322df308e5afc33551c9fa2e3d6cf2109a78e367d474ced40fc067e01cf9ca88b29736e649fab5
7
+ data.tar.gz: 999297ba319055f39163cd2d6f495c512a830a7d5b54cff557f595b8e8b646493c757a7196ea5e1f9ca0df1a8386f97ac90157e3e35676f72be24011ba06ec2a
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ pkg/
3
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ install: gem install HTML-AutoTag
3
+ rvm:
4
+ - 2.1.0
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ # - 1.8.7
8
+ # - jruby-18mode
data/Changes ADDED
@@ -0,0 +1,4 @@
1
+ Revision history for html-autotag-ruby
2
+
3
+ 0.0.1
4
+ - First version, released on an unsuspecting world.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Spreadsheet-HTML.gemspec
4
+ gemspec
data/License.md ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 Jeff Anderson
2
+ ================================
3
+
4
+ MIT License
5
+ -----------
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ Dir.glob( './t/*.rb' ).each { |file| require file }
7
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'Spreadsheet/HTML/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "Spreadsheet-HTML"
8
+ spec.version = Spreadsheet::HTML::VERSION
9
+ spec.authors = ["jeffa"]
10
+ spec.email = ["jeffa@cpan.org"]
11
+ spec.description = %q{Just another HTML table generator}
12
+ spec.summary = %q{Generate HTML tables with ease (HTML4, XHTML and HTML5).}
13
+ spec.homepage = "https://github.com/jeffa/spreadsheet-html-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency 'rake', '~> 0'
23
+ spec.add_runtime_dependency 'HTML-AutoTag', '~> 1.0', '>= 1.0.1'
24
+ end
@@ -0,0 +1,204 @@
1
+ require "Spreadsheet/HTML/version"
2
+ require "HTML/AutoTag"
3
+
4
+ # i cannot hasa instantiate HTML::AutoTag inside Spreadsheet::HTML?!?
5
+ module Auto
6
+ class Tag < HTML::AutoTag
7
+ end
8
+ end
9
+
10
+ module Spreadsheet
11
+ class HTML
12
+
13
+ def self.gen( *args )
14
+ self.new.generate( *args )
15
+ end
16
+
17
+ def north( *args )
18
+ generate( *args, 'theta' => 0 )
19
+ end
20
+
21
+ def portrait( *args )
22
+ generate( *args, 'theta' => 0 )
23
+ end
24
+
25
+ def west( *args )
26
+ generate( *args, 'theta' => -270, 'tgroups' => 0 )
27
+ end
28
+
29
+ def landscape( *args )
30
+ generate( *args, 'theta' => -270, 'tgroups' => 0 )
31
+ end
32
+
33
+ def east( *args )
34
+ generate( *args, 'theta' => 90, 'tgroups' => 0, 'pinhead' => 1 )
35
+ end
36
+
37
+ def south( *args )
38
+ generate( *args, 'theta' => -180, 'tgroups' => 0, 'pinhead' => 1 )
39
+ end
40
+
41
+ def generate( *args )
42
+ params = _process( args )
43
+
44
+ if params['theta'] and params['flip']
45
+ params['theta'] *= -1
46
+ end
47
+
48
+ if !params['theta'] # north
49
+ if params['flip']
50
+ params['data'] = params['data'].map {|a| a.reverse }
51
+ end
52
+
53
+ elsif params['theta'] == 90 # east
54
+ if params['pinhead'] and !params['headless']
55
+ params['data'] = params['data'].transpose.map{|a| a.push( a.shift ) }
56
+ else
57
+ params['data'] = params['data'].transpose.map{|a| a.reverse }
58
+ end
59
+
60
+ elsif params['theta'] == -90
61
+ if params['pinhead'] and !params['headless']
62
+ params['data'] = params['data'].transpose.reverse.map {|a| a.push( a.shift ) }
63
+ else
64
+ params['data'] = params['data'].transpose.reverse.map {|a| a.reverse }
65
+ end
66
+
67
+ elsif params['theta'] == 180
68
+ if params['pinhead'] and !params['headless']
69
+ params['data'] = params['data'].push( params['data'].shift ).map {|a| a.reverse }
70
+ else
71
+ params['data'] = params['data'].reverse.map {|a| a.reverse }
72
+ end
73
+
74
+ elsif params['theta'] == -180 # south
75
+ if params['pinhead'] and !params['headless']
76
+ params['data'] = params['data'].push( params['data'].shift )
77
+ else
78
+ params['data'] = params['data'].reverse
79
+ end
80
+
81
+ elsif params['theta'] == 270
82
+ params['data'] = params['data'].transpose.reverse
83
+
84
+ elsif params['theta'] == -270 # west
85
+ params['data'] = params['data'].transpose
86
+
87
+ end
88
+
89
+ return _make_table( params )
90
+ end
91
+
92
+ def initialize( *args )
93
+ (args[0] || []).each do |key,val|
94
+ self.instance_eval { class << self; self end }.send(:attr_accessor, key)
95
+ self.send( "#{key}=", val )
96
+ end
97
+ end
98
+
99
+ def _make_table( params )
100
+ cdata = [] # TODO: insert caption and colgroup
101
+
102
+ if params['tgroups'] && params['tgroups'] > 0
103
+
104
+ body = params['data']
105
+ head = body.shift() unless params['matrix'] && data.size() > 2
106
+ foot = body.pop() if !params['matrix'] && params['tgroups'] > 1 and data.size() > 2
107
+
108
+ head_row = { 'tag' => 'tr', 'attr' => params['thead.tr'] || {}, 'cdata' => head }
109
+ foot_row = { 'tag' => 'tr', 'attr' => params['tfoot.tr'] || {}, 'cdata' => foot }
110
+ body_rows = body.map{ |r| { 'tag' => 'tr', 'attr' => params['tr'] || {}, 'cdata' => r } }
111
+
112
+ cdata.push({ 'tag' => 'thead', 'attr' => params['thead'] || {}, 'cdata' => head_row }) if head
113
+ cdata.push({ 'tag' => 'tfoot', 'attr' => params['tfoot'] || {}, 'cdata' => foot_row }) if foot
114
+ cdata.push({ 'tag' => 'tbody', 'attr' => params['tbody'] || {}, 'cdata' => body_rows })
115
+
116
+ else
117
+ cdata.push( params['data'].map { |c| { 'tag' => 'tr', 'attr' => params['tr'] || {}, 'cdata' => c } } )
118
+ end
119
+
120
+ return params['auto'].tag( 'tag' => 'table', 'attr' => params['table'] || {}, 'cdata' => cdata )
121
+ end
122
+
123
+ def _process( args )
124
+ params = _args( args )
125
+
126
+ # headings is an alias for -r0
127
+ params['-r0'] = params['headings']
128
+
129
+ index = {}
130
+ if params['data'][0].size()
131
+ # implement index mapping
132
+ end
133
+
134
+ data = []
135
+ empty = params.has_key?('empty') ? params['empty'] : '&nbsp'
136
+ tag = ( params['matrix'] or params['headless'] ) ? 'td' : 'th'
137
+
138
+ params['data'].each do |row|
139
+ r = []
140
+ row.each do |col|
141
+ r.push( { 'tag' => tag, 'attr' => params[tag] || {}, 'cdata' => col.to_s } )
142
+ end
143
+ data.push( r )
144
+ tag = 'td'
145
+ end
146
+
147
+ params['data'] = data
148
+ params['data'].shift if params['headless']
149
+
150
+ return params
151
+ end
152
+
153
+ def _args( things )
154
+
155
+ data = []
156
+ args = []
157
+ while !things.empty?
158
+
159
+ if things[0].kind_of?(Array)
160
+ data.push( things.shift() )
161
+ if things[0].kind_of?(Array)
162
+ data.push( things.shift() )
163
+ else
164
+ args.push( things.shift() ) if !things.empty?
165
+ args.push( things.shift() ) if !things.empty?
166
+ end
167
+ else
168
+ args.push( things.shift() ) if !things.empty?
169
+ args.push( things.shift() ) if !things.empty?
170
+ end
171
+
172
+ end
173
+
174
+ params = {}
175
+ self.instance_variables.each do |attr|
176
+ params[attr[1..-1]] = self.instance_variable_get attr
177
+ end
178
+
179
+ (args || []).each do |hash|
180
+ hash.each do |key,val|
181
+ params[key] = val
182
+ end
183
+ end
184
+
185
+ params['auto'] = Auto::Tag.new(
186
+ 'encodes' => params['encodes'],
187
+ 'indent' => params['indent'],
188
+ 'level' => params['level'],
189
+ 'sorted' => params['attr_sort'],
190
+ )
191
+
192
+ if !params['data'] and data[0].kind_of?(Array)
193
+ data = [ data ] if !data[0][0].kind_of?(Array)
194
+ params['data'] = data[0]
195
+ end
196
+
197
+ params['data'] = params['data'].clone
198
+
199
+ return params
200
+ end
201
+
202
+ end
203
+
204
+ end
@@ -0,0 +1,5 @@
1
+ module Spreadsheet
2
+ class HTML
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/readme.md ADDED
@@ -0,0 +1,34 @@
1
+ Spreadsheet-HTML (ruby)
2
+ =======================
3
+ Just another HTML table generator for ruby.
4
+
5
+ Description
6
+ -----------
7
+ Generate HTML tables with ease (HTML4, XHTML and HTML5).
8
+
9
+ Synopsis
10
+ --------
11
+ ```ruby
12
+ require 'Spreadsheet/HTML'
13
+
14
+ data = Array[ [1,2,3], [4,5,6] ]
15
+ generator = Spreadsheet::HTML.new()
16
+ puts generator.generate( data )
17
+ puts generator.generate( 'data' => data )
18
+
19
+ generator = Spreadsheet::HTML.new( 'data' => data )
20
+ puts generator.portrait()
21
+ puts generator.landscape()
22
+ ```
23
+
24
+ Installation
25
+ ------------
26
+ We are currently pre-Alpha, so installation is not recommended.
27
+
28
+ * rake test
29
+ * rake build
30
+ * ~~rake install~~ :skull:
31
+
32
+ License and Copyright
33
+ ---------------------
34
+ See [License](License.md).
data/t/01-load.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "test/unit"
2
+
3
+ class TestLoad < Test::Unit::TestCase
4
+
5
+ def test_load
6
+ assert_nothing_raised { require "Spreadsheet/HTML.rb" }
7
+ end
8
+
9
+ end
data/t/02-run.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "test/unit"
2
+ require "Spreadsheet/HTML"
3
+
4
+ class Test_Run < Test::Unit::TestCase
5
+
6
+ def test_instance
7
+
8
+ data = Array[[1,'a'], [2,'b']]
9
+ html = '<table><tr><th>1</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></table>'
10
+
11
+ assert_equal(
12
+ html,
13
+ Spreadsheet::HTML.new( 'data' => data ).generate(),
14
+ "via constructor"
15
+ )
16
+
17
+ gen = Spreadsheet::HTML.new
18
+
19
+ assert_equal(
20
+ html,
21
+ gen.generate( [1,'a'], [2,'b'] ),
22
+ "two array refs"
23
+ )
24
+
25
+ assert_equal(
26
+ html,
27
+ gen.generate( data ),
28
+ "one array ref"
29
+ )
30
+
31
+ assert_equal(
32
+ html,
33
+ gen.generate( 'data' => data ),
34
+ "one named arg"
35
+ )
36
+
37
+ end
38
+
39
+ def test_class
40
+
41
+ data = Array[[1,'a'], [2,'b']]
42
+ html = '<table><tr><th>1</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></table>'
43
+
44
+ assert_equal(
45
+ html,
46
+ Spreadsheet::HTML.gen( [1,'a'], [2,'b'] ),
47
+ "two array refs"
48
+ )
49
+
50
+ assert_equal(
51
+ html,
52
+ Spreadsheet::HTML.gen( data ),
53
+ "one array ref"
54
+ )
55
+
56
+ assert_equal(
57
+ html,
58
+ Spreadsheet::HTML.gen( 'data' => data ),
59
+ "one named arg"
60
+ )
61
+
62
+ end
63
+
64
+ end
data/t/03-matrix.rb ADDED
@@ -0,0 +1,70 @@
1
+ require "test/unit"
2
+ require "Spreadsheet/HTML"
3
+
4
+ class Test_Matrix < Test::Unit::TestCase
5
+
6
+ def test_instance
7
+
8
+ data = Array[[1,'a'], [2,'b']]
9
+ html = '<table><tr><td>1</td><td>a</td></tr><tr><td>2</td><td>b</td></tr></table>'
10
+
11
+ assert_equal(
12
+ html,
13
+ Spreadsheet::HTML.new( 'data' => data, 'matrix' => 1 ).generate(),
14
+ "via constructor only"
15
+ )
16
+
17
+ assert_equal(
18
+ html,
19
+ Spreadsheet::HTML.new( 'data' => data ).generate( 'matrix' => 1 ),
20
+ "via constructor and method"
21
+ )
22
+
23
+ gen = Spreadsheet::HTML.new
24
+
25
+ assert_equal(
26
+ html,
27
+ gen.generate( [1,'a'], [2,'b'], 'matrix' => 1 ),
28
+ "two array refs"
29
+ )
30
+
31
+ assert_equal(
32
+ html,
33
+ gen.generate( data, 'matrix' => 1 ),
34
+ "one array ref"
35
+ )
36
+
37
+ assert_equal(
38
+ html,
39
+ gen.generate( 'data' => data, 'matrix' => 1 ),
40
+ "one named arg"
41
+ )
42
+
43
+ end
44
+
45
+ def test_class
46
+
47
+ data = Array[[1,'a'], [2,'b']]
48
+ html = '<table><tr><td>1</td><td>a</td></tr><tr><td>2</td><td>b</td></tr></table>'
49
+
50
+ assert_equal(
51
+ html,
52
+ Spreadsheet::HTML.gen( [1,'a'], [2,'b'], 'matrix' => 1 ),
53
+ "two array refs"
54
+ )
55
+
56
+ assert_equal(
57
+ html,
58
+ Spreadsheet::HTML.gen( data, 'matrix' => 1 ),
59
+ "one array ref"
60
+ )
61
+
62
+ assert_equal(
63
+ html,
64
+ Spreadsheet::HTML.gen( 'data' => data, 'matrix' => 1 ),
65
+ "one named arg"
66
+ )
67
+
68
+ end
69
+
70
+ end