Class: PPTX::OPC::Package
- Inherits:
-
Object
- Object
- PPTX::OPC::Package
- Defined in:
- lib/pptx/opc/package.rb
Overview
Package allows the following:
-
Create new package based on an existing PPTX file (extracted ZIP for better version control)
-
Replace certain files with generated parts
Instance Method Summary collapse
- #base ⇒ Object
- #content_types ⇒ Object
- #has_part?(name) ⇒ Boolean
-
#initialize(base = nil) ⇒ Package
constructor
Create a new package using the directory strucuture base.
- #marshal_part(name) ⇒ Object
-
#next_part_with_prefix(prefix, suffix) ⇒ Object
Used e.g.
- #part(name) ⇒ Object
-
#parts ⇒ Object
Returns a Hash of name -> Part Do not modify the hash directly, but use set_part.
- #presentation ⇒ Object
- #set_part(name, obj, content_type = nil) ⇒ Object
-
#stream_zip(ignore_part_exceptions = false) ⇒ Object
Stream a ZIP file while it’s being generated The returned PackageStreamer instance has an each method that takes a block, which is called multiple times with chunks of data.
- #template_part(name) ⇒ Object
- #to_zip ⇒ Object
Constructor Details
#initialize(base = nil) ⇒ Package
Create a new package using the directory strucuture base
12 13 14 15 16 |
# File 'lib/pptx/opc/package.rb', line 12 def initialize(base = nil) base ||= base || File.join(File.dirname(__FILE__), '..', 'base.pptx') @base = File.absolute_path(base) @parts = Hash[default_part_names.map {|fn| [fn, nil]}] end |
Instance Method Details
#base ⇒ Object
18 19 20 |
# File 'lib/pptx/opc/package.rb', line 18 def base @base end |
#content_types ⇒ Object
22 23 24 |
# File 'lib/pptx/opc/package.rb', line 22 def content_types @content_types ||= ContentTypes.new(self, '[Content_Types].xml') end |
#has_part?(name) ⇒ Boolean
26 27 28 |
# File 'lib/pptx/opc/package.rb', line 26 def has_part?(name) @parts.has_key?(name) end |
#marshal_part(name) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/pptx/opc/package.rb', line 45 def marshal_part(name) part = @parts[name] if part if part.is_a? BasePart part.marshal elsif part.instance_of? String # TODO wrap binary parts, probably as references when implementing streaming part else raise "marshal_part: Unknown part type: '#{p.class}' for part '#{name}'" end else template_part name end end |
#next_part_with_prefix(prefix, suffix) ⇒ Object
Used e.g. for creating slides with proper numbering (i.e. slide1.xml, slide2.xml)
62 63 64 65 66 67 68 |
# File 'lib/pptx/opc/package.rb', line 62 def next_part_with_prefix(prefix, suffix) num = @parts.select { |n, _| n.start_with?(prefix) && n.end_with?(suffix) } .map { |n, _| n[prefix.length..-1][0..-(suffix.size + 1)].to_i } .max num = (num || 0) + 1 "#{prefix}#{num}#{suffix}" end |
#part(name) ⇒ Object
30 31 32 |
# File 'lib/pptx/opc/package.rb', line 30 def part(name) @parts[name] end |
#parts ⇒ Object
Returns a Hash of name -> Part Do not modify the hash directly, but use set_part.
36 37 38 |
# File 'lib/pptx/opc/package.rb', line 36 def parts @parts end |
#presentation ⇒ Object
77 78 79 |
# File 'lib/pptx/opc/package.rb', line 77 def presentation @presentation ||= PPTX::Presentation.new(self, 'ppt/presentation.xml') end |
#set_part(name, obj, content_type = nil) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/pptx/opc/package.rb', line 70 def set_part(name, obj, content_type = nil) @parts[name] = obj if content_type content_types.set_override(name, content_type) end end |
#stream_zip(ignore_part_exceptions = false) ⇒ Object
Stream a ZIP file while it’s being generated The returned PackageStreamer instance has an each method that takes a block, which is called multiple times with chunks of data. When adding BinaryParts (e.g. S3ObjectPart or FilePart), these are also streamed into the ZIP file, so you can e.g. stream objects directly from S3 to the client, encoded in the ZIP file. Use this e.g. in a Rails controller as ‘self.response_body`.
88 89 90 |
# File 'lib/pptx/opc/package.rb', line 88 def stream_zip(ignore_part_exceptions=false) PackageStreamer.new(self, ignore_part_exceptions) end |
#template_part(name) ⇒ Object
40 41 42 43 |
# File 'lib/pptx/opc/package.rb', line 40 def template_part(name) file = File.join(@base, name) IO.read(file) if File.exist? file end |
#to_zip ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pptx/opc/package.rb', line 92 def to_zip buffer = ::StringIO.new('') Zip::OutputStream.write_buffer(buffer) do |out| @parts.each do |name, _| out.put_next_entry name out.write marshal_part(name) end end buffer.string end |