clamsy 0.0.1 → 0.0.2

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.
@@ -1,3 +1,9 @@
1
+ === 0.0.2 (Apr 22, 2010)
2
+
3
+ = 2nd official (yet still as embarrassing) release
4
+
5
+ * instead of using system ps2pdf & pdf2ps commands, we use rghost gem to take care of ps/pdf conversions [#ngty]
6
+
1
7
  === 0.0.1 (Apr 21, 2010)
2
8
 
3
9
  = 1st official (yet embarrassing) release
@@ -11,7 +11,7 @@ Ruby wrapper for generating a single pdf for multiple contexts from an odt templ
11
11
  (how bad it is depends on ur platform) & even manual setting up of
12
12
  cups printer is needed
13
13
 
14
- == Using It
14
+ == Using It (sorry, still WIP for more decent examples)
15
15
 
16
16
  #1. Generating single context pdf:
17
17
 
@@ -53,10 +53,9 @@ Ruby wrapper for generating a single pdf for multiple contexts from an odt templ
53
53
 
54
54
  * add support for configuration, eg. Clamsy.configure {|config| ... }
55
55
 
56
- * implement strategy to check for missing commands & prompt user to install packages
56
+ * automate cups-pdf printer setup
57
57
 
58
- * instead of using system to call the various commands, maybe we write extensions
59
- for these foreign function calls (eg. using ffi ??)
58
+ * avoid making system call to 'ooffice'
60
59
 
61
60
  == Note on Patches/Pull Requests
62
61
 
data/Rakefile CHANGED
@@ -11,8 +11,8 @@ begin
11
11
  gem.homepage = "http://github.com/ngty/clamsy"
12
12
  gem.authors = ["NgTzeYang"]
13
13
  gem.add_dependency "rubyzip", "= 0.9.4"
14
+ gem.add_dependency "rghost", "= 0.8.7.2"
14
15
  gem.add_development_dependency "bacon", ">= 1.1.0"
15
- gem.add_development_dependency "differ", ">= 0.1.1"
16
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
17
  end
18
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clamsy}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["NgTzeYang"]
12
- s.date = %q{2010-04-21}
12
+ s.date = %q{2010-04-22}
13
13
  s.description = %q{}
