prawn-grouping 0.1.0

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: 5692a85c43374a5de24dac64509274378aa5c72b
4
+ data.tar.gz: 79786b0fde6e1651615a4c09c5e6694618de1673
5
+ SHA512:
6
+ metadata.gz: f714a85cc2fa8ef6d28201d306595bc7327d505d7e465c5ee94fbc28c9161797db9489efbca23cb892f7018607b08d91c8dcbb49f9edccd8ee0e26fb89aa40ab
7
+ data.tar.gz: ddeb45661c67e8a377ecf8b8c8baa8e7f27bcf934fb6c277ac6fda5ba12382adc64a942e2bcbe9c5933ef66f3546ef1a25392bda68d01c3e57e707e1eee97305
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ /bin/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - rbx-2
7
+ - jruby-19mode
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: rbx-2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/prawn/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Daniel Dengler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Prawn::Grouping
2
+
3
+ [![Build Status](https://travis-ci.org/ddengler/prawn-grouping.png?branch=master)](https://travis-ci.org/ddengler/prawn-grouping)
4
+ [![Code Climate](https://codeclimate.com/github/ddengler/prawn-grouping.png)](https://codeclimate.com/github/ddengler/prawn-grouping)
5
+
6
+ An **experimental** gem to add a more flexible grouping option to the excellent prawn gem.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'prawn-grouping'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install prawn-grouping
21
+
22
+ ## Usage
23
+
24
+ ### Grouping
25
+
26
+ ```ruby
27
+ Prawn::Document.new do
28
+ 20.times { text "Regular text" }
29
+ group do
30
+ 20.times { text "Paragraphs not separated unless neccessary" }
31
+ end
32
+ end
33
+ ```
34
+
35
+ ### Nested Groups
36
+
37
+ ```ruby
38
+ Prawn::Document.new do
39
+ 5.times { text "Regular text" }
40
+ group do
41
+ 15.times { text "Paragraphs not separated unless neccessary" }
42
+ group do
43
+ 30.times { text "Subparagraphs not separated unless neccessary" }
44
+ end
45
+ end
46
+ end
47
+ ```
48
+
49
+ Do not overuse nesting because a every combination has to be tested recursively in a temporary document
50
+
51
+ ### Options
52
+
53
+ Currently three callbacks are supported:
54
+
55
+ * `:fits_current_context` A proc called before the content is rendered and does fit context.
56
+ * `:fits_new_context` A proc called before the content is rendered and does fit a single context.
57
+ * `:too_tall` A proc called before the content is rendered and does not fit a single context.
58
+
59
+ ```ruby
60
+ Prawn::Document.new do
61
+ 5.times { text "Regular text" }
62
+
63
+ group :too_tall => lambda { start_new_page } do
64
+ 15.times { text "Paragraphs not separated unless neccessary" }
65
+ end
66
+ end
67
+ ```
68
+
69
+ The example above starts a new page if the content is too tall for a single page. Default behavior would be to just append the content.
70
+
71
+
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( http://github.com/<my-github-username>/prawn-grouping/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => [:spec]
5
+
6
+ desc "Run all rspec files"
7
+ RSpec::Core::RakeTask.new("spec") do |c|
8
+ c.rspec_opts = "-t ~unresolved"
9
+ end
10
+
11
+ desc "Run a console with the gem loaded"
12
+ task :console do
13
+ require 'irb'
14
+ require 'irb/completion'
15
+ require_relative 'lib/prawn/grouping'
16
+
17
+ ARGV.clear
18
+ IRB.start
19
+ end
@@ -0,0 +1,66 @@
1
+ require "prawn"
2
+ require "prawn/grouping/version"
3
+
4
+ module Prawn
5
+ module Grouping
6
+
7
+ # Groups a given block vertiacally within the current context, if possible.
8
+ #
9
+ # Parameters are:
10
+ #
11
+ # <tt>options</tt>:: A hash for grouping options.
12
+ # <tt>:too_tall</tt>:: A proc called before the content is rendered and
13
+ # does not fit a single context.
14
+ # <tt>:fits_new_context</tt>:: A proc called before the content is
15
+ # rendered and does fit a single context.
16
+ # <tt>:fits_current_context</tt>:: A proc called before the content is
17
+ # rendered and does fit context.
18
+ #
19
+ def group(options = {}, &b)
20
+ too_tall = options[:too_tall]
21
+ fits_new_context = options[:fits_new_context]
22
+ fits_current_context = options[:fits_current_context]
23
+
24
+ # create a temporary document with current context and offset
25
+ pdf = create_box_clone
26
+ pdf.y = y
27
+ pdf.instance_exec pdf, &b
28
+
29
+ if pdf.page_count > 1
30
+ # create a temporary document without offset
31
+ pdf = create_box_clone
32
+ pdf.instance_exec pdf, &b
33
+
34
+ if pdf.page_count > 1
35
+ # does not fit new context
36
+ too_tall.call if too_tall
37
+ b.call(self)
38
+ else
39
+ fits_new_context.call if fits_new_context
40
+ bounds.move_past_bottom
41
+ b.call(self)
42
+ end
43
+ return false
44
+ else
45
+ # just render it
46
+ fits_current_context.call if fits_current_context
47
+ b.call(self)
48
+ return true
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def create_box_clone
55
+ Prawn::Document.new(:page_size => state.page.size, :page_layout => state.page.layout) do |pdf|
56
+ pdf.margin_box = @bounding_box.dup
57
+ pdf.text_formatter = @text_formatter.dup
58
+ pdf.font_families.update font_families
59
+ pdf.font font.name
60
+ pdf.font_size font_size
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ Prawn::Document.extensions << Prawn::Grouping
@@ -0,0 +1,5 @@
1
+ module Prawn
2
+ module Grouping
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'prawn/grouping/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "prawn-grouping"
8
+ spec.version = Prawn::Grouping::VERSION
9
+ spec.authors = ["Daniel Dengler"]
10
+ spec.email = ["me@ddengler.com"]
11
+ spec.summary = "Grouping extension for prawn pdf generator"
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+ spec.platform = Gem::Platform::RUBY
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = Dir[ "spec/*_spec.rb" ]
18
+ spec.require_path = "lib"
19
+ spec.required_ruby_version = '>= 1.9.3'
20
+ spec.required_rubygems_version = ">= 1.3.6"
21
+
22
+ spec.add_dependency "prawn", "~> 0.15.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pdf-inspector", "~> 1.1.0"
28
+ spec.add_development_dependency "pdf-reader", "~>1.2"
29
+ spec.add_development_dependency "guard"
30
+ spec.add_development_dependency "guard-rspec"
31
+ spec.add_development_dependency "growl"
32
+ end
@@ -0,0 +1,99 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "Prawn::Grouping" do
4
+ it "returns true if the content fits in the current context" do
5
+ pdf = Prawn::Document.new do
6
+ val = group { text "FooBar" }
7
+ (!!val).should == true
8
+ end
9
+ pages = PDF::Inspector::Page.analyze(pdf.render).pages
10
+ pages.size.should == 1
11
+ end
12
+
13
+ it "can be called with a block argument" do
14
+ pdf = Prawn::Document.new
15
+ val = pdf.group do |pdf|
16
+ pdf.text "FooBar"
17
+ end
18
+ (!!val).should == true
19
+
20
+ pages = PDF::Inspector::Page.analyze(pdf.render).pages
21
+ pages.size.should == 1
22
+ end
23
+
24
+ it "calls callbacks according to content length" do
25
+ called = 0
26
+ pdf = Prawn::Document.new do
27
+ group :fits_current_context => lambda { called = 1 } do
28
+ 20.times { text "FooBar 1" }
29
+ end
30
+ called.should == 1
31
+
32
+ group :fits_new_context => lambda { called = 2 } do
33
+ 40.times { text "FooBar 2" }
34
+ end
35
+ called.should == 2
36
+
37
+ group :too_tall => lambda { called = 3 } do
38
+ 100.times { text "FooBar 3" }
39
+ end
40
+ called.should == 3
41
+ end
42
+ end
43
+
44
+ it "should allow nesting of groups" do
45
+ pdf = Prawn::Document.new do
46
+ 5.times { text "1" }
47
+ group do
48
+ 10.times { text "1.1" }
49
+ end
50
+ group do
51
+ 15.times { text "1.2" }
52
+ group do
53
+ 30.times { text "1.2.1" }
54
+ end
55
+ end
56
+ end
57
+ pages = PDF::Inspector::Page.analyze(pdf.render).pages
58
+ pages.size.should == 2
59
+ end
60
+
61
+ # the following example were copied to fit the original spec from
62
+ # https://github.com/prawnpdf/prawn/blob/master/spec/document_spec.rb
63
+ it "should group a simple block on a single page" do
64
+ pdf = Prawn::Document.new do
65
+ self.y = 50
66
+ val = group do
67
+ text "Hello"
68
+ text "World"
69
+ end
70
+
71
+ # group should return a false value since a new page was started
72
+ (!!val).should == false
73
+ end
74
+ pages = PDF::Inspector::Page.analyze(pdf.render).pages
75
+ pages.size.should == 2
76
+ pages[0][:strings].should == []
77
+ pages[1][:strings].should == ["Hello", "World"]
78
+ end
79
+
80
+
81
+
82
+ it "should group within individual column boxes" do
83
+ pdf = Prawn::Document.new do
84
+ # Set up columns with grouped blocks of 0..49. 0 to 49 is slightly short
85
+ # of the height of one page / column, so each column should get its own
86
+ # group (every column should start with zero).
87
+ column_box([0, bounds.top], :width => bounds.width, :columns => 7) do
88
+ 10.times do
89
+ group { 50.times { |i| text(i.to_s) } }
90
+ end
91
+ end
92
+ end
93
+
94
+ # Second page should start with a 0 because it's a new group.
95
+ pages = PDF::Inspector::Page.analyze(pdf.render).pages
96
+ pages.size.should == 2
97
+ pages[1][:strings].first.should == '0'
98
+ end
99
+ end
@@ -0,0 +1,14 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ require "prawn/grouping"
5
+
6
+ require "pdf/reader"
7
+ require "pdf/inspector"
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+ config.order = 'random'
14
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-grouping
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Dengler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prawn
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.15.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.15.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pdf-inspector
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pdf-reader
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: growl
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - me@ddengler.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - .rspec
148
+ - .travis.yml
149
+ - Gemfile
150
+ - Guardfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - lib/prawn/grouping.rb
155
+ - lib/prawn/grouping/version.rb
156
+ - prawn-grouping.gemspec
157
+ - spec/grouping_spec.rb
158
+ - spec/spec_helper.rb
159
+ homepage: ''
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - '>='
170
+ - !ruby/object:Gem::Version
171
+ version: 1.9.3
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - '>='
175
+ - !ruby/object:Gem::Version
176
+ version: 1.3.6
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.0.14
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Grouping extension for prawn pdf generator
183
+ test_files:
184
+ - spec/grouping_spec.rb