This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathingredient.rb
executable file
·267 lines (232 loc) · 5.65 KB
/
ingredient.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# ingredient.rb
# Copyright (c) UnaMesa Association 2004-2008
# License: Creative Commons Attribution ShareAlike 3.0 License http://creativecommons.org/licenses/by-sa/3.0/
require 'cgi'
require 'tempfile'
require 'tiddler'
require 'splitter'
class String
def to_file(file_name) #:nodoc:
File.open(file_name,"w") { |f| f << self }
end
end
class Ingredient
def initialize(line, type, attributes=nil, raw=false)
@attributes = attributes
@line = line
@filename = line
@type = type
@raw = raw
end
def filename
@filename
end
def attributes
@attributes
end
def type
@type
end
def raw
@raw
end
def Ingredient.stripcomments
@@stripcomments
end
def Ingredient.stripcomments=(stripcomments)
@@stripcomments = stripcomments
end
def Ingredient.compress
@@compress
end
def Ingredient.compress=(compress)
@@compress = compress
end
def Ingredient.compresstype=(compresstype)
@@compresstype = compresstype
end
def Ingredient.compressplugins
@@compressplugins
end
def Ingredient.compressplugins=(compressplugins)
@@compressplugins = compressplugins
end
def Ingredient.compressdeprecated
@@compressdeprecated
end
def Ingredient.compressdeprecated=(compressdeprecated)
@@compressdeprecated = compressdeprecated
end
def Ingredient.compresshead
@@compresshead
end
def Ingredient.compresshead=(compresshead)
@@compresshead = compresshead
end
def Ingredient.keepallcomments
@@keepallcomments
end
def Ingredient.keepallcomments=(keepallcomments)
@@keepallcomments = keepallcomments
end
def to_s
#"to_s_#{@type}".to_sym
subtype = type.split('.')
if(@type == "tline")
return @line
elsif(@raw == true)
return @line
end
@filename = Recipe.injectEnv(@filename)
@filename = @filename.gsub("%20", " ")
if(subtype[0] == "list")
elsif(subtype[0] == "tiddler")
if(@filename =~ /\.tiddler$/)
return to_s_retiddle(subtype[0])
elsif(@filename =~ /\.html$/)
out = ''
tiddlers = Splitter.extractTiddlers(@filename,URI.parse(@filename).fragment.split("%20"))
tiddlers.each do |tiddler|
out << tiddler.to_div
end
return out
else
return to_s_tiddler
end
elsif(subtype[0] == "shadow")
if(@filename =~ /\.tiddler$/)
return to_s_retiddle(subtype[0])
elsif(@filename =~ /\.tid$/)
return to_s_tiddler
else
# have a non-tidler in the shadow area, so output it raw
return to_s_line(subtype[0])
end
elsif(subtype[0] == "plugin")
return to_s_plugin
else
if(@filename =~ /\.tiddler$/)
# not in tiddler, shadow or plugin, so output raw content of tiddler
return to_s_raw(subtype[0])
elsif(@filename =~ /\.tid$/)
return to_s_raw_tid(subtype[0])
else
return to_s_line(subtype[0])
end
end
end
protected
def to_s_tiddler
compress = false
compress = true if @attributes == "-c"
tiddler = Tiddler.new
tiddler.load(@filename)
if @attributes
params = @attributes.split(" ")
params.each do |param|
if(param =~ /\.js$/)
jsFile = File.join(File.dirname(@filename),param)
tiddler.appendJs(jsFile)
end
end
end
tiddler.setAttributes(@attributes)
return tiddler.to_div("tiddler",true,compress)
end
def to_s_plugin
tiddler = Tiddler.new
tiddler.load(@filename)
tiddler.setAttributes(@attributes)
return tiddler.to_plugin
end
def to_s_retiddle(subtype)
tiddler = Tiddler.new
tiddler.loadDiv(@filename)
tiddler.setAttributes(@attributes)
return tiddler.to_div(subtype)
end
def to_s_raw(subtype)
tiddler = Tiddler.new
tiddler.loadDiv(@filename)
tiddler.setAttributes(@attributes)
return tiddler.to_raw(subtype)
end
def to_s_raw_tid(subtype)
tiddler = Tiddler.new
tiddler.load(@filename)
tiddler.setAttributes(@attributes)
return tiddler.to_raw(subtype)
end
def to_s_line(subtype)
open(@filename) do |infile|
out = ""
infile.each_line do |line|
if(@@keepallcomments)
out << line
elsif(@@stripcomments)
out << line unless(line.strip =~ /^\/\//)
else
out << line unless(line.strip =~ /^\/\/#/)
end
end
if(@@compress=="f" && (subtype == "js" || subtype =='jquery') && @filename !~ /\/Lingo/ && @filename !~ /\/locale/)
puts "commpressing: " + @filename
out = Ingredient.compressor(out)
end
if(subtype == "jshead" && @@compresshead == true)
if(@filename !~ /\.min\./)
out = Ingredient.compressor(out)
end
if(@filename !~ /\.pack\./)
comment = ""
re = /^\/\*(.*?)\*\//m
m = re.match(out)
while m
comment += m[0] + "\n"
m = re.match(m.post_match)
end
out = comment + Ingredient.packr(out)
end
end
return out
end
end
public
def Ingredient.compressor(input)
inputfile = Tempfile.new('compress')
inputfile.print(input)
inputfile.close
outputfile = Tempfile.new('compress')
outputfile.close
if(@@compresstype == "rhino")
done = system("java -jar custom_rhino.jar -c #{inputfile.path} > #{outputfile.path} 2>&1")
else
done = system("java -jar yuicompressor-2.4.2.jar #{inputfile.path} > #{outputfile.path} --type js")
end
if(done)
compressed = File.read(outputfile.path)
else
compressed = input
if(@@compresstype == "rhino")
STDERR.puts("Could not compress with custom_rhino.jar.")
else
STDERR.puts("Could not compress with yuicompressor.jar.")
end
if(@@compress!="")
STDERR.puts("Cooking failed.")
exit
end
end
return compressed
end
def Ingredient.packr(input)
begin
require 'packr'
compressed = Packr.pack(input, :shrink_vars => true, :base62 => true)
rescue LoadError
STDERR.puts("Missing Ruby gem Packr - install using: gem install packr. Cooking failed")
exit
end
return compressed
end
end