docxedit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Olivier Amblet <olivier@amblet.net>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,31 @@
1
+ Ruby Docx Edit
2
+ ==============
3
+
4
+ A very minimal Ruby library that let you edit .docx file in some simple way
5
+
6
+ * make string replacement in .docx document
7
+ * insert new paragraphe using XML before or after a block of text
8
+
9
+ This is not meant to be fully functional or even completed in the future, take this code if you want at your own risk.
10
+
11
+
12
+
13
+ require 'docxedit'
14
+ doc = DocxEdit::Docx.new('mydocfile.docx')
15
+ doc.contains? /A simple string/ #return true if regexp is match in one of the document xml node
16
+ doc.replace(/str_to_find/, "replacement") #replace the matched string with "replacement" string
17
+ block = doc.find_block_with_content("[[EXACT block content]]") #return a ContentBlock object
18
+
19
+ # insert a new block of text before block
20
+ doc.insert(:before, block, ContentBlock.new("<w:p><w:r>A new block for this file.</w:r></w:p>", "A new block for this file."))
21
+
22
+ VERSIONS
23
+ ========
24
+ 0.0.1 First release
25
+
26
+ ROADMAP
27
+ =======
28
+ No more development planned for now
29
+
30
+
31
+ homepage: https://github.com/oliamb/docxedit
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ Bundler.require(:default)
4
+
5
+ unless Kernel.respond_to?(:require_relative)
6
+ module Kernel
7
+ def require_relative(path)
8
+ require File.join(File.dirname(caller[0]), path.to_str)
9
+ end
10
+ end
11
+ end
12
+
13
+ require_relative "docxedit/docx"
@@ -0,0 +1,21 @@
1
+ module DocxEdit
2
+ class ContentBlock
3
+
4
+ attr_accessor :content
5
+
6
+ def initialize(xml, content)
7
+ @xml = xml
8
+ @content_key = content
9
+ @content = content
10
+ end
11
+
12
+ def xml
13
+ unless(@content_key == @content)
14
+ @node = REXML::XPath.first(@xml, "//*[text()='#{@content_key}']]")
15
+ @node.text = @content
16
+ @content_key = @content
17
+ end
18
+ return @xml
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,70 @@
1
+ require 'zip/zip'
2
+ require "rexml/document"
3
+ require_relative 'content_block'
4
+
5
+ module DocxEdit
6
+ class Docx
7
+ attr_reader :zip_file, :xml_document
8
+
9
+ def initialize(path)
10
+ @zip_file = Zip::ZipFile.new(path)
11
+ @xml_document = REXML::Document.new(read_content)
12
+ end
13
+
14
+ def contains?(text)
15
+ REXML::XPath.each @xml_document, XPATH_ALL_TEXT_NODE do |n|
16
+ return true if n.text =~ text
17
+ end
18
+ return false
19
+ end
20
+
21
+ # Persist changes in the Zip file
22
+ def commit
23
+ write_content
24
+ end
25
+
26
+ def replace(reg_to_match, replacement)
27
+ REXML::XPath.each @xml_document, XPATH_ALL_TEXT_NODE do |n|
28
+ n.text = n.text.gsub(reg_to_match, replacement)
29
+ end
30
+ end
31
+
32
+ def find_block_with_content(exact_content_string)
33
+ node = REXML::XPath.first(@xml_document, "//w:p[descendant-or-self::*[text()='#{exact_content_string}']]")
34
+ return ContentBlock.new(node, exact_content_string)
35
+ end
36
+
37
+ # insert the xml of a content block :before or :after the anchor_block
38
+ def insert_block(position, anchor_block, new_block)
39
+ case position
40
+ when :before
41
+ anchor_block.xml.previous_sibling = new_block.xml
42
+ when :after
43
+ anchor_block.xml.next_sibling = new_block.xml
44
+ else
45
+ raise "position argument must be one of :before, :after"
46
+ end
47
+ end
48
+
49
+ def remove_block(block)
50
+ block.xml.remove
51
+ end
52
+
53
+ private
54
+ DOCUMENT_FILE_PATH = 'word/document.xml'
55
+ XPATH_ALL_TEXT_NODE = "//*[text()]"
56
+
57
+ def read_content()
58
+ return text = @zip_file.read(DOCUMENT_FILE_PATH)
59
+ end
60
+
61
+ def write_content()
62
+ @zip_file.get_output_stream(DOCUMENT_FILE_PATH) do |input|
63
+ output = ""
64
+ @xml_document.write(output, 0)
65
+ input.write output
66
+ end
67
+ @zip_file.commit
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module DocxEdit
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docxedit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Olivier Amblet
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-10 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: minimal .docx file editor.
36
+ email: olivier@amblet.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/docxedit/content_block.rb
45
+ - lib/docxedit/docx.rb
46
+ - lib/docxedit/version.rb
47
+ - lib/docxedit.rb
48
+ - LICENSE
49
+ - README
50
+ has_rdoc: true
51
+ homepage: http://livingweb.ch
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 23
74
+ segments:
75
+ - 1
76
+ - 3
77
+ - 6
78
+ version: 1.3.6
79
+ requirements: []
80
+
81
+ rubyforge_project: docxedit
82
+ rubygems_version: 1.5.0
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Minimal Word Open Document format editor.
86
+ test_files: []
87
+