crayfish 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2012 Bingoentrepenøren AS
1
+ Copyright 2012 Bingoentreprenøren AS
2
2
  Copyright 2012 Patrick Hanevold
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Crayfish
2
2
 
3
3
  PDF templating for Rails.
4
+ Crayfish basically translates HTML to PDF.
5
+ The idea is that you can use your existing view helpers in your PDF's.
6
+ The HTML support is quite limited at the moment.
7
+
8
+ You can also use the prawn API in your templates.
4
9
 
5
10
  ## Installation
6
11
 
@@ -17,70 +22,57 @@ gem 'prawn', :git => 'git://github.com/teknobingo/prawn.git', :branch => 'master
17
22
 
18
23
  ## Example (say app/views/main/show.pdf.crayfish):
19
24
 
20
- ``` ruby
21
- <%
22
- @location = { :name => 'GitHub', :zip_code => '0000', :city => 'Internet' }
23
-
24
- form %Q{
25
- Apples %c{ }x%c{ } boxes | =%c{ }
26
- Pears %c{ }+ bananas%c{ } | =%c{ }
27
- }, :title => 'Sold in kjosk %c{ }'
28
-
29
- move_down 6
30
- %>
31
-
32
- <% form do |f| %>
33
- <% f.heading 'Sold in kjosk '+f.field(' ') %>
34
- Apples <%= f.field(' ') %>x<%= f.field(' ') %> boxes <%= f.span %> =<%= f.field(' ') %>
35
- Pears <%= f.field(' ') %>+ bananas<%= f.field(' ') %> <%= f.span %> =<%= f.field(' ') %>
36
- <% end -%>
37
-
38
- <% table do |f| %>
39
- <% f.field :label => 'Repport (dd.mm.YYYY)', :value => Date.today %>
40
- <% end -%>
41
-
42
- <% table do |t| %>
43
- <% t.row do |r| %>
44
- <% r.label 'Permit'; r.span; r.text ' ' %>
45
- <% r.field :label => 'Time', :value => ' ' %>
46
- <% end -%>
47
- <% t.row do |r| %>
48
- <% r.label 'Cash'; r.span; r.text ' ' %>
49
- <% end -%>
50
- <% t.row do |r| %>
51
- <% r.label 'Organizer'; r.span; r.text 'Crayfish' %>
52
- <% end -%>
53
- <% t.row_for @location do |r| %>
54
- <% r.field :label => 'Location' , :value => @location[:name] %>
55
- <% r.field :zip_code %>
56
- <% r.field :city %>
57
- <% end -%>
58
- <% end -%>
59
-
60
- <% form do |f| %>
61
- <% f.row do |r| %>
62
- <% r.label 'Permit'; r.span; r.text ' ' %>
63
- <% r.field :label => 'Time', :value => ' ' %>
64
- <% end -%>
65
- <% f.row do |r| %>
66
- <% r.label 'Cash'; r.span; r.text ' ' %>
67
- <% end -%>
68
- <% f.row do |r| %>
69
- <% r.label 'Organizer'; r.span; r.text 'Crayfish' %>
70
- <% end -%>
71
- <% f.row_for @location do |r| %>
72
- <% r.field :label => 'Location' , :value => @location[:name] %>
73
- <% r.field :zip_code %>
74
- <% r.field :city %>
75
- <% end -%>
76
- <% end -%>
25
+ ``` html
26
+ <table>
27
+ <tr>
28
+ <th colspan="6" align="center" style="background-color:#aaffaa">Time Table</th>
29
+ </tr>
30
+ <tr>
31
+ <th rowspan="6" style="background-color:#aaffaa">Hours</th>
32
+ <th style="background-color:#ffaaaa">Mon</th>
33
+ <th style="background-color:#ffaaaa">Tue</th>
34
+ <th style="background-color:#ffaaaa">Wed</th>
35
+ <th style="background-color:#ffaaaa">Thu</th>
36
+ <th style="background-color:#ffaaaa">Fri</th>
37
+ </tr>
38
+ <tr>
39
+ <td>Science</td>
40
+ <td>Maths</td>
41
+ <td>Science</td>
42
+ <td>Maths</td>
43
+ <td>Arts</td>
44
+ </tr>
45
+ <tr>
46
+ <td>Social</td>
47
+ <td>History</td>
48
+ <td>English</td>
49
+ <td>Social</td>
50
+ <td>Sports</td>
51
+ </tr>
52
+ <tr>
53
+ <th colspan="5" align="center">Lunch</th>
54
+ </tr>
55
+ <tr>
56
+ <td>Science</td>
57
+ <td>Maths</td>
58
+ <td>Science</td>
59
+ <td>Maths</td>
60
+ <td rowspan="2">Project</td>
61
+ </tr>
62
+ <tr>
63
+ <td>Social</td>
64
+ <td>History</Td>
65
+ <td>English</td>
66
+ <td>Social</td>
67
+ </tr>
68
+ </table>
77
69
  ```
78
70
 
79
- Which gives:
71
+ Which gives you a single paged PDF looking like this:
80
72
 
81
73
  ![](http://github.com/patrickhno/crayfish/raw/master/doc/example.png)
82
74
 
83
- You could also use Prawn directly if you want to:
75
+ You can also use Prawn directly:
84
76
 
85
77
  ``` Ruby
