docx_replace 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
 - data/CHANGELOG +3 -0
 - data/Gemfile +4 -0
 - data/LICENSE.txt +22 -0
 - data/README.md +61 -0
 - data/Rakefile +1 -0
 - data/docx_replace.gemspec +20 -0
 - data/lib/docx_replace.rb +60 -0
 - data/lib/docx_replace/version.rb +3 -0
 - metadata +70 -0
 
    
        data/.gitignore
    ADDED
    
    
    
        data/CHANGELOG
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2013 Adam Albrecht
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,61 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # Docx Replace
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            This gem allows you to generate .docx files in your rails or ruby app by
         
     | 
| 
      
 4 
     | 
    
         
            +
            embedding variables inside of a .docx template. This is purposefully
         
     | 
| 
      
 5 
     | 
    
         
            +
            meant to be simple and feature-light.
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                gem 'docx_replace'
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                $ bundle
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                $ gem install docx_replace
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            Inside of a rails controller, your code might look something like this:
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 26 
     | 
    
         
            +
            def user_report
         
     | 
| 
      
 27 
     | 
    
         
            +
              @user = User.find(params[:user_id])
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
              respond_to do |format|
         
     | 
| 
      
 30 
     | 
    
         
            +
                format.docx do
         
     | 
| 
      
 31 
     | 
    
         
            +
                  # Initialize DocxReplace with your template
         
     | 
| 
      
 32 
     | 
    
         
            +
                  doc = DocxReplace::Doc.new("#{Rails.root}/lib/docx_templates/my_template.docx", "#{Rails.root}/tmp")
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                  # Replace some variables. $var$ convention is used here, but not required.
         
     | 
| 
      
 35 
     | 
    
         
            +
                  doc.replace("$first_name$", @user.first_name)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  doc.replace("$last_name$", @user.last_name)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  doc.replace("$user_bio$", @user.bio)
         
     | 
| 
      
 38 
     | 
    
         
            +
                  
         
     | 
| 
      
 39 
     | 
    
         
            +
                  # Write the document back to a temporary file
         
     | 
| 
      
 40 
     | 
    
         
            +
                  tmp_file = TempFile.new('word_tempate', "#{Rails.root}/tmp")
         
     | 
| 
      
 41 
     | 
    
         
            +
                  doc.commit(tmp_file)
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                  # Respond to the request by sending the temp file
         
     | 
| 
      
 44 
     | 
    
         
            +
                  send_file tmp_file.path, filename: "user_#{@user.id}_report.docx", disposition: 'attachment'
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
            end
         
     | 
