konjac 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
- require "rdoc/task"
2
+ # require "rdoc/task" this is weirdly busted for some reason
3
3
  require "rspec/core/rake_task"
4
4
  require "sdoc"
5
5
 
@@ -12,15 +12,31 @@ module Konjac
12
12
  # following will work too:
13
13
  #
14
14
  # doc = Konjac::Office.word
15
+ #
16
+ # From there, you can send some basic reading and writing instructions to the
17
+ # document:
18
+ #
19
+ # doc.read 1
20
+ # doc.read :paragraph => 1
21
+ # doc.read 1, :type => :shape
22
+ #
23
+ # doc.write "First paragraph", 1
24
+ #
25
+ # doc.export
26
+ # doc.import
15
27
  module Office
16
- autoload :Generic, "konjac/office/generic"
17
- autoload :Mac, "konjac/office/mac"
18
- autoload :OS, "konjac/office/os"
19
- autoload :Tag, "konjac/office/tag"
20
- autoload :Windows, "konjac/office/windows"
21
- autoload :XML, "konjac/office/windows"
28
+ autoload :Base, "konjac/office/base"
29
+ autoload :Mac, "konjac/office/mac"
30
+ autoload :OS, "konjac/office/os"
31
+ autoload :Tag, "konjac/office/tag"
32
+ autoload :Windows, "konjac/office/windows"
33
+ autoload :XML, "konjac/office/windows"
22
34
 
23
35
  class << self
36
+ # Creates a new object inheriting from Base. The application chosen is
37
+ # based on the user's environment and the type of the document. For
38
+ # example, a file with a .docx extension will default to Microsoft Word,
39
+ # and on OSX it will default to Konjac::Office::Mac::Word
24
40
  def new(path)
25
41
  env = environment
26
42
  return nil if env.nil?
@@ -35,6 +51,10 @@ module Konjac
35
51
  end
36
52
  end
37
53
 
54
+ # The user's environment. Currently, this just detects whether an OS is
55
+ # Windows, Mac or other (simply because Microsoft Office is unavailable
56
+ # on Linux, so scripting something like LibreOffice or OpenOffice is more
57
+ # long-term).
38
58
  def environment
39
59
  if OS.mac?
40
60
  Mac
@@ -47,6 +67,18 @@ module Konjac
47
67
 
48
68
  private
49
69
 
70
+ # If possible, retrieves the appropriate class inheriting from Office::Base for the user
71
+ # based on his environment or OS. For example, if a user types
72
+ #
73
+ # doc = Konjac::Office.word
74
+ #
75
+ # on Mac that will be the same thing as a call to
76
+ #
77
+ # doc = Konjac::Mac::Office.new
78
+ #
79
+ # whereas for a user on Windows it will be equivalent to
80
+ #
81
+ # doc = Konjac::Windows::Office.new
50
82
  def method_missing(name, *args, &block)
51
83
  env = environment
52
84
  return super if env.nil?
@@ -58,6 +90,8 @@ module Konjac
58
90
  end
59
91
  end
60
92
 
93
+ # A list of valid environments based on the directories in the
94
+ # lib/konjac/office folder of this gem
61
95
  def valid_environments
62
96
  return @environments unless @environments.nil?
63
97
 
@@ -71,6 +105,7 @@ module Konjac
71
105
  @environments
72
106
  end
73
107
 
108
+ # Converts snake_case into CamelCase
74
109
  def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
75
110
  if first_letter_in_uppercase
76
111
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
@@ -79,6 +114,7 @@ module Konjac
79
114
  end
80
115
  end
81
116
 
117
+ # Converts CamelCase into snake_case
82
118
  def underscore(camel_cased_word)
83
119
  camel_cased_word.to_s.gsub(/::/, '/').
84
120
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
@@ -3,11 +3,13 @@ module Konjac
3
3
  module Office
4
4
  # This is a basic class that contains all the methods that are universal
5
5
  # across OSes, file formats, applications, etc.
6
- class Generic
6
+ class Base
7
7
  autoload :Item, "konjac/office/item"
8
8
 
9
- attr_reader :document, :index, :current
9
+ # The active document
10
+ attr_reader :document
10
11
 
12
+ # Creates a new Base object
11
13
  def initialize(location = nil)
12
14
  @document = open(File.expand_path(location)) unless location.nil?
13
15
  @document = active_document
@@ -21,10 +23,29 @@ module Konjac
21
23
  :document => @document,
22
24
  :delimiter => "\v"
23
25
  }
24
- @index = 0
25
- @current = nil
26
26
  end
27
27
 
