Class: Powerpoint::Presentation

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/powerpoint/presentation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#copy_media, #pixle_to_pt, #read_template, #render_view, #require_arguments

Constructor Details

#initializePresentation

Returns a new instance of Presentation.



11
12
13
# File 'lib/powerpoint/presentation.rb', line 11

def initialize
  @slides = []
end

Instance Attribute Details

#slidesObject (readonly)

Returns the value of attribute slides.



9
10
11
# File 'lib/powerpoint/presentation.rb', line 9

def slides
  @slides
end

Instance Method Details

#add_intro(title, subtitile = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/powerpoint/presentation.rb', line 15

def add_intro(title, subtitile = nil)
  existing_intro_slide = @slides.select {|s| s.class == Powerpoint::Slide::Intro}[0]
  slide = Powerpoint::Slide::Intro.new(presentation: self, title: title, subtitile: subtitile)
  if existing_intro_slide
    @slides[@slides.index(existing_intro_slide)] = slide 
  else
    @slides.insert 0, slide
  end
end

#add_pictorial_slide(title, image_path, coords = {}) ⇒ Object



29
30
31
# File 'lib/powerpoint/presentation.rb', line 29

def add_pictorial_slide(title, image_path, coords = {})
  @slides << Powerpoint::Slide::Pictorial.new(presentation: self, title: title, image_path: image_path, coords: coords)
end

#add_picture_description_slide(title, image_path, content = []) ⇒ Object



37
38
39
# File 'lib/powerpoint/presentation.rb', line 37

def add_picture_description_slide(title, image_path, content = [])
  @slides << Powerpoint::Slide::DescriptionPic.new(presentation: self, title: title, image_path: image_path, content: content)
end

#add_text_picture_slide(title, image_path, content = []) ⇒ Object



33
34
35
# File 'lib/powerpoint/presentation.rb', line 33

def add_text_picture_slide(title, image_path, content = [])
  @slides << Powerpoint::Slide::TextPicSplit.new(presentation: self, title: title, image_path: image_path, content: content)
end

#add_textual_slide(title, content = []) ⇒ Object



25
26
27
# File 'lib/powerpoint/presentation.rb', line 25

def add_textual_slide(title, content = [])
  @slides << Powerpoint::Slide::Textual.new(presentation: self, title: title, content: content)
end

#file_typesObject



72
73
74
# File 'lib/powerpoint/presentation.rb', line 72

def file_types
  slides.map {|slide| slide.file_type if slide.respond_to? :file_type }.compact.uniq
end

#save(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/powerpoint/presentation.rb', line 41

def save(path)
  Dir.mktmpdir do |dir|
    extract_path = "#{dir}/extract_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}"

    # Copy template to temp path
    FileUtils.copy_entry(TEMPLATE_PATH, extract_path)

    # Remove keep files
    Dir.glob("#{extract_path}/**/.keep").each do |keep_file|
      FileUtils.rm_rf(keep_file)
    end

    # Render/save generic stuff
    render_view('content_type.xml.erb', "#{extract_path}/[Content_Types].xml")
    render_view('presentation.xml.rel.erb', "#{extract_path}/ppt/_rels/presentation.xml.rels")
    render_view('presentation.xml.erb', "#{extract_path}/ppt/presentation.xml")
    render_view('app.xml.erb', "#{extract_path}/docProps/app.xml")

    # Save slides
    slides.each_with_index do |slide, index|
      slide.save(extract_path, index + 1)
    end

    # Create .pptx file
    File.delete(path) if File.exist?(path)
    Powerpoint.compress_pptx(extract_path, path)
  end

  path
end