ms-docx 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/README.md +33 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ms_docx.rb +15 -0
- data/lib/ms_docx/document.rb +164 -0
- data/lib/ms_docx/erorrs.rb +9 -0
- data/lib/ms_docx/paragraph.rb +26 -0
- data/lib/ms_docx/version.rb +12 -0
- data/ms-docx.gemspec +40 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 18c63dd5cd383c2d4787d25e3e77d22435010881
|
4
|
+
data.tar.gz: 48bdcd669b61b51f47b18725417d136d3d06ce93
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6742d760bf2ba0293f45fe3b749266fa34dabd21fc1109ff9a6f49e86bdc26f84f581968f6bba27640ea1e370b81e1bf41ad832452acbacefdda46c56990c67
|
7
|
+
data.tar.gz: 24d8aec05037982c7798fdaf41663dfa7e55c044b1c3ee2e788edeaf627368be7437d1cb6f62a8b593a87222ef81b221bed2ec2ca6af41cec606a37a52a2948a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ms-docx (0.1.0)
|
5
|
+
nokogiri (>= 1.8.1)
|
6
|
+
rubyzip (>= 1.0.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.3)
|
12
|
+
mini_portile2 (2.3.0)
|
13
|
+
nokogiri (1.8.1)
|
14
|
+
mini_portile2 (~> 2.3.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
rspec (3.7.0)
|
17
|
+
rspec-core (~> 3.7.0)
|
18
|
+
rspec-expectations (~> 3.7.0)
|
19
|
+
rspec-mocks (~> 3.7.0)
|
20
|
+
rspec-core (3.7.0)
|
21
|
+
rspec-support (~> 3.7.0)
|
22
|
+
rspec-expectations (3.7.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-mocks (3.7.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.7.0)
|
28
|
+
rspec-support (3.7.0)
|
29
|
+
rubyzip (1.2.1)
|
30
|
+
yard (0.9.10)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
bundler (~> 1.16)
|
37
|
+
ms-docx!
|
38
|
+
rake (~> 10.0)
|
39
|
+
rspec (~> 3.0)
|
40
|
+
yard (>= 0.9.10)
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
1.16.0
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# MsDocx
|
2
|
+
|
3
|
+
TODO: Delete this and the text above, and describe your gem
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ms-docx'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ms-docx
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ms-docx.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'ms_docx'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ms_docx.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'ms_docx/version'
|
2
|
+
require 'zip'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'ms_docx/erorrs'
|
5
|
+
require 'ms_docx/paragraph'
|
6
|
+
require 'ms_docx/document'
|
7
|
+
|
8
|
+
# @author Alex Mercury
|
9
|
+
|
10
|
+
# Dependency:
|
11
|
+
# gem 'rubyzip', '>= 1.0.0'
|
12
|
+
# gem 'nokogiri', '>= 1.8.1'
|
13
|
+
|
14
|
+
module MsDocx
|
15
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
module MsDocx
|
2
|
+
# @author Alex Mercury
|
3
|
+
# @example
|
4
|
+
# doc = MsDocx::Document.open('1.docx')
|
5
|
+
#
|
6
|
+
# puts doc.paragraphs.first.text
|
7
|
+
# # => 'some text'
|
8
|
+
#
|
9
|
+
class Document
|
10
|
+
|
11
|
+
CONTENT_ENTRY = 'word/document.xml'
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
# Open .docx file
|
16
|
+
# @param file_path[String] file path
|
17
|
+
# @return [MsDocx::Document] document object
|
18
|
+
def open(file_path)
|
19
|
+
MsDocx::Document.new(file_path)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(file_path, *args)
|
25
|
+
if File.exist? file_path
|
26
|
+
@file_path = file_path
|
27
|
+
@temp_file_path = file_path.sub(/\.docx/, '~temp.docx')
|
28
|
+
@docx_zip = Zip::File.open(file_path)
|
29
|
+
else
|
30
|
+
raise MsDocx::Errors::FileNotExist, "File '#{file_path}' doesn't exist ❗"
|
31
|
+
end
|
32
|
+
|
33
|
+
@doc = @docx_zip.find_entry(CONTENT_ENTRY).get_input_stream.read
|
34
|
+
|
35
|
+
@xml = Nokogiri::XML::Document.parse @doc
|
36
|
+
|
37
|
+
@paragraphs = []
|
38
|
+
|
39
|
+
@xml.xpath('//w:body//w:p').each do |w_p|
|
40
|
+
@paragraphs << MsDocx::Paragraph.new(w_p)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# @xml.xpath('//w:p//w:r//w:t').each do |p|
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# puts p.content
|
48
|
+
#
|
49
|
+
#
|
50
|
+
# p.content = 'test'
|
51
|
+
#
|
52
|
+
#
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
|
56
|
+
#
|
57
|
+
#
|
58
|
+
# File.open(new_file, 'wb') {|f| f.write(buffer.string) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def paragraphs
|
62
|
+
@paragraphs
|
63
|
+
end
|
64
|
+
|
65
|
+
def each_paragraph(&block)
|
66
|
+
@paragraphs.each do |paragraph|
|
67
|
+
block.call(paragraph)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# block.call(self)
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_text
|
81
|
+
@paragraphs.map(&:text).join("\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_xml
|
85
|
+
|
86
|
+
body = @xml.at('//w:body')
|
87
|
+
|
88
|
+
body.children = ''
|
89
|
+
|
90
|
+
@paragraphs.each do |paragraph|
|
91
|
+
body.add_child(paragraph.to_xml)
|
92
|
+
end
|
93
|
+
|
94
|
+
@xml.at('//w:document').children = body
|
95
|
+
|
96
|
+
@xml.to_xml
|
97
|
+
end
|
98
|
+
|
99
|
+
def save_file(file_path)
|
100
|
+
|
101
|
+
@buffer = Zip::OutputStream.write_buffer do |out|
|
102
|
+
@docx_zip.entries.each do |e|
|
103
|
+
if e.name == CONTENT_ENTRY
|
104
|
+
|
105
|
+
# doc = get_xml_doc_from_stream(e.get_input_stream.read)
|
106
|
+
|
107
|
+
# doc.xpath('//w:body//w:p').each do |pt|
|
108
|
+
|
109
|
+
# pt.xpath(pt.path + '//w:t').
|
110
|
+
|
111
|
+
# p pt.to_xml
|
112
|
+
|
113
|
+
# block.call(pt)
|
114
|
+
|
115
|
+
# p pt.to_xml
|
116
|
+
|
117
|
+
# w_r_list = pt.xpath(pt.path + '//w:r')
|
118
|
+
|
119
|
+
# if w_r_list.length > 1
|
120
|
+
# w_r_list[1..-1].each{|pp| pp.remove }
|
121
|
+
|
122
|
+
# p pt.to_xml
|
123
|
+
# p w_r_list.to_xml
|
124
|
+
# end
|
125
|
+
|
126
|
+
# pt
|
127
|
+
|
128
|
+
# p w_r_list.to_xml
|
129
|
+
|
130
|
+
|
131
|
+
# w_r_list.first.xpath(w_r_list.first.path + '//w:t').map{|pp| block.call(pp)} if w_r_list.first
|
132
|
+
|
133
|
+
|
134
|
+
# p nil
|
135
|
+
# p nil
|
136
|
+
# p nil
|
137
|
+
|
138
|
+
|
139
|
+
# end
|
140
|
+
|
141
|
+
|
142
|
+
# p doc.to_xml
|
143
|
+
|
144
|
+
out.put_next_entry(CONTENT_ENTRY)
|
145
|
+
out.write self.to_xml
|
146
|
+
else
|
147
|
+
out.put_next_entry(e.name)
|
148
|
+
e.write_to_zip_output_stream out
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
File.open(file_path, 'wb') {|f| f.write(@buffer.string) }
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def get_xml_doc_from_stream(stream)
|
160
|
+
Nokogiri::XML::Document.parse stream
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module MsDocx
|
2
|
+
# @author Alex Mercury
|
3
|
+
class Paragraph
|
4
|
+
|
5
|
+
# Nokogiri::XML::Element
|
6
|
+
def initialize(xml_element)
|
7
|
+
@xml_element = xml_element
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_xml
|
11
|
+
@xml_element.to_xml
|
12
|
+
end
|
13
|
+
|
14
|
+
def text
|
15
|
+
@xml_element.xpath(@xml_element.path + '//w:r//w:t').map(&:content).join('')
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def text=(value)
|
20
|
+
|
21
|
+
text_node = @xml_element.xpath(@xml_element.path + '//w:r//w:t').first
|
22
|
+
text_node.content = value if text_node
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MsDocx
|
2
|
+
|
3
|
+
# x.0.0 level changes for backwards incompatible API changes, such as changes that will break existing users code if they update
|
4
|
+
MAJOR = 0
|
5
|
+
# 0.x.0 level changes for any backwards compatible API changes, such as new functionality/features
|
6
|
+
MINOR = 1
|
7
|
+
# 0.0.x level changes for implementation level detail changes, such as small bug fixes
|
8
|
+
PATCH = 0
|
9
|
+
|
10
|
+
VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}".freeze
|
11
|
+
|
12
|
+
end
|
data/ms-docx.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'ms_docx/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ms-docx'
|
9
|
+
spec.version = MsDocx::VERSION
|
10
|
+
spec.authors = ['alexmercury']
|
11
|
+
spec.email = ['rormercury@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
|
14
|
+
spec.description = %q{Write a longer description or delete this line.}
|
15
|
+
spec.homepage = 'https://github.com/itbeast/ms-docx'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
37
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
38
|
+
spec.add_runtime_dependency 'rubyzip', '~> 1.0'
|
39
|
+
spec.add_runtime_dependency 'nokogiri', '~> 1.8'
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ms-docx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- alexmercury
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubyzip
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: nokogiri
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.8'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.8'
|
97
|
+
description: Write a longer description or delete this line.
|
98
|
+
email:
|
99
|
+
- rormercury@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/ms_docx.rb
|
114
|
+
- lib/ms_docx/document.rb
|
115
|
+
- lib/ms_docx/erorrs.rb
|
116
|
+
- lib/ms_docx/paragraph.rb
|
117
|
+
- lib/ms_docx/version.rb
|
118
|
+
- ms-docx.gemspec
|
119
|
+
homepage: https://github.com/itbeast/ms-docx
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.6.13
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: Write a short summary, because RubyGems requires one.
|
143
|
+
test_files: []
|