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 +4 -4
- data/README.md +2 -4
- data/Rakefile +1 -1
- data/lib/htmltoword.rb +7 -7
- data/lib/htmltoword/action_controller.rb +22 -21
- data/lib/htmltoword/document.rb +31 -43
- data/lib/htmltoword/version.rb +1 -1
- metadata +32 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4caa8437b60de362de112d27ca2d3bd127e018b9
|
4
|
+
data.tar.gz: bff1647e7f309e5195e74aeee693e76de6587102
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
37
|
+
document = Htmltoword::Document.create(my_html, word_template_file_name)
|
40
38
|
```
|
41
39
|
|
42
40
|
### With Rails
|
data/Rakefile
CHANGED
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
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
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
|
23
|
+
disposition = options.delete(:disposition) || 'attachment'
|
23
24
|
if file_name = options.delete(:filename)
|
24
|
-
file_name +=
|
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
|
-
|
36
|
-
send_data
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
data/lib/htmltoword/document.rb
CHANGED
@@ -3,20 +3,19 @@ module Htmltoword
|
|
3
3
|
class << self
|
4
4
|
include HtmltowordHelper
|
5
5
|
|
6
|
-
def create
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
15
|
-
template += extension unless template
|
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
|
-
|
19
|
-
|
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
|
31
|
-
File.join(Htmltoword.config.default_xslt_path, '
|
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
|
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
|
-
#
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
55
|
+
buffer.string
|
68
56
|
end
|
69
57
|
end
|
70
58
|
|
71
|
-
def replace_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
|
-
|
75
|
-
|
76
|
-
|
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
|
data/lib/htmltoword/version.rb
CHANGED
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.
|
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:
|
140
|
+
homepage: http://github.com/karnov/htmltoword
|
113
141
|
licenses:
|
114
142
|
- MIT
|
115
143
|
metadata: {}
|