Sha256: c2c4314abd65b2d4eb7b6eaf17944577ad1a617e2c9e873b091cce785b1eb369

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

module Eggplant
  class Slides

    def initialize path, options={}
      @slides = []
      @title = nil
      Dir[path+'/**/*.md'].each do |file|
        if !(file =~ /README.md$/)
          process file, File.read(file)
        end
      end
    end

    def size
      @slides.size
    end

    def to_html
      @slides.map do |slide|
        render_slide slide[:body], slide[:options]
      end.join "\n"
    end

    private

    def process filename, content
      # if there are no !SLIDE markers, then make the file define one slide
      unless content =~ /^!SLIDE/m
        content = "!SLIDE\n" + content
      end

      title = filename.split('/').pop.gsub(/\.md$/, '')
      slides = content.split(/^!SLIDE/)
      slides.delete('')
      seq = 1 if slides.size > 1
      slides.each do |slide|
        slide, options = parse_options(title, slide.split("\n"))
        @slides << {:body => slide, :options => options}
      end
    end

    def render_slide body, options
      body = Eggplant::Markdown.new(body).to_html
      <<-HTML
<div class="slide #{options[:class]}"
    data-title="#{options[:title]}">
  #{body}
</div>
      HTML
    end

    def parse_options title, lines
      options = lines.shift.strip.scan(/[a-z]*="[^"]*"/i) rescue []
      options_hash = {}
      options.each do |option|
        key, value = option.split('=')
        options_hash[key.to_sym] = value.gsub('"', '')
      end
      options_hash[:title] ||= title
      options_hash[:class] ||= ''
      return lines.join("\n"), options_hash
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eggplant-0.2.2 lib/eggplant/slides.rb
eggplant-0.2.1 lib/eggplant/slides.rb
eggplant-0.2.0 lib/eggplant/slides.rb