thamble 1.1.0 → 1.2.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: be087dbd34683e86222c190ef4249057adfe43faad8e372cea1e498d8a62a5cf
4
- data.tar.gz: 3bf1c710554f51d5573063d8814791b99257f554215eb53303df1f0a7096d82f
3
+ metadata.gz: 292f4d36d629c5774c43a3df0b0ef11364aea32d3accf6d31c83cb31756600a4
4
+ data.tar.gz: 535958ace8a281764f54030d367579d5b551e7d7e915a04e60f3dc51481857ac
5
5
  SHA512:
6
- metadata.gz: 0ea505f39ff8901cd49c1d65627ae0be026cb564dbca373b68e41028ec629dd629794bb4b7d9e52212fbd1efd72e6eaa4facd58a3697cea7b10b80534f450bd1
7
- data.tar.gz: f38d8910459727b2b8c90f6bbd7aa6c6a7e656e7606c4c7a5cf196534c3af7620651336aa4c58b6f91465b3ccba5459b22cab0bd074b3da1aa0defa30ed0b2c8
6
+ metadata.gz: 5fe7aa6a608b50a2cdef03610b5a061a58901b2cd8795b28e99803c7a7a2bc86b2d60a5817052e79499658615ca973b44200561c90fe733ebd7c63e594709aea
7
+ data.tar.gz: 72414ab56d9483e93c53b3861eccbeef44a6f5b7e5b97905f4a1c4b4a43fc9d634026486b44bd95f01144fdb4663c562370995b484b4dc1a85d65687e1004cb1
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ === 1.2.0 (2022-07-07)
2
+
3
+ * Make thamble/rails work with modern versions of Rails (jeremyevans)
4
+
5
+ * Make fallback HTML escaping code use cgi instead of rack (jeremyevans)
6
+
7
+ * Add Thamble.raw for simpler creation of raw strings (jeremyevans)
8
+
1
9
  === 1.1.0 (2019-11-20)
2
10
 
3
11
  * Support :column_th option for using th instead of td for first cell in each row (jeremyevans)
data/README.rdoc CHANGED
@@ -1,72 +1,77 @@
1
1
  = Thamble
2
2
 
3
- Thamble exposes a single method, table, for easily creating HTML
4
- tables from enumerable objects.
3
+ Thamble creates HTML tables from enumerable objects.
5
4
 
6
- = Installation
5
+ == Installation
7
6
 
8
7
  gem install thamble
9
8
 
10
- = Source Code
9
+ == Source Code
11
10
 
12
11
  Source code is available on GitHub at https://github.com/jeremyevans/thamble
13
12
 
14
- = Examples
13
+ == Examples
15
14
 
16
- The default behavior just expects an enumerable of arrays (such as an array of
17
- arrays), and returns a string containing the HTML TABLE output:
15
+ The Thamble.table method expects an enumerable of arrays (such as an array of
16
+ arrays), and returns a string containing the HTML table output:
18
17
 
19
18
  puts Thamble.table([[1, 2]])
20
19
 
21
- <table>
22
- <tbody>
23
- <tr>
24
- <td>1</td>
25
- <td>2</td>
26
- </tr>
27
- </tbody>
28
- </table>
20
+ output:
21
+
22
+ <table>
23
+ <tbody>
24
+ <tr>
25
+ <td>1</td>
26
+ <td>2</td>
27
+ </tr>
28
+ </tbody>
29
+ </table>
29
30
 
30
31
  If a block is passed to the method, all items in the enumerable are yielded to
31
32
  the block, and the block should return an array with the data to use in the table:
32
33
 
33
34
  puts Thamble.table([{:a=>1, :b=>2}, {:a=>3, :b=>4}]){|l| [l[:b], l[:a] + 10]}
34
35
 
35
- <table>
36
- <tbody>
37
- <tr>
38
- <td>2</td>
39
- <td>11</td>
40
- </tr>
41
- <tr>
42
- <td>4</td>
43
- <td>13</td>
44
- </tr>
45
- </tbody>
46
- </table>
36
+ output:
37
+
38
+ <table>
39
+ <tbody>
40
+ <tr>
41
+ <td>2</td>
42
+ <td>11</td>
43
+ </tr>
44
+ <tr>
45
+ <td>4</td>
46
+ <td>13</td>
47
+ </tr>
48
+ </tbody>
49
+ </table>
47
50
 
48
51
  You can use the :headers option to set custom headers:
49
52
 
50
53
  puts Thamble.table([[1, 2], [3, 4]], :headers=>['A', 'B'])
51
54
 
