obf 0.8.9 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/obf/pdf.rb +50 -18
  3. data/lib/obf/utils.rb +17 -5
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1915660dacca273497702204f9950fbb1b233e88
4
- data.tar.gz: 4cf715b2d432814c876e5e764f3cc413efdb33c9
3
+ metadata.gz: 928a651051ba2b19566f6aacb7f878c201406818
4
+ data.tar.gz: 74dfc75968fc195799cbc503f8faadd8cca402bf
5
5
  SHA512:
6
- metadata.gz: 3c6013923795b65000fa54d4f1ffe05a7e1091d3dff4d77c8aa6bc4fbaca5fa785a0f7c184cee3fc5b70931f289ae644945a5c0abe3ff8822dabfd6b8ce264af
7
- data.tar.gz: b6a0b843f551e19abc5ff5f4df5dccb2e493687cb1382f6506120138e204a7b6a98c5a5d1d78f82872a760b24187a95613eaecc17a8ff8ab5ae9e006f1e897b8
6
+ metadata.gz: 9f505c1aba547fbeb2cddc2c424832bb3d530043d8c1c5945295cea8a5fa69910341a4e39b7a688f7014a59db8c8a4db4fd7ee89197d9167ef6b5db580b7be2c
7
+ data.tar.gz: bd5d3f9a87f70db44147df8a38569cca921e9d10aa0c1bb717669ca452b045cca1f0e0e6b533aa70a53eda650359b8903f4458c50a33dd2c1e4a445f037d322c
@@ -34,33 +34,55 @@ module OBF::PDF
34
34
  def self.build_pdf(obj, dest_path, zipper, opts={})
35
35
  OBF::Utils.as_progress_percent(0, 1.0) do
36
36
  # parse obf, draw as pdf
37
- pdf = Prawn::Document.new(
37
+ doc_opts = {
38
38
  :page_layout => :landscape,
39
39
  :page_size => [8.5*72, 11*72],
40
40
  :info => {
41
41
  :Title => obj['name']
42
42
  }
43
- )
43
+ }
44
+ pdf = Prawn::Document.new(doc_opts)
44
45
  font = opts['font'] if opts['font'] && File.exists?(opts['font'])
45
46
  font ||= File.expand_path('../../TimesNewRoman.ttf', __FILE__)
46
47
  pdf.font(font) if File.exists?(font)
47
48
 
48
-
49
+ multi_render_paths = []
49
50
  if obj['boards']
51
+ multi_render = obj['boards'].length > 20
50
52
  obj['boards'].each_with_index do |board, idx|
51
53
  pre = idx.to_f / obj['boards'].length.to_f
52
54
  post = (idx + 1).to_f / obj['boards'].length.to_f
53
55
  OBF::Utils.as_progress_percent(pre, post) do
54
- pdf.start_new_page unless idx == 0
55
- build_page(pdf, board, {
56
- 'zipper' => zipper,
57
- 'pages' => obj['pages'],
58
- 'headerless' => !!opts['headerless'],
59
- 'font' => font,
60
- 'text_on_top' => !!opts['text_on_top'],
61
- 'transparent_background' => !!opts['transparent_background'],
62
- 'text_case' => opts['text_case']
63
- })
56
+ # if more than 10 pages, build each page individually
57
+ # and combine them afterwards
58
+ if multi_render
59
+ path = OBF::Utils.temp_path("stash-#{idx}.pdf")
60
+ pdf = Prawn::Document.new(doc_opts)
61
+ build_page(pdf, board, {
62
+ 'zipper' => zipper,
63
+ 'pages' => obj['pages'],
64
+ 'headerless' => !!opts['headerless'],
65
+ 'font' => font,
66
+ 'links' => false,
67
+ 'text_on_top' => !!opts['text_on_top'],
68
+ 'transparent_background' => !!opts['transparent_background'],
69
+ 'text_case' => opts['text_case']
70
+ })
71
+ pdf.render_file(path)
72
+ multi_render_paths << path
73
+ else
74
+ pdf.start_new_page unless idx == 0
75
+ build_page(pdf, board, {
76
+ 'zipper' => zipper,
77
+ 'pages' => obj['pages'],
78
+ 'headerless' => !!opts['headerless'],
79
+ 'font' => font,
80
+ 'links' => true,
81
+ 'text_on_top' => !!opts['text_on_top'],
82
+ 'transparent_background' => !!opts['transparent_background'],
83
+ 'text_case' => opts['text_case']
84
+ })
85
+ end
64
86
  end
65
87
  end
66
88
  else
@@ -72,8 +94,12 @@ module OBF::PDF
72
94
  'text_case' => opts['text_case']
73
95
  })
74
96
  end
97
+ if multi_render_paths.length > 0
98
+ `gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=#{dest_path} #{multi_render_paths.join(' ')}`
99
+ else
100
+ pdf.render_file(dest_path)
101
+ end
75
102
 
76
- pdf.render_file(dest_path)
77
103
  end
78
104
  end
79
105
 
@@ -92,7 +118,7 @@ module OBF::PDF
92
118
 
93
119
  if options['pages']
94
120
  page_num = options['pages'][obj['id']]
95
- pdf.add_dest("page#{page_num}", pdf.dest_fit)
121
+ pdf.add_dest("page#{page_num}", pdf.dest_fit) if options['links']
96
122
  end
