Class: ShowOff

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

Constant Summary collapse

@@downloads =

Track downloadable files

Hash.new
nil
@@current =

The current slide that the presenter is viewing

Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ ShowOff

Returns a new instance of ShowOff.



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

def initialize(app=nil)
  super(app)
  @logger = Logger.new(STDOUT)
  @logger.formatter = proc { |severity,datetime,progname,msg| "#{progname} #{msg}\n" }
  @logger.level = settings.verbose ? Logger::DEBUG : Logger::WARN

  @review = settings.review

  dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  @logger.debug(dir)

  showoff_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  settings.pres_dir ||= Dir.pwd
  @root_path = "."

  settings.pres_dir = File.expand_path(settings.pres_dir)
  if (settings.pres_file)
    ShowOffUtils.presentation_config_file = settings.pres_file
  end

  # Load configuration for page size and template from the
  # configuration JSON file
  if File.exists?(ShowOffUtils.presentation_config_file)
    showoff_json = JSON.parse(File.read(ShowOffUtils.presentation_config_file))
    settings.showoff_config = showoff_json

    # Set options for encoding, template and page size
    settings.encoding = showoff_json["encoding"]
    settings.page_size = showoff_json["page-size"] || "Letter"
    settings.pres_template = showoff_json["templates"]
  end

  @logger.debug settings.pres_template

  @cached_image_size = {}
  @logger.debug settings.pres_dir
  @pres_name = settings.pres_dir.split('/').pop
  require_ruby_files

  # Default asset path
  @asset_path = "./"


  # Initialize Markdown Configuration
  MarkdownConfig::setup(settings.pres_dir)
end

Instance Attribute Details

#cached_image_sizeObject (readonly)

Returns the value of attribute cached_image_size.



30
31
32
# File 'lib/showoff.rb', line 30

def cached_image_size
  @cached_image_size
end

Class Method Details

.do_static(what) ⇒ Object



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'lib/showoff.rb', line 911

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

  # Sinatra now aliases new to new!
  # https://github.com/sinatra/sinatra/blob/v1.3.3/lib/sinatra/base.rb#L1369
  showoff = ShowOff.new!

  name = showoff.instance_variable_get(:@pres_name)
  path = showoff.instance_variable_get(:@root_path)
  logger = showoff.instance_variable_get(:@logger)

  data = showoff.send(what, true)

  if data.is_a?(File)
    FileUtils.cp(data.path, "#{name}.pdf")
  else
    out = File.expand_path("#{path}/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.settings.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
    [/img src=[\"\'].\/file\/(.*?)[\"\']/, /style=[\"\']background: url\(\'file\/(.*?)'/].each do |regex|
      data.scan(regex).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
    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\([\"\']?(?!https?:\/\/)(.*?)[\"\']?\)/).flatten.each do |path|
          path.gsub!(/(\#.*)$/, '') # get rid of the anchor
          path.gsub!(/(\?.*)$/, '') # get rid of the query
          logger.debug 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

.pres_dir_currentObject



123
124
125
126
# File 'lib/showoff.rb', line 123

def self.pres_dir_current
  opt = {:pres_dir => Dir.pwd}
  ShowOff.set opt
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/showoff.rb', line 996

def authorized?
  if not settings.showoff_config.has_key? 'password'
    # if no password is set, then default to allowing access to localhost
    request.env['REMOTE_HOST'] == 'localhost' or request.ip == '127.0.0.1'
  else
    auth   ||= Rack::Auth::Basic::Request.new(request.env)
    user     = settings.showoff_config['user'] || ''
    password = settings.showoff_config['password']
    auth.provided? && auth.basic? && auth.credentials && auth.credentials == [user, password]
  end
end

#eval_ruby(code) ⇒ Object



982
983
984
985
986
# File 'lib/showoff.rb', line 982

def eval_ruby code
  eval(code).to_s
rescue => e
  e.message
end

#guidObject



1008
1009
1010
1011
# File 'lib/showoff.rb', line 1008

def guid
  # this is a terrifyingly simple GUID generator
  (0..15).to_a.map{|a| rand(16).to_s(16)}.join
end

#protected!Object

Basic auth boilerplate



989
990
991
992
993
994
# File 'lib/showoff.rb', line 989

def protected!
  unless authorized?
    response['WWW-Authenticate'] = %(Basic realm="#{@title}: Protected Area")
    throw(:halt, [401, "Not authorized\n"])
  end
end

#require_ruby_filesObject



128
129
130
# File 'lib/showoff.rb', line 128

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


1013
1014
1015
# File 'lib/showoff.rb', line 1013

def valid_cookie
  (request.cookies['presenter'] == @@cookie)
end