52
- <table>
53
- <thead>
54
- <tr>
55
- <th>A</th>
56
- <th>B</th>
57
- </tr>
58
- </thead>
59
- <tbody>
60
- <tr>
61
- <td>1</td>
62
- <td>2</td>
63
- </tr>
64
- <tr>
65
- <td>3</td>
66
- <td>4</td>
67
- </tr>
68
- </tbody>
69
- </table>
55
+ output:
56
+
57
+ <table>
58
+ <thead>
59
+ <tr>
60
+ <th>A</th>
61
+ <th>B</th>
62
+ </tr>
63
+ </thead>
64
+ <tbody>
65
+ <tr>
66
+ <td>1</td>
67
+ <td>2</td>
68
+ </tr>
69
+ <tr>
70
+ <td>3</td>
71
+ <td>4</td>
72
+ </tr>
73
+ </tbody>
74
+ </table>
70
75
 
71
76
  Other options supported are:
72
77
 
@@ -81,7 +86,31 @@ Other options supported are:
81
86
  the row that returns a hash
82
87
  :widths :: Array of widths for each column
83
88
 
84
- = Rails Support
89
+ == Escaping
90
+
91
+ By default, Thamble escapes all output. You can use the Thamble.raw method
92
+ to return a string marked as already being escaped.
93
+
94
+ puts Thamble.table([['&1234;', Thamble.raw('&1234;')]])
95
+
96
+ output:
97
+
98
+ <table>
99
+ <tbody>
100
+ <tr>
101
+ <td>&amp;1234;</td>
102
+ <td>&1234;</td>
103
+ </tr>
104
+ </tbody>
105
+ </table>
106
+
107
+ The raw method is also exposed on the second object yielded to Thamble.table:
108
+
109
+ puts Thamble.table([['1', '&1234;']]) do |row, table|
110
+ row.map{|value| table.raw(value )}
111
+ end
112
+
113
+ == Rails Support
85
114
 
86
115
  Thamble comes with basic rails support via:
87
116
 
@@ -92,10 +121,10 @@ This allows you to use the following style in your templates:
92
121
 
93
122
  <%= thamble([[1, 2], [3, 4]], :headers=>['A', 'B']) %>
94
123
 
95
- = License
124
+ == License
96
125
 
97
126
  MIT
98
127
 
99
- = Author
128
+ == Author
100
129
 
101
130
  Jeremy Evans <code@jeremyevans.net>
data/lib/thamble/rails.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen-string-literal: true
2
2
 
3
- require 'thamble'
3
+ require_relative '../thamble'
4
4
  require 'active_support/core_ext/string/output_safety'
5
5
  ActiveSupport::SafeBuffer.send(:include, Thamble::Raw)
6
6
 
7
7
  module Thamble
8
8
  module RailsHelper
9
9
  def thamble(*a, &block)
10
- raw Thamble.table(*a, &block)
10
+ Thamble.table(*a, &block).html_safe
11
11
  end
12
12
  end
13
13
  end
data/lib/thamble.rb CHANGED
@@ -36,6 +36,12 @@ module Thamble
36
36
  end
37
37
  module_function :table
38
38
 
39
+ # Return a raw string, which will not be HTML escaped in Thamble's output.
40
+ def raw(s)
41
+ RawString.new(s)
42
+ end
43
+ module_function :raw
44
+
39
45
  # The Table class stores the rows and attributes to use for the HTML tags,
40
46
  # and contains helper methods for common formatting.
41
47
  class Table
@@ -197,18 +203,20 @@ module Thamble
197
203
 
198
204
  begin
199
205
  require 'cgi/escape'
206
+ rescue LoadError
207
+ # :nocov:
208
+ # Handle old Ruby versions not supporting cgi/escape
209
+ require 'cgi'
210
+ else
200
211
  unless CGI.respond_to?(:escapeHTML) # work around for JRuby 9.1
201
212
  CGI = Object.new
202
213
  CGI.extend(defined?(::CGI::Escape) ? ::CGI::Escape : ::CGI::Util)
203
214
  end
204
- def escape_html(value)
205
- CGI.escapeHTML(value.to_s)
206
- end
207
- rescue LoadError
208
- require 'rack/utils'
209
- def escape_html(value)
210
- Rack::Utils.escape_html(value.to_s)
211
- end
215
+ # :nocov:
216
+ end
217
+
218
+ def escape_html(value)
219
+ CGI.escapeHTML(value.to_s)
212
220
  end
213
221
 
214
222
  # A HTML-escaped version of the given argument.
