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
/
recipe.rb
executable file
·385 lines (349 loc) · 10.5 KB
/
recipe.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# recipe.rb
# Copyright (c) UnaMesa Association 2004-2008
# License: Creative Commons Attribution ShareAlike 3.0 License http://creativecommons.org/licenses/by-sa/3.0/
require 'ingredient'
require "fileutils"
require 'net/http'
require 'uri'
class Recipe
def initialize(filename, outdir=nil, isTemplate=false, outputfile=nil)
@outdir = outdir ||= ""
@outputfile = outputfile ||= ""
@ingredients = Array.new
@addons = Hash.new
@tiddlers = Hash.new
@defaultTiddlersFilename = ""
@filename = Recipe.injectEnv(filename)
@dirname = File.dirname(@filename)
open(@filename) do |file|
file.each_line { |line| genIngredient(@dirname, line, isTemplate) }
end
end
def scanrecipe
if(@ingredients.length > 0)
@ingredients.each do |ingredient|
if(ingredient.type == "list")
if(@addons.has_key?(ingredient.filename))
@addons.fetch(ingredient.filename).each{ |ingredient| writeToDishScan(ingredient) }
end
else
writeToDishScan(ingredient)
end
end
end
end
def cook
puts "Creating file: " + outfilename
if(@ingredients.length > 0)
File.open(outfilename, File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
@ingredients.each do |ingredient|
if(ingredient.type == "list")
if(ingredient.filename=="title")
# write the title from the shadow tiddlers if available
title = ""
if(@tiddlers["SiteTitle"])
title << @tiddlers["SiteTitle"].contents
title << " - " if(@tiddlers["SiteSubtitle"])
end
title << @tiddlers["SiteSubtitle"].contents if(@tiddlers["SiteSubtitle"])
out << title + "\n" if title
end
if(@@splash && ingredient.filename=="posthead")
writeSplashStyles(out)
end
if(@@splash && ingredient.filename=="prebody")
writeSplash(out)
end
type = ingredient.filename
block = ""
if(@addons.has_key?(ingredient.filename))
@addons.fetch(ingredient.filename).each do |ingredient|
type = ingredient.type
b = writeToDish(block, ingredient)
block += b if(b)
end
end
if((Ingredient.compress=~/[pry]+/ && type == "js") || (Ingredient.compressplugins=~/[pry]+/ && type == "jquery") || (Ingredient.compressdeprecated=~/[pry]+/ && type == "jsdeprecated"))
if(Ingredient.compress=~/[pry]+/ || Ingredient.compressplugins=~/[pry]+/ || Ingredient.compressdeprecated=~/[pry]+/)
block = Ingredient.compressor(block)
if(Ingredient.compress=~/.?p.+/ || Ingredient.compressplugins=~/.?p.+/ || Ingredient.compressdeprecated=~/.?p.+/)
block = Ingredient.packr(block)
end
end
end
out << block
else
writeToDish(out, ingredient)
end
end
end
end
@addons.fetch("copy", Array.new).each { |ingredient| copyFile(ingredient) }
end
def Recipe.env(name)
ENV[name] || ''
end
def Recipe.injectEnv(path)
while path =~ /\$.*?\//
path = env($&[1...-1]) + '/' + $'
end
return path
end
def Recipe.quiet
@@quiet
end
def Recipe.quiet=(quiet)
@@quiet = quiet
end
def Recipe.ignorecopy
@@ignorecopy
end
def Recipe.ignorecopy=(ignorecopy)
@@ignorecopy = ignorecopy
end
def Recipe.root
@@root
end
def Recipe.root=(root)
@@root = root
end
def Recipe.plugins
@@plugins
end
def Recipe.plugins=(plugins)
@@plugins = plugins
end
def Recipe.splash
@@splash
end
def Recipe.splash=(splash)
@@splash = splash
end
def Recipe.section
@@section
end
def Recipe.section=(section)
@@section = section
end
protected
def outdir
@outdir.empty? ? "" : File.join(@outdir, "")
end
def outfilename
if(@outputfile == "")
return outdir + Recipe.env('TW_COOK_OUTPUT_PREFIX') + File.basename(@filename.sub(".recipe", ""))
else
return outdir + Recipe.env('TW_COOK_OUTPUT_PREFIX') + @outputfile
end
end
def ingredients
@ingredients
end
def addons
@addons
end
def genIngredient(dirname, line, isTemplate=false)
if(isTemplate)
if(line =~ /<!--@@.*@@-->/)
@ingredients << Ingredient.new(line.strip.slice(6..-6), "list")
elsif(line =~ /<!--@@.*@@-->/)
@ingredients << Ingredient.new(line.strip.slice(9..-9), "list")
elsif(line =~ /<!--<<.*>>-->/)
item = line.strip.slice(6..-6)
c = item.index(' ')
if(c != nil)
item = item[(c + 1)...item.length].strip
end
@ingredients << Ingredient.new(item, "list")
else
@ingredients << Ingredient.new(line.sub("\r", ""), "tline")
end
else
if(line.strip == "" || line[0, 1]=='#')
return
end
if(line =~ /@.*@/)
@ingredients << Ingredient.new(line.strip.slice(1..-2), "list")
elsif(line =~ /template\:/)
value = line.sub(/template\:/, "").strip
if(value =~ /^https?/ || value[0,1]=='/')
file = value
else
file = File.join(dirname,value)
end
loadSubrecipe(file,true)
elsif(line =~ /recipe\:/)
value = line.sub(/recipe\:/, "").strip
if(value =~ /^https?/ || value[0,1]=='/')
file = value
else
file = File.join(dirname,value)
end
loadSubrecipe(file,false)
elsif(line =~ /\:/)
c = line.index(':')
key = line[0, c].strip
value = line[(c + 1)...line.length].strip
d = value.index('.')
if(d != nil)
c = value[(d + 1)...value.length].strip.index(' ')
if(c != nil)
c += d + 1
end
else
c = value.index(' ')
end
if(c != nil)
attributes = value[(c + 1)...value.length].strip
value = value[0, c].strip
end
if(value =~ /^https?/ || value[0,1]=='/')
file = value
else
file = File.join(dirname,value)
end
addAddOns(key, file, attributes)
loadSubrecipe(file + ".deps",false) if File.exists?(file + ".deps")
elsif(line =~ /\=/)
c = line.index('=')
key = line[0, c].strip
value = line[(c + 1)...line.length]
addAddOns(key, value, nil, true)
#@ingredients << Ingredient.new(value, "text")
else
file = File.join(dirname, line.chomp)
@ingredients << Ingredient.new(file, "line")
loadSubrecipe(file + ".deps",false) if(File.exists?(file + ".deps"))
end
end
end
def loadSubrecipe(subrecipename, isTemplate)
recipe = Recipe.new(subrecipename, @outdir, isTemplate)
@ingredients = @ingredients + recipe.ingredients
recipe.addons.each { |key, value, attributes| addAddOns(key, value, attributes) }
end
def addAddOns(key, value, attributes=nil, raw=false)
addonarray = @addons.fetch(key, Array.new)
if(value.class == Array)
addonarray = addonarray + value
elsif(value.class == Ingredient)
addonarray.push(value)
else
ingredient = Ingredient.new(value, key, attributes, raw)
addonarray.push(ingredient)
end
@addons.store(key, addonarray)
end
def writeToDishScan(ingredient)
if (!ingredient.is_a? String)
if(ingredient.type=="shadow" || ingredient.type=="tiddler")
# save copies of all the shadow tiddlers in scan pass
name = File.basename(ingredient.filename,".tiddler")
if(Tiddler.looksLikeShadow?(ingredient.filename))
tiddler = Tiddler.new
tiddler.loadDiv(ingredient.filename)
if(Tiddler.isShadow?(tiddler.title))
@tiddlers[tiddler.title] = tiddler
if(tiddler.title=="DefaultTiddlers")
@defaultTiddlersFilename = ingredient.filename
end
end
end
end
end
end
def writeToDish(outfile, ingredient)
if (!ingredient.is_a? String)
if(ingredient.type == "title")
return if(@tiddlers["SiteTitle"]||@tiddlers["SiteSubtitle"]) # don't write the title if it is available from the tiddlers
end
end
return if(@@section!="" && @@section!=ingredient.type)
if(outfile.is_a? String)
outfile = ingredient.to_s
else
puts "Writing: " + ingredient.filename if !@@quiet && ingredient.type!="tline" && ingredient.raw==false
outfile << ingredient
end
end
def writeSplashStyles(out)
out << "<style type=\"text/css\">\n"
out << "#contentWrapper {display:none;}\n"
out << "#splashScreen {display:block;}\n"
out << ".title {color:#841;}\n"
out << ".subtitle {color:#666;}\n"
out << ".header {background:#04b;}\n"
out << ".headerShadow {color:#000;}\n"
out << ".headerShadow a {font-weight:normal; color:#000;}\n"
out << ".headerForeground {color:#fff;}\n"
out << ".headerForeground a {font-weight:normal; color:#8cf;}\n"
out << ".shadow .title {color:#666;}\n"
out << "</style>\n"
end
def writeSplash(out)
pageTemplate = @tiddlers["PageTemplate"]
viewTemplate = @tiddlers["ViewTemplate"]
return if !pageTemplate || !viewTemplate
sitetitle = @tiddlers["SiteTitle"]
sitetitle = sitetitle.contents if sitetitle
sitesubtitle = @tiddlers["SiteSubtitle"]
sitesubtitle = sitesubtitle.contents if sitesubtitle
defaultTiddlers = Array.new
if(@tiddlers["DefaultTiddlers"])
list = @tiddlers["DefaultTiddlers"].contents
items = list.split(" ")
count = items.length
i = 0
while(i<count)
x = items[i].sub("[[","").sub("]]","");
defaultTiddlers.push(x)
i = i+1
end
end
splash = pageTemplate.contents
#puts "pageTemplate:"+splash
tiddlers = ""
defaultTiddlers.each do |title|
tiddler = Tiddler.new
filename = @defaultTiddlersFilename.sub(/DefaultTiddlers/,title)
if(!Tiddler.isShadow?(title))
filename = filename.sub(/shadows/,"content")
end
tiddler.loadDiv(filename);
tiddlers += tiddler.to_html(viewTemplate.contents)
end
splash = splash.gsub(/<!--\{\{\{-->/,"");
splash = splash.gsub(/<!--\}\}\}-->/,"");
#splash = splash.gsub(/<div id='/,"<div id='s_")
splash = splash.gsub(/<span class='siteTitle' refresh='content' tiddler='SiteTitle'><\/span>/,"<span class=\"siteTitle\">#{sitetitle}</span>")
splash = splash.gsub(/<span class='siteSubtitle' refresh='content' tiddler='SiteSubtitle'><\/span>/,"<span class=\"siteSubtitle\">#{sitesubtitle}</span>")
splash = splash.sub(/<div id='tiddlerDisplay'><\/div>/,"<div id=\"s_tiddlerDisplay\">#{tiddlers}</div>")
splash = splash.gsub(/ macro='[\w \.\[\]:]*'/,"")
splash = splash.gsub(/<div class='(\w*)'/,"<div class=\"\\1\"")
splash = splash.gsub(/<div id='(\w*)'/,"<div id=\"\\1\"")
#puts "splash:"+splash
out << "<div id=\"splashScreen\">\n"
out << splash
out << "</div>\n"
end
def copyFile(ingredient)
if(!@@ignorecopy)
puts "Copying: " + ingredient.filename if(!@@quiet)
if ingredient.filename =~ /^https?/
downloadFile(ingredient.filename)
else
FileUtils.copy(ingredient.filename, File.join(outdir, File.basename(ingredient.filename)))
end
end
end
private
def downloadFile(url)
uri = URI.parse(url)
Net::HTTP.start(uri.host) { |http|
resp = http.get(uri.path)
open(File.join(outdir, File.basename(url)), "wb") { |file|
file.write(resp.body)
}
}
end
end