minad-imaginator 0.1.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.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/Manifest.txt +5 -0
  3. data/README.markdown +40 -0
  4. data/Rakefile +10 -0
  5. data/lib/imaginator.rb +223 -0
  6. metadata +68 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Daniel Mendler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/Manifest.txt ADDED
@@ -0,0 +1,5 @@
1
+ Manifest.txt
2
+ lib/imaginator.rb
3
+ README.markdown
4
+ LICENSE
5
+ Rakefile
data/README.markdown ADDED
@@ -0,0 +1,40 @@
1
+ README
2
+ ======
3
+
4
+ Imaginator is a ruby library to generate images from LaTeX/Graphviz code in a background job. This library is derived from my latex-renderer library.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Imaginator.uri = 'drbunix://tmp/imaginator.sock'
10
+ Imaginator.dir = '/tmp/imaginator'
11
+
12
+ Imaginator.run do |server|
13
+ server.add_renderer(:math, Imaginator::LaTeX.new)
14
+ server.add_renderer(:dot, Imaginator::Graphviz.new(:cmd => 'dot'))
15
+ server.add_renderer(:neato, Imaginator::Graphviz.new(:cmd => 'neato'))
16
+ server.add_renderer(:twopi, Imaginator::Graphviz.new(:cmd => 'twopi'))
17
+ server.add_renderer(:circo, Imaginator::Graphviz.new(:cmd => 'circo'))
18
+ server.add_renderer(:fdp, Imaginator::Graphviz.new(:cmd => 'fdp'))
19
+ end
20
+
21
+ ...
22
+
23
+ name = Imaginator.get.enqueue(:math, '1+1')
24
+ ...
25
+ filename = Imaginator.get.result(name)
26
+
27
+ TODO
28
+ ----
29
+
30
+ * Write unit tests or specs
31
+
32
+ Authors
33
+ -------
34
+
35
+ Daniel Mendler
36
+
37
+ License
38
+ -------
39
+
40
+ This library is released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'hoe'
2
+
3
+ $:.unshift 'lib'
4
+ require 'imaginator'
5
+
6
+ Hoe.new 'imaginator', Imaginator::VERSION do |imaginator|
7
+ imaginator.developer 'Daniel Mendler', 'mail@daniel-mendler.de'
8
+ imaginator.summary = 'Image generator for LaTeX/Graphviz source'
9
+ end
10
+
data/lib/imaginator.rb ADDED
@@ -0,0 +1,223 @@
1
+ require 'digest'
2
+ require 'fileutils'
3
+ require 'thread'
4
+ require 'monitor'
5
+ require 'drb'
6
+
7
+ class Imaginator
8
+ VERSION = "0.1.2"
9
+
10
+ class LaTeX
11
+ def initialize(opts = {})
12
+ @opts = {
13
+ :convert_opts => '-trim -density 120',
14
+ :format => 'png',
15
+ :debug => false
16
+ }
17
+ @opts[:blacklist] = %w{
18
+ include def command loop repeat open toks output input
19
+ catcode name \\every \\errhelp \\errorstopmode \\scrollmode
20
+ \\nonstopmode \\batchmode \\read \\write csname \\newhelp
21
+ \\uppercase \\lowercase \\relax \\aftergroup \\afterassignment
22
+ \\expandafter \\noexpand \\special $$
23
+ }
24
+ @opts.update(opts)
25
+ end
26
+
27
+ def format
28
+ @opts[:format]
29
+ end
30
+
31
+ def render(code, file)
32
+ begin
33
+ temp_dir = create_temp_dir(file)
34
+ latex2dvi(temp_dir, code)
35
+ dvi2ps(temp_dir)
36
+ ps2image(temp_dir, file)
37
+ rescue
38
+ FileUtils.rm_rf(temp_dir) if !@opts[:debug]
39
+ else
40
+ FileUtils.rm_rf(temp_dir)
41
+ end
42
+ end
43
+
44
+ def process(code)
45
+ errors = @opts[:blacklist].select { |cmd| code.include?(cmd) }
46
+ errors.empty? || raise(ArgumentError.new("Invalid LaTeX commands #{errors.join(', ')}"))
47
+ code.strip
48
+ end
49
+
50
+ private
51
+
52
+ def template(code)
53
+ <<END # {{{
54
+ \\documentclass{minimal}
55
+ \\newcommand\\use[2][]{\\IfFileExists{#2.sty}{\\usepackage[#1]{#2}}{}}
56
+ \\use[utf8]{inputenc}
57
+ \\use{amsmath}
58
+ \\use{amsfonts}
59
+ \\use{amssymb}
60
+ \\use{mathrsfs}
61
+ \\use{esdiff}
62
+ \\use{cancel}
63
+ \\use[dvips,usenames]{color}
64
+ \\use{nicefrac}
65
+ \\use[fraction=nice]{siunitx}
66
+ \\use{mathpazo}
67
+ \\begin{document}
68
+ $$
69
+ #{code}
70
+ $$
71
+ \\end{document}
72
+ END
73
+ # }}}
74
+ end
75
+
76
+ def create_temp_dir(file)
77
+ temp_dir = file + '-tmp'
78
+ FileUtils.mkdir_p(temp_dir)
79
+ temp_dir
80
+ end
81
+
82
+ def sh(cmd, args)
83
+ `#{cmd} #{args} 2>&1 > /dev/null`
84
+ raise RuntimeError.new("Execution of #{cmd} failed with status #{$?}") if $? != 0
85
+ end
86
+
87
+ def latex2dvi(dir, code)
88
+ tex_file = File.join(dir, 'code.tex')
89
+ File.open(tex_file, 'w') {|f| f.write(template(code)) }
90
+ sh('latex', "--interaction=nonstopmode --output-directory=#{dir} #{tex_file}")
91
+ end
92
+
93
+ def dvi2ps(dir)
94
+ file = File.join(dir, 'code')
95
+ sh('dvips', "-E #{file}.dvi -o #{file}.ps")
96
+ end
97
+
98
+ def ps2image(dir, file)
99
+ ps_file = File.join(dir, 'code.ps')
100
+ sh('convert', "#{@opts[:convert_opts]} #{ps_file} #{file}")
101
+ end
102
+ end
103
+
104
+ class Graphviz
105
+ def initialize(opts = {})
106
+ @opts = {
107
+ :format => 'png',
108
+ :cmd => 'dot'
109
+ }
110
+ @opts.update(opts)
111
+ end
112
+
113
+ def format
114
+ @opts[:format]
115
+ end
116
+
117
+ def render(code, file)
118
+ IO.popen(@opts[:cmd] + " -T #{@opts[:format]} -o #{file}", 'w') {|io|
119
+ io.write(code)
120
+ io.close
121
+ }
122
+ end
123
+
124
+ def process(code)
125
+ code.strip
126
+ end
127
+ end
128
+
129
+ @uri = nil
130
+ @dir = nil
131
+
132
+ class<< self
133
+ attr_accessor :uri, :dir
134
+
135
+ def get
136
+ raise(ArgumentError, "No URI given") if !@uri
137
+ @server || (@client ||= DRb::DRbObject.new(nil, @uri))
138
+ end
139
+
140
+ def run
141
+ if !running?
142
+ raise(ArgumentError, "No target directory given") if !@dir
143
+ @server = Imaginator.new(@dir)
144
+ yield(@server) if block_given?
145
+ DRb.start_service(@uri, @server)
146
+ Thread.new do
147
+ loop { @server.process }
148
+ end
149
+ end
150
+ end
151
+
152
+ def running?
153
+ raise(ArgumentError, "No URI given") if !@uri
154
+ @server || DRb::DRbObject.new(nil, @uri).respond_to?(:enqueue)
155
+ rescue
156
+ false
157
+ end
158
+ end
159
+
160
+ def initialize(dir)
161
+ @dir = dir
162
+ @renderer = {}
163
+ @queue = []
164
+ @queue.extend(MonitorMixin)
165
+ @empty = @queue.new_cond
166
+ FileUtils.mkdir_p(@dir, :mode => 0755)
167
+ end
168
+
169
+ def add_renderer(type, renderer)
170
+ @renderer[type.to_sym] = renderer
171
+ end
172
+
173
+ def enqueue(type, code)
174
+ type = type.to_sym
175
+ renderer = @renderer[type]
176
+ raise(ArgumentError, "Renderer not found") if !renderer
177
+ format = renderer.format
178
+ code = renderer.process(code)
179
+ name = Digest::MD5.hexdigest(code) + '.' + format
180
+ file = File.join(@dir, name)
181
+ if !File.exists?(file)
182
+ @queue.synchronize do
183
+ @queue << [name, type, code]
184
+ @empty.signal
185
+ end
186
+ end
187
+ name
188
+ end
189
+
190
+ def result(name)
191
+ file = File.join(@dir, name)
192
+ if !File.exists?(file)
193
+ while enqueued?(name) do
194
+ sleep 0.1
195
+ end
196
+ end
197
+ raise(RuntimeError, 'Image could not be generated') if !File.exists?(file)
198
+ file
199
+ end
200
+
201
+ def process
202
+ name, type, code = @queue.synchronize do
203
+ while @queue.empty? do
204
+ @empty.wait
205
+ end
206
+ @queue.first
207
+ end
208
+ @renderer[type].render(code, File.join(@dir, name)) rescue nil
209
+ @queue.synchronize do
210
+ @queue.shift
211
+ end
212
+ end
213
+
214
+ private
215
+
216
+ def enqueued?(name)
217
+ @queue.synchronize do
218
+ @queue.any? do |x|
219
+ x[0] == name
220
+ end
221
+ end
222
+ end
223
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minad-imaginator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Mendler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.3
24
+ version:
25
+ description:
26
+ email:
27
+ - mail@daniel-mendler.de
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - Manifest.txt
34
+ files:
35
+ - Manifest.txt
36
+ - lib/imaginator.rb
37
+ - README.markdown
38
+ - LICENSE
39
+ - Rakefile
40
+ has_rdoc: true
41
+ homepage:
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README.txt
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: imaginator
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Image generator for LaTeX/Graphviz source
67
+ test_files: []
68
+