86
78
  <% table [
@@ -105,7 +97,7 @@ You could also use Prawn directly if you want to:
105
97
 
106
98
  (The MIT License)
107
99
 
108
- Copyright (c) 2012 Bingoentrepenøren AS
100
+ Copyright (c) 2012 Bingoentreprenøren AS
109
101
  Copyright (c) 2012 Patrick Hanevold
110
102
 
111
103
  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:
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
@@ -20,15 +20,16 @@
20
20
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
 
23
- require 'erubis'
24
-
25
23
  module Crayfish
26
24
  module ActionView
27
25
 
28
26
  def render *args
29
27
  if @branch_level
30
28
  @branch_level += 1
29
+ stack = @output_buffer
30
+ @output_buffer = ::ActionView::OutputBuffer.new
31
31
  super
32
+ @output_buffer = stack
32
33
  @branch_level -= 1
33
34
  else
34
35
  super
@@ -45,23 +46,27 @@ module Crayfish
45
46
  end
46
47
  end
47
48
 
48
- def paint template
49
+ def output_buffer
50
+ @output_buffer ||= ::ActionView::OutputBuffer.new
51
+ end
52
+
53
+ def paint template,raw=false
49
54
  begin
50
- erb = Erubis::Eruby.new(template,:bufvar=>'@_out_buf')
51
- eval(erb.src)
55
+ html = raw ? template : eval(template)
56
+ CrayHtml.new(self,@pdf).draw(html)
52
57
  rescue => e
53
- no = 0
54
- ::Rails.logger.debug erb.src.split("\n").map{ |line| no+=1; "#{no}: #{line}" }.join("\n")
55
- ::Rails.logger.debug e.message
56
- ::Rails.logger.debug e.backtrace.join("\n")
58
+ no = 0
59
+ ::Rails.logger.debug template.split("\n").map{ |line| no+=1; "#{no}: #{line}" }.join("\n")
60
+ ::Rails.logger.debug e.message
61
+ ::Rails.logger.debug e.backtrace.join("\n")
57
62
  end
58
- @pdf.render if @branch_level == 0
63
+ @pdf.render if @branch_level == 0 and !raw
59
64
  end
60
65
 
61
66
  def flush paint=true
62
- buf = @_out_buf || ''
63
- @pdf.text buf if paint
64
- @_out_buf = ''
67
+ buf = @output_buffer.to_s || ''
68
+ paint(buf,true) if paint
69
+ @output_buffer = ::ActionView::OutputBuffer.new
65
70
  buf
66
71
  end
67
72
 
@@ -74,37 +79,6 @@ module Crayfish
74
79
  end
75
80
  end
76
81
 
77
- def form *args, &block
78
- flush
79
- if block_given?
80
- pdf = CrayForm.new(self,@pdf)
81
- block.call pdf
82
- pdf.draw @_out_buf
83
- else
84
- form = args[0] || ''
85
- options = args[1] || {}
86
-
87
- pdf = CrayForm.new(self,@pdf,options.merge(:span => /\|/, :element => /%c{[^}]*}/))
88
- pdf.heading options[:title] if options[:title]
89
- pdf.send(:form_body,form,options)
90
- pdf.draw @_out_buf
91
- end
92
- @_out_buf = ''
93
- end
94
-
95
- def table *args, &block
96
- if block_given?
97
- flush
98
-
99
- pdf = CrayTable.new(self,@pdf)
100
- block.call pdf
101
- pdf.draw @_out_buf
102
- else
103
- flush
104
- @pdf.table *args
105
- end
106
- end
107
-
108
82
  end