97
123
  # header
98
124
  if !options['headerless']
@@ -107,7 +133,9 @@ module OBF::PDF
107
133
  pdf.fill_color "6D81D1"
108
134
  pdf.fill_and_stroke_polygon([5, 50], [35, 85], [35, 70], [95, 70], [95, 30], [35, 30], [35, 15])
109
135
  pdf.fill_color "ffffff"
110
- pdf.formatted_text_box [{:text => "Go Back", :anchor => "page1"}], :at => [10, 90], :width => 80, :height => 80, :align => :center, :valign => :center, :overflow => :shrink_to_fit
136
+ text_options = {:text => "Go Back"}
137
+ text_options[:anchor] = "page1" if options['links']
138
+ pdf.formatted_text_box [text_options], :at => [10, 90], :width => 80, :height => 80, :align => :center, :valign => :center, :overflow => :shrink_to_fit
111
139
  pdf.fill_color "ffffff"
112
140
  pdf.fill_and_stroke_rounded_rectangle [110, 100], (doc_width - 200 - 20), 100, default_radius
113
141
  pdf.fill_color "DDDB54"
@@ -197,7 +225,9 @@ module OBF::PDF
197
225
  pdf.stroke_color "eeeeee"
198
226
  pdf.fill_and_stroke_rounded_rectangle [button_width - 18, page_vertical], 20, text_height, 5
199
227
  pdf.fill_color "000000"
200
- pdf.formatted_text_box [{:text => page, :anchor => "page#{page}"}], :at => [button_width - 18, page_vertical], :width => 20, :height => text_height, :align => :center, :valign => :center
228
+ text_options = {:text => page}
229
+ text_options[:anchor] = "page#{page}" if options['links']
230
+ pdf.formatted_text_box [text_options], :at => [button_width - 18, page_vertical], :width => 20, :height => text_height, :align => :center, :valign => :center
201
231
  end
202
232
  end
203
233
 
@@ -222,7 +252,9 @@ module OBF::PDF
222
252
  end
223
253
  pdf.fill_color "000000"
224
254
  if options['pages']
225
- pdf.formatted_text_box [{:text => options['pages'][obj['id']], :anchor => "page1"}], :at => [doc_width - 100, text_height], :width => 100, :height => text_height, :align => :right, :valign => :center, :overflow => :shrink_to_fit
255
+ text_options = {:text => options['pages'][obj['id']]}
256
+ text_options[:anchor] = "page1" if options['links']
257
+ pdf.formatted_text_box [text_options], :at => [doc_width - 100, text_height], :width => 100, :height => text_height, :align => :right, :valign => :center, :overflow => :shrink_to_fit
226
258
  end
227
259
  end
228
260
  end
@@ -9,6 +9,7 @@ module OBF::Utils
9
9
  data = Base64.strict_decode64(url.split(/\,/, 2)[1])
10
10
  else
11
11
  uri = url.match(/\%/) ? url : URI.escape(url)
12
+ uri = self.sanitize_url(uri)
12
13
  res = Typhoeus.get(uri)
13
14
  content_type = res.headers['Content-Type']
14
15
  data = res.body
@@ -28,6 +29,15 @@ module OBF::Utils
28
29
  }
29
30
  res
30
31
  end
32
+
33
+ def self.sanitize_url(url)
34
+ uri = URI.parse(url) rescue nil
35
+ return nil unless uri
36
+ return nil if (!defined?(Rails) || !Rails.env.development?) && (uri.host.match(/^127/) || uri.host.match(/localhost/) || uri.host.match(/^0/) || uri.host.to_s == uri.host.to_i.to_s)
37
+ port_suffix = ""
38
+ port_suffix = ":#{uri.port}" if (uri.scheme == 'http' && uri.port != 80)
39
+ "#{uri.scheme}://#{uri.host}#{port_suffix}#{uri.path}#{uri.query && "?#{uri.query}"}"
40
+ end
31
41
 
32
42
  def self.identify_file(path)
33
43
  name = File.basename(path) rescue nil
@@ -166,13 +176,15 @@ module OBF::Utils
166
176
  else
167
177
  background ||= 'white'
168
178
  size = 400
169
- if image['content_type'] && image['content_type'].match(/svg/) && background != 'white'
179
+ path = file.path
180
+ if image['content_type'] && image['content_type'].match(/svg/)
170
181
  `convert -background none -density 300 -resize #{size}x#{size} -gravity center -extent #{size}x#{size} #{file.path} #{file.path}.png`
171
- `convert #{file.path}.png -density 300 -resize #{size}x#{size} -background "#{background}" -gravity center -extent #{size}x#{size} #{file.path}.jpg`
172
- else
173
- `convert #{file.path} -density 300 -resize #{size}x#{size} -background "#{background}" -gravity center -extent #{size}x#{size} #{file.path}.jpg`
182
+ # `rsvg-convert -w #{size} -h #{size} -a #{file.path} > #{file.path}.png`
183
+ path = "#{file.path}.png"
174
184
  end
175
- "#{file.path}.jpg"
185
+ `convert #{path} -density 300 -resize #{size}x#{size} -background "#{background}" -gravity center -extent #{size}x#{size} #{path}.jpg`
186
+
187
+ "#{path}.jpg"
176
188
  end
177
189
  end
178
190
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Whitmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-03 00:00:00.000000000 Z
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json