class Deck::Slide

Attributes

classes[R]
markdown_text[R]

Public Class Methods

from_file(markdown_file) click to toggle source

todo: test this method on its own

# File lib/deck/slide.rb, line 11
def self.from_file markdown_file
  markdown_file = markdown_file.path if markdown_file.is_a? File   # fix for Ruby 1.8.7
  split File.read(markdown_file)
end
new(options = {}) click to toggle source
Calls superclass method
# File lib/deck/slide.rb, line 58
def initialize options = {}
  super options

  @classes = process_classes
  @markdown_text = ""
end
split(content) click to toggle source

given a chunk of Markdown text, splits it into an array of Slide objects todo: move into SlideDeck?

# File lib/deck/slide.rb, line 18
def self.split content
  unless content =~ /^\<?!SLIDE/m     # this only applies to files with no !SLIDEs at all, which is odd
    content = content.
      gsub(/^# /m, "<!SLIDE>\n# ").
      gsub(/^(.*)\n(===+)/, "<!SLIDE>\n\\1\n\\2")
  end

  lines = content.split("\n")
  slides = []
  slides << (slide = Slide.new)
  until lines.empty?
    line = lines.shift
    if line =~ /^<?!SLIDE(.*)>?/
      slides << (slide = Slide.new(:classes => $1))

    elsif (line =~ /^# / or lines.first =~ /^(===+)/) and !slide.empty?
      # every H1 defines a new slide, unless there's a !SLIDE before it
      slides << (slide = Slide.new)
      slide << line

    elsif line =~ /^\.notes/
      # don't include notes

    else
      slide << line
    end
  end

  slides.delete_if {|slide| slide.empty? }

  slides
end

Public Instance Methods

<<(s) click to toggle source
# File lib/deck/slide.rb, line 98
def <<(s)
  if s.strip =~ /^\s*<?!VIDEO +([^\s>]*)>?$/
    youtube_id = $1
    # see https://developers.google.com/youtube/player_parameters
    s = %Q(<iframe class="video youtube" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/#{youtube_id}" frameborder="0"></iframe>\n)
  end
  @markdown_text << s
  @markdown_text << "\n"
end
==(other) click to toggle source
# File lib/deck/slide.rb, line 65
def ==(other)
  Slide === other and
  @classes == other.classes and
  @markdown_text == other.markdown_text
end
content() click to toggle source
# File lib/deck/slide.rb, line 124
def content
  section :class => @classes, :id => slide_id do
    text "\n" # markdown HTML should be left-aligned, in case of PRE blocks and other quirks
    html = markdown.render(markdown_text)
    html = munge(html)
    rawtext html
  end
end
empty?() click to toggle source
# File lib/deck/slide.rb, line 108
def empty?
  @markdown_text.strip == ""
end
markdown() click to toggle source
# File lib/deck/slide.rb, line 84
def markdown
  @@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
    :no_intra_emphasis => true,
    :tables => true,
    :fenced_code_blocks => true,
    :no_intra_emphasis => true,
    :autolink => true,
    :strikethrough => true,
    :lax_html_blocks => false,
    :space_after_headers => true,
    :superscript => false
  )
end
process_classes() click to toggle source
# File lib/deck/slide.rb, line 71
def process_classes
  ["slide"] + case @classes
  when NilClass
    []
  when String
    @classes.strip.chomp('>').split
  when Array
    @classes
  else
    raise "can't deal with :classes => #{@classes.inspect}"
  end
end
slide_id() click to toggle source
# File lib/deck/slide.rb, line 118
def slide_id
  @slide_id ||= begin
    title.downcase.gsub(/[^\w\s]/, '').strip.gsub(/\s/, '_')
  end
end
title() click to toggle source
# File lib/deck/slide.rb, line 112
def title
  lines = @markdown_text.strip.split("\n")
  raise "an empty slide has no id" if lines.empty?
  lines.first.gsub(/^[#=]*/, '').strip
end

Private Instance Methods

munge(html) click to toggle source
# File lib/deck/slide.rb, line 151
def munge html
  doc = noko_doc(html)
  if mutate_h1? doc
    doc.css('h1').each {|node| node.node_name = "h2"}
    doc.css('body').inner_html + "\n"
  else
    html
  end
end
mutate_h1?(doc) click to toggle source

if there is an H1, change it to an H2, unless it’s the only thing there TODO: or unless the slide class is whatever

# File lib/deck/slide.rb, line 137
def mutate_h1? doc
  h1s = doc.css('h1') || []
  if h1s.size == 0
    false
  else
    stuff = doc.css('body>*')
    if stuff.size == 1
      false
    else
      true
    end
  end
end