109
83
  end
110
84
 
@@ -0,0 +1,233 @@
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 'nokogiri'
24
+
25
+ module Prawn
26
+ class Table
27
+ class Cell
28
+ class Subtable
29
+ # prawn suicide prevention
30
+ def inline_format= something
31
+ end
32
+ end
33
+ end
34
+
35
+ attr_accessor :post_resize
36
+ end
37
+ end
38
+
39
+ module Crayfish
40
+ class CrayHtml
41
+
42
+ attr_reader :pdf
43
+
44
+ def initialize fish,pdf
45
+ @fish = fish
46
+ @pdf = pdf
47
+ end
48
+
49
+ def apply_style cell,style
50
+ # background-color:#ccccff
51
+ style.split(';').each do |style|
52
+ if /^background-color:#(?<color>.*)$/ =~ style
53
+ cell.background_color = color
54
+ end
55
+ end
56
+ end
57
+
58
+ def traverse_children node,path,params={ :table => nil, :tr => nil }
59
+ count = {}
60
+ node.children.each{ |node| count[node.name.to_sym] = (count[node.name.to_sym]||0) + 1 }
61
+ counters = Hash[*count.map{ |k,v| [k,0] }.flatten]
62
+ node.children.map do |node|
63
+ postfix = ''
64
+ if count[node.name.to_sym] > 1
65
+ postfix = "[#{counters[node.name.to_sym]}]"
66
+ counters[node.name.to_sym] += 1
67
+ end
68
+ compile node, "#{path}/#{node.name}#{postfix}", '', params
69
+ end
70
+ end
71
+
72
+ def style cell,node,scope
73
+ cell[:colspan] = node.attributes['colspan'].value.to_i if node.attributes['colspan']
74
+ cell[:rowspan] = node.attributes['rowspan'].value.to_i if node.attributes['rowspan']
75
+ cell[:valign] = :center if cell[:rowspan]
76
+ cell[:align] = node.attributes['align'].value.to_sym if node.attributes['align']
77
+ scope[:table_styles] << { :row => scope[:table].size, :col => scope[:tr].size, :style => node.attributes['style'].value } if node.attributes['style']
78
+ end
79
+
80
+ def compile node, path = "/#{node.name}", postfix='',scope={ :table => nil, :tr => nil }
81
+ name = node.name.to_sym
82
+
83
+ case name
84
+ when :img
85
+ # we would really like to use image_path here
86
+ image = {:image => "app#{node.attributes['src'].value}"}
87
+ image[:width] = node.attributes['width'].value.to_f if node.attributes['width']
88
+ image[:height] = node.attributes['height'].value.to_f if node.attributes['height']
89
+ return image
90
+
91
+ when :table
92
+ table = []
93
+ table_styles = []
94
+ traverse_children node, "#{path}#{postfix}", :table => table, :table_styles => table_styles, :tr => nil
95
+
96
+ # apply style
97
+ full_width = 540
98
+ attribs = { :cell_style => { :inline_format => true } }
99
+ if node.attributes['width'] and /^(?<percent>.*)%$/ =~ node.attributes['width'].value
100
+ attribs[:post_resize] = "#{percent}%"
101
+ end
102
+ attribs[:cell_style][:borders] = [] if node.attributes['border'] and node.attributes['border'].value=='0'
103
+ table = @pdf.make_table(table, attribs)
104
+ table_styles.each do |style|
105
+ apply_style(table.row(style[:row]).column(style[:col]),style[:style])
106
+ end
107
+ scope[:td] << table if scope[:td]
108
+ return table
109
+
110
+ when :tr
111
+ row = []
112
+ traverse_children node, "#{path}#{postfix}", :table => scope[:table], :table_styles => scope[:table_styles], :tr => row
113
+ scope[:table] << row
114
+ return :tr => row
115
+
116
+ when :td
117
+ cells = []
118
+ traverse_children node, "#{path}#{postfix}", :table => scope[:table], :table_styles => scope[:table_styles], :tr => scope[:tr], :td => cells
119
+ if cells.size <= 1
120
+ cells << "" if cells.size == 0
121
+ cell = { :content => cells.first.kind_of?(String) ? cells.first.strip : cells.first }
122
+ style cell,node,scope
123
+ scope[:tr] << cell
124
+ else
125
+ scope[:tr] << cells
126
+ end
127
+ return
128
+
129
+ when :th
130
+ cells = []
131
+ traverse_children node, "#{path}#{postfix}", :table => scope[:table], :table_styles => scope[:table_styles], :tr => scope[:tr], :td => cells
132
+ if cells.size <= 1
133
+ cells << "" if cells.size == 0
134
+ cell = { :content => "<b>#{cells.first.strip}</b>", :align => :center }
135
+ style cell,node,scope
136
+ scope[:tr] << cell
137
+ else
138
+ scope[:tr] << cells
139
+ end
140
+ return
141
+
142
+ when :text
143
+ if scope[:td] and node.content.strip.size > 0
144
+ scope[:td] << node.content.strip
145
+ end
146
+ when :html
147
+ when :body
148
+ else
149
+ ::Rails.logger.debug "\e[1;31munknown node #{node.name} in CrayHtml\e[0m"
150
+ end
151
+
152
+ traverse_children node, "#{path}#{postfix}"
153
+ end
154
+
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
+ def post_resize node,parent_width
177
+ if node.kind_of?(Prawn::Table::Cell::Subtable)
178
+ post_resize node.subtable,parent_width
179
+ elsif node.kind_of?(Prawn::Table)
180
+ if node.post_resize
181
+ if /^(?<size>.*)%$/ =~ node.post_resize
182
+ node.instance_variable_set '@column_widths', nil
183
+ # reset constraints
184
+ node.cells.each do |cell|
185
+ cell.instance_variable_set '@max_width', parent_width
186
+ end
187
+ node.width = parent_width
188
+ node.send(:set_column_widths)
189
+ node.send(:position_cells)
190
+ end
191
+ end
192
+ column_widths = node.column_widths
193
+ (0..node.row_length).map do |n|
194
+
195
+ row = node.row(n).columns(0..-1)
196
+
197
+ row.each_with_index do |cell,i|
198
+ colspan = 1
199
+ row.to_a[i+1..-1].each do |cell|
200
+ break unless cell.kind_of?(Prawn::Table::Cell::SpanDummy)
201
+ colspan += 1
202
+ end
203
+ post_resize cell,column_widths[i..(i+colspan-1)].sum
204
+ end
205
+ end
206
+ elsif node.kind_of? Array
207
+ node.each{ |c| post_resize c,parent_width }
208
+ elsif node.kind_of? Hash
209
+ node.each do |k,v|
210
+ post_resize v,parent_width
211
+ end
212
+ end
213
+ end
214
+
215
+ def draw text
216
+ doc = Nokogiri::HTML(text)
217
+
218
+ doc.children.map do |element|
219
+ compile element
220
+ end.flatten.each do |prawn|
221
+ if prawn.kind_of? Hash
222
+ if prawn.has_key? :image
223
+ pdf.image prawn[:image], prawn
224
+ end
225
+ else
226
+ post_resize prawn,540
227
+ prawn.draw
228
+ end
229
+ end
230
+ end
231
+
232
+ end
233
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
@@ -22,11 +22,13 @@
22
22
 