metadata CHANGED
@@ -1,23 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thamble
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-20 00:00:00.000000000 Z
11
+ date: 2022-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rack
14
+ name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: minitest
28
+ name: minitest-global_expectations
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest-global_expectations
42
+ name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '4'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '4'
55
55
  description: |
56
56
  Thamble exposes a single method, table, for easily creating HTML
57
57
  tables from enumerable objects.
@@ -66,14 +66,16 @@ files:
66
66
  - CHANGELOG
67
67
  - MIT-LICENSE
68
68
  - README.rdoc
69
- - Rakefile
70
69
  - lib/thamble.rb
71
70
  - lib/thamble/rails.rb
72
- - spec/thamble_spec.rb
73
71
  homepage: http://github.com/jeremyevans/thamble
74
72
  licenses:
75
73
  - MIT
76
- metadata: {}
74
+ metadata:
75
+ bug_tracker_uri: https://github.com/jeremyevans/thamble/issues
76
+ changelog_uri: https://github.com/jeremyevans/thamble/blob/master/CHANGELOG
77
+ mailing_list_uri: https://github.com/jeremyevans/thamble/discussions
78
+ source_code_uri: https://github.com/jeremyevans/thamble
77
79
  post_install_message:
78
80
  rdoc_options:
79
81
  - "--quiet"
@@ -89,14 +91,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
91
  requirements:
90
92
  - - ">="
91
93
  - !ruby/object:Gem::Version
92
- version: '0'
94
+ version: 1.9.2
93
95
  required_rubygems_version: !ruby/object:Gem::Requirement
94
96
  requirements:
95
97
  - - ">="
96
98
  - !ruby/object:Gem::Version
97
99
  version: '0'
98
100
  requirements: []
99
- rubygems_version: 3.0.3
101
+ rubygems_version: 3.3.7
100
102
  signing_key:
101
103
  specification_version: 4
102
104
  summary: Create HTML Tables from Enumerables
