eml_to_pdf 0.4.0 → 0.4.1

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: 8066ee85637834e40eabff881ca5dc0c507dfeec
4
- data.tar.gz: 482c532373eebd9c8ac1dd53c8855d05c0f03e4d
3
+ metadata.gz: 3937b46f020a4ba5bbac39fdfd2c80beaf47f92b
4
+ data.tar.gz: 58105137f1f5b3de7afd8e7560c141d67f230f36
5
5
  SHA512:
6
- metadata.gz: a966380147ebf6245004730e86b042738346d5b426ba839fe96613bb575a090887e108b6fee5149a7991a1edd167a798f88e8d050678096e85c14646d3ad2776
7
- data.tar.gz: 4955d0bc20a0ad90c44fa2b00a70814cb66dd316f7e765fef4e2d4efd57337c83d72a8a92ea6420cced9a6e90a2e972a46d987004f3871f01566cf87b877a139
6
+ metadata.gz: 933861a07fe0aa3e26dd57f70e3f1f1a1e36b9f58260fd6890b62d90ac2a5873d6211131195f3aa12da86bbc035a8f60809efd42cc2643a1c0dbeedb2503e2e9
7
+ data.tar.gz: de3fdd4b821015d8d13dc2da849501ae0d5209d2fcfb959a15cc20a92167f88afb8eab715f399f0c04fcef4c3da731d7e861f5ddbaf3c242976f7052a57f8406
data/.codeclimate.yml ADDED
@@ -0,0 +1,5 @@
1
+ languages:
2
+ Ruby: true
3
+ exclude_paths:
4
+ - "lib/eml_to_pdf/extraction_step.rb"
5
+ - "test/"
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.4.1
data/.travis.yml CHANGED
@@ -1,8 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
+ - 2.4.1
4
5
  before_install:
5
- - gem install bundler -v 1.10.6
6
+ - gem install bundler -v 1.14.6
6
7
  - wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
7
8
  - tar -xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
8
9
  - export PATH=$PATH:$PWD/wkhtmltox/bin/
data/README.md CHANGED
@@ -4,7 +4,8 @@ Welcome to EmlToPdf.
4
4
  A small converter to convert an eml file to a pdf.
