htmltoword 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fa3068c3f8e782d969f024274e6c7fe0a8a4c71
4
- data.tar.gz: 97813785c2a375ed483e2e0514b78c6a7930935d
3
+ metadata.gz: 4caa8437b60de362de112d27ca2d3bd127e018b9
4
+ data.tar.gz: bff1647e7f309e5195e74aeee693e76de6587102
5
5
  SHA512:
6
- metadata.gz: 20a097c3b3dda40aaef5f946b7404a9fb1b3affbc6a40065ddb1bf80f40e33262b9e8c8421839746cfadb35e8e3a4ddec0515b340ff085ec31107c91c128436c
7
- data.tar.gz: cf9ee7063de6dc3c0b0cd50c46a4169e22f3d46f9425dc40b47d0978305d8e970437e3ee215aecfddd949a501c49263420b6c1248fb8fa2dcbe35363e0db7042
6
+ metadata.gz: 5cffd3d1832113c2227e06098188baa5b22c9514de05c77b7d18c4f2a58ce52937efe4f5144c04eb1fbeaec716fca1300a96874a429646060d78179b2b97b2a0
7
+ data.tar.gz: c5ac4f35a6e7a032f57df40bf4e1b4ea7e3fdfa7de8543179a0ca10d6323819e2c3163af8518bf3f5fcde567a4d7e282f5e5a1550a75b2f59f0afbfce2f9b8d1
data/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # Ruby Html to word Gem [![Code Climate](https://codeclimate.com/github/nickfrandsen/htmltoword.png)](https://codeclimate.com/github/nickfrandsen/htmltoword) [![Build Status](https://travis-ci.org/nickfrandsen/htmltoword.png)](https://travis-ci.org/nickfrandsen/htmltoword)
2
2
 
3
- ## OBS: This repository is no longer being maintained. Please take a look at https://github.com/karnov/htmltoword
4
-
5
3
  This simple gem allows you to create MS Word docx documents from simple html documents. This makes it easy to create dynamic reports and forms that can be downloaded by your users as simple MS Word docx files.
6
4
 
7
5
  Add this line to your application's Gemfile:
@@ -25,7 +23,7 @@ Using the default word file as template
25
23
  require 'htmltoword'
26
24
 
27
25
  my_html = '<html><head></head><body><p>Hello</p></body></html>'
28
- file = Htmltoword::Document.create my_html, file_name
26
+ document = Htmltoword::Document.create(my_html)
29
27
  ```
30
28
 
31
29
  Using your custom word file as a template, where you can setup your own style for normal text, h1,h2, etc.
@@ -36,7 +34,7 @@ require 'htmltoword'
36
34
  Htmltoword.config.custom_templates_path = 'some_path'
37
35
 
38
36
  my_html = '<html><head></head><body><p>Hello</p></body></html>'
39
- file = Htmltoword::Document.create my_html, file_name, word_template_file_name
37
+ document = Htmltoword::Document.create(my_html, word_template_file_name)
40
38
  ```
41
39
 
42
40
  ### With Rails
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
3
  task :default => :spec
4
- RSpec::Core::RakeTask.new
4
+ RSpec::Core::RakeTask.new
data/lib/htmltoword.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  # encoding: UTF-8
2
- require 'action_controller'
3
- require 'action_view'
2
+ # require 'action_controller'
3
+ # require 'action_view'
4
4
  require 'nokogiri'
5
5
  require 'zip'
6
- require 'htmltoword/configuration'
6
+ require_relative 'htmltoword/configuration'
7
7
 
8
8
  module Htmltoword
9
9
  class << self
@@ -20,7 +20,7 @@ module Htmltoword
20
20
  end
21
21
 
22
22
 
23
- require 'htmltoword/version'
24
- require 'htmltoword/htmltoword_helper'
25
- require 'htmltoword/document'
26
- require 'htmltoword/action_controller'
23
+ require_relative 'htmltoword/version'
24
+ require_relative 'htmltoword/htmltoword_helper'
25
+ require_relative 'htmltoword/document'
26
+ require_relative 'htmltoword/action_controller'
@@ -1,15 +1,16 @@
1
+ require 'rack/test'
1
2
  require 'action_controller'