23
23
  module Crayfish
24
24
  module Rails
25
- class Base
25
+ class Base < ::ActionView::Base
26
26
 
27
27
  def self.call template
28
28
  "setup;" +
29
- "paint \"#{template.source}\""
29
+ "paint <<__this_is_quite_unique__\n" +
30
+ ::ActionView::Template::Handlers::ERB.call(template) +
31
+ "\n__this_is_quite_unique__\n"
30
32
  end
31
33
 
32
34
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
@@ -1,3 +1,25 @@
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
+
1
23
  module Crayfish
2
- VERSION = "0.0.1"
24
+ VERSION = "0.0.2"
3
25
  end
data/lib/crayfish.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
@@ -33,11 +33,7 @@ require 'crayfish/action_controller'
33
33
  require 'crayfish/action_view'
34
34
  require 'crayfish/rails/pdf'
35
35
  require 'crayfish/rails/base'
36
- require 'crayfish/elements/container.rb'
37
- require 'crayfish/elements/cell_helper.rb'
38
- require 'crayfish/elements/row.rb'
39
- require 'crayfish/elements/table.rb'
40
- require 'crayfish/elements/form.rb'
36
+ require 'crayfish/html.rb'
41
37
 
42
38
  class ActionController::Base
43
39
  include Crayfish::ActionController
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
data/test/test_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Bingoentrepenøren AS
1
+ # Copyright (c) 2012 Bingoentreprenøren AS
2
2
  # Copyright (c) 2012 Patrick Hanevold
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
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.1
4
+ version: 0.0.2
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-02 00:00:00.000000000Z
12
+ date: 2012-03-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70304152295180 !ruby/object:Gem::Requirement
16
+ requirement: &70349031035300 !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: *70304152295180
24
+ version_requirements: *70349031035300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: prawn
27
- requirement: &70304152291860 !ruby/object:Gem::Requirement
27
+ requirement: &70349031033800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70304152291860
35
+ version_requirements: *70349031033800
36
+ - !ruby/object:Gem::Dependency
37
+ name: nokogiri
38
+ requirement: &70349031031560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70349031031560
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: sqlite3
38
- requirement: &70304152290940 !ruby/object:Gem::Requirement
49
+ requirement: &70349031029500 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70304152290940
57
+ version_requirements: *70349031029500
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: mocha
49
- requirement: &70304152290180 !ruby/object:Gem::Requirement
60
+ requirement: &70349031027420 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,7 +65,7 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70304152290180
68
+ version_requirements: *70349031027420
58
69
  description: Crayfish - PDF templating for Rails
