class RubyPowerpoint::Slide

Attributes

presentation[R]
slide_file_name[R]
slide_number[R]

Public Class Methods

new(presentation, slide_xml_path) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 12
def initialize presentation, slide_xml_path
  @presentation = presentation
  @slide_xml_path = slide_xml_path
  @slide_number = extract_slide_number_from_path slide_xml_path
  @slide_notes_xml_path = "ppt/notesSlides/notesSlide#{@slide_number}.xml"
  @slide_file_name = extract_slide_file_name_from_path slide_xml_path

  parse_slide
  parse_slide_notes
  parse_relation
end

Public Instance Methods

content() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 42
def content
  content_elements @slide_xml
end
images() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 55
def images
  image_elements(@relation_xml)
    .map.each do |node|
      @presentation.files.file.open(
        node['Target'].gsub('..', 'ppt'))
    end
end
notes_content() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 46
def notes_content
  content_elements @slide_notes_xml
end
paragraphs() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 67
def paragraphs
  paragraph_element @slide_xml
end
parse_relation() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 34
def parse_relation
  @relation_xml_path = "ppt/slides/_rels/#{@slide_file_name}.rels"
  if @presentation.files.file.exist? @relation_xml_path
    relation_doc = @presentation.files.file.open @relation_xml_path
    @relation_xml = Nokogiri::XML::Document.parse relation_doc
  end
end
parse_slide() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 24
def parse_slide
  slide_doc = @presentation.files.file.open @slide_xml_path
  @slide_xml = Nokogiri::XML::Document.parse slide_doc
end
parse_slide_notes() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 29
def parse_slide_notes
  slide_notes_doc = @presentation.files.file.open @slide_notes_xml_path rescue nil
  @slide_notes_xml = Nokogiri::XML::Document.parse(slide_notes_doc) if slide_notes_doc
end
slide_num() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 63
def slide_num
  @slide_xml_path.match(/slide([0-9]*)\.xml$/)[1].to_i
end
title() click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 50
def title
  title_elements = title_elements(@slide_xml)
  title_elements.join(" ") if title_elements.length > 0
end

Private Instance Methods

content_elements(xml) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 85
def content_elements(xml)
  xml.xpath('//a:t').collect{ |node| node.text }
end
element_is_image(node) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 105
def element_is_image(node)
  node['Type'].include? 'image'
end
element_is_title(shape) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 101
def element_is_title(shape)
  shape.xpath('.//p:nvSpPr/p:nvPr/p:ph').select{ |prop| prop['type'] == 'title' || prop['type'] == 'ctrTitle' }.length > 0
end
extract_slide_file_name_from_path(path) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 77
def extract_slide_file_name_from_path path
  path.gsub('ppt/slides/', '')
end
extract_slide_number_from_path(path) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 73
def extract_slide_number_from_path path
  path.gsub('ppt/slides/slide', '').gsub('.xml', '').to_i
end
image_elements(xml) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 89
def image_elements(xml)
  xml.css('Relationship').select{ |node| element_is_image(node) }
end
paragraph_element(xml) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 97
def paragraph_element(xml)
  xml.xpath('//a:p').collect{ |node| RubyPowerpoint::Paragraph.new(self, node) }
end
shape_elements(xml) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 93
def shape_elements(xml)
  xml.xpath('//p:sp')
end
title_elements(xml) click to toggle source
# File lib/ruby_powerpoint/slide.rb, line 81
def title_elements(xml)
  shape_elements(xml).select{ |shape| element_is_title(shape) }
end