crayfish 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,7 +53,7 @@ module Crayfish
53
53
  def paint template,raw=false
54
54
  begin
55
55
  html = raw ? template : eval(template)
56
- CrayHtml.new(self,@pdf).draw(html)
56
+ Html.new(self,@pdf).draw(html)
57
57
  rescue => e
58
58
  no = 0
59
59
  ::Rails.logger.debug template.split("\n").map{ |line| no+=1; "#{no}: #{line}" }.join("\n")
data/lib/crayfish/html.rb CHANGED
@@ -32,12 +32,37 @@ module Prawn
32
32
  end
33
33
  end
34
34
 
35
+ def crayfish_debug_dump node
36
+ if node.kind_of? Prawn::Table
37
+ (0..node.row_length).map do |n|
38
+ crayfish_debug_dump node.row(n).map{ |c| crayfish_debug_dump c }
39
+ end
40
+ elsif node.kind_of? Prawn::Table::Cell::Text
41
+ node.content
42
+ elsif node.kind_of? Array
43
+ node.map{ |c| crayfish_debug_dump c }
44
+ elsif node.kind_of? String
45
+ node
46
+ elsif node.kind_of? Fixnum
47
+ node
48
+ elsif node.kind_of? Hash
49
+ node.map{ |k,v| [k,crayfish_debug_dump(v)] }
50
+ else
51
+ node.class.name
52
+ end
53
+ end
54
+
55
+ # inspect with human readable output
56
+ def inspect
57
+ "<Prawn::Table #{crayfish_debug_dump(self).inspect}>"
58
+ end
59
+
35
60
  attr_accessor :post_resize
36
61
  end
37
62
  end
38
63
 
39
64
  module Crayfish
40
- class CrayHtml
65
+ class Html
41
66
 
42
67
  attr_reader :pdf
43
68
 
@@ -100,12 +125,17 @@ module Crayfish
100
125
  attribs[:post_resize] = "#{percent}%"
101
126
  end
102
127
  attribs[:cell_style][:borders] = [] if node.attributes['border'] and node.attributes['border'].value=='0'
103
- table = @pdf.make_table(table, attribs)
128
+
129
+ pdf_table = @pdf.make_table(table, attribs)
104
130
  table_styles.each do |style|
105
- apply_style(table.row(style[:row]).column(style[:col]),style[:style])
131
+ apply_style(pdf_table.row(style[:row]).column(style[:col]),style[:style])
106
132
  end
107
- scope[:td] << table if scope[:td]
108
- return table
133
+ scope[:td] << pdf_table if scope[:td]
134
+ return pdf_table
135
+
136
+ when :tbody
137
+ traverse_children node, "#{path}#{postfix}", :table => scope[:table], :table_styles => scope[:table_styles], :tr => nil
138
+ return
109
139
 
110
140
  when :tr
111
141
  row = []
@@ -152,27 +182,6 @@ module Crayfish
152
182
  traverse_children node, "#{path}#{postfix}"
153
183
  end
154
184
 
155
- # pp debug_dump(prawn_table)
156
- # def debug_dump node
157
- # if node.kind_of? Prawn::Table
158
- # (0..node.row_length).map do |n|
159
- # debug_dump node.row(n).map{ |c| debug_dump c }
160
- # end
161
- # elsif node.kind_of? Prawn::Table::Cell::Text
162
- # node.content
163
- # elsif node.kind_of? Array
164
- # node.map{ |c| debug_dump c }
165
- # elsif node.kind_of? String
166
- # node
167
- # elsif node.kind_of? Fixnum
168
- # node
169
- # elsif node.kind_of? Hash
170
- # node.map{ |k,v| [k,debug_dump(v)] }
171
- # else
172
- # node.class.name
173
- # end
174
- # end
175
-
176
185
  def post_resize node,parent_width
177
186
  if node.kind_of?(Prawn::Table::Cell::Subtable)
178
187
  post_resize node.subtable,parent_width
@@ -21,5 +21,5 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
23
  module Crayfish
24
- VERSION = "0.0.2"
24
+ VERSION = "0.1.0"
25
25
  end
@@ -69,159 +69,4 @@ class CrayfishTest < ActiveSupport::TestCase
69
69
  @view.send(:setup)
70
70
  end
71
71
 