28
+ # Finds the item at the specified indices. This method accepts both
29
+ # variadic inputs (i.e. <tt>1, 2, 3</tt>) or the equivalent hash (i.e.
30
+ # <tt>:sheet => 1, :row => 2, :cell => 3</tt>. The following pairs are
31
+ # equivalent:
32
+ #
33
+ # doc[1]
34
+ # doc[:paragraph => 1]
35
+ # doc.item_at 1
36
+ # doc.item_at :paragraph => 1
37
+ #
38
+ # xl[1, 1, 1]
39
+ # xl.item_at :sheet => 1, :row => 1, :cell => 1
40
+ #
41
+ # pp[1, 1]
42
+ # pp.item_at :slide => 1, :shape => 1
43
+ #
44
+ # doc[1, :type => :shape]
45
+ # doc :shape => 1, :type => :shape
46
+ # doc.shape_at 1
47
+ #
48
+ # along with all the obvious permutations.
28
49
  def [](*args)
29
50
  opts = parse_args(*args)
30
51
  return shape_at(opts) if opts[:type] == :shape
@@ -33,10 +54,13 @@ module Konjac
33
54
  end
34
55
  alias :item_at :[]
35
56
 
57
+ # Sets the item at the specified indices to the value of the first
58
+ # argument or the <tt>:text</tt> member of the supplied hash
36
59
  def []=(*args)
37
60
  write args.pop, *args
38
61
  end
39
62
 
63
+ # Retrieves the shape at the specified indices
40
64
  def shape_at(*args)
41
65
  last_item = args.last
42
66
  last_item = { :type => :shape }.merge(last_item) if last_item.is_a?(Hash)
@@ -44,22 +68,27 @@ module Konjac
44
68
  Item.new @shape_opts.merge(opts)
45
69
  end
46
70
 
71
+ # Writes to the item at the specified indices
47
72
  def write(text, *args)
48
73
  item_at(*args).write text
49
74
  end
50
75
 
76
+ # Reads from the item at the specified indices
51
77
  def read(*args)
52
78
  item_at(*args).read
53
79
  end
54
80
 
81
+ # Loads Tags from the supplied path
55
82
  def tags
56
83
  Tag.load path
57
84
  end
58
85
 
86
+ # Exports Tags to the specified path
59
87
  def export
60
88
  Tag.dump data, path
61
89
  end
62
90
 
91
+ # Imports Tags from the specified path into the +document+
63
92
  def import
64
93
  tags.each do |tag|
65
94
  if tag.changed? && !tag.blank?
@@ -90,10 +119,6 @@ module Konjac
90
119
  parsed = Hash[@item_opts[:ref_path].zip(args)].merge(parsed)
91
120
  end
92
121
  end
93
-
94
- def clean(text, opts)
95
-
96
- end
97
122
  end
98
123
  end
99
124
  end
@@ -1,8 +1,14 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
- class Generic
4
+ class Base
5
+ # An item class dealing with hierarchy in which to search the
6
+ # document. For example, with Word it will search paragraphs and for
7
+ # PowerPoint it will search slides then shapes. This class then will
8
+ # follow the hierarchy to text objects and is capable of reading from and
9
+ # writing to them.
5
10
  class Item
11
+ # Creates a new Item
6
12
  def initialize(opts = {})
7
13
  @opts = opts
8
14
 
@@ -13,6 +19,9 @@ module Konjac
13
19
  @ref = item
14
20
  end
15
21
 
22
+ # Retrieves the content from the Item. Note that without getter or
23
+ # setter methods this may not actually retrieve anything but the message
24
+ # we intend to send to the scripting interface.
16
25
  def content
17
26
  return @content unless @content.nil?
18
27
 
@@ -23,10 +32,13 @@ module Konjac
23
32
  @content
24
33
  end
25
34
 
35
+ # Reads the item's text content, cleaned up according to the supplied
36
+ # delimiters and strippable items
26
37
  def read
27
38
  clean content.send(@opts[:read])
28
39
  end
29
40
 
41
+ # Writes to the item
30
42
  def write(text)
31
43
  content.send @opts[:write], text
32
44
  end
@@ -41,7 +53,11 @@ module Konjac
41
53
  end
42
54
 
43
55
  def clean(text)
44
- text.gsub(@opts[:strippable], "").split(@opts[:delimiter])
56
+ if text == :missing_value
57
+ [""]
58
+ else
59
+ text.gsub(@opts[:strippable], "").split(@opts[:delimiter])
60
+ end
45
61
  end
46
62
  end
47
63
  end
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
+ # The Office for Mac namespace
4
5
  module Mac
5
6
  autoload :Excel, "konjac/office/mac/excel"
6
7
  autoload :PowerPoint, "konjac/office/mac/power_point"
@@ -2,7 +2,9 @@
2
2
  module Konjac
3
3
  module Office