59
70
  email:
60
71
  - patrick.hanevold@gmail.com
@@ -64,11 +75,7 @@ extra_rdoc_files: []
64
75
  files:
65
76
  - lib/crayfish/action_controller.rb
66
77
  - lib/crayfish/action_view.rb
67
- - lib/crayfish/elements/cell_helper.rb
68
- - lib/crayfish/elements/container.rb
69
- - lib/crayfish/elements/form.rb
70
- - lib/crayfish/elements/row.rb
71
- - lib/crayfish/elements/table.rb
78
+ - lib/crayfish/html.rb
72
79
  - lib/crayfish/rails/base.rb
73
80
  - lib/crayfish/rails/pdf.rb
74
81
  - lib/crayfish/version.rb
@@ -123,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
130
  version: '0'
124
131
  segments:
125
132
  - 0
126
- hash: -589067026738800894
133
+ hash: -3931493668150893834
127
134
  required_rubygems_version: !ruby/object:Gem::Requirement
128
135
  none: false
129
136
  requirements:
@@ -132,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
139
  version: '0'
133
140
  segments:
134
141
  - 0
135
- hash: -589067026738800894
142
+ hash: -3931493668150893834
136
143
  requirements: []
137
144
  rubyforge_project:
138
145
  rubygems_version: 1.8.15