3
+
2
4
  unless defined? Mime::DOCX
3
- Mime::Type.register "application/vnd.openxmlformats-officedocument.wordprocessingml.document", :docx
5
+ Mime::Type.register 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', :docx
4
6
  end
5
7
 
6
8
  ActionController::Renderers.add :docx do |filename, options|
7
- unless formats.include?(:docx) || Rails.version < '3.2'
8
- formats[0] = :docx
9
- end
9
+ formats[0] = :docx unless formats.include?(:docx) || Rails.version < '3.2'
10
10
 
11
+ # This is ugly and should be solved with regular file utils
11
12
  if options[:template] == action_name
12
- if filename =~ /^([^\/]+)\/(.+)$/
13
+ if filename =~ %r{^([^\/]+)/(.+)$}
13
14
  options[:prefixes] ||= []
14
15
  options[:prefixes].unshift $1
15
16
  options[:template] = $2
@@ -19,34 +20,34 @@ ActionController::Renderers.add :docx do |filename, options|
19
20
  end
20
21
 
21
22
  # disposition / filename
22
- disposition = options.delete(:disposition) || 'attachment'
23
+ disposition = options.delete(:disposition) || 'attachment'
23
24
  if file_name = options.delete(:filename)
24
- file_name += ".docx" unless file_name =~ /\.docx$/
25
+ file_name += '.docx' unless file_name =~ /\.docx$/
25
26
  else
26
- file_name = "#{filename.gsub(/^.*\//,'')}.docx"
27
+ file_name = "#{filename.gsub(/^.*\//, '')}.docx"
27
28
  end
28
29
 
29
30
  # other properties
30
31
  word_template = options.delete(:word_template) || nil
32
+ extras = options.delete(:extras) || false
31
33
  # content will come from property content unless not specified
32
34
  # then it will look for a template.
33
35
  content = options.delete(:content) || render_to_string(options)
34
36
 
35
- doc = Htmltoword::Document.create content, file_name, word_template
36
- send_data File.read(doc.path), :filename => file_name, :type => Mime::DOCX, :disposition => disposition
37
+ document = Htmltoword::Document.create(content, word_template, extras)
38
+ send_data document, filename: file_name, type: Mime::DOCX, disposition: disposition
37
39
  end
38
40
 
39
- # For respond_with default
40
- begin
41
- ActionController::Responder
42
- rescue LoadError
43
- else
44
- class ActionController::Responder
45
- def to_docx
46
- if @default_response
47
- @default_response.call(options)
48
- else
49
- controller.render({:docx => controller.action_name}.merge(options))
41
+ if defined? ActionController::Responder
42
+ module ActionController
43
+ # For respond_with default
44
+ class Responder
45
+ def to_docx
46
+ if @default_response
47
+ @default_response.call(options)
48
+ else
49
+ controller.render({ docx: controller.action_name }.merge(options))
50
+ end
50
51
  end
51
52
  end
52
53
  end
@@ -3,20 +3,19 @@ module Htmltoword
3
3
  class << self
4
4
  include HtmltowordHelper
5
5
 
6
- def create content, file_name, template_name=nil
7
- file_name += extension unless file_name =~ /\.docx$/
8
- template_name += extension if template_name && !(template_name =~ /\.docx$/)
9
- word_file = new(template_file(template_name), file_name)
10
- word_file.replace_file content
11
- word_file.save
6
+ def create(content, template_name = nil, extras = false)
7
+ template_name += extension if template_name && !template_name.end_with?(extension)
8
+ document = new(template_file(template_name))
9
+ document.replace_file(content, Document.doc_xml_file, extras)
10
+ document.generate
12
11
  end
13
12
 
14
- def create_with_content template, file_name, content, set=nil
15
- template += extension unless template =~ /\.docx$/
16
- word_file = new(template_file(template), file_name)
13
+ def create_with_content(template, content, set=nil, extras = false)
14
+ template += extension unless template.end_with?(extension)
17
15
  content = replace_values(content, set) if set
18
- word_file.replace_file content
19
- word_file.save
16
+ document = new(template_file(template))
17
+ document.replace_file(content, Document.doc_xml_file, extras)
18
+ document.generate
20
19
  end