4
4
  module Mac
5
+ # Excel for Mac
5
6
  class Excel < Shared
7
+ # Creates a new Excel object
6
8
  def initialize(path = nil)
7
9
  super "Microsoft Excel", path
8
10
  @strippable = //
@@ -2,7 +2,9 @@
2
2
  module Konjac
3
3
  module Office
4
4
  module Mac
5
+ # PowerPoint for Mac
5
6
  class PowerPoint < Shared
7
+ # Creates a new PowerPoint object
6
8
  def initialize(path = nil)
7
9
  super "Microsoft PowerPoint", path
8
10
  @strippable = //
@@ -4,7 +4,9 @@ require "appscript"
4
4
  module Konjac
5
5
  module Office
6
6
  module Mac
7
- class Shared < Generic
7
+ # Inherits from Base and adds some shared methods to the Mac interface
8
+ class Shared < Base
9
+ # Creates a new Shared object
8
10
  def initialize(app_name, path = nil)
9
11
  @application = Appscript.app(app_name)
10
12
  super path
@@ -18,6 +20,7 @@ module Konjac
18
20
  })
19
21
  end
20
22
 
23
+ # Open the document at the supplied +path+ using the current application
21
24
  def open(path)
22
25
  @application.open path
23
26
  end
@@ -2,8 +2,16 @@
2
2
  module Konjac
3
3
  module Office
4
4
  module Mac
5
+ # Word for Mac
5
6
  class Word < Shared
7
+ # A basic class to deal with Word's ridiculously awful handling
8
+ # of replacing text in paragraphs. Instead of simply setting the
9
+ # paragraph's text to something new, you have to select all but the last
10
+ # character (which is a newline) then write. Otherwise, your paragraph
11
+ # might inherit the formatting of the line below it, which is especially
12
+ # a problem with tables.
6
13
  class WordItem < Item
14
+ # Writes to a Word item
7
15
  def write(text)
8
16
  para_start = @ref.text_object.start_of_content.get
9
17
  para_end = @ref.text_object.end_of_content.get
@@ -14,10 +22,9 @@ module Konjac
14
22
  end
15
23
  end
16
24
 
25
+ # Creates a new Word item
17
26
  def initialize(path = nil)
18
27
  super "Microsoft Word", path