5
5
  This gem uses `wkhtmltopdf`. You can get the installer from [here](http://wkhtmltopdf.org/downloads.html)
6
6
 
7
- [![Build Status](https://travis-ci.org/Elektron1c97/eml_to_pdf.svg?branch=master)](https://travis-ci.org/Elektron1c97/eml_to_pdf)
7
+ [![Build Status](https://travis-ci.org/siegy22/eml_to_pdf.svg?branch=master)](https://travis-ci.org/siegy22/eml_to_pdf)
8
+ [![Code Climate](https://codeclimate.com/github/siegy22/eml_to_pdf/badges/gpa.svg)](https://codeclimate.com/github/siegy22/eml_to_pdf)
8
9
 
9
10
  ## Installation
10
11
 
@@ -58,7 +59,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
58
59
 
59
60
  ## Contributing
60
61
 
61
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/eml_to_pdf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siegy22/eml_to_pdf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
62
63
 
63
64
 
64
65
  ## License
data/eml_to_pdf.gemspec CHANGED
@@ -22,10 +22,10 @@ Gem::Specification.new do |spec|
22
22
  spec.required_ruby_version = ">= 2.2.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.10"
25
- spec.add_development_dependency "rake", "~> 11.0"
25
+ spec.add_development_dependency "rake", "~> 12.0"
26
26
  spec.add_development_dependency 'minitest', '~> 5.9'
27
27
 
28
28
  spec.add_runtime_dependency 'filesize', '~> 0.1.1'
29
29
  spec.add_runtime_dependency 'mail', '~> 2.5', '>= 2.5.4'
30
- spec.add_runtime_dependency 'nokogiri', '~> 1.6', '>= 1.6.7.2'
30
+ spec.add_runtime_dependency 'nokogiri', '~> 1.7'
31
31
  end
data/exe/eml_to_pdf CHANGED
@@ -1,13 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- # TODO: use this to convert whole folders
3
- # Dir["test/fixtures/emails/*.eml"].each do |test_file|
4
- # puts "> converting #{test_file}"
5
- # begin
6
- # EmlToPdf.convert(test_file, "#{test_file[0..-5]}_output.pdf")
7
- # rescue NoMethodError => e
8
- # puts e.message
9
- # end
10
- # end
11
2
 
12
3
  if ARGV[0] && ARGV[1]
13
4
  require "bundler/setup"
data/lib/eml_to_pdf.rb CHANGED
@@ -5,6 +5,8 @@ require "eml_to_pdf/converter"
5
5
  require "eml_to_pdf/email"
6
6
  require "eml_to_pdf/wkhtmltopdf"
7
7
  require "eml_to_pdf/metadata_context"
8
+ require "eml_to_pdf/extraction_step"
9
+ require "eml_to_pdf/extraction_step_list"
8
10
 
9
11
  module EmlToPdf
10
12
  def self.convert(input, output)
@@ -5,12 +5,6 @@ require "erb"
5
5
 
6
6
  module EmlToPdf
7
7
  class Email
8
- MIME_TYPES = {
9
- plain_text: "text/plain",
10
- html: "text/html",
11
- multipart_alternative: "multipart/alternative"
12
- }
13
-
14
8
  TEMPLATES_PATH = Pathname.new(File.expand_path(__dir__)) + "templates"
15
9
 
16
10
  def initialize(input_path)
@@ -19,26 +13,15 @@ module EmlToPdf
19
13
  end
20
14
 
21
15
  def to_html
22
- html = text_parts(@mail).join
16
+ extraction = ExtractionStep.new(@mail)
17
+ extraction = extraction.next until extraction.finished?
18
+ html = extraction.to_html
23
19
  html = resolve_cids_from_attachments(html, @mail.all_parts)
24
20
  html = add_mail_metadata_to_html(@mail, html)
25
21
  html
26
22
  end
27
23
 
28
24
  private
29
- def text_parts(mail_or_part)
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)
36
- end
37
- else
38
- [text_body(mail_or_part)]
39
- end.flatten
40
- end
41
-
42
25
  def visible_attachments(mail)
43
26
  mail.attachments.select do |attachment|
44
27
  !attachment.inline?
@@ -64,42 +47,6 @@ module EmlToPdf
64
47
  end
65
48
  end
66
49
 
67
- def extract_best_part(parts)
68
- if multipart_part = parts.detect(&:multipart?)
69
- multipart_part
70
- elsif html_part = find_body_with_type(parts, :html)
71
- html_part
72
- elsif text_part = find_body_with_type(parts, :plain_text)
73
- text_part
74
- else
75
- "can't find useable part"
76
- end
77
- end
78
-
79
- def find_body_with_type(parts, type)
80
- parts.detect do |part|
81
- part.mime_type == MIME_TYPES[type]
82
- end
83
- end
84
-
85
- def multipart_alternative?(part)
86
- part.mime_type == MIME_TYPES[:multipart_alternative]
87
- end
88
-
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
97
- end
98
-
99
- def wrap_text_in_pre_tag(text)
100
- "<pre>#{text}</pre>"
101
- end
102
-
103
50
  def add_mail_metadata_to_html(mail, html)
104
51
  context = MetadataContext.new(from: save_extract_header(mail, :from),
105
52
  to: save_extract_header(mail, :to),
@@ -123,11 +70,7 @@ module EmlToPdf
123
70
  end
124
71
 
125
72
  def save_extract_header(mail, header)
126
- if header = mail.header[header]
127
- header.decoded
128
- else
129
- ""
130
- end
73
+ (mail.header[header] && mail.header[header].decoded) || ""
131
74
  end
132
75
  end
133
76
  end
@@ -1,54 +1,60 @@
1
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
2
  class ExtractionStep
3
+ MIME_TYPES = {
4
+ plain_text: "text/plain",
5
+ html: "text/html",
6
+ multipart_alternative: "multipart/alternative"
7
+ }
8
+
6
9
  def initialize(mail_or_part)
7
10
  @mail_or_part = mail_or_part
8
11
  end
9
12
 
10
- # extraction = ExtractionStep.new(@mail)
11
- # extraction = extraction.next until extraction.finished?
12
-
13
13
  def next
14
14
  if multipart_alternative?(@mail_or_part)
15
15
  best_part = extract_best_part(@mail_or_part.parts)
16
16
  ExtractionStep.new(best_part)
17
17
  elsif @mail_or_part.multipart?
18
-
18
+ ExtractionStepList.new(@mail_or_part.parts.map { |part| ExtractionStep.new(part) })
19
19
  else
20
-
20
+ self
21
21
  end
22
22
  end
23
23
 
24
24
  def finished?
25
- @mail_or_part.parts.none?(&:multipart?)
25
+ !@mail_or_part.multipart?
26
26
  end
27
27
 
28
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)
29
+ text_body(@mail_or_part)
30
+ end
31
+
32
+ private
33
+ def multipart_alternative?(part)
34
+ part.mime_type == MIME_TYPES[:multipart_alternative]
35
+ end
36
+
37
+ def text_body(mail_or_part)
38
+ if mail_or_part.mime_type == MIME_TYPES[:html] && !mail_or_part.attachment?
39
+ mail_or_part.decoded
40
+ elsif mail_or_part.mime_type == MIME_TYPES[:plain_text] && !mail_or_part.attachment?
41
+ wrap_text_in_pre_tag(mail_or_part.decoded)
33
42
  else
34
43
  ""
35
44
  end
36
45
  end
37
46
 
38
47
  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
+ parts.detect(&:multipart?) ||
49
+ find_body_with_type(parts, :html) ||
50
+ find_body_with_type(parts, :plain_text) ||
51
+ EmptyPart.new
48
52
  end
49
53
 
50
- def multipart_alternative?(part)
51
- part.mime_type == MIME_TYPES[:multipart_alternative]
54
+ def find_body_with_type(parts, type)
55
+ parts.detect do |part|
56
+ part.mime_type == MIME_TYPES[type]
57
+ end
52
58
  end
53
59
 
54
60
  def wrap_text_in_pre_tag(text)
@@ -0,0 +1,24 @@
1
+ module EmlToPdf
2
+ class ExtractionStepList
3
+ def initialize(steps)
4
+ @steps = steps
5
+ end
6
+
7
+ def next
8
+ self.class.new(@steps.map(&:next))
9
+ end
10
+
11
+ def finished?
12
+ @steps.all?(&:finished?)
13
+ end
14
+
15
+ def to_html
16
+ @steps.flatten.map(&:to_html).join
17
+ end
18
+
19
+ private
20
+ def multipart_alternative?(part)
21
+ part.mime_type == MIME_TYPES[:multipart_alternative]
22
+ end
23
+ end
24
+ end
@@ -1,14 +1,9 @@
1
1
  require "filesize"
2
2
  require "cgi"
3
+ require "ostruct"
3
4
 
4
5
  module EmlToPdf
5
- class MetadataContext
6
- def initialize(metadata)
7
- metadata.each do |k, v|
8
- eval("@#{k}=v")
9
- end
10
- end
11
-
6
+ class MetadataContext < OpenStruct
12
7
  def config
13
8
  EmlToPdf.configuration
14
9
  end
@@ -3,25 +3,25 @@
3
3
  <table>
4
4
  <tr>
5
5
  <td><strong><%= config.from_label %></strong></td>
6
- <td><%= html_escape(@from) %></td>
6
+ <td><%= html_escape(from) %></td>
7
7
  </tr>
8
8
  <tr>
9
9
  <td><strong><%= config.to_label %></strong></td>
10
- <td><%= html_escape(@to) %></td>
10
+ <td><%= html_escape(to) %></td>
11
11
  </tr>
12
12
  <tr>
13
13
  <td><strong><%= config.cc_label %></strong></td>
14
- <td><%= html_escape(@cc) %></td>
14
+ <td><%= html_escape(cc) %></td>
15
15
  </tr>
16
16
  <tr>
17
17
  <td><strong><%= config.date_label %></strong></td>
18
- <td><%= format_date(@date) if @date %></td>
18
+ <td><%= format_date(date) if date %></td>
19
19
  </tr>
20
20
  </table>
21
- <h3><%= @subject %></h3>
21
+ <h3><%= subject %></h3>
22
22
  <hr>
23
23
  <ul class="email-attachments">
24
- <% @attachments.each do |att| %>
24
+ <% attachments.each do |att| %>
25
25
  <li class="attachement">
26
26
  <span class="filename"><%= att.filename %></span>
27
27
  <span class="file-size"><%= format_attachment_size(att) %></span>
@@ -1,3 +1,3 @@
1
1
  module EmlToPdf
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
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.0
4
+ version: 0.4.1
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-20 00:00:00.000000000 Z
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '11.0'
33
+ version: '12.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '11.0'
40
+ version: '12.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -92,20 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '1.6'
96
- - - ">="
97
- - !ruby/object:Gem::Version
98
- version: 1.6.7.2
95
+ version: '1.7'
99
96
  type: :runtime
100
97
  prerelease: false
101
98
  version_requirements: !ruby/object:Gem::Requirement
102
99
  requirements:
103
100
  - - "~>"
104
101
  - !ruby/object:Gem::Version
105
- version: '1.6'
106
- - - ">="
107
- - !ruby/object:Gem::Version
108
- version: 1.6.7.2
102
+ version: '1.7'
109
103
  description: This gem uses wkhtmltopdf to convert an eml to a pdf. (eml -> html ->
110
104
  pdf)
111
105
  email:
@@ -115,6 +109,7 @@ executables:
115
109
  extensions: []
116
110
  extra_rdoc_files: []
117
111
  files:
112
+ - ".codeclimate.yml"
118
113
  - ".gitignore"
119
114
  - ".ruby-version"
120
115
  - ".travis.yml"
@@ -133,6 +128,7 @@ files:
133
128
  - lib/eml_to_pdf/email.rb
134
129
  - lib/eml_to_pdf/empty_part.rb
135
130
  - lib/eml_to_pdf/extraction_step.rb
131
+ - lib/eml_to_pdf/extraction_step_list.rb
136
132
  - lib/eml_to_pdf/metadata_context.rb
137
133
  - lib/eml_to_pdf/templates/heading.html.erb
138
134
  - lib/eml_to_pdf/templates/style.html
@@ -159,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
155
  version: '0'
160
156
  requirements: []
161
157
  rubyforge_project:
162
- rubygems_version: 2.4.5
158
+ rubygems_version: 2.6.11
163
159
  signing_key:
164
160
  specification_version: 4
165
161
  summary: This gem allows you to convert an eml (email) into a pdf.