module Prez::Helpers

Protected Instance Methods

capture() { || ... } click to toggle source
# File lib/prez/helpers.rb, line 80
def capture()
  buf = ""
  old_buffer = @output_buffer
  @output_buffer = buf
  yield
  buf.to_s
ensure
  @output_buffer = old_buffer
end
duration(value) click to toggle source
# File lib/prez/helpers.rb, line 12
def duration(value)
  if @duration
    raise Prez::Error.new("The duration helper can only be called once!")
  end

  if value.kind_of?(String) && value.split(":").size > 3
    raise Prez::Eror.new("Only hours:minutes:seconds are supported for the duration!")
  end

  @duration = value
  nil
end
duration_amount() click to toggle source
# File lib/prez/helpers.rb, line 25
def duration_amount
  return if @duration.nil? || @duration.to_i <= 0

  # Ensure it is an integer first... then we can normalize based
  # on standard times (ie, no 70 seconds, or 100 minutes)
  if @duration.kind_of? String
    @duration = @duration.split ":"
    @duration.map! &:to_i
    value = @duration.pop || 0
    value += (@duration.pop || 0) * 60
    value += (@duration.pop || 0) * 60 * 60
    @duration = value
  end

  # Ensure we don't have a floating point number
  @duration = @duration.to_i
  seconds = @duration % 60
  minutes = (@duration / 60) % 60
  hours = @duration / 60 / 60

  if hours > 0
    "%d:%02d:%02d" % [hours, minutes, seconds]
  elsif minutes > 0
    "%2d:%02d" % [minutes, seconds]
  else
    seconds.to_s
  end
end
element(options = {}) { || ... } click to toggle source
# File lib/prez/helpers.rb, line 125
def element(options = {})
  tag = options.fetch :tag, :div
  classes = ["prez-element"]
  classes << options[:class] if options[:class]
  attributes = [%{class="#{classes.join " "}"}]

  if options[:id]
    attributes << %{id="#{options[:id]}"}
  end

  if options[:style]
    attributes << %{style="#{options[:style]}"}
  end

  concat %{<#{tag} #{attributes.join " "}>}
  yield
  concat %{</#{tag}>}
end
element_js(up:, down:) click to toggle source
# File lib/prez/helpers.rb, line 144
def element_js(up:, down:)
  concat Prez::JavascriptElement.new(capture(&up), capture(&down)).to_s
end
html_escape(value = nil, &block) click to toggle source
# File lib/prez/helpers.rb, line 66
def html_escape(value = nil, &block)
  if block
    value = capture &block
  end

  value = CGI.escape_html value

  if block
    concat value
  else
    value
  end
end
image(name, options = {}) click to toggle source
# File lib/prez/helpers.rb, line 154
def image(name, options = {})
  Prez::Assets.image name, options
rescue Prez::Files::MissingError
  raise Prez::Error.new("Could not find image file: '#{name}'")
end
javascript(name) click to toggle source
# File lib/prez/helpers.rb, line 160
def javascript(name)
  Prez::Assets.javascript name
rescue Prez::Files::MissingError
  raise Prez::Error.new("Could not find file: '#{name}.js'")
end
launch(new_window: true) click to toggle source
# File lib/prez/helpers.rb, line 54
def launch(new_window: true)
  if new_window
    @launch = :new_window
  else
    @launch = :current_window
  end
end
launch_type() click to toggle source
# File lib/prez/helpers.rb, line 62
def launch_type
  @launch
end
notes() { || ... } click to toggle source
# File lib/prez/helpers.rb, line 148
def notes
  concat %{<div class="prez-notes">}
  yield
  concat %{</div>}
end
reset_helpers!() click to toggle source
# File lib/prez/helpers.rb, line 7
def reset_helpers!
  @launch = :choose
  @duration = nil
end
slide(options = {}) { || ... } click to toggle source
# File lib/prez/helpers.rb, line 90
def slide(options = {})
  classes = ["prez-slide"]
  classes << options[:class] if options[:class]
  align = options.fetch :align, :center

  case align
  when :left
    classes << "left-aligned"
  when :right
    classes << "right-aligned"
  when :center
    classes << "center-aligned"
  else
    raise Prez::Error.new("Invalid slide align: #{align.inspect}")
  end

  attributes = [%{class="#{classes.join " "}"}]

  if options[:id]
    attributes << %{id="#{options[:id]}"}
  end

  if options[:style]
    attributes << %{style="#{options[:style]}"}
  end

  if options[:duration]
    attributes << %{data-duration="#{options[:duration]}"}
  end

  concat %{<div #{attributes.join " "}>}
  yield
  concat %{</div>}
end
stylesheet(name) click to toggle source
# File lib/prez/helpers.rb, line 166
def stylesheet(name)
  Prez::Assets.stylesheet name
rescue Prez::Files::MissingError
  raise Prez::Error.new("Could not find file: '#{name}.css'")
end