14
14
  s.email = %q{ngty77@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -54,17 +54,17 @@ Gem::Specification.new do |s|
54
54
 
55
55
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
56
  s.add_runtime_dependency(%q<rubyzip>, ["= 0.9.4"])
57
+ s.add_runtime_dependency(%q<rghost>, ["= 0.8.7.2"])
57
58
  s.add_development_dependency(%q<bacon>, [">= 1.1.0"])
58
- s.add_development_dependency(%q<differ>, [">= 0.1.1"])
59
59
  else
60
60
  s.add_dependency(%q<rubyzip>, ["= 0.9.4"])
61
+ s.add_dependency(%q<rghost>, ["= 0.8.7.2"])
61
62
  s.add_dependency(%q<bacon>, [">= 1.1.0"])
62
- s.add_dependency(%q<differ>, [">= 0.1.1"])
63
63
  end
64
64
  else
65
65
  s.add_dependency(%q<rubyzip>, ["= 0.9.4"])
66
+ s.add_dependency(%q<rghost>, ["= 0.8.7.2"])
66
67
  s.add_dependency(%q<bacon>, [">= 1.1.0"])
67
- s.add_dependency(%q<differ>, [">= 0.1.1"])
68
68
  end
69
69
  end
70
70
 
@@ -3,9 +3,12 @@ require 'digest/md5'
3
3
  require 'tempfile'
4
4
  require 'zip/zip'
5
5
  require 'clamsy/tenjin'
6
+ require 'rghost'
6
7
 
7
8
  module Clamsy
8
9
 
10
+ class MissingFileError < Exception ; end
11
+
9
12
  class << self
10
13
 
11
14
  def process(contexts, template_odt, final_pdf)
@@ -28,7 +31,7 @@ module Clamsy
28
31
  (@trashable_tmp_files || []).select {|f| f.path }.map(&:unlink)
29
32
  end
30
33
 
31
- def get_tmp_file(file_name)
34
+ def tmp_file(file_name)
32
35
  ((@trashable_tmp_files ||= []) << Tempfile.new(file_name))[-1]
33
36
  end
34
37
 
@@ -57,7 +60,7 @@ module Clamsy
57
60
  def working_odt
58
61
  (@working_odts ||= {})[@context_id] ||=
59
62
  begin
60
- dest_odt = get_tmp_file(@context_id)
63
+ dest_odt = tmp_file(@context_id)
61
64
  File.copy(@template_odt, dest_odt.path) ; dest_odt
62
65
  end
63
66
  end
@@ -66,7 +69,7 @@ module Clamsy
66
69
  lambda do |entry|
67
70
  (@workers ||= {})[entry.to_s] ||=
68
71
  begin
69
- tmp_file = get_tmp_file("#{@context_id}.#{File.basename(entry.to_s)}")
72
+ tmp_file = tmp_file("#{@context_id}.#{File.basename(entry.to_s)}")
70
73
  tmp_file.write(entry.get_input_stream.read)
71
74
  tmp_file.close
72
75
  Tenjin::Template.new(tmp_file.path)
@@ -86,24 +89,16 @@ module Clamsy
86
89
  # to be set up in cups.
87
90
  ODT_TO_PDF_CMD = "ooffice -norestore -nofirststartwizard -nologo -headless -pt Cups-PDF"
88
91
 
89
- # PDF to PS & vice versa
90
- PDF_TO_PS_CMD = "pdf2ps"
91
- PS_TO_PDF_CMD = "ps2pdf"
92
-
93
- # Misc commands
94
- CAT_CMD = 'cat'
95
-
96
92
  class << self
97
93
 
98
94
  include HasTrashableTempFiles
99
95
 
100
96
  def print_odts_to_pdf(from_odts, to_pdf)
101
97
  begin
102
- tmp_ps = get_tmp_file(File.basename(to_pdf, '.pdf')).path
103
- system([
104
- "#{CAT_CMD} #{convert_odts_to_pss(from_odts).join(' ')} > #{tmp_ps}",
105
- "#{PS_TO_PDF_CMD} #{tmp_ps} #{to_pdf}"
106
- ].join(' && '))
98
+ tmp_ps = tmp_file(File.basename(to_pdf, '.pdf'))
99
+ from_odts.map {|odt| tmp_ps << odt_to_ps_stream(odt) }
100
+ tmp_ps.close
101
+ gs_convert(:pdf, tmp_ps.path, to_pdf)
107
102
  ensure
108
103
  trash_tmp_files
109
104
  end
@@ -111,16 +106,23 @@ module Clamsy
111
106
 
112
107
  private
113
108
 
114
- def convert_odts_to_pss(odts)
115
- odts.map(&:path).map do |odt_file|
116
- ps_file = get_tmp_file(basename = File.basename(odt_file, '.odt')).path
117
- pdf_file = File.join(PDF_OUTPUT_DIR, basename) + '.pdf'
118
- system("#{ODT_TO_PDF_CMD} #{odt_file}")
119
- # Abit clumsy ... but prevents occasional error for subsequent PDF_TO_PS_CMD
120
- 0.upto(10) {|_| File.exists?(pdf_file) ? break : sleep(1) }
121
- system("#{PDF_TO_PS_CMD} #{pdf_file} #{ps_file}")
122
- ps_file
123
- end
109
+ def odt_to_ps_stream(odt)
110
+ ensure_file_exists!(odt_path = odt.path)
111
+ pdf_path = File.join(PDF_OUTPUT_DIR, File.basename(odt_path, '.odt')) + '.pdf'
112
+ system("#{ODT_TO_PDF_CMD} #{odt_path}")
113
+ gs_convert(:ps, pdf_path)
114
+ end
115
+
116
+ def gs_convert(format, src_path, dest_path=nil)
117
+ ensure_file_exists!(src_path)
118
+ method, opts = dest_path ? [:path, {:filename => dest_path}] : [:read, {}]
119
+ RGhost::Convert.new(src_path).to(format, opts).send(method)
120
+ end
121
+
122
+ def ensure_file_exists!(file_path)
123
+ exist_flg = false
124
+ 0.upto(10) {|i| File.exists?(file_path) ? (exist_flg = true; break) : sleep(1) }
125
+ exist_flg or raise MissingFileError.new(file_path)
124
126
  end
125
127
 
126
128
  end
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'bacon'
3
3
  require 'differ'
4
4
  require 'tempfile'
5
+ require 'digest/md5'
5
6
 
6
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -19,7 +20,7 @@ shared 'has standard files support' do
19
20
  data_file("#{name}_example.pdf")
20
21
  end
21
22
  def comparable_content(file)
22
- `pdf2ps #{file} -`.grep(/^[^%][^%]?/).
23
+ RGhost::Convert.new(file).to(:ps).read.grep(/^[^%][^%]?/).
23
24
  reject {|line| line =~ /^q\[\-?\d+( \-?\d+){5}\]concat\n$/ }
24
25
  end
25
26
  def trash_tmp_files
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - NgTzeYang
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-21 00:00:00 +08:00
17
+ date: 2010-04-22 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -32,31 +32,32 @@ dependencies:
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- name: bacon
35
+ name: rghost
36
36
  prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "="
40
40
  - !ruby/object:Gem::Version
41
41
  segments:
42
- - 1
43
- - 1
44
42
  - 0
45
- version: 1.1.0
46
- type: :development
43
+ - 8
44
+ - 7
45
+ - 2
46
+ version: 0.8.7.2
47
+ type: :runtime
47
48
  version_requirements: *id002
48
49
  - !ruby/object:Gem::Dependency
49
- name: differ
50
+ name: bacon
50
51
  prerelease: false
51
52
  requirement: &id003 !ruby/object:Gem::Requirement
52
53
  requirements:
53
54
  - - ">="
54
55
  - !ruby/object:Gem::Version
55
56
  segments:
56
- - 0
57
57
  - 1
58
58
  - 1
59
- version: 0.1.1
59
+ - 0
60
+ version: 1.1.0
60
61
  type: :development
61
62
  version_requirements: *id003
62
63
  description: ""