powerpoint 1.6 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmRkNjczMjVlMGFjMWMxMWYxOTNhY2NhNDFiMjA3MmU1NjU1ODNhZg==
4
+ YTIyN2VhNDU0N2I4MTFkZTQxNTBmNzhiZTQ5OTYxMzA1NWFmOTgxNg==
5
5
  data.tar.gz: !binary |-
6
- Mjg1OGZiYmFjY2ZiNjExY2JmYjkxNDk2MWJkMGYyMmI3MTk2ZDM5Zg==
6
+ MGNjNGJmODJlNzI2YTFiYzEyMzVlZGYxZTJhMDVmMTAyY2M5NWY0MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTRhMmJiYjI0YWY4MDBhYTliZTYzOTU5ZjU4ODI5N2E0Yzk1OGM4NTI3YjEx
10
- NmY2ZjBlZDVjZjZmNzY0YThmMWQ4N2VkYTE5OTdiNjRjOWUxMmZiYmU5ZjIy
11
- ZGVjMDhmY2I5ZmM5NmE0Nzc3NzVjZWM4NzI1NmUzZTQxZDJjNTU=
9
+ MWYxMmE0ODg5Y2E1ZWU3MjZiYzhjYzkyYmUxOTlhNGRiZmU1NWM5M2JhZmI1
10
+ YzUxZjBmZDE0YTVhYTMxNjE1OGZlNjg0OGQ1ODhjZTlmM2EzMDk3N2U3MTYz
11
+ NGY2OGE0ZjRiMjYwNzI1N2EyNzI4NDNkNzA5NTliYjg1MThhYTA=
12
12
  data.tar.gz: !binary |-
13
- MWJhZjNkODJkYmUwNGZlMDgxNGZlYzg1NGRhNzMyNGQ0N2JiYmEwZDczMGVk
14
- ZjU1Y2UwYjUwYzdhNmYyNTc4ODdiOTczNGIxNmQ1YTlhMzFiN2I3YzZjMzdj
15
- ZjljYTZmYjI5MzI1OTM3OTk2ZmZkZDUyMTU0YzRlMDI0YzJlNTI=
13
+ MWJkZDI5OTdiYzc5YTBmYmQ4ZTM3NjhiZmJjODk2ODUxNzMwY2E5YTg5YmZi
14
+ MzlkMjY4ZWZlMmIzNDk0MTAzYTZiYWU1NTUyZmI4YzA4NjViNGMwMWU0MzYw
15
+ OGNkOThjNmY5OGJhNGUzODRjOTQ0OWVkZjY5ODI4ZDJmMmVmYjE=
@@ -3,35 +3,11 @@ require 'powerpoint/util'
3
3
  require 'powerpoint/slide/intro'
4
4
  require 'powerpoint/slide/textual'
5
5
  require 'powerpoint/slide/pictorial'
6
+ require 'powerpoint/compression'
6
7
  require 'powerpoint/presentation'
7
8
 
8
9
  module Powerpoint
9
-
10
- spec = Gem::Specification.find_by_name("powerpoint")
11
- ROOT_PATH = spec.gem_dir
10
+ ROOT_PATH = File.expand_path("../..", __FILE__)
12
11
  TEMPLATE_PATH = "#{ROOT_PATH}/template/"
13
-
14
- def self.decompress_pptx in_path, out_path
15
- Zip::File.open(in_path) do |zip_file|
16
- zip_file.each do |f|
17
- f_path = File.join(out_path, f.name)
18
- FileUtils.mkdir_p(File.dirname(f_path))
19
- zip_file.extract(f, f_path) unless File.exist?(f_path)
20
- end
21
- end
22
- end
23
-
24
- def self.compress_pptx in_path, out_path
25
- Zip::File.open(out_path, Zip::File::CREATE) do |zip_file|
26
- Dir.glob("#{in_path}/**/*", ::File::FNM_DOTMATCH).each do |path|
27
- zip_path = path.gsub("#{in_path}/","")
28
- next if zip_path == "." || zip_path == ".." || zip_path.match(/DS_Store/)
29
- begin
30
- zip_file.add(zip_path, path)
31
- rescue Zip::ZipEntryExistsError
32
- raise "#{out_path} allready exists!"
33
- end
34
- end
35
- end
36
- end
12
+ VIEW_PATH = "#{ROOT_PATH}/lib/powerpoint/views/"
37
13
  end