@@ -1,70 +0,0 @@
1
- # Copyright (c) 2012 Bingoentrepenø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
- module Crayfish
24
-
25
- class CellHelper < Prawn::Table::Cell::Text
26
-
27
- def initialize cell, options={}
28
- @pdf = cell.instance_variable_get('@pdf')
29
- @type = options[:type] || :text
30
- super @pdf,cell.instance_variable_get('@point'),{ :content => cell.instance_variable_get('@content') }
31
- end
32
-
33
- def width val=nil
34
- if val
35
- self.width = val
36
- self
37
- else
38
- super()
39
- end
40
- end
41
-
42
- def draw_content
43
- if @type == :field
44
- x = -5.25 # FIXME: guesswork
45
- y = @pdf.cursor + padding[1] - (@pdf.font.line_gap + @pdf.font.descender)/2 + 3 # FIXME: guesswork
46
- w = spanned_content_width + FPTolerance + padding[2] + padding[3]
47
- h = @pdf.font.height + padding[0] + padding[1]
48
-
49
- old = @pdf.fill_color
50
- @pdf.fill_color = 'ffffff'
51
- @pdf.rectangle [x, y], w, h
52
- @pdf.fill
53
- @pdf.fill_color = old
54
-
55
- @pdf.stroke_line([x, y], [w+x, y])
56
- @pdf.stroke_line([w+x, y], [w+x, y-h])
57
- @pdf.stroke_line([w+x, y-h],[x, y-h])
58
- @pdf.stroke_line([x, y-h],[x, y])
59
- end
60
-
61
- super
62
- end
63
-
64
- def inspect
65
- "<CellHelper \"#{content}\">"
66
- end
67
-
68
- end
69
-
70
- end
@@ -1,64 +0,0 @@
1
- # Copyright (c) 2012 Bingoentrepenø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
- module Crayfish
24
- class CrayContainer
25
-
26
- attr_accessor :raw
27
- attr_reader :options,:tokens,:pdf
28
-
29
- def initialize fish, pdf, options = {}
30
- @fish = fish
31
- @pdf = pdf
32
- @raw = []
33
- @options = options
34
- @tokens = { :span => options[:span] || /%\|/, :element => options[:element] || /%c{[^}]*}/ }
35
- end
36
-
37
- def append stuff, options={}
38
- @raw << stuff
39
- end
40
-
41
- def field str
42
- "%c{#{str}}"
43
- end
44
-
45
- def span
46
- '%|'
47
- end
48
-
49
- def row *args, &block
50
- raise "row must have block" unless block_given?
51
- row = CrayRow.new(@fish,pdf,self)
52
- block.call row
53
- row.draw ''
54
- end
55
-
56
- def row_for *args, &block
57
- raise "row_for must have block" unless block_given?
58
- row = CrayRow.new(@fish,pdf,self,args.first)
59
- block.call row
60
- row.draw ''
61
- end
62
-
63
- end
64
- end
@@ -1,161 +0,0 @@
1
- # Copyright (c) 2012 Bingoentrepenø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
- module Crayfish
24
- class CrayForm < CrayContainer
25
-
26
- def initialize fish, pdf, options = {}
27
- @fish=fish
28
- @spans = []
29
- super
30
- end
31
-
32
- # Flush accumulated text and append a (prawn) element
33
- def append stuff, options={}
34
- buf = @fish.send(:flush,false)
35
- @spans << (options[:spans] || [])
36
- super
37
- #form_body(buf) if buf.strip != ''
38
- buf
39
- end
40
-
41
- def heading str
42
- append [form_body(str,:spacing => 0)]
43
- end
44
-
45
- # Draws the form in all its glory
46
- def draw text
47
- form_body(text,@options)
48
-
49
- allign_spans
50
-
51
- table = pdf.make_table raw, :width => 540, :cell_style => { :padding => [0,0,0,0], :border_width => 1 }
52
-
53
- pdf.fill_color 'CCCCFF'
54
- pdf.fill { @pdf.rectangle [0, pdf.cursor], table.width, table.height }
55
-
56
- pdf.fill_color '000000'
57
- table.draw
58
- table
59
- end
60
-
61
- private
62
-
63
- def allign_spans
64
- # group by span count
65
- # in the future we may want to group by span id's or paths to allow much more fine grained controll of span allignment
66
- groups = @spans.map{ |row| row.max }.uniq.select{ |cnt| cnt }
67
- i = -1
68
- indexed = @spans.map{ |span| i+=1; {:index => i, :row => raw[i] } }
69
-
70
- groups.each do |group_cnt|
71
- # all rows with group_cnt number of spans
72
- considder = indexed.select{ |row| @spans[row[:index]].max == group_cnt }.map{ |row| row[:index] }
73
-
74
- # the span width map
75
- width_map = considder.map do |index|
76
- spans = @spans[index]
77
- row = raw[index].first[:content]
78
- widths = row.row(0).columns(0..-1).map{ |cell| cell.natural_content_width }
79
-
80
- i = 0
81
- spans.map do |span_index|
82
- span = widths[i..span_index-1]
83
- i=span_index
84
- span.sum
85
- end
86
- end
87
-
88
- alligned_widths = width_map.transpose.map{ |col| col.max }
89
-
90
- # allign cells
91
- considder.each do |index|
92
- row_width = reminding_width = raw[index].first[:content].row(0).width
93
- raw[index].first[:content].row(0).columns(0..alligned_widths.size-1).each_with_index do |cell,i|
94
- cell.width = alligned_widths[i]
95
- reminding_width -= cell.width
96
- end
97
- # fit reminding cells in row
98
- # TODO: respect constraints!
99
- x = row_width-reminding_width
100
- w = reminding_width / raw[index].first[:content].row(0).columns(alligned_widths.size..-1).size
101
- raw[index].first[:content].row(0).columns(alligned_widths.size..-1).each do |cell|
102
- cell.x = x
103
- cell.width = w
104
- x += w
105
- end
106
- end
107
- end
108
- end
109
-
110
- def _text text,options = {}
111
- Crayfish::CellHelper.new(Prawn::Table::Cell.make(pdf, text), options)
112
- end
113
-
114
- def _field width=nil
115
- cell = _text('', { :type => :field })
116
- cell.width(width) if width
117
- cell
118
- end
119
-
120
- def _space width=nil
121
- cell = _text('')
122
- cell.width(width) if width
123
- end
124
-
125
- def form_body form,options = {}
126
- color = 'CCCCFF'
127
-
128
- space_width = 10
129
-
130
- lines = form.split("\n").map{ |line| line.strip }.select{ |line| line.size > 0 }
131
- return unless lines.size > 0
132
-
133
- spans_per_line = lines.map{ |line| line.split(tokens[:span]).size-1 }
134
- raise 'the number of spans must be the same on all lines' unless spans_per_line.uniq.size <= 1
135
- spans_per_line = spans_per_line.first
136
-
137
- span_table = lines.map{ |line| line.split(tokens[:span]) }
138
-
139
- # split text from fields in all spans
140
- table = span_table.map do |row|
141
- row.map do |span|
142
- span.split(tokens[:element]).zip(span.scan(tokens[:element])).flatten
143
- end
144
- end
145
-
146
- # translate each span into a subtable
147
- table.map! do |row|
148
- row.map do |span|
149
- pdf.make_table [ span.map{ |cell| cell =~ /^#{tokens[:element].source}$/ ? _field(cell.count(' ')*space_width) : _text(cell) } ],:cell_style => { :borders => [] }
150
- end
151
- end
152
-
153
- padding = options[:spacing] || (pdf.font.line_gap + pdf.font.descender)
154
-
155
- # note that the spans above are in sub-tables, we need adressable spans (like xpath) if we want to use them further
156
- # for now, pass a empty span list for this row (this form body is appended as a single row)
157
- append [pdf.make_table(table, :width => 540, :cell_style => {:padding => [padding,0,padding,0], :borders => [] })], :spans => []
158
- end
159
-
160
- end
161
- end
@@ -1,70 +0,0 @@
1
- # Copyright (c) 2012 Bingoentrepenø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
- module Crayfish
24
- class CrayRow < CrayContainer
25
-
26
- def initialize fish,pdf,table,model=nil
27
- @table = table
28
- @color = 'CCCCFF'
29
- @spans = []
30
- @model = model
31
- super fish,pdf
32
- end
33
-
34
- def label label
35
- append :content => label, :background_color => @color
36
- end
37
-
38
- def text text
39
- append :content => text, :background_color => 'ffffff'
40
- end
41
-
42
- def span
43
- @spans << @raw.size
44
- end
45
-
46
- def field *args
47
- label = ''
48
- value = ''
49
- if args.first.kind_of? Symbol
50
- raise "you must use row_for(model) to reference with symbols" unless @model
51
- label = I18n.t args.first, :scope => [:activerecord, :attributes, @model.class.name.underscore.to_sym], :default => args.first.to_s.titleize
52
- value = @model.respond_to?(args.first) ? @model.send(args.first) : @model[args.first]
53
- else
54
- label = args.first[:label]
55
- value = args.first[:value]
56
- end
57
- self.label label
58
- text value
59
- end
60
-
61
- def draw text
62
- if @table.kind_of? CrayForm
63
- @table.append [ { :content => @table.pdf.make_table([raw], :width => 540) } ], :spans => @spans
64
- else
65
- @table.append raw
66
- end
67
- end
68
-
69
- end
70
- end
@@ -1,51 +0,0 @@
1
- # Copyright (c) 2012 Bingoentrepenø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
- module Crayfish
24
- class CrayTable < CrayContainer
25
-
26
- attr_reader :pdf
27
-
28
- def initialize fish,pdf
29
- @color = 'CCCCFF'
30
- super
31
- end
32
-
33
- def field *args
34
- label = ''
35
- value = ''
36
- if args.first.kind_of? Symbol
37
- label = args.first.to_s
38
- value = ''
39
- else
40
- label = args.first[:label]
41
- value = args.first[:value]
42
- end
43
- append [{ :content => label, :background_color => @color}, value]
44
- end
45
-
46
- def draw text
47
- pdf.table raw, :width => 540
48
- end
49
-
50
- end
51
- end