72
- test "table" do
73
-
74
- sub_table = @view.make_table([[' ', { :content => 'Label4'}]])
75
-
76
- @view.instance_variable_get('@pdf').expects(:table).with(
77
- [
78
- [{:content => 'Label'}, sub_table],
79
- [{:content => 'label2'}, ''],
80
- [{:content => 'Label3'}, 'hello']
81
- ], {:width => 540})
82
-
83
- @view.send(:table,[
84
- [{ :content => 'Label'}, sub_table],
85
- [{ :content => 'label2'}, ''],
86
- [{ :content => 'Label3'}, 'hello'],
87
- ], :width => 540)
88
- end
89
-
90
- test "table block with rows" do
91
- @view.instance_variable_get('@pdf').expects(:table).with(
92
- [
93
- [
94
- {:content => 'Label1', :background_color => 'CCCCFF'},
95
- {:content => ' ', :background_color => 'ffffff'},
96
- {:content => 'Label2', :background_color => 'CCCCFF'},
97
- {:content => ' ', :background_color => 'ffffff'}
98
- ], [
99
- {:content => 'Label3', :background_color => 'CCCCFF'},
100
- {:content => ' ', :background_color => 'ffffff'}
101
- ], [
102
- {:content => 'Label4', :background_color => 'CCCCFF'},
103
- {:content => 'foo', :background_color => 'ffffff'}
104
- ], [
105
- {:content => 'Label5', :background_color => 'CCCCFF'},
106
- {:content => 'Hello', :background_color => 'ffffff'},
107
- {:content => 'Zip Code', :background_color => 'CCCCFF'},
108
- {:content => '0000', :background_color => 'ffffff'},
109
- {:content => 'City', :background_color => 'CCCCFF'},
110
- {:content => 'Internet', :background_color => 'ffffff'}
111
- ]
112
- ], {:width => 540}
113
- )
114
-
115
- location = { :name => 'Hello', :zip_code => '0000', :city => 'Internet'}
116
- @view.send(:table) do |t|
117
- t.row do |r|
118
- r.label 'Label1'; r.span; r.text ' '
119
- r.field :label => 'Label2', :value => ' '
120
- end
121
- t.row do |r|
122
- r.label 'Label3'; r.span; r.text ' '
123
- end
124
- t.row do |r|
125
- r.label 'Label4'; r.span; r.text 'foo'
126
- end
127
- t.row_for location do |r|
128
- r.field :label => 'Label5' , :value => location[:name]
129
- r.field :zip_code
130
- r.field :city
131
- end
132
- end
133
- end
134
-
135
- test "view delegates basic form to CrayForm" do
136
-
137
- text = %Q{
138
- Aplles %c{ }x%c{ } boxes | =%c{ }
139
- Pears %c{ }+ bananas%c{ } | =%c{ }
140
- }
141
-
142
- form = mock('CrayFrom')
143
- form.expects(:heading).with('Fruits %c{ }')
144
- form.expects(:form_body).with(text, :title => 'Fruits %c{ }')
145
- form.expects(:draw).with('')
146
- Crayfish::CrayForm.expects(:new).with(
147
- instance_of(CrayfishTest::ActionView),
148
- instance_of(Prawn::Document),
149
- :title => 'Fruits %c{ }', :span => /\|/, :element => /%c{[^}]*}/
150
- ).returns(form)
151
-
152
- @view.send(:form,text, :title => 'Fruits %c{ }')
153
- end
154
-
155
- def table_contents table
156
- table.rows(0..-1).map{ |cell|
157
- if cell.kind_of?(Prawn::Table::Cell::Text)
158
- cell.content
159
- elsif cell.kind_of?(Prawn::Table::Cell::Subtable)
160
- table_contents(cell.subtable)
161
- elsif cell.kind_of?(Crayfish::CellHelper)
162
- cell.content
163
- else
164
- cell.class.name
165
- end
166
- }
167
- end
168
-
169
- def cell_positions table
170
- table.rows(0..-1).map{ |cell|
171
- if cell.kind_of?(Prawn::Table::Cell::Text)
172
- cell.x
173
- elsif cell.kind_of?(Prawn::Table::Cell::Subtable)
174
- cell_positions(cell.subtable)
175
- elsif cell.kind_of?(Crayfish::CellHelper)
176
- cell.x
177
- else
178
- cell.class.name
179
- end
180
- }
181
- end
182
-
183
- test "CrayForm's form_body" do
184
-
185
- text = %Q{
186
- Apples %c{ }x%c{ } boxes | =%c{ }
187
- Pears %c{ }+ bananas%c{ } | =%c{ }
188
- }
189
-
190
- pdf = Prawn::Document.new()
191
-
192
- view = mock('Crayfish::ActionView')
193
- view.expects(:flush).at_least_once
194
-
195
- form = Crayfish::CrayForm.new(view,pdf,:title => 'Fruits %c{ }', :span => /\|/, :element => /%c{[^}]*}/)
196
- form.heading 'Fruits %c{ }'
197
- form.send(:form_body,text, :title => 'Fruits %c{ }')
198
- table = form.draw ''
199
-
200
- assert_equal 3,table.row_length
201
- assert_equal 1,table.column_length
202
-
203
- assert_equal 1,table.row(0).size
204
- assert_equal 1,table.row(1).size
205
- assert_equal 1,table.row(2).size
206
-
207
- assert_equal 1,table.row(0).first.subtable.row_length
208
- assert_equal 1,table.row(0).first.subtable.column_length
209
-
210
- assert_equal table_contents(table),
211
- [[["Fruits ", ""]],
212
- "",
213
- [["Apples ", "", "x", "", " boxes ", ""],
214
- [" =", ""],
215
- ["Pears ", "", "+ bananas", "", " ", ""],
216
- [" =", ""]]]
217
-
218
- # check span alignment
219
- n=0
220
- aligned_cells = cell_positions(table).last.select{ |item| n+=1; item if n.even? }.map{ |row| row.last }
221
-
222
- assert_equal 2,aligned_cells.size
223
- assert_equal 1,aligned_cells.uniq.size
224
-
225
- end
226
-
227
72
  end
