Class: Shoes

Inherits:
Object
  • Object
show all
Defined in:
lib/shoes/pack.rb,
lib/shoes/image.rb,
lib/shoes.rb

Constant Summary

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.languageObject

Returns the value of attribute language



39
40
41
# File 'lib/shoes.rb', line 39

def language
  @language
end

.localeObject

Returns the value of attribute locale



39
40
41
# File 'lib/shoes.rb', line 39

def locale
  @locale
end

Class Method Details

.args!Object



337
338
339
340
341
342
343
# File 'lib/shoes.rb', line 337

def self.args!
  if RUBY_PLATFORM !~ /darwin/ and ARGV.empty?
    Shoes.splash
  end
  OPTS.parse! ARGV
  ARGV[0] or true
end

.image_cache_path(hash, ext) ⇒ Object



7
8
9
10
11
# File 'lib/shoes/image.rb', line 7

def self.image_cache_path hash, ext
  dir = File.join(CACHE_DIR, hash[0,2])
  Dir.mkdir(dir) unless File.exists?(dir)
  File.join(dir, hash[2..-1]) + ext.downcase
end

.image_temp_path(uri, uext) ⇒ Object



4
5
6
# File 'lib/shoes/image.rb', line 4

def self.image_temp_path uri, uext
  File.join(Dir::tmpdir, "#{uri.host}-#{Time.now.usec}" + uext)
end

.make_help_pageObject



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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/shoes/help.rb', line 348

def Shoes.make_help_page
  font "#{DIR}/fonts/Coolvetica.ttf" unless Shoes::FONTS.include? "Coolvetica"
  proc do
    extend Shoes::Manual
    docs = load_docs Shoes::Manual.path

    style(Shoes::Image, :margin => 8, :margin_left => 100)
    style(Shoes::Code, :stroke => "#C30")
    style(Shoes::LinkHover, :stroke => green, :fill => nil)
    style(Shoes::Para, :size => 12, :stroke => "#332")
    style(Shoes::Tagline, :size => 12, :weight => "bold", :stroke => "#eee", :margin => 6)
    style(Shoes::Caption, :size => 24)
    background "#ddd".."#fff", :angle => 90

    stack do
      background black
      stack :margin_left => 118 do
        para "The Shoes Manual", :stroke => "#eee", :margin_top => 8, :margin_left => 17, 
          :margin_bottom => 0
        @title = title docs[0][0], :stroke => white, :margin => 4, :margin_left => 14,
          :margin_top => 0, :font => "Coolvetica"
      end
      background "rgb(66, 66, 66, 180)".."rgb(0, 0, 0, 0)", :height => 0.7
      background "rgb(66, 66, 66, 100)".."rgb(255, 255, 255, 0)", :height => 20, :bottom => 0
    end
    @doc =
      stack :margin_left => 130, :margin_top => 20, :margin_bottom => 50, :margin_right => 50 + gutter,
        &dewikify(docs[0][-1]['description'], true)
    add_next_link(0, -1)
    stack :top => 80, :left => 0, :attach => Window do
      @toc = {}
      stack :margin => 12, :width => 130, :margin_top => 20 do
        docs.each do |sect_s, sect_h|
          sect_cls = sect_h['class']
          para strong(link(sect_s, :stroke => black) { open_section(sect_s) }),
            :size => 11, :margin => 4, :margin_top => 0
          @toc[sect_cls] =
            stack :hidden => @toc.empty? ? false : true do
              links = sect_h['sections'].map do |meth_s, meth_h|
                [link(meth_s) { open_methods(meth_s) }, "\n"]
              end.flatten
              links[-1] = {:size => 9, :margin => 4, :margin_left => 10}
              para *links
            end
        end
      end
      stack :margin => 12, :width => 118, :margin_top => 6 do
        background "#330", :curve => 4
        para "Not finding it? Try ", strong(link("Search", :stroke => white) { show_search }), "!", :stroke => "#ddd", :size => 9, :align => "center", :margin => 6
      end
      stack :margin => 12, :width => 118 do
        inscription "Shoes #{Shoes::RELEASE_NAME}\nRevision: #{Shoes::REVISION}",
          :size => 7, :align => "center", :stroke => "#999"
      end
    end
    image :width => 120, :height => 120, :top => -18, :left => 6 do
      image "#{DIR}/static/shoes-icon.png", :width => 100, :height => 100, :top => 10, :left => 10 
      glow 2
    end
  end