21
20
 
22
21
  def extension
@@ -27,54 +26,43 @@ module Htmltoword
27
26
  'word/document.xml'
28
27
  end
29
28
 
30
- def default_xslt_template
31
- File.join(Htmltoword.config.default_xslt_path, 'html_to_wordml.xslt')
29
+ def xslt_template(extras = false)
30
+ File.join(Htmltoword.config.default_xslt_path, (extras ? 'htmltoword.xslt' : 'base.xslt'))
32
31
  end
33
32
  end
34
33
 
35
- def initialize(template_path, file_name)
36
- @file_name = file_name
34
+ def initialize(template_path)
37
35
  @replaceable_files = {}
38
36
  @template_path = template_path
39
37
  end
40
38
 
41
- def file_name
42
- @file_name
43
- end
44
-
45
39
  #
46
- # It creates missing folders if needed, creates a new zip/word file on the
47
- # specified location, copies all the files from the template word document
48
- # and replace the content of the ones to be replaced.
49
- # It will create a tempfile and return it. The rails app using the gem
50
- # should decide what to do with it.
40
+ # Generate a string representing the contents of a docx file.
51
41
  #
52
- #
53
- def save
54
- Tempfile.open([file_name, Document.extension], type: 'application/zip') do |output_file|
55
- Zip::File.open(@template_path) do |template_zip|
56
- Zip::OutputStream.open(output_file.path) do |out|
57
- template_zip.each do |entry|
58
- out.put_next_entry entry.name
59
- if @replaceable_files[entry.name]
60
- out.write(@replaceable_files[entry.name])
61
- else
62
- out.write(template_zip.read(entry.name))
63
- end
42
+ def generate
43
+ Zip::File.open(@template_path) do |template_zip|
44
+ buffer = Zip::OutputStream.write_buffer do |out|
45
+ template_zip.each do |entry|
46
+ out.put_next_entry entry.name
47
+ if @replaceable_files[entry.name]
48
+ source = entry.get_input_stream.read.sub(/(<w:body>)(.*?)(<w:sectPr)/, "\\1#{@replaceable_files[entry.name]}\\3")
49
+ out.write(source)
50
+ else
51
+ out.write(template_zip.read(entry.name))
64
52
  end
65
53
  end
66
54
  end
67
- output_file
55
+ buffer.string
68
56
  end
69
57
  end
70
58
 
71
- def replace_file html, file_name=Document.doc_xml_file
59
+ def replace_file(html, file_name = Document.doc_xml_file, extras = false)
72
60
  html = html.presence || '<body></body>'
73
- source = Nokogiri::HTML(html.gsub(/>\s+</, "><"))
74
- xslt = Nokogiri::XSLT( File.read(Document.default_xslt_template) )
75
- source = xslt.transform( source ) unless (source/"/html").blank?
76
- @replaceable_files[file_name] = source.to_s
61
+ source = Nokogiri::HTML(html.gsub(/>\s+</, '><'))
62
+ template = Document.xslt_template(extras)
63
+ xslt = Nokogiri::XSLT(File.open(template))
64
+ source = xslt.apply_to(source).gsub(/\s*xmlns:(\w+)="(.*?)\s*"/,'')
65
+ @replaceable_files[file_name] = source
77
66
  end
78
-
79
67
  end
80
68
  end
@@ -1,3 +1,3 @@
1
1
  module Htmltoword
2
- VERSION = "0.2.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmltoword
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Frandsen, Cristina Matonte
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '1.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: methadone
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rmultimarkdown
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description: Convert html to word docx document.
98
126
  email:
99
127
  - nick.rowe.frandsen@gmail.com, anitsirc1@gmail.com
@@ -109,7 +137,7 @@ files:
109
137
  - lib/htmltoword/document.rb
110
138
  - lib/htmltoword/htmltoword_helper.rb
111
139
  - lib/htmltoword/version.rb
112
- homepage: https://github.com/karnov/htmltoword
140
+ homepage: http://github.com/karnov/htmltoword
113
141
  licenses:
114
142
  - MIT
115
143
  metadata: {}