data/test/html_test.rb ADDED
@@ -0,0 +1,63 @@
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
+ # Copyright (c) 2012 Patrick Hanevold
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require 'test_helper'
24
+
25
+ class HtmlTest < ActiveSupport::TestCase
26
+
27
+ def setup
28
+ @html = ::Crayfish::Html.new(nil,nil)
29
+ end
30
+
31
+ test "compile img" do
32
+ img = stub('html img')
33
+ img.stubs(:name).returns(:img)
34
+ img.stubs(:attributes).returns({ 'src' => OpenStruct.new(:value => '/some url')})
35
+ assert_equal @html.compile(img), :image=>"app/some url"
36
+ end
37
+
38
+ test "compile empty td" do
39
+ td = stub('html td')
40
+ td.stubs(:name).returns(:td)
41
+ td.stubs(:children).returns([])
42
+ td.stubs(:attributes).returns({})
43
+ row = []
44
+ @html.compile(td,'/td','',:tr => row)
45
+ assert_equal row,[{:content => ''}]
46
+ end
47
+
48
+ test "compile td with text" do
49
+ text = stub('text')
50
+ text.stubs(:name).returns(:text)
51
+ text.stubs(:content).returns('test')
52
+ text.stubs(:children).returns([])
53
+
54
+ td = stub('html td')
55
+ td.stubs(:name).returns(:td)
56
+ td.stubs(:children).returns([ text ])
57
+ td.stubs(:attributes).returns({})
58
+ row = []
59
+ @html.compile(td,'/td','',:tr => row)
60
+ assert_equal row,[{:content => 'test'}]
61
+ end
62
+
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crayfish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-13 00:00:00.000000000Z
12
+ date: 2012-03-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70349031035300 !ruby/object:Gem::Requirement
16
+ requirement: &70322535618780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70349031035300
24
+ version_requirements: *70322535618780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: prawn
27
- requirement: &70349031033800 !ruby/object:Gem::Requirement
27
+ requirement: &70322535617400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70349031033800
35
+ version_requirements: *70322535617400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &70349031031560 !ruby/object:Gem::Requirement
38
+ requirement: &70322535615140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70349031031560
46
+ version_requirements: *70322535615140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sqlite3
49
- requirement: &70349031029500 !ruby/object:Gem::Requirement
49
+ requirement: &70322535613040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70349031029500
57
+ version_requirements: *70322535613040
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &70349031027420 !ruby/object:Gem::Requirement
60
+ requirement: &70322535611060 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70349031027420
68
+ version_requirements: *70322535611060
69
69
  description: Crayfish - PDF templating for Rails
70
70
  email:
71
71
  - patrick.hanevold@gmail.com
@@ -115,6 +115,7 @@ files:
115
115
  - test/dummy/public/500.html
116
116
  - test/dummy/public/favicon.ico
117
117
  - test/dummy/script/rails
118
+ - test/html_test.rb
118
119
  - test/test_helper.rb
119
120
  homepage: http://github.com/patrickhno
120
121
  licenses: []
@@ -130,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  segments:
132
133
  - 0
133
- hash: -3931493668150893834
134
+ hash: -1624713932211142251
134
135
  required_rubygems_version: !ruby/object:Gem::Requirement
135
136
  none: false
136
137
  requirements:
@@ -139,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
140
  version: '0'
140
141
  segments:
141
142
  - 0
142
- hash: -3931493668150893834
143
+ hash: -1624713932211142251
143
144
  requirements: []
144
145
  rubyforge_project:
145
146
  rubygems_version: 1.8.15
@@ -178,4 +179,5 @@ test_files:
178
179
  - test/dummy/public/500.html
179
180
  - test/dummy/public/favicon.ico
180
181
  - test/dummy/script/rails
182
+ - test/html_test.rb
181
183
  - test/test_helper.rb