@@ -0,0 +1,25 @@
1
+ module Powerpoint
2
+ def self.decompress_pptx(in_path, out_path)
3
+ Zip::File.open(in_path) do |zip_file|
4
+ zip_file.each do |f|
5
+ f_path = File.join(out_path, f.name)
6
+ FileUtils.mkdir_p(File.dirname(f_path))
7
+ zip_file.extract(f, f_path) unless File.exist?(f_path)
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.compress_pptx(in_path, out_path)
13
+ Zip::File.open(out_path, Zip::File::CREATE) do |zip_file|
14
+ Dir.glob("#{in_path}/**/*", ::File::FNM_DOTMATCH).each do |path|
15
+ zip_path = path.gsub("#{in_path}/","")
16
+ next if zip_path == "." || zip_path == ".." || zip_path.match(/DS_Store/)
17
+ begin
18
+ zip_file.add(zip_path, path)
19
+ rescue Zip::ZipEntryExistsError
20
+ raise "#{out_path} already exists!"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -6,13 +6,13 @@ module Powerpoint
6
6
  class Presentation
7
7
  include Powerpoint::Util
8
8
 
9
- attr_reader :pptx_path, :extract_path, :slides
9
+ attr_reader :slides
10
10
 
11
11
  def initialize
12
12
  @slides = []
13
13
  end
14
14
 
15
- def add_intro title, subtitile=nil
15
+ def add_intro(title, subtitile = nil)
16
16
  existing_intro_slide = @slides.select {|s| s.class == Powerpoint::Slide::Intro}[0]
17
17
  slide = Powerpoint::Slide::Intro.new(presentation: self, title: title, subtitile: subtitile)
18
18
  if existing_intro_slide
@@ -22,39 +22,41 @@ module Powerpoint
22
22
  end
23
23
  end
24
24
 
25
- def add_textual_slide title, content=[]
25
+ def add_textual_slide(title, content = [])
26
26
  @slides << Powerpoint::Slide::Textual.new(presentation: self, title: title, content: content)
27
27
  end
28
28
 
29
- def add_pictorial_slide title, image_path, coords={}
29
+ def add_pictorial_slide(title, image_path, coords = {})
30
30
  @slides << Powerpoint::Slide::Pictorial.new(presentation: self, title: title, image_path: image_path, coords: coords)
31
31
  end
32
32
 
33
- def save path
34
- # Copy template to temp path
35
- @extract_path = File.join(Dir.tmpdir, "extract_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}")
36
- FileUtils.copy_entry TEMPLATE_PATH, @extract_path
33
+ def save(path)
34
+ Dir.mktmpdir do |dir|
35
+ extract_path = "#{dir}/extract_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}"
37
36
 
38
- # Remove keep files
39
- FileUtils.rm_rf("#{@extract_path}/ppt/_rels/.keep")
40
- FileUtils.rm_rf("#{@extract_path}/ppt/media/.keep")
41
- FileUtils.rm_rf("#{@extract_path}/ppt/slides/_rels/.keep")
37
+ # Copy template to temp path
38
+ FileUtils.copy_entry(TEMPLATE_PATH, extract_path)
42
39
 
43
- # Render/save generic stuff
44
- File.open("#{extract_path}/[Content_Types].xml", 'w') { |f| f << render_view('content_type.xml.erb') }
45
- File.open("#{extract_path}/ppt/_rels/presentation.xml.rels", 'w') { |f| f << render_view('presentation.xml.rel.erb') }
46
- File.open("#{extract_path}/ppt/presentation.xml", 'w') { |f| f << render_view('presentation.xml.erb') }
40
+ # Remove keep files
41
+ Dir.glob("#{extract_path}/**/.keep").each do |keep_file|
42
+ FileUtils.rm_rf(keep_file)
43
+ end
47
44
 
48
- # Save slides
49
- slides.each_with_index do |slide, index|
50
- slide.save(index + 1)
45
+ # Render/save generic stuff
46
+ render_view('content_type.xml.erb', "#{extract_path}/[Content_Types].xml")
47
+ render_view('presentation.xml.rel.erb', "#{extract_path}/ppt/_rels/presentation.xml.rels")
48
+ render_view('presentation.xml.erb', "#{extract_path}/ppt/presentation.xml")
49
+
50
+ # Save slides
51
+ slides.each_with_index do |slide, index|
52
+ slide.save(extract_path, index + 1)
53
+ end
54
+
55
+ # Create .pptx file
56
+ File.delete(path) if File.exist?(path)
57
+ Powerpoint.compress_pptx(extract_path, path)
51
58
  end
52
59
 
53
- # Create .pptx file
54
- @pptx_path = path
55
- File.delete(path) if File.exist?(path)
56
- Powerpoint.compress_pptx @extract_path, @pptx_path
57
- FileUtils.rm_rf(@extract_path)
58
60
  path
59
61
  end
60
62
 
@@ -62,4 +64,4 @@ module Powerpoint
62
64
  slides.select {|slide| slide.class == Powerpoint::Slide::Pictorial}.map(&:file_type).uniq
63
65
  end
64
66
  end
65
- end
67
+ end
@@ -13,20 +13,20 @@ module Powerpoint
13
13
  options.each {|k, v| instance_variable_set("@#{k}", v)}
14
14
  end
15
15
 
16
- def save(index)
17
- save_rel_xml(index)
18
- save_slide_xml(index)
16
+ def save(extract_path, index)
17
+ save_rel_xml(extract_path, index)
18
+ save_slide_xml(extract_path, index)
19
19
  end
20
20
 
21
- private
22
-
23
- def save_rel_xml index
24
- File.open("#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels", 'w'){ |f| f << render_view('textual_rel.xml.erb') }
21
+ def save_rel_xml(extract_path, index)
22
+ render_view('textual_rel.xml.erb', "#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels")
25
23
  end
24
+ private :save_rel_xml
26
25
 
27
- def save_slide_xml index
28
- File.open("#{extract_path}/ppt/slides/slide#{index}.xml", 'w'){ |f| f << render_view('intro_slide.xml.erb') }
26
+ def save_slide_xml(extract_path, index)
27
+ render_view('intro_slide.xml.erb', "#{extract_path}/ppt/slides/slide#{index}.xml")
29
28
  end
29
+ private :save_slide_xml
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -17,20 +17,20 @@ module Powerpoint
17
17
  @image_name = File.basename(@image_path)
18
18
  end
19
19
 
20
- def save(index)
21
- copy_media_to_extract_path
22
- save_rel_xml(index)
23
- save_slide_xml(index)
20
+ def save(extract_path, index)
21
+ copy_media(extract_path)
22
+ save_rel_xml(extract_path, index)
23
+ save_slide_xml(extract_path, index)
24
24
  end
25
25
 
26
26
  def file_type
27
27
  File.extname(image_name).gsub('.', '')
28
28
  end
29
29
 
30
- private
31
-
32
30
  def default_coords
33
- slide_width = pixle_to_pt(720); slide_height = pixle_to_pt(540); default_width = pixle_to_pt(550)
31
+ slide_width = pixle_to_pt(720)
32
+ default_width = pixle_to_pt(550)
33
+
34
34
  return {} unless dimensions = FastImage.size(image_path)
35
35
  image_width, image_height = dimensions.map {|d| pixle_to_pt(d)}
36
36
  new_width = default_width < image_width ? default_width : image_width
@@ -38,19 +38,22 @@ module Powerpoint
38
38
  new_height = (image_height.to_f * ratio).round
39
39
  {x: (slide_width / 2) - (new_width/2), y: pixle_to_pt(120), cx: new_width, cy: new_height}
40
40
  end
41
+ private :default_coords
41
42
 
42
- def copy_media_to_extract_path
43
+ def copy_media(extract_path)
43
44
  FileUtils.copy_file(@image_path, "#{extract_path}/ppt/media/#{@image_name}")
44
45
  end
46
+ private :copy_media
45
47
 
46
- def save_rel_xml index
47
- File.open("#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels", 'w'){ |f| f << render_view('pictorial_rel.xml.erb') }
48
+ def save_rel_xml(extract_path, index)
49
+ render_view('pictorial_rel.xml.erb', "#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels")
48
50
  end
51
+ private :save_rel_xml
49
52
 
50
- def save_slide_xml index
51
- File.open("#{extract_path}/ppt/slides/slide#{index}.xml", 'w'){ |f| f << render_view('pictorial_slide.xml.erb') }
53
+ def save_slide_xml(extract_path, index)
54
+ render_view('pictorial_slide.xml.erb', "#{extract_path}/ppt/slides/slide#{index}.xml")
52
55
  end
53
-
56
+ private :save_slide_xml
54
57
  end
55
58
  end
56
- end
59
+ end
@@ -13,20 +13,20 @@ module Powerpoint
13
13
  options.each {|k, v| instance_variable_set("@#{k}", v)}
14
14
  end
15
15
 
16
- def save(index)
17
- save_rel_xml(index)
18
- save_slide_xml(index)
16
+ def save(extract_path, index)
17
+ save_rel_xml(extract_path, index)
18
+ save_slide_xml(extract_path, index)
19
19
  end
20
20
 
21
- private
22
-
23
- def save_rel_xml index
24
- File.open("#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels", 'w'){ |f| f << render_view('textual_rel.xml.erb') }
21
+ def save_rel_xml(extract_path, index)
22
+ render_view('textual_rel.xml.erb', "#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels")
25
23
  end
24
+ private :save_rel_xml
26
25
 
27
- def save_slide_xml index
28
- File.open("#{extract_path}/ppt/slides/slide#{index}.xml", 'w'){ |f| f << render_view('textual_slide.xml.erb') }
26
+ def save_slide_xml(extract_path, index)
27
+ render_view('textual_slide.xml.erb', "#{extract_path}/ppt/slides/slide#{index}.xml")
29
28
  end
29
+ private :save_slide_xml
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -5,22 +5,20 @@ module Powerpoint
5
5
  px * 12700
6
6
  end
7
7
 
8
- def render_view(filename)
9
- view_contents = read_template(filename)
8
+ def render_view(template_name, path)
9
+ view_contents = read_template(template_name)
10
10
  renderer = ERB.new(view_contents)
11
- renderer.result( binding )
11
+ data = renderer.result(binding)
12
+
13
+ File.open(path, 'w') { |f| f << data }
12
14
  end
13
15
 
14
16
  def read_template(filename)
15
- File.read(Powerpoint::ROOT_PATH + "/lib/powerpoint/views/#{filename}")
17
+ File.read("#{Powerpoint::VIEW_PATH}/#{filename}")
16
18
  end
17
19
 
18
20
  def require_arguments(required_argements, argements)
19
21
  raise ArgumentError unless required_argements.all? {|required_key| argements.keys.include? required_key}
20
22
  end
21
-
22
- def extract_path
23
- @presentation.extract_path
24
- end
25
23
  end
26
- end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Powerpoint
2
- VERSION = "1.6"
2
+ VERSION = "1.7"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: powerpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.6'
4
+ version: '1.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - pythonicrubyist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-29 00:00:00.000000000 Z
11
+ date: 2015-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,7 @@ files:
108
108
  - README.rdoc
109
109
  - Rakefile
110
110
  - lib/powerpoint.rb
111
+ - lib/powerpoint/compression.rb
111
112
  - lib/powerpoint/presentation.rb
112
113
  - lib/powerpoint/slide/intro.rb
113
114
  - lib/powerpoint/slide/pictorial.rb