| 
      
 48 
     | 
    
         
            +
            ```
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
            ## Contributing
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
            1. Fork it
         
     | 
| 
      
 54 
     | 
    
         
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         
     | 
| 
      
 55 
     | 
    
         
            +
            3. Commit your changes (`git commit -am 'Add some feature'`)
         
     | 
| 
      
 56 
     | 
    
         
            +
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
      
 57 
     | 
    
         
            +
            5. Create new Pull Request
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
            ## Credits
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
            Much of this code is based on an older gem called [docxedit](https://github.com/oliamb/docxedit). This has a few more features, but is very sensitive to the formatting of the .docx template.
         
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "bundler/gem_tasks"
         
     | 
| 
         @@ -0,0 +1,20 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'docx_replace/version'
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            Gem::Specification.new do |gem|
         
     | 
| 
      
 7 
     | 
    
         
            +
              gem.name          = "docx_replace"
         
     | 
| 
      
 8 
     | 
    
         
            +
              gem.version       = DocxReplace::VERSION
         
     | 
| 
      
 9 
     | 
    
         
            +
              gem.authors       = ["Adam Albrecht"]
         
     | 
| 
      
 10 
     | 
    
         
            +
              gem.email         = ["adam.albrecht@gmail.com"]
         
     | 
| 
      
 11 
     | 
    
         
            +
              gem.description   = %q{Find and replace variables inside a Micorsoft Word (.docx) template}
         
     | 
| 
      
 12 
     | 
    
         
            +
              gem.summary       = %q{Find and replace variables inside a Micorsoft Word (.docx) template}
         
     | 
| 
      
 13 
     | 
    
         
            +
              gem.homepage      = "https://github.com/adamalbrecht/docx_replace"
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              gem.files         = `git ls-files`.split($/)
         
     | 
| 
      
 16 
     | 
    
         
            +
              gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         
     | 
| 
      
 17 
     | 
    
         
            +
              gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
      
 18 
     | 
    
         
            +
              gem.require_paths = ["lib"]
         
     | 
| 
      
 19 
     | 
    
         
            +
              gem.add_runtime_dependency "rubyzip"
         
     | 
| 
      
 20 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/docx_replace.rb
    ADDED
    
    | 
         @@ -0,0 +1,60 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "docx_replace/version"
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'zip/zip'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'tempfile'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module DocxReplace
         
     | 
| 
      
 6 
     | 
    
         
            +
              class Doc
         
     | 
| 
      
 7 
     | 
    
         
            +
                def initialize(path, temp_dir=nil)
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @zip_file = Zip::ZipFile.new(path)
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @temp_dir = temp_dir
         
     | 
| 
      
 10 
     | 
    
         
            +
                  read_docx_file
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def replace(pattern, replacement, multiple_occurrences=false)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  if multiple_occurrences
         
     | 
| 
      
 15 
     | 
    
         
            +
                    @document_content.gsub!(pattern, replacement)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  else
         
     | 
| 
      
 17 
     | 
    
         
            +
                    @document_content.sub!(pattern, replacement)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  end
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                def commit(new_path=nil)
         
     | 
| 
      
 22 
     | 
    
         
            +
                  write_back_to_file(new_path)
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                private
         
     | 
| 
      
 26 
     | 
    
         
            +
                DOCUMENT_FILE_PATH = 'word/document.xml'
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                def read_docx_file
         
     | 
| 
      
 29 
     | 
    
         
            +
                  @document_content = @zip_file.read(DOCUMENT_FILE_PATH)
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                def write_back_to_file(new_path=nil)
         
     | 
| 
      
 33 
     | 
    
         
            +
                  if @temp_dir.nil?
         
     | 
| 
      
 34 
     | 
    
         
            +
                    temp_file = Tempfile.new('docxedit-')
         
     | 
| 
      
 35 
     | 
    
         
            +
                  else
         
     | 
| 
      
 36 
     | 
    
         
            +
                    temp_file = Tempfile.new('docxedit-', @temp_dir)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
                  Zip::ZipOutputStream.open(temp_file.path) do |zos|
         
     | 
| 
      
 39 
     | 
    
         
            +
                    @zip_file.entries.each do |e|
         
     | 
| 
      
 40 
     | 
    
         
            +
                      unless e.name == DOCUMENT_FILE_PATH
         
     | 
| 
      
 41 
     | 
    
         
            +
                        zos.put_next_entry(e.name)
         
     | 
| 
      
 42 
     | 
    
         
            +
                        zos.print e.get_input_stream.read
         
     | 
| 
      
 43 
     | 
    
         
            +
                      end
         
     | 
| 
      
 44 
     | 
    
         
            +
                    end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                    zos.put_next_entry(DOCUMENT_FILE_PATH)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    zos.print @document_content
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                  if new_path.nil?
         
     | 
| 
      
 51 
     | 
    
         
            +
                    path = @zip_file.name
         
     | 
| 
      
 52 
     | 
    
         
            +
                    FileUtils.rm(path)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  else
         
     | 
| 
      
 54 
     | 
    
         
            +
                    path = new_path
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
                  FileUtils.mv(temp_file.path, path)
         
     | 
| 
      
 57 
     | 
    
         
            +
                  @zip_file = Zip::ZipFile.new(path)
         
     | 
| 
      
 58 
     | 
    
         
            +
                end
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,70 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: docx_replace
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.0.0
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Adam Albrecht
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-04-16 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 14 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 15 
     | 
    
         
            +
              name: rubyzip
         
     | 
| 
      
 16 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 17 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 18 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 19 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 20 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 21 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 22 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 25 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 26 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 27 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 28 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 29 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 30 
     | 
    
         
            +
            description: Find and replace variables inside a Micorsoft Word (.docx) template
         
     | 
| 
      
 31 
     | 
    
         
            +
            email:
         
     | 
| 
      
 32 
     | 
    
         
            +
            - adam.albrecht@gmail.com
         
     | 
| 
      
 33 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 34 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 35 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 36 
     | 
    
         
            +
            files:
         
     | 
| 
      
 37 
     | 
    
         
            +
            - .gitignore
         
     | 
| 
      
 38 
     | 
    
         
            +
            - CHANGELOG
         
     | 
| 
      
 39 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 40 
     | 
    
         
            +
            - LICENSE.txt
         
     | 
| 
      
 41 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 42 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 43 
     | 
    
         
            +
            - docx_replace.gemspec
         
     | 
| 
      
 44 
     | 
    
         
            +
            - lib/docx_replace.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            - lib/docx_replace/version.rb
         
     | 
| 
      
 46 
     | 
    
         
            +
            homepage: https://github.com/adamalbrecht/docx_replace
         
     | 
| 
      
 47 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 48 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 49 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 50 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 51 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 52 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 53 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 54 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 55 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 56 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 57 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 58 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 59 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 60 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 61 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 62 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 63 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 64 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 65 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 66 
     | 
    
         
            +
            rubygems_version: 1.8.25
         
     | 
| 
      
 67 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 68 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 69 
     | 
    
         
            +
            summary: Find and replace variables inside a Micorsoft Word (.docx) template
         
     | 
| 
      
 70 
     | 
    
         
            +
            test_files: []
         
     |