data/Rakefile DELETED
@@ -1,46 +0,0 @@
1
- require "rake"
2
- require "rake/clean"
3
-
4
- CLEAN.include ["thamble-*.gem", "rdoc", "coverage"]
5
-
6
- desc "Build thamble gem"
7
- task :package=>[:clean] do |p|
8
- sh %{#{FileUtils::RUBY} -S gem build thamble.gemspec}
9
- end
10
-
11
- ### Specs
12
-
13
- desc "Run specs"
14
- task :spec do
15
- sh "#{FileUtils::RUBY} -I lib spec/thamble_spec.rb"
16
- end
17
-
18
- task :default => :spec
19
-
20
- ### RDoc
21
-
22
- RDOC_DEFAULT_OPTS = ["--quiet", "--line-numbers", "--inline-source", '--title', 'Thamble: Create HTML Tables from Enumerables']
23
-
24
- begin
25
- gem 'rdoc', '= 3.12.2'
26
- gem 'hanna-nouveau'
27
- RDOC_DEFAULT_OPTS.concat(['-f', 'hanna'])
28
- rescue Gem::LoadError
29
- end
30
-
31
- rdoc_task_class = begin
32
- require "rdoc/task"
33
- RDoc::Task
34
- rescue LoadError
35
- require "rake/rdoctask"
36
- Rake::RDocTask
37
- end
38
-
39
- RDOC_OPTS = RDOC_DEFAULT_OPTS + ['--main', 'README.rdoc']
40
-
41
- rdoc_task_class.new do |rdoc|
42
- rdoc.rdoc_dir = "rdoc"
43
- rdoc.options += RDOC_OPTS
44
- rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
45
- end
46
-
data/spec/thamble_spec.rb DELETED
@@ -1,82 +0,0 @@
1
- require 'rubygems'
2
- require File.join(File.dirname(File.expand_path(__FILE__)), '../lib/thamble')
3
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
4
- require 'minitest/global_expectations/autorun'
5
-
6
- describe "Thamble.table" do
7
- def table(*a, &block)
8
- Thamble.table(*a, &block).gsub(/\s+\n/m, '').gsub("\n", '')
9
- end
10
-
11
- it 'should return string with HTML table for enumerable' do
12
- table([[1, 2]]).must_equal '<table><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
13
- end
14
-
15
- it 'should :support :column_th option for first column being th' do
16
- table([[1, 2]], :column_th=>true).must_equal '<table><tbody><tr><th>1</th><td>2</td></tr></tbody></table>'
17
- end
18
-
19
- it 'should support :headers option for the headers' do
20
- table([[1, 2]], :headers=>%w'a b').must_equal '<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
21
- end
22
-
23
- it 'should support :headers option as a string with comma separators' do
24
- table([[1, 2]], :headers=>'a,b').must_equal '<table><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
25
- end
26
-
27
- it 'should support :widths option for setting width for each column' do
28
- table([[1, 2]], :widths=>[3,4]).must_equal '<table><colgroup><col width="3" /><col width="4" /></colgroup><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
29
- end
30
-
31
- it 'should yield each object in enumerable to block to return data row to use' do
32
- table([[1, 2]]){|l, t| l.map{|i| i*2}}.must_equal '<table><tbody><tr><td>2</td><td>4</td></tr></tbody></table>'
33
- end
34
-
35
- it 'should be able to create tags using the table.tag method' do
36
- table([[1, 2]]){|l, t| l.map{|i| t.tag(:b, i*2)}}.must_equal '<table><tbody><tr><td><b>2</b></td><td><b>4</b></td></tr></tbody></table>'
37
- table([[1, 2]]){|l, t| l.map{|i| t.tag(:b, i*2, i=>i)}}.must_equal '<table><tbody><tr><td><b 1="1">2</b></td><td><b 2="2">4</b></td></tr></tbody></table>'
38
- end
39
-
40
- it 'should be able to create links using the table.a method' do
41
- table([[1, 2]]){|l, t| l.map{|i| t.a(i*2, 'foo')}}.must_equal '<table><tbody><tr><td><a href="foo">2</a></td><td><a href="foo">4</a></td></tr></tbody></table>'
42
- table([[1, 2]]){|l, t| l.map{|i| t.a(i*2, 'foo', i=>i)}}.must_equal '<table><tbody><tr><td><a 1="1" href="foo">2</a></td><td><a 2="2" href="foo">4</a></td></tr></tbody></table>'
43
- end
44
-
45
- it 'should support :table option for the table attribtues' do
46
- table([[1, 2]], :table=>{:class=>'foo'}).must_equal '<table class="foo"><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
47
- end
48
-
49
- it 'should support :tr option for the tr attributes' do
50
- table([[1, 2]], :tr=>{:class=>'foo'}).must_equal '<table><tbody><tr class="foo"><td>1</td><td>2</td></tr></tbody></table>'
51
- table([[1, 2]], :headers=>%w'a b', :tr=>{:class=>'foo'}).must_equal '<table><thead><tr class="foo"><th>a</th><th>b</th></tr></thead><tbody><tr class="foo"><td>1</td><td>2</td></tr></tbody></table>'
52
- end
53
-
54
- it 'should support :td option for the td attributes' do
55
- table([[1, 2]], :td=>{:class=>'foo'}).must_equal '<table><tbody><tr><td class="foo">1</td><td class="foo">2</td></tr></tbody></table>'
56
- end
57
-
58
- it 'should support :th option for the th attributes' do
59
- table([[1, 2]], :headers=>%w'a b', :th=>{:class=>'foo'}).must_equal '<table><thead><tr><th class="foo">a</th><th class="foo">b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
60
- end
61
-
62
- it 'should support a proc for the :tr option' do
63
- table([[1, 2]], :tr=>proc{|row| {:class=>"foo#{row.join}"}}).must_equal '<table><tbody><tr class="foo12"><td>1</td><td>2</td></tr></tbody></table>'
64
- table([[1, 2]], :headers=>%w'a b', :tr=>proc{|row| {:class=>"foo#{row.join}"}}).must_equal '<table><thead><tr class="fooab"><th>a</th><th>b</th></tr></thead><tbody><tr class="foo12"><td>1</td><td>2</td></tr></tbody></table>'
65
- end
66
-
67
- it 'should support a proc for the :td option' do
68
- table([[1, 2]], :td=>proc{|v, i, row| {:class=>"foo#{row.join}-#{v}-#{i}"}}).must_equal '<table><tbody><tr><td class="foo12-1-0">1</td><td class="foo12-2-1">2</td></tr></tbody></table>'
69
- end
70
-
71
- it 'should support a proc for the :th option' do
72
- table([[1, 2]], :headers=>%w'a b', :th=>proc{|v| {:class=>"foo#{v}"}}).must_equal '<table><thead><tr><th class="fooa">a</th><th class="foob">b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
73
- end
74
-
75
- it 'should support :caption option for the table caption' do
76
- table([[1, 2]], :headers=>%w'a b', :caption=>'Foo').must_equal '<table><caption>Foo</caption><thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
77
- end
78
-
79
- it 'should be callable as a method if including the module' do
80
- Class.new{include Thamble}.new.send(:table, [[1, 2]]).gsub(/\s+\n/m, '').gsub("\n", '').must_equal '<table><tbody><tr><td>1</td><td>2</td></tr></tbody></table>'
81
- end
82
- end