rescue => e
  p e.message
  p e.class
end

.make_packObject



156
157
158
159
# File 'lib/shoes.rb', line 156

def self.make_pack
  require 'shoes/pack'
  Shoes.app(:width => 500, :height => 380, :resizable => false, &PackMake)
end

.manual_as(format, *args) ⇒ Object



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
# File 'lib/shoes.rb', line 190

def self.manual_as format, *args
  require 'shoes/search'
  require 'shoes/help'

  case format
  when :shoes
    Shoes.app(:width => 720, :height => 640, &Shoes::Help)
  else
    extend Shoes::Manual
    man = self
    dir, = args
    FileUtils.mkdir_p File.join(dir, 'static')
    FileUtils.cp "static/shoes-icon.png", "#{dir}/static"
    %w[manual.css code_highlighter.js code_highlighter_ruby.js].
      each { |x| FileUtils.cp "static/#{x}", "#{dir}/static" }
    html_bits = proc do
      proc do |sym, text|
      case sym when :intro
        div.intro { p { self << man.manual_p(text, dir) } }
      when :code
        pre { code.rb text.gsub(/^\s*?\n/, '') }
      when :colors
        color_names = (Shoes::COLORS.keys*"\n").split("\n").sort
        color_names.each do |color|
          c = Shoes::COLORS[color.intern]
          f = c.dark? ? "white" : "black"
          div.color(:style => "background: #{c}; color: #{f}") { h3 color; p c }
        end
      when :index
        tree = man.class_tree
        shown = []
        i = 0
        index_p = proc do |k, subs|
          unless shown.include? k
            i += 1
            p "#{k}", :style => "margin-left: #{20*i}px"
            subs.uniq.sort.each do |s|
              index_p[s, tree[s]]
            end if subs
            i -= 1
            shown << k
          end
        end
        tree.sort.each &index_p
      #   index_page
      when :list
        ul { text.each { |x| li { self << man.manual_p(x, dir) } } }
      else
        send(TITLES[sym] || :p) { self << man.manual_p(text, dir) }
      end
      end
    end

    docs = load_docs(Shoes::Manual::PATH)
    sections = docs.map { |x,| x }

    docn = 1
    docs.each do |title1, opt1|
      subsect = opt1['sections'].map { |x,| x }
      menu = sections.map do |x|
        [x, (subsect if x == title1)]
      end

      path1 = File.join(dir, title1.gsub(/\W/, ''))
      make_html("#{path1}.html", title1, menu) do
        h2 "The Shoes Manual"
        h1 title1
        man.wiki_tokens opt1['description'], true, &instance_eval(&html_bits)
        p.next { text "Next: "
          a opt1['sections'].first[1]['title'], :href => "#{opt1['sections'].first[0]}.html" }
      end

      optn = 1
      opt1['sections'].each do |title2, opt2|
        path2 = File.join(dir, title2)
        make_html("#{path2}.html", opt2['title'], menu) do
          h2 "The Shoes Manual"
          h1 opt2['title']
          man.wiki_tokens opt2['description'], true, &instance_eval(&html_bits)
          opt2['methods'].each do |title3, desc3|
            sig, val = title3.split(/\s+»\s+/, 2)
            aname = sig[/^[^(=]+=?/].gsub(/\s/, '').downcase
            a :name => aname
            div.method do
              a sig, :href => "##{aname}"
              text " » #{val}" if val
            end
            div.sample do
              man.wiki_tokens desc3, &instance_eval(&html_bits)
            end
          end
          if opt1['sections'][optn]
            p.next { text "Next: "
              a opt1['sections'][optn][1]['title'], :href => "#{opt1['sections'][optn][0]}.html" }
          elsif docs[docn]
            p.next { text "Next: "
              a docs[docn][0], :href => "#{docs[docn][0].gsub(/\W/, '')}.html" }
          end
          optn += 1
        end
      end

      docn += 1
    end
  end
end


185
186
# File 'lib/shoes.rb', line 185

def self.manual_link(sect)
end

.manual_p(str, path) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/shoes.rb', line 161

def self.manual_p(str, path)
  str.gsub(/\n+\s*/, " ").
    gsub(/&/, '&amp;').gsub(/>/, '&gt;').gsub(/>/, '&lt;').gsub(/"/, '&quot;').
    gsub(/`(.+?)`/m, '<code>\1</code>').gsub(/\[\[BR\]\]/i, "<br />\n").
    gsub(/\^(.+?)\^/m, '\1').
    gsub(/'''(.+?)'''/m, '<strong>\1</strong>').gsub(/''(.+?)''/m, '<em>\1</em>').
    gsub(/\[\[(http:\/\/\S+?)\]\]/m, '<a href="\1" target="_new">\1</a>').
    gsub(/\[\[(http:\/\/\S+?) (.+?)\]\]/m, '<a href="\1" target="_new">\2</a>').
    gsub(/\[\[(\S+?)\]\]/m) do
      ms, mn = $1.split(".", 2)
      if mn
        '<a href="' + ms + '.html#' + mn + '">' + mn + '</a>'
      else
        '<a href="' + ms + '.html">' + ms + '</a>'
      end
    end.
    gsub(/\[\[(\S+?) (.+?)\]\]/m, '<a href="\1.html">\2</a>').
    gsub(/\!(\{[^}\n]+\})?([^!\n]+\.\w+)\!/) do
      x = "static/#$2"
      FileUtils.cp("#{DIR}/#{x}", "#{path}/#{x}") if File.exists? "#{DIR}/#{x}"
      '<img src="' + x + '" />'
    end
end

.mount(path, meth, &blk) ⇒ Object



311
312
313
# File 'lib/shoes.rb', line 311

def self.mount(path, meth, &blk)
  @mounts << [path, meth || blk]
end

.read_file(path) ⇒ Object



392
393
394
395
396
397
398
# File 'lib/shoes.rb', line 392

def self.read_file path
  if RUBY_VERSION =~ /^1\.9/
    File.open(path, 'r:utf-8') { |f| f.read }
  else
    File.read(path)
  end
end

.run(path) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/shoes.rb', line 317

def self.run(path)
  uri = URI(path)
  @mounts.each do |mpath, rout|
    m, *args = *path.match(/^#{mpath}$/)
    if m
      unless rout.is_a? Proc
        rout = rout[0].instance_method(rout[1])
      end
      return [rout, args]
    end
  end
  case uri.path when "/"
    [nil]
  when SHOES_URL_RE
    [proc { eval(URI("http://#$1:53045#$2").read) }]
  else
    [NotFound]
  end
end

.setup(&blk) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/shoes.rb', line 90

def self.setup &blk
  require 'shoes/setup'
  line = caller[0]
  return if @setups[line]
  script = line[/^(.+?):/, 1]
  set = Shoes::Setup.new(script, &blk)
  @setups[line] = true
  unless set.no_steps?
    raise SettingUp
  end
end

.show_logObject



301
302
303
304
305
306
307
308
309
# File 'lib/shoes.rb', line 301

def self.show_log
  require 'shoes/log'
  return if @log_app and Shoes.APPS.include? @log_app
  @log_app =
    Shoes.app do
      extend Shoes::LogWindow
      setup
    end
end

.show_manualObject



297
298
299
# File 'lib/shoes.rb', line 297

def self.show_manual
  manual_as :shoes
end

.show_selectorObject



102
103
104
105
# File 'lib/shoes.rb', line 102

def self.show_selector
  fname = ask_open_file
  Shoes.visit(fname) if fname
end

.splashObject



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
# File 'lib/shoes.rb', line 107

def self.splash
  font "#{DIR}/fonts/Lacuna.ttf"
  Shoes.app :width => 400, :height => 300, :resizable => false do  
    style(Para, :align => "center", :weight => "bold", :font => "Lacuna Regular", :size => 13)
    style(Link, :stroke => yellow, :underline => nil)
    style(LinkHover, :stroke => yellow, :fill => nil)

    x1 = 77; y1 = 122
    x2 = 148; y2 = -122
    x3 = 245; y3 = 0

    nofill
    strokewidth 40.0

    @waves = stack :top => 0, :left => 0

    stack :margin => 22 do
      para "Welcome to", :stroke => "#DFA", :margin => 0
      para "SHOES", :size => 48, :stroke => "#DFA", :margin_top => 0
      stack do
        background black(0.2), :curve => 8
        para link("Open an App.") { Shoes.show_selector and close }, :margin => 12, :margin_bottom => 4
        para link("Read the Manual.") { Shoes.show_manual and close }, :margin => 12 
      end
      inscription "Alt-Slash opens the console.", :stroke => "#DFA", :align => "center"
    end

    animate(10) do |ani|
      a = Math.sin(ani * 0.02) * 20
      @waves.clear do
        background white
        y = -30
        16.times do |i|
          shape do
            move_to x = (-300 - (i*(a*0.8))), y
            c = (a + 14) * 0.01
            stroke rgb(i * 0.06, c + 0.1, 0.1, 1.0 - (ani * 0.0003))
            4.times do
              curve_to x1 + x, (y1-(i*a)) + y, x2 + x, (y2+(i*a)) + y, x3 + x, y3 + y
              x += x3
            end
          end
          y += 30
        end
      end
    end
  end
end

.uri(str) ⇒ Object



345
346
347
348
349
350
351
# File 'lib/shoes.rb', line 345

def self.uri(str)
  if str =~ SHOES_URL_RE
    URI("http://#$1:53045#$2")
  else
    URI(str) rescue nil
  end
end

.url(path, meth) ⇒ Object



400
401
402
# File 'lib/shoes.rb', line 400

def self.url(path, meth)
  Shoes.mount(path, [self, meth])
end

.visit(path) ⇒ Object



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
386
387
388
389
390
# File 'lib/shoes.rb', line 353

def self.visit(path)
  uri = Shoes.uri(path)

  case uri
  when URI::HTTP
    str = uri.read
    if str !~ /Shoes\.app/
      Shoes.app do
        eval(uri.read)
      end
    else
      eval(uri.read)
    end
  else
    path = File.expand_path(path.gsub(/\\/, "/"))
    if path =~ /\.shy$/
      require 'shoes/shy'
      base = File.basename(path, ".shy")
      tmpdir = "%s/shoes-%s.%d" % [Dir.tmpdir, base, $$]
      shy = Shy.x(path, tmpdir)
      Dir.chdir(tmpdir)
      Shoes.debug "Loaded SHY: #{shy.name} #{shy.version} by #{shy.creator}"
      path = shy.launch
    else
      Dir.chdir(File.dirname(path))
      path = File.basename(path)
    end

    $0.replace path

    code = read_file(path)
    eval(code, TOPLEVEL_BINDING, path)
  end
rescue SettingUp
rescue Object => e
  error(e)
  show_log
end

Instance Method Details

#snapshot(options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/shoes/image.rb', line 12

def snapshot(options = {}, &block)
  options[:format]   ||= :svg

  options[:filename] ||= ( tf_path = ( require 'tempfile'
               tf = Tempfile.new(File.basename(__FILE__)).path ))

  _snapshot options do
    block.call
  end
  return File.read(options[:filename])
ensure
  File.unlink(tf_path) if tf_path
end