rubypost 0.0.7 → 0.0.8

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.
data/lib/objects.rb CHANGED
@@ -1,217 +1,203 @@
1
- module RubyPost
2
-
3
- #base class for rubypost
4
- class Object
5
- #returns the string of metapost commmands
6
- def compile
7
- '%compile not implemented for ' + self.class.to_s
8
- end
9
- end
10
-
11
- #stores the macros that particular drawbles need.
12
- #This should really be a private class.
13
- class Macros < Object
14
-
15
- def initialize
16
- @inputs = Hash.new
17
- end
18
-
19
- #uses hash to make sure we never input same thing twice
20
- def add_input(s)
21
- @inputs[s] = nil
22
- end
23
-
24
- def compile
25
- str = String.new
26
- @inputs.each_key do
27
- |k| str = str + "input " + k + ";\n"
28
- end
29
- str
30
- end
31
-
32
- end
33
-
34
- #stores the macros that particular drawbles need.
35
- #This should really be a private class.
36
- class PicturePrecompiler < Object
37
-
38
- def initialize
39
- @pictures = Array.new
40
- end
41
-
42
- def add_picture(s)
43
- @pictures.push(s)
44
- end
45
-
46
- def compile
47
- str = "picture " + @@org_picture + ";\n" + @@org_picture + " := currentpicture;\n"
48
- @pictures.each do
49
- |p| str = str + p.precompile + "\n"
50
- end
51
- str
52
- end
53
-
54
- end
55
-
56
- #module variable to be altered by drawables that
57
- #need a particular metapost macro input.
58
- @@picture_precompiler = PicturePrecompiler.new
59
-
60
- #module variable to be altered by drawables that
61
- #need a particular metapost macro input.
62
- @@Inputs = Macros.new
63
-
64
- #this is the origninal metapost picture that is
65
- #what actually gets displayed. You shouldn't use
66
- #this name for any other picture!
67
- @@org_picture = "ORIGINAL_PICTURE"
68
-
69
- #metapost file
70
- #A metapost file can contain many figures.
71
- #Notes: Filenames cannot contain underscores for view to work! <br>
72
- #compile_to_str has a dodgy backspace handler.
73
- class File < Object
74
-
75
- attr_writer :fname
76
-
77
- @@start_of_file = <<END_OF_STRING
78
- prologues := 2;
79
- filenametemplate "%j-%c.mps";
80
- verbatimtex
81
- %&latex
82
- \\documentclass{minimal}
83
- \\begin{document}
84
- etex
85
- END_OF_STRING
86
-
87
- #input 'sarith' so that metapost can read exponential notation
88
- def initialize(fname = nil)
89
- @figures = Array.new
90
- @fname = fname
91
- end
92
-
93
- #set a string at the very start of the file. For example, prologues
94
- #latex setup etc. The default string is: <br>
95
- #prologues := 2;<br>
96
- #filenametemplate "%j-%c.mps";<br>
97
- #verbatimtex<br>
98
- #%&latex<br>
99
- #\documentclass{minimal}<br>
100
- #\begin{document}<br>
101
- #etex<br>
102
- def startstring(str)
103
- @@start_of_file = str
104
- end
105
-
106
- #add a new figure to this mpost file
107
- def add_figure(f)
108
- @figures.push(f)
109
- end
110
-
111
- #returns the mp file as a str
112
- def compile_to_string
113
- str = @@start_of_file + @@Inputs.compile
114
- #save the original metapost picture
115
- str = str + @@picture_precompiler.compile
116
- @figures.each_index do
117
- |i| str = str + 'beginfig(' + (i+1).to_s + ");\n" + @figures[i].compile + "\n"
118
- end
119
- str = str + "end;\n"
120
- #remove the backspaces
121
- strback = str.gsub(/.[\b]/, '')
122
- if (strback==nil)
123
- return str
124
- else
125
- return strback
126
- end
127
- end
128
-
129
- #writes the string of metapost commands to a file named 'fname.mp'
130
- def compile_to_file(fname=@fname)
131
- @fname = fname
132
- IO::File.open(fname + '.mp','w') { |f| f.puts self.compile_to_string }
133
- end
134
-
135
- #calls compile_to_file and writes the
136
- #and copmiles the metapost commands if mpost is in the path
137
- def compile(fname=@fname)
138
- compile_to_file(fname)
139
- system('mpost ' + fname + '.mp')
140
- end
141
-
142
- #default view command is view_dvi
143
- def view
144
- view_dvi
145
- end
146
-
147
- #assumes that the file has already been compiled by metapost. ie compile_to_ps
148
- #has already been called. This assumes that the yap viewer and tex is in your path. Install
149
- #miktex to get these by default. <p>
150
- #Notes: Filenames cannot contain underscores for view_dvi to work. The "tex mproof" will
151
- #not work with underscores
152
- def view_dvi
153
- str = 'tex mproof'
154
- @figures.each_index { |i| str = str + ' ' + @fname + '.' + (i+1).to_s }
155
- system(str)
156
- system('yap mproof.dvi')
157
- end
158
-
159
- end
160
-
161
- #wrapper for the metapost figure
162
- #Figures actually become the figures that will to be viewed
163
- class Figure < Object
164
-
165
- def initialize
166
- @draw_commands = Array.new
167
- end
168
-
169
- def add_drawable(d)
170
- @draw_commands.push(d)
171
- end
172
-
173
- def compile
174
- str = String.new
175
- @draw_commands.each do |d|
176
- str = str + d.compile + "\n"
177
- end
178
- str = str + "endfig;\n"
179
- return str
180
- end
181
-
182
- end
183
-
184
- #end module RubyPost
185
- end
186
-
187
- #Add the compile functionality to Ruby Numeric. Calling
188
- #compile will add the cm, inch, bp metapost sizes to the
189
- #to_s
190
- class Numeric
191
- def cm
192
- @mpsize = 'cm'
193
- self
194
- end
195
- def inch
196
- @mpsize = 'inch'
197
- self
198
- end
199
- def bp
200
- @mpsize = 'bp'
201
- self
202
- end
203
- #A nifty thing here is that if you dont specify the size
204
- #if wont print anything
205
- def compile
206
- #@mpsize='cm' if(@mpsize==nil)
207
- to_s + @mpsize.to_s
208
- end
209
- end
210
-
211
- #Add the compile functionality to Ruby String class
212
- #it just calls to_s
213
- class String
214
- def compile
215
- to_s
216
- end
217
- end
1
+ module RubyPost
2
+
3
+ #base class for rubypost
4
+ class Object
5
+ #returns the string of metapost commmands
6
+ def compile
7
+ '%compile not implemented for ' + self.class.to_s
8
+ end
9
+ end
10
+
11
+ #stores the macros that particular drawbles need.
12
+ #This should really be a private class.
13
+ class Macros < Object
14
+
15
+ def initialize
16
+ @inputs = Hash.new
17
+ end
18
+
19
+ #uses hash to make sure we never input same thing twice
20
+ def add_input(s)
21
+ @inputs[s] = nil
22
+ end
23
+
24
+ def compile
25
+ str = String.new
26
+ @inputs.each_key do
27
+ |k| str = str + "input " + k + ";\n"
28
+ end
29
+ str
30
+ end
31
+
32
+ end
33
+
34
+ #Defines all the pictures at the front of the file
35
+ class PicturePrecompiler < Object
36
+
37
+ def initialize
38
+ @pictures = Array.new
39
+ end
40
+
41
+ def add_picture(s)
42
+ @pictures.push(s)
43
+ end
44
+
45
+ def compile
46
+ str = "picture " + @@org_picture + ";\n" + @@org_picture + " := currentpicture;\n"
47
+ @pictures.each do
48
+ |p| str = str + p.precompile + "\n"
49
+ end
50
+ str
51
+ end
52
+
53
+ end
54
+
55
+ #module variable to be altered by drawables that
56
+ #need a particular metapost macro input.
57
+ @@picture_precompiler = PicturePrecompiler.new
58
+
59
+ #module variable to be altered by drawables that
60
+ #need a particular metapost macro input.
61
+ @@Inputs = Macros.new
62
+
63
+ #this is the origninal metapost picture that is
64
+ #what actually gets displayed. You shouldn't use
65
+ #this name for any other picture!
66
+ @@org_picture = "ORIGINAL_PICTURE"
67
+
68
+ #metapost file
69
+ #A metapost file can contain many figures.
70
+ #Notes: Filenames cannot contain underscores for view to work! <br>
71
+ #compile_to_str has a dodgy backspace handler.
72
+ class File < Object
73
+
74
+ attr_writer :fname,
75
+ #name of div viewer program to use
76
+ :dvi_viewer,
77
+ #option for metapost command line
78
+ :metapost_options
79
+
80
+ @@start_of_file = <<END_OF_STRING
81
+ prologues := 2;
82
+ END_OF_STRING
83
+
84
+ #input 'sarith' so that metapost can read exponential notation
85
+ def initialize(fname = nil)
86
+ @figures = Array.new
87
+ @fname = fname
88
+ @dvi_viewer = 'yap'
89
+ @metapost_options = '-interaction=nonstopmode'
90
+ end
91
+
92
+ #add a new figure to this mpost file
93
+ def add_figure(f)
94
+ @figures.push(f)
95
+ end
96
+
97
+ #returns the mp file as a str
98
+ def compile_to_string
99
+ str = @@start_of_file + @@Inputs.compile
100
+ #save the original metapost picture
101
+ str = str + @@picture_precompiler.compile
102
+ @figures.each_index do
103
+ |i| str = str + 'beginfig(' + (i+1).to_s + ");\n" + @figures[i].compile + "\n"
104
+ end
105
+ str = str + "end;\n"
106
+ #remove the backspaces
107
+ strback = str.gsub(/.[\b]/, '')
108
+ if (strback==nil)
109
+ return str
110
+ else
111
+ return strback
112
+ end
113
+ end
114
+
115
+ #writes the string of metapost commands to a file named 'fname.mp'
116
+ def compile_to_file(fname=@fname)
117
+ @fname = fname
118
+ IO::File.open(fname + '.mp','w') { |f| f.puts self.compile_to_string }
119
+ end
120
+
121
+ #calls compile_to_file and writes the
122
+ #and copmiles the metapost commands if mpost is in the path
123
+ def compile(fname=@fname)
124
+ compile_to_file(fname)
125
+ system('mpost ' + @metapost_options + ' ' + fname + '.mp')
126
+ end
127
+
128
+ #default view command is view_dvi
129
+ def view
130
+ view_dvi
131
+ end
132
+
133
+ #assumes that the file has already been compiled by metapost. ie compile_to_ps
134
+ #has already been called. This assumes that the yap viewer and tex is in your path. Install
135
+ #miktex to get these by default. <p>
136
+ #Notes: Filenames cannot contain underscores for view_dvi to work. The "tex mproof" will
137
+ #not work with underscores
138
+ def view_dvi
139
+ str = 'tex mproof'
140
+ @figures.each_index { |i| str = str + ' ' + @fname + '.' + (i+1).to_s }
141
+ system(str)
142
+ system(@dvi_viewer + ' mproof.dvi')
143
+ end
144
+
145
+ end
146
+
147
+ #wrapper for the metapost figure
148
+ #Figures actually become the figures that will to be viewed
149
+ class Figure < Object
150
+
151
+ def initialize
152
+ @draw_commands = Array.new
153
+ end
154
+
155
+ def add_drawable(d)
156
+ @draw_commands.push(d)
157
+ end
158
+
159
+ def compile
160
+ str = String.new
161
+ @draw_commands.each do |d|
162
+ str = str + d.compile + "\n"
163
+ end
164
+ str = str + "endfig;\n"
165
+ return str
166
+ end
167
+
168
+ end
169
+
170
+ #end module RubyPost
171
+ end
172
+
173
+ #Add the compile functionality to Ruby Numeric. Calling
174
+ #compile will add the cm, inch, bp metapost sizes to the
175
+ #to_s
176
+ class Numeric
177
+ def cm
178
+ @mpsize = 'cm'
179
+ self
180
+ end
181
+ def inch
182
+ @mpsize = 'inch'
183
+ self
184
+ end
185
+ def bp
186
+ @mpsize = 'bp'
187
+ self
188
+ end
189
+ #A nifty thing here is that if you dont specify the size
190
+ #if wont print anything
191
+ def compile
192
+ #@mpsize='cm' if(@mpsize==nil)
193
+ to_s + @mpsize.to_s
194
+ end
195
+ end
196
+
197
+ #Add the compile functionality to Ruby String class
198
+ #it just calls to_s
199
+ class String
200
+ def compile
201
+ to_s
202
+ end
203
+ end