Class: Hyaslide::SlideLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/hyaslide/slide_loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSlideLoader

Returns a new instance of SlideLoader.



13
14
15
# File 'lib/hyaslide/slide_loader.rb', line 13

def initialize
  @slides = []
end

Instance Attribute Details

#slidesObject (readonly)

Returns the value of attribute slides.



11
12
13
# File 'lib/hyaslide/slide_loader.rb', line 11

def slides
  @slides
end

Class Method Details

.load_slide(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hyaslide/slide_loader.rb', line 33

def self.load_slide(name)
  File.open("#{src_path(name)}/pages.rb", "w+") do |f|
    data = File.read("data/#{name}/slide.md")
    markdown = Redcarpet::Markdown.new(Hyaslide::Renderer.create(name), fenced_code_blocks: true)
    f.write markdown.render(data)
  end

  if File.exist?("data/#{name}/script.md")
    File.open("#{script_path(name)}/script.html", "w+") do |f|
      script = File.read("data/#{name}/script.md")
      markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(hard_wrap: true), autolink: true, fenced_code_blocks: true)
      f.write markdown.render(script)
    end
  end

  File.open("#{src_path(name)}/app.rb", "w+") do |f|
    f.write Tilt::StringTemplate.new(File.expand_path('../../../template/app.rb', __FILE__)).render(Object.new, name: name)
  end

  File.open("#{src_path(name)}/script.rb", "w+") do |f|
    f.write Tilt::StringTemplate.new(File.expand_path('../../../template/script.rb', __FILE__)).render(Object.new, name: name)
  end
end

.script_path(name) ⇒ Object



61
62
63
# File 'lib/hyaslide/slide_loader.rb', line 61

def self.script_path(name)
  "app/scripts/#{name}"
end

.src_path(name) ⇒ Object



57
58
59
# File 'lib/hyaslide/slide_loader.rb', line 57

def self.src_path(name)
  "app/slides/#{name}"
end

Instance Method Details

#add_slide(name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hyaslide/slide_loader.rb', line 65

def add_slide(name)
  init_slide(name)

  EM.defer do
    FSSM.monitor("data/#{name}", %w(slide.md script.md)) do
      update {|base, relative| Hyaslide::SlideLoader.load_slide(name) }
      delete {|base, relative|}
      create {|base, relative|}
    end
  end
end

#init_slide(name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hyaslide/slide_loader.rb', line 17

def init_slide(name)
  Dir.mkdir('app/slides') unless Dir.exist?('app/slides')
  Dir.mkdir('app/scripts') unless Dir.exist?('app/scripts')
  Dir.mkdir(SlideLoader.src_path(name)) unless Dir.exist?(SlideLoader.src_path(name))
  Dir.mkdir(SlideLoader.script_path(name)) unless Dir.exist?(SlideLoader.script_path(name))

  File.foreach("data/#{name}/slide.md") do |line|
    if line =~ /\A# (.+)/
      @slides << {name: name, title: $1}
      break
    end
  end

  SlideLoader.load_slide(name)
end