19
- @index = 1
20
- @current = @document.paragraphs[@index]
21
28
  @item_opts.merge!({
22
29
  :ref_path => [:paragraph],
23
30
  :content_path => [:text_object, :content],
@@ -89,16 +96,6 @@ module Konjac
89
96
  @document.paragraphs.get.size
90
97
  end
91
98
  alias :length :size
92
-
93
- # Goes to the next paragraph
94
- def succ
95
- @index += 1
96
- @current = @current.next_paragraph
97
- rescue
98
- @index -= 1
99
- nil
100
- end
101
- alias :next :succ
102
99
  end
103
100
  end
104
101
  end
@@ -1,9 +1,22 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
+ # A tag item, used to export and import data to and from the document
4
5
  class Tag
5
- attr_accessor :indices, :removed, :added, :type
6
+ # The indices used to scan the document's hierarchy to reach the item
7
+ attr_accessor :indices
8
+
9
+ # The text that has been removed
10
+ attr_accessor :removed
11
+
12
+ # The text with which to replaced the removed content
13
+ attr_accessor :added
14
+
15
+ # The type, used to identify special objects such as a
16
+ # <tt>:shape</tt>. Defaults to +nil+.
17
+ attr_accessor :type
6
18
 
19
+ # Creates a new tag item
7
20
  def initialize
8
21
  @indices = nil
9
22
  @removed = []
@@ -42,6 +55,8 @@ module Konjac
42
55
  end
43
56
 
44
57
  class << self
58
+ # A list of regular expressions for parsing a document in unified
59
+ # diff-esque style into Tags
45
60
  TAG_MATCHES = {
46
61
  :header => /^(?:---|\+\+\+)/,
47
62
  :comment => /^\@\@ ([a-z]*) ?([\d, ]+) \@\@$/i,
@@ -73,6 +88,8 @@ module Konjac
73
88
  tags
74
89
  end
75
90
 
91
+ # Dumps the tags into a .diff document with the same name as the
92
+ # document from which those tags were extracted
76
93
  def dump(tags, path, opts = {})
77
94
  original_path = path.sub(/\.diff$/, "")
78
95
  diff_path = "#{original_path}.diff"
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
+ # The Office for Windows namespace
4
5
  module Windows
5
6
  autoload :Excel, "konjac/office/windows/excel"
6
7
  autoload :PowerPoint, "konjac/office/windows/power_point"
@@ -1,2 +1 @@
1
- # coding: utf-8
2
1
 
@@ -1,7 +1,8 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
- module xml
4
+ # The XML (Open Office XML, that is) namespace
5
+ module XML
5
6
  autoload :Excel, "konjac/office/xml/excel"
6
7
  autoload :PowerPoint, "konjac/office/xml/powerpoint"
7
8
  autoload :Shared, "konjac/office/xml/shared"
@@ -1,6 +1,8 @@
1
1
  # coding: utf-8
2
2
  module Konjac
3
3
  module Office
4
+ # Specialized XML handling for with the Office Open XML-based formats in
5
+ # Office 2007+
4
6
  module XML
5
7
  # Imports the text content of a tag file into a Word 2003+, utilizing a
6
8
  # cleaned-up version of the document's original XML structure
@@ -1,4 +1,4 @@
1
1
  module Konjac
2
2
  # The current version number of Konjac
3
- VERSION = "0.3.3"
3
+ VERSION = "0.3.4"
4
4
  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.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-15 00:00:00.000000000 Z
12
+ date: 2012-03-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amatch
16
- requirement: &70324813508240 !ruby/object:Gem::Requirement
16
+ requirement: &70346758881440 !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: *70324813508240
24
+ version_requirements: *70346758881440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: git
27
- requirement: &70324813507600 !ruby/object:Gem::Requirement
27
+ requirement: &70346758880980 !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: *70324813507600
35
+ version_requirements: *70346758880980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: highline
38
- requirement: &70324813506800 !ruby/object:Gem::Requirement
38
+ requirement: &70346758880560 !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: *70324813506800
46
+ version_requirements: *70346758880560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &70324813505260 !ruby/object:Gem::Requirement
49
+ requirement: &70346758880140 !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: *70324813505260
57
+ version_requirements: *70346758880140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: nokogiri
60
- requirement: &70324813504560 !ruby/object:Gem::Requirement
60
+ requirement: &70346758879720 !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: *70324813504560
68
+ version_requirements: *70346758879720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rb-appscript
71
- requirement: &70324813503920 !ruby/object:Gem::Requirement
71
+ requirement: &70346758879300 !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: *70324813503920
79
+ version_requirements: *70346758879300
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sdoc
82
- requirement: &70324813503360 !ruby/object:Gem::Requirement
82
+ requirement: &70346758878840 !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: *70324813503360
90
+ version_requirements: *70346758878840
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: term-ansicolor
93
- requirement: &70324813502040 !ruby/object:Gem::Requirement
93
+ requirement: &70346758878420 !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: *70324813502040
101
+ version_requirements: *70346758878420
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: trollop
104
- requirement: &70324813500600 !ruby/object:Gem::Requirement
104
+ requirement: &70346758878000 !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: *70324813500600
112
+ version_requirements: *70346758878000
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: autotest
115
- requirement: &70324813499600 !ruby/object:Gem::Requirement
115
+ requirement: &70346758877580 !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: *70324813499600
123
+ version_requirements: *70346758877580
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: autotest-fsevent
126
- requirement: &70324813498700 !ruby/object:Gem::Requirement
126
+ requirement: &70346758877120 !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: *70324813498700
134
+ version_requirements: *70346758877120
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: autotest-growl
137
- requirement: &70324813498080 !ruby/object:Gem::Requirement
137
+ requirement: &70346758876700 !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: *70324813498080
145
+ version_requirements: *70346758876700
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: bundler
148
- requirement: &70324813497640 !ruby/object:Gem::Requirement
148
+ requirement: &70346758876280 !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: *70324813497640
156
+ version_requirements: *70346758876280
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: rspec
159
- requirement: &70324813497100 !ruby/object:Gem::Requirement
159
+ requirement: &70346758875840 !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: *70324813497100
167
+ version_requirements: *70346758875840
168
168
  description: A Ruby command-line utility for translating files using a YAML wordlist
169
169
  email:
170
170
  - bryan.mckelvey@gmail.com
@@ -191,7 +191,7 @@ files:
191
191
  - lib/konjac/installer.rb
192
192
  - lib/konjac/language.rb
193
193
  - lib/konjac/office.rb
194
- - lib/konjac/office/generic.rb
194
+ - lib/konjac/office/base.rb
195
195
  - lib/konjac/office/item.rb
196
196
  - lib/konjac/office/mac.rb
197
197
  - lib/konjac/office/mac/excel.rb
@@ -246,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
246
  version: '0'
247
247
  requirements: []
248
248
  rubyforge_project: konjac
249
- rubygems_version: 1.8.10
249
+ rubygems_version: 1.8.17
250
250
  signing_key:
251
251
  specification_version: 3
252
252
  summary: A Ruby command-line utility for translating files using a YAML wordlist