eml_to_pdf 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/README.md +14 -0
- data/lib/eml_to_pdf.rb +13 -5
- data/lib/eml_to_pdf/configuration.rb +22 -0
- data/lib/eml_to_pdf/email.rb +26 -43
- data/lib/eml_to_pdf/empty_part.rb +19 -0
- data/lib/eml_to_pdf/extraction_step.rb +58 -0
- data/lib/eml_to_pdf/metadata_context.rb +8 -1
- data/lib/eml_to_pdf/templates/heading.html.erb +4 -4
- data/lib/eml_to_pdf/version.rb +1 -1
- data/lib/eml_to_pdf/wkhtmltopdf.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e81efeb010801805fe22e63ff844e4a3f400955c
|
4
|
+
data.tar.gz: 1a02a94a289f960e79827c5f63d927d6d37a2888
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b613139c02acf27408c43fad2114e34db2321ea9fa2b3709923f4dbc610d15f62751bce105a06e046b7d7246487b6e46d59a785fbe0afc84efe47decb1b70e8c
|
7
|
+
data.tar.gz: 43e6e8b27008a6482229fa8077af78bcc245b0987d905c3fb7636b3de4f387308a9a459aa6413f05bd3df2df000580fa26583622a195808dc7f74a558bdc76cc
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/README.md
CHANGED
@@ -24,6 +24,20 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
+
#### Basic Setup
|
28
|
+
|
29
|
+
Use the `configure` method to configure your labels
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
EmlToPdf.configure do |config|
|
33
|
+
config.from_label = "From =>"
|
34
|
+
config.to_label = "To =>"
|
35
|
+
config.cc_label = "Cc =>"
|
36
|
+
config.date_label = "Date =>"
|
37
|
+
config.date_format = "%d.%m.%Y" # see Date#strftime
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
27
41
|
Use the `convert` method to covert an eml to a pdf
|
28
42
|
|
29
43
|
```ruby
|
data/lib/eml_to_pdf.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
|
-
require "cgi"
|
2
|
-
require "mail"
|
3
|
-
require "erb"
|
4
|
-
require "nokogiri"
|
5
|
-
require "filesize"
|
6
1
|
require "eml_to_pdf/version"
|
2
|
+
require "eml_to_pdf/configuration"
|
7
3
|
require "eml_to_pdf/converter"
|
8
4
|
require "eml_to_pdf/converter"
|
9
5
|
require "eml_to_pdf/email"
|
@@ -14,4 +10,16 @@ module EmlToPdf
|
|
14
10
|
def self.convert(input, output)
|
15
11
|
Converter.new(input, output).convert
|
16
12
|
end
|
13
|
+
|
14
|
+
def self.configure
|
15
|
+
yield configuration if block_given?
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset_configuration!
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
17
25
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module EmlToPdf
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :from_label, :to_label, :cc_label, :date_label
|
4
|
+
attr_reader :date_format
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@from_label = "From"
|
8
|
+
@to_label = "To"
|
9
|
+
@cc_label = "Cc"
|
10
|
+
@date_label = "Date"
|
11
|
+
@date_format = "%Y-%m-%d %H:%M:%S %z"
|
12
|
+
end
|
13
|
+
|
14
|
+
def date_format=(value)
|
15
|
+
if value.is_a?(String)
|
16
|
+
@date_format = value
|
17
|
+
else
|
18
|
+
raise ArgumentError, "You can only use a String to format the date. (See Date#strftime)"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/eml_to_pdf/email.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require "pathname"
|
2
|
+
require "nokogiri"
|
3
|
+
require "mail"
|
4
|
+
require "erb"
|
2
5
|
|
3
6
|
module EmlToPdf
|
4
7
|
class Email
|
5
8
|
MIME_TYPES = {
|
6
9
|
plain_text: "text/plain",
|
7
10
|
html: "text/html",
|
8
|
-
|
11
|
+
multipart_alternative: "multipart/alternative"
|
9
12
|
}
|
10
13
|
|
11
14
|
TEMPLATES_PATH = Pathname.new(File.expand_path(__dir__)) + "templates"
|
@@ -22,29 +25,20 @@ module EmlToPdf
|
|
22
25
|
html
|
23
26
|
end
|
24
27
|
|
28
|
+
private
|
25
29
|
def text_parts(mail_or_part)
|
26
|
-
if mail_or_part.multipart?
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
text_parts(part)
|
33
|
-
end
|
30
|
+
if mail_or_part.multipart? && multipart_alternative?(mail_or_part)
|
31
|
+
best_part = extract_best_part(mail_or_part.parts)
|
32
|
+
text_parts(best_part)
|
33
|
+
elsif mail_or_part.multipart?
|
34
|
+
mail_or_part.parts.map do |part|
|
35
|
+
text_parts(part)
|
34
36
|
end
|
35
37
|
else
|
36
|
-
|
37
|
-
wrap_text_in_pre_tag(mail_or_part.decoded)
|
38
|
-
elsif mail_or_part.mime_type == MIME_TYPES[:html] && !mail_or_part.attachment?
|
39
|
-
mail_or_part.decoded
|
40
|
-
else
|
41
|
-
""
|
42
|
-
end
|
43
|
-
[best_part]
|
38
|
+
[text_body(mail_or_part)]
|
44
39
|
end.flatten
|
45
40
|
end
|
46
41
|
|
47
|
-
private
|
48
42
|
def visible_attachments(mail)
|
49
43
|
mail.attachments.select do |attachment|
|
50
44
|
!attachment.inline?
|
@@ -52,10 +46,10 @@ module EmlToPdf
|
|
52
46
|
end
|
53
47
|
|
54
48
|
def resolve_cids_from_attachments(html, attachments)
|
55
|
-
cid_list(attachments).inject(html) do |
|
49
|
+
cid_list(attachments).inject(html) do |html_text, key_and_value|
|
56
50
|
k, v = key_and_value
|
57
|
-
|
58
|
-
|
51
|
+
html_text.gsub!(k, v)
|
52
|
+
html_text
|
59
53
|
end
|
60
54
|
end
|
61
55
|
|
@@ -70,19 +64,7 @@ module EmlToPdf
|
|
70
64
|
end
|
71
65
|
end
|
72
66
|
|
73
|
-
def
|
74
|
-
if multipart_alternative?(mail_or_part)
|
75
|
-
[extract_best_representation_from_parts(mail_or_part.parts)]
|
76
|
-
elsif multipart_mixed?(mail_or_part)
|
77
|
-
mail_or_part.parts.map do |part|
|
78
|
-
html_body(part)
|
79
|
-
end
|
80
|
-
else
|
81
|
-
[extract_best_representation_from_parts(Array(mail_or_part))]
|
82
|
-
end.flatten
|
83
|
-
end
|
84
|
-
|
85
|
-
def extract_best_representation_from_parts(parts)
|
67
|
+
def extract_best_part(parts)
|
86
68
|
if multipart_part = parts.detect(&:multipart?)
|
87
69
|
multipart_part
|
88
70
|
elsif html_part = find_body_with_type(parts, :html)
|
@@ -100,17 +82,18 @@ module EmlToPdf
|
|
100
82
|
end
|
101
83
|
end
|
102
84
|
|
103
|
-
def multipart_mixed?(part)
|
104
|
-
part.mime_type == MIME_TYPES[:multipart_mixed]
|
105
|
-
end
|
106
|
-
|
107
85
|
def multipart_alternative?(part)
|
108
|
-
part.mime_type == MIME_TYPES[:multipart_alternative]
|
86
|
+
part.mime_type == MIME_TYPES[:multipart_alternative]
|
109
87
|
end
|
110
88
|
|
111
|
-
|
112
|
-
|
113
|
-
|
89
|
+
def text_body(mail_or_part)
|
90
|
+
if mail_or_part.mime_type == MIME_TYPES[:html] && !mail_or_part.attachment?
|
91
|
+
mail_or_part.decoded
|
92
|
+
elsif mail_or_part.mime_type == MIME_TYPES[:plain_text] && !mail_or_part.attachment?
|
93
|
+
wrap_text_in_pre_tag(mail_or_part.decoded)
|
94
|
+
else
|
95
|
+
""
|
96
|
+
end
|
114
97
|
end
|
115
98
|
|
116
99
|
def wrap_text_in_pre_tag(text)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module EmlToPdf
|
2
|
+
|
3
|
+
# TODO: in email#to_html remove recursive function with this class
|
4
|
+
# for a better control and to be able to make more unit tests.
|
5
|
+
class ExtractionStep
|
6
|
+
def initialize(mail_or_part)
|
7
|
+
@mail_or_part = mail_or_part
|
8
|
+
end
|
9
|
+
|
10
|
+
# extraction = ExtractionStep.new(@mail)
|
11
|
+
# extraction = extraction.next until extraction.finished?
|
12
|
+
|
13
|
+
def next
|
14
|
+
if multipart_alternative?(@mail_or_part)
|
15
|
+
best_part = extract_best_part(@mail_or_part.parts)
|
16
|
+
ExtractionStep.new(best_part)
|
17
|
+
elsif @mail_or_part.multipart?
|
18
|
+
|
19
|
+
else
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def finished?
|
25
|
+
@mail_or_part.parts.none?(&:multipart?)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_html
|
29
|
+
if html_part = @mail_or_part.html_part
|
30
|
+
html_part
|
31
|
+
elsif text_part = @mail_or_part.text_part
|
32
|
+
wrap_text_in_pre_tag(text_part)
|
33
|
+
else
|
34
|
+
""
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def extract_best_part(parts)
|
39
|
+
if multipart_part = parts.detect(&:multipart?)
|
40
|
+
multipart_part
|
41
|
+
elsif html_part = find_body_with_type(parts, :html)
|
42
|
+
html_part
|
43
|
+
elsif text_part = find_body_with_type(parts, :plain_text)
|
44
|
+
text_part
|
45
|
+
else
|
46
|
+
"can't find useable part"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def multipart_alternative?(part)
|
51
|
+
part.mime_type == MIME_TYPES[:multipart_alternative]
|
52
|
+
end
|
53
|
+
|
54
|
+
def wrap_text_in_pre_tag(text)
|
55
|
+
"<pre>#{text}</pre>"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require "filesize"
|
2
|
+
require "cgi"
|
3
|
+
|
1
4
|
module EmlToPdf
|
2
5
|
class MetadataContext
|
3
6
|
def initialize(metadata)
|
@@ -6,12 +9,16 @@ module EmlToPdf
|
|
6
9
|
end
|
7
10
|
end
|
8
11
|
|
12
|
+
def config
|
13
|
+
EmlToPdf.configuration
|
14
|
+
end
|
15
|
+
|
9
16
|
def format_attachment_size(attachment)
|
10
17
|
Filesize.from("#{attachment.body.decoded.size} B").pretty
|
11
18
|
end
|
12
19
|
|
13
20
|
def format_date(date)
|
14
|
-
date.strftime(
|
21
|
+
date.strftime(config.date_format)
|
15
22
|
end
|
16
23
|
|
17
24
|
def html_escape(str)
|
@@ -2,19 +2,19 @@
|
|
2
2
|
<div class="email-metadata">
|
3
3
|
<table>
|
4
4
|
<tr>
|
5
|
-
<td><strong
|
5
|
+
<td><strong><%= config.from_label %></strong></td>
|
6
6
|
<td><%= html_escape(@from) %></td>
|
7
7
|
</tr>
|
8
8
|
<tr>
|
9
|
-
<td><strong
|
9
|
+
<td><strong><%= config.to_label %></strong></td>
|
10
10
|
<td><%= html_escape(@to) %></td>
|
11
11
|
</tr>
|
12
12
|
<tr>
|
13
|
-
<td><strong
|
13
|
+
<td><strong><%= config.cc_label %></strong></td>
|
14
14
|
<td><%= html_escape(@cc) %></td>
|
15
15
|
</tr>
|
16
16
|
<tr>
|
17
|
-
<td><strong
|
17
|
+
<td><strong><%= config.date_label %></strong></td>
|
18
18
|
<td><%= format_date(@date) if @date %></td>
|
19
19
|
</tr>
|
20
20
|
</table>
|
data/lib/eml_to_pdf/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module EmlToPdf
|
2
2
|
class Wkhtmltopdf
|
3
3
|
def self.convert(input, output_path)
|
4
|
-
IO.popen("wkhtmltopdf --encoding utf-8 --footer-center [page] - #{output_path} 2>&1", "r+") do |pipe|
|
4
|
+
IO.popen("wkhtmltopdf --encoding utf-8 --footer-center [page] --footer-spacing 2.5 - #{output_path} 2>&1", "r+") do |pipe|
|
5
5
|
pipe.puts(input)
|
6
6
|
pipe.close_write
|
7
7
|
pipe.readlines # TODO save output
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eml_to_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yves Siegrist
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".gitignore"
|
119
|
+
- ".ruby-version"
|
119
120
|
- ".travis.yml"
|
120
121
|
- CODE_OF_CONDUCT.md
|
121
122
|
- Gemfile
|
@@ -127,8 +128,11 @@ files:
|
|
127
128
|
- eml_to_pdf.gemspec
|
128
129
|
- exe/eml_to_pdf
|
129
130
|
- lib/eml_to_pdf.rb
|
131
|
+
- lib/eml_to_pdf/configuration.rb
|
130
132
|
- lib/eml_to_pdf/converter.rb
|
131
133
|
- lib/eml_to_pdf/email.rb
|
134
|
+
- lib/eml_to_pdf/empty_part.rb
|
135
|
+
- lib/eml_to_pdf/extraction_step.rb
|
132
136
|
- lib/eml_to_pdf/metadata_context.rb
|
133
137
|
- lib/eml_to_pdf/templates/heading.html.erb
|
134
138
|
- lib/eml_to_pdf/templates/style.html
|