Class: ShowOff

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/showoff.rb

Constant Summary collapse

Version =
VERSION = '0.4.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ ShowOff

Returns a new instance of ShowOff.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/showoff.rb', line 42

def initialize(app=nil)
  super(app)
  puts dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  if Dir.pwd == dir
    options.pres_dir = dir + '/example'
    @root_path = "."
  else
    options.pres_dir = Dir.pwd
    @root_path = ".."
  end
  @cached_image_size = {}
  puts options.pres_dir
  @pres_name = options.pres_dir.split('/').pop
  require_ruby_files
end

Instance Attribute Details

#cached_image_sizeObject (readonly)

Returns the value of attribute cached_image_size.



36
37
38
# File 'lib/showoff.rb', line 36

def cached_image_size
  @cached_image_size
end

Class Method Details

.do_static(what) ⇒ Object



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

def self.do_static(what)
  what = "index" if !what

  # Nasty hack to get the actual ShowOff module
  showoff = ShowOff.new
  while !showoff.is_a?(ShowOff)
    showoff = showoff.instance_variable_get(:@app)
  end
  name = showoff.instance_variable_get(:@pres_name)
  path = showoff.instance_variable_get(:@root_path)
  data = showoff.send(what, true)
  if data.is_a?(File)
    FileUtils.cp(data.path, "#{name}.pdf")
  else
    out  = "#{path}/#{name}/static"
    # First make a directory
    FileUtils.makedirs(out)
    # Then write the html
    file = File.new("#{out}/index.html", "w")
    file.puts(data)
    file.close
    # Now copy all the js and css
    my_path = File.join( File.dirname(__FILE__), '..', 'public')
    ["js", "css"].each { |dir|
      FileUtils.copy_entry("#{my_path}/#{dir}", "#{out}/#{dir}")
    }
    # And copy the directory
    Dir.glob("#{my_path}/#{name}/*").each { |subpath|
      base = File.basename(subpath)
      next if "static" == base
      next unless File.directory?(subpath) || base.match(/\.(css|js)$/)
      FileUtils.copy_entry(subpath, "#{out}/#{base}")
    }

    # Set up file dir
    file_dir = File.join(out, 'file')
    FileUtils.makedirs(file_dir)
    pres_dir = showoff.options.pres_dir

    # ..., copy all user-defined styles and javascript files
    Dir.glob("#{pres_dir}/*.{css,js}").each { |path|
      FileUtils.copy(path, File.join(file_dir, File.basename(path)))
    }

    # ... and copy all needed image files
    data.scan(/img src=\".\/file\/(.*?)\"/).flatten.each do |path|
      dir = File.dirname(path)
      FileUtils.makedirs(File.join(file_dir, dir))
      FileUtils.copy(File.join(pres_dir, path), File.join(file_dir, path))
    end
    # copy images from css too
    Dir.glob("#{pres_dir}/*.css").each do |css_path|
      File.open(css_path) do |file|
        data = file.read
        data.scan(/url\((.*)\)/).flatten.each do |path|
          p path
          dir = File.dirname(path)
          FileUtils.makedirs(File.join(file_dir, dir))
          FileUtils.copy(File.join(pres_dir, path), File.join(file_dir, path))
        end
      end
    end
  end
end

Instance Method Details

#eval_ruby(code) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/showoff.rb', line 387

def eval_ruby code
  Object.new.instance_eval do
    eval(code).to_s
  end
rescue => e
  e.message
end

#require_ruby_filesObject



58
59
60
# File 'lib/showoff.rb', line 58

def require_ruby_files
  Dir.glob("#{options.pres_dir}/*.rb").map { |path| require path }
end