konjac 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,6 +2,7 @@
2
2
  .bundle
3
3
  .DS_Store
4
4
  Gemfile.lock
5
+ ~$*.*
5
6
  doc/
6
7
  pkg/*
7
8
  spec/office/bin/sample.*.diff
@@ -41,15 +41,16 @@ module Konjac
41
41
  end
42
42
  end
43
43
 
44
+ # Provides the delimiters used for Word documents
44
45
  def delimiter(type)
45
46
  if type.nil?
46
- "\v"
47
+ "\r"
47
48
  else
48
49
  case type
49
50
  when :shape
50
- "\r"
51
- else
52
51
  "\v"
52
+ else
53
+ "\r"
53
54
  end
54
55
  end
55
56
  end
@@ -17,13 +17,17 @@ module Konjac
17
17
  end
18
18
 
19
19
  def write(text, *args)
20
- if args.empty?
20
+ opts = super(text, *args)
21
+ if opts.map(&:last).all?(&:nil?)
21
22
  @current.formula.set text
22
- else
23
+ elsif opts[:type].nil? || opts[:type].empty?
23
24
  opts = parse_args(*args)
24
25
  @document.sheets[opts[:sheet]]
25
26
  .rows[opts[:row]]
26
27
  .cells[opts[:cell]].formula.set text
28
+ else
29
+ @document.sheets[opts[:sheet]]
30
+ .shapes[opts[:row]].text_frame.characters.content.set text
27
31
  end
28
32
  end
29
33
 
@@ -39,18 +43,35 @@ module Konjac
39
43
  tags << temp unless temp.blank?
40
44
  end
41
45
  end
46
+
47
+ # TODO: I should optimize this later like above, to prevent large
48
+ # shapes from getting out of hand
49
+ sheet.shapes.get.each_with_index do |shape, index|
50
+ temp = Tag.new
51
+ temp.indices = [s + 1, index + 1]
52
+ temp.removed = temp.added =
53
+ clean(shape.text_frame.characters.content.get, :shape)
54
+ temp.type = :shape
55
+ tags << temp unless temp.blank?
56
+ end rescue NoMethodError # ignore sheets without shapes
42
57
  end
43
58
  tags
44
59
  end
45
60
 
46
61
  # Finds the paragraph indicated by the provided index
62
+ # TODO: Clean up the second unless statement
47
63
  def find(*args)
48
- unless args.empty?
49
- @indices = args
64
+ unless args.empty? || args.nil?
50
65
  opts = parse_args(*args)
51
- @current = @document.sheets[opts[:sheet]]
52
- .rows[opts[:row]]
53
- .cells[opts[:cell]]
66
+ if (opts[:type].nil? || opts[:type].empty?) && !opts.map(&:last).all?(&:nil?)
67
+ @index = [opts[:sheet], opts[:row], opts[:cell]]
68
+ @current = @document.sheets[opts[:sheet]]
69
+ .rows[opts[:row]]
70
+ .cells[opts[:cell]]
71
+ elsif opts[:type] == :shape
72
+ return @document.sheets[opts[:sheet]]
73
+ .shapes[opts[:row]].text_frame.characters.content.get
74
+ end
54
75
  end
55
76
 
56
77
  @current.formula.get
@@ -60,7 +81,7 @@ module Konjac
60
81
  # fetches all row and column elements and can thus be very expensive for
61
82
  # large spreadsheets.
62
83
  def size
63
- @document.sheets.inject(0) do |result, sheet|
84
+ @document.sheets.get.inject(0) do |result, sheet|
64
85
  range = sheet.used_range
65
86
  result += range.rows.get.size * range.columns.get.size
66
87
  end
@@ -70,4 +91,3 @@ module Konjac
70
91
  end
71
92
  end
72
93
  end
73
-
@@ -6,7 +6,6 @@ module Konjac
6
6
  def initialize(path = nil)
7
7
  super "Microsoft PowerPoint", path
8
8
  @strippable = //
9
- @delimiter = "\r"
10
9
  @parse_order = [:slide, :shape]
11
10
  find 1, 1
12
11
  end
@@ -42,11 +41,13 @@ module Konjac
42
41
 
43
42
  # Finds the paragraph indicated by the provided index
44
43
  def find(*args)
45
- unless args.empty?
44
+ unless args.empty? || args.nil?
46
45
  @indices = args
47
46
  opts = parse_args(*args)
48
- @current = @document.slides[opts[:slide]]
49
- .shapes[opts[:shape]]
47
+ unless opts.map(&:last).all?(&:nil?)
48
+ @current = @document.slides[opts[:slide]]
49
+ .shapes[opts[:shape]]
50
+ end
50
51
  end
51
52
 
52
53
  @current.text_frame.text_range.content.get
@@ -50,7 +50,7 @@ module Konjac
50
50
  temp = Tag.new
51
51
  temp.indices = [index + 1]
52
52
  temp.removed = temp.added =
53
- clean(shape.text_frame.text_range.content.get)
53
+ clean(shape.text_frame.text_range.content.get, :shape)
54
54
  temp.type = :shape
55
55
  tags << temp unless temp.blank?
56
56
  end
@@ -91,6 +91,20 @@ module Konjac
91
91
  end
92
92
  alias :next :succ
93
93
 
94
+ # Provides the delimiters used for Word documents
95
+ def delimiter(type)
96
+ if type.nil?
97
+ "\v"
98
+ else
99
+ case type
100
+ when :shape
101
+ "\r"
102
+ else
103
+ "\v"
104
+ end
105
+ end
106
+ end
107
+
94
108
  # Selects the paragraph indicated by an indicated, or +nil+ to select
95
109
  # the current paragraph
96
110
  def select(*args)
@@ -1,4 +1,4 @@
1
1
  module Konjac
2
2
  # The current version number of Konjac
3
- VERSION = "0.3.1"
3
+ VERSION = "0.3.2"
4
4
  end
Binary file
Binary file
@@ -2,4 +2,100 @@
2
2
  require File.dirname(__FILE__) + "/../spec_helper"
3
3
 
4
4
  describe "Excel", :excel do
5
+ describe "opening the sample document" do
6
+ before :all do
7
+ @export_target = StringIO.new <<-eof
8
+ --- /Users/bryan/Projects/konjac/spec/office/bin/sample.xlsx
9
+ +++ /Users/bryan/Projects/konjac/spec/office/bin/sample.xlsx
10
+ @@ 1,1,1 @@
11
+ -=UPPER("Formula")
12
+ +=UPPER("Formula")
13
+ @@ 1,2,4 @@
14
+ -32768
15
+ +32768
16
+ @@ 1,3,2 @@
17
+ -No formula
18
+ +No formula
19
+ @@ 1,5,1 @@
20
+ -Cell with
21
+ -a line break
22
+ +Cell with
23
+ +a line break
24
+ @@ shape 1,1 @@
25
+ -Text box
26
+ -with soft return
27
+ +Text box
28
+ +with soft return
29
+ @@ 2,4,3 @@
30
+ -New sheet
31
+ +New sheet
32
+ eof
33
+ @spec_path = File.dirname(File.expand_path(__FILE__))
34
+ @path = File.join(@spec_path, "bin", "sample.xlsx")
35
+ @diff_path = "#{@path}.diff"
36
+ @book = Office.excel(@path)
37
+ @original_size = @book.size
38
+ end
39
+
40
+ after :all do
41
+ @book.close :saving => false
42
+ end
43
+
44
+ it "should open the test document" do
45
+ @book.read.should == ['=UPPER("Formula")']
46
+ end
47
+
48
+ it "should return the same path" do
49
+ @book.path.should == @path
50
+ end
51
+
52
+ it "should export tags to a tags file" do
53
+ File.delete @diff_path rescue Errno::ENOENT
54
+ @book.export
55
+ File.exists?(@diff_path).should == true
56
+ end
57
+
58
+ it "should have exported the expected tags" do
59
+ @export_target.rewind
60
+ tags = File.open(@diff_path)
61
+ tags.each_line do |tag|
62
+ tag.should == @export_target.readline
63
+ end
64
+ end
65
+
66
+ it "should read some arbitrary items correctly" do
67
+ @book.read(1, 3, 2).should == ["No formula"]
68
+ @book.read(1, 5, 1).should == ["Cell with", "a line break"]
69
+ @book.read(1, 2, 4).should == ["32768"]
70
+ @book.read(1, 1, :type => :shape).should == ["Text box", "with soft return"]
71
+ end
72
+
73
+ it "should import all tags correctly" do
74
+ @export_target.rewind
75
+ @book.find 1, 1, 1
76
+ index = 0
77
+ lines = []
78
+ File.open(@diff_path, "w") do |file|
79
+ @export_target.each do |line|
80
+ if line =~ /^\+/ && line !~ /^\+\+\+/
81
+ line = "#{line.chomp} - #{index}"
82
+ lines << line[1..-1]
83
+ index += 1
84
+ end
85
+
86
+ file.puts line
87
+ end
88
+ end
89
+ @book.import
90
+ @book.data.each do |tag|
91
+ tag.added.each do |tag_part|
92
+ tag_part.should == lines.shift
93
+ end
94
+ end
95
+ end
96
+
97
+ it "should not have any change in size" do
98
+ @book.size.should == @original_size
99
+ end
100
+ end
5
101
  end
@@ -2,4 +2,98 @@
2
2
  require File.dirname(__FILE__) + "/../spec_helper"
3
3
 
4
4
  describe "PowerPoint", :power_point do
5
+ describe "opening the sample document" do
6
+ before :all do
7
+ @export_target = StringIO.new <<-eof
8
+ --- /Users/bryan/Projects/konjac/spec/office/bin/sample.pptx
9
+ +++ /Users/bryan/Projects/konjac/spec/office/bin/sample.pptx
10
+ @@ 1,1 @@
11
+ -Title
12
+ +Title
13
+ @@ 1,2 @@
14
+ -Subtitle
15
+ +Subtitle
16
+ @@ 2,1 @@
17
+ -Page title
18
+ +Page title
19
+ @@ 2,2 @@
20
+ -Bullet point 1
21
+ -Bullet point 2 with a line break
22
+ -Bullet point 3
23
+ -Indented bullet point
24
+ +Bullet point 1
25
+ +Bullet point 2 with a line break
26
+ +Bullet point 3
27
+ +Indented bullet point
28
+ @@ 2,3 @@
29
+ -Text box
30
+ +Text box
31
+ eof
32
+ @spec_path = File.dirname(File.expand_path(__FILE__))
33
+ @path = File.join(@spec_path, "bin", "sample.pptx")
34
+ @diff_path = "#{@path}.diff"
35
+ @deck = Office.power_point(@path)
36
+ @original_size = @deck.size
37
+ end
38
+
39
+ after :all do
40
+ @deck.close :saving => false
41
+ end
42
+
43
+ it "should open the test document" do
44
+ @deck.read.should == ["Title"]
45
+ end
46
+
47
+ it "should return the same path" do
48
+ @deck.path.should == @path
49
+ end
50
+
51
+ it "should export tags to a tags file" do
52
+ File.delete @diff_path rescue Errno::ENOENT
53
+ @deck.export
54
+ File.exists?(@diff_path).should == true
55
+ end
56
+
57
+ it "should have exported the expected tags" do
58
+ @export_target.rewind
59
+ tags = File.open(@diff_path)
60
+ tags.each_line do |tag|
61
+ tag.should == @export_target.readline
62
+ end
63
+ end
64
+
65
+ it "should read some arbitrary items correctly" do
66
+ @deck.read(1, 2).should == ["Subtitle"]
67
+ @deck.read(2, 2).should == ["Bullet point 1", "Bullet point 2\vwith a line break", "Bullet point 3", "Indented bullet point"]
68
+ @deck.read(2, 3).should == ["Text box"]
69
+ end
70
+
71
+ it "should import all tags correctly" do
72
+ @export_target.rewind
73
+ @deck.find 1, 1, 1
74
+ index = 0
75
+ lines = []
76
+ File.open(@diff_path, "w") do |file|
77
+ @export_target.each do |line|
78
+ if line =~ /^\+/ && line !~ /^\+\+\+/
79
+ line = "#{line.chomp} - #{index}"
80
+ lines << line[1..-1]
81
+ index += 1
82
+ end
83
+
84
+ file.puts line
85
+ end
86
+ end
87
+ @deck.import
88
+ @deck.data.each do |tag|
89
+ tag.added.each do |tag_part|
90
+ tag_part.should == lines.shift
91
+ end
92
+ end
93
+ end
94
+
95
+ it "should not have any change in size" do
96
+ @deck.size.should == @original_size
97
+ end
98
+ end
5
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konjac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amatch
16
- requirement: &70305013220300 !ruby/object:Gem::Requirement
16
+ requirement: &70103350529580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70305013220300
24
+ version_requirements: *70103350529580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: git
27
- requirement: &70305013219840 !ruby/object:Gem::Requirement
27
+ requirement: &70103350528820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70305013219840
35
+ version_requirements: *70103350528820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: highline
38
- requirement: &70305013219080 !ruby/object:Gem::Requirement
38
+ requirement: &70103350527780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70305013219080
46
+ version_requirements: *70103350527780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &70305013218100 !ruby/object:Gem::Requirement
49
+ requirement: &70103350526520 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70305013218100
57
+ version_requirements: *70103350526520
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nokogiri
60
- requirement: &70305013216940 !ruby/object:Gem::Requirement
60
+ requirement: &70103350525840 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70305013216940
68
+ version_requirements: *70103350525840
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rb-appscript
71
- requirement: &70305013216160 !ruby/object:Gem::Requirement
71
+ requirement: &70103350525260 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70305013216160
79
+ version_requirements: *70103350525260
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sdoc
82
- requirement: &70305013215520 !ruby/object:Gem::Requirement
82
+ requirement: &70103350524360 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70305013215520
90
+ version_requirements: *70103350524360
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: term-ansicolor
93
- requirement: &70305013214700 !ruby/object:Gem::Requirement
93
+ requirement: &70103350522660 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70305013214700
101
+ version_requirements: *70103350522660
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: trollop
104
- requirement: &70305013213200 !ruby/object:Gem::Requirement
104
+ requirement: &70103350521760 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70305013213200
112
+ version_requirements: *70103350521760
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: autotest
115
- requirement: &70305013212040 !ruby/object:Gem::Requirement
115
+ requirement: &70103350520920 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70305013212040
123
+ version_requirements: *70103350520920
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: autotest-fsevent
126
- requirement: &70305013211000 !ruby/object:Gem::Requirement
126
+ requirement: &70103350519960 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70305013211000
134
+ version_requirements: *70103350519960
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: autotest-growl
137
- requirement: &70305013210200 !ruby/object:Gem::Requirement
137
+ requirement: &70103350519460 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70305013210200
145
+ version_requirements: *70103350519460
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: bundler
148
- requirement: &70305013209760 !ruby/object:Gem::Requirement
148
+ requirement: &70103350519040 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *70305013209760
156
+ version_requirements: *70103350519040
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: rspec
159
- requirement: &70305013209200 !ruby/object:Gem::Requirement
159
+ requirement: &70103350516560 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,7 +164,7 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *70305013209200
167
+ version_requirements: *70103350516560
168
168
  description: A Ruby command-line utility for translating files using a YAML wordlist
169
169
  email:
170
170
  - bryan.mckelvey@gmail.com
@@ -217,6 +217,8 @@ files:
217
217
  - spec/dictionary_spec.rb
218
218
  - spec/language_spec.rb
219
219
  - spec/office/bin/sample.docx
220
+ - spec/office/bin/sample.pptx
221
+ - spec/office/bin/sample.xlsx
220
222
  - spec/office/excel_spec.rb
221
223
  - spec/office/generic_spec.rb
222
224
  - spec/office/power_point_spec.rb
@@ -254,6 +256,8 @@ test_files:
254
256
  - spec/dictionary_spec.rb
255
257
  - spec/language_spec.rb
256
258
  - spec/office/bin/sample.docx
259
+ - spec/office/bin/sample.pptx
260
+ - spec/office/bin/sample.xlsx
257
261
  - spec/office/excel_spec.rb
258
262
  - spec/office/generic_spec.rb
259
263
  - spec/office/power_point_spec.rb