eml_to_pdf_ext 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +26 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +1156 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/eml_to_pdf.gemspec +31 -0
- data/exe/eml_to_pdf +9 -0
- data/lib/eml_to_pdf.rb +29 -0
- data/lib/eml_to_pdf/configuration.rb +23 -0
- data/lib/eml_to_pdf/converter.rb +14 -0
- data/lib/eml_to_pdf/email.rb +82 -0
- data/lib/eml_to_pdf/empty_part.rb +19 -0
- data/lib/eml_to_pdf/extraction_step.rb +64 -0
- data/lib/eml_to_pdf/extraction_step_list.rb +24 -0
- data/lib/eml_to_pdf/file_email_provider.rb +11 -0
- data/lib/eml_to_pdf/memory_email_provider.rb +11 -0
- data/lib/eml_to_pdf/metadata_context.rb +27 -0
- data/lib/eml_to_pdf/templates/heading.html.erb +31 -0
- data/lib/eml_to_pdf/templates/style.html +56 -0
- data/lib/eml_to_pdf/version.rb +3 -0
- data/lib/eml_to_pdf/wkhtmltopdf.rb +17 -0
- data/tmp/.gitkeep +0 -0
- metadata +167 -0
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.5
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.4.0
|
4
|
+
- 2.3.2
|
5
|
+
- 2.2.2
|
6
|
+
- 2.1.5
|
7
|
+
before_install:
|
8
|
+
- gem install bundler -v 1.10.6
|
9
|
+
- wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
|
10
|
+
- tar -xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
|
11
|
+
- export PATH=$PATH:$PWD/wkhtmltox/bin/
|
12
|
+
addons:
|
13
|
+
code_climate:
|
14
|
+
repo_token: 316f9b6cace768629cd46c6f0196f8f090d6d9f8740ce5802ff0dce48ca95d51
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yves Siegrist
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# EmlToPdf
|
2
|
+
|
3
|
+
Welcome to SwordPL/EmlToPdf fork.
|
4
|
+
A small converter to convert an eml file to a pdf.
|
5
|
+
This gem uses `wkhtmltopdf`. You can get the installer from [here](http://wkhtmltopdf.org/downloads.html)
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/SwordPL/eml_to_pdf.svg?branch=master)](https://travis-ci.org/SwordPL/eml_to_pdf)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/SwordPL/eml_to_pdf/badges/gpa.svg)](https://codeclimate.com/github/SwordPL/eml_to_pdf)
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'eml_to_pdf'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install eml_to_pdf
|
24
|
+
|
25
|
+
## Usage
|
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 do |date|
|
38
|
+
date.strftime("%Y")
|
39
|
+
end
|
40
|
+
config.metadata_visible = false
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
Use the `convert` method to covert an eml to a pdf
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
EmlToPdf.convert("~/Desktop/my_test_email.eml", "~/Desktop/converted_email.pdf")
|
48
|
+
```
|
49
|
+
|
50
|
+
Or you can use the executable
|
51
|
+
|
52
|
+
$ eml_to_pdf input-path output-path
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
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.
|
63
|
+
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "eml_to_pdf"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/eml_to_pdf.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eml_to_pdf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'eml_to_pdf_ext'
|
8
|
+
spec.version = EmlToPdf::VERSION
|
9
|
+
spec.authors = ['Yves Siegrist', 'Hubert Pomorski']
|
10
|
+
spec.email = %w(Elektron1c97@gmail.com hubert.pomorski@sabre.com)
|
11
|
+
|
12
|
+
spec.description = %q{This gem uses wkhtmltopdf to convert an eml to a pdf. (eml -> html -> pdf)}
|
13
|
+
spec.summary = %q{This gem allows you to convert an eml (email) into a pdf.}
|
14
|
+
spec.homepage = 'https://github.com/SwordPL/eml_to_pdf'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.1.5'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
25
|
+
spec.add_development_dependency 'rake', '~> 11.0'
|
26
|
+
spec.add_development_dependency 'minitest', '~> 5.9'
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'filesize', '~> 0.1.1'
|
29
|
+
spec.add_runtime_dependency 'mail', '~> 2.5', '>= 2.5.4'
|
30
|
+
spec.add_runtime_dependency 'nokogiri', '>= 1.5.2'
|
31
|
+
end
|
data/exe/eml_to_pdf
ADDED
data/lib/eml_to_pdf.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "eml_to_pdf/version"
|
2
|
+
require "eml_to_pdf/configuration"
|
3
|
+
require "eml_to_pdf/converter"
|
4
|
+
require "eml_to_pdf/converter"
|
5
|
+
require "eml_to_pdf/email"
|
6
|
+
require "eml_to_pdf/wkhtmltopdf"
|
7
|
+
require "eml_to_pdf/metadata_context"
|
8
|
+
require "eml_to_pdf/extraction_step"
|
9
|
+
require "eml_to_pdf/extraction_step_list"
|
10
|
+
require "eml_to_pdf/file_email_provider"
|
11
|
+
require "eml_to_pdf/memory_email_provider"
|
12
|
+
|
13
|
+
module EmlToPdf
|
14
|
+
def self.convert(input, output)
|
15
|
+
Converter.new(input, output).convert
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
yield configuration if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.configuration
|
23
|
+
@configuration ||= Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.reset_configuration!
|
27
|
+
@configuration = Configuration.new
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module EmlToPdf
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :from_label, :to_label, :cc_label, :date_label, :metadata_visible
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@from_label, @to_label, @cc_label, @date_label= 'From', 'To', 'Cc', 'Date'
|
7
|
+
@date_format = lambda { |date| date.strftime('%Y-%m-%d %H:%M:%S %z') }
|
8
|
+
@metadata_visible = true
|
9
|
+
end
|
10
|
+
|
11
|
+
def date_format(&block)
|
12
|
+
if block_given?
|
13
|
+
@date_format = block
|
14
|
+
else
|
15
|
+
@date_format
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def format_date(date)
|
20
|
+
@date_format.call(date)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module EmlToPdf
|
2
|
+
class Converter
|
3
|
+
def initialize(input_path, output_path)
|
4
|
+
@input_path = input_path
|
5
|
+
@output_path = output_path
|
6
|
+
end
|
7
|
+
|
8
|
+
def convert
|
9
|
+
email = Email.new(@input_path)
|
10
|
+
html = email.to_html
|
11
|
+
Wkhtmltopdf.convert(html, @output_path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "nokogiri"
|
3
|
+
require "erb"
|
4
|
+
|
5
|
+
module EmlToPdf
|
6
|
+
class Email
|
7
|
+
TEMPLATES_PATH = Pathname.new(File.expand_path(__dir__)) + 'templates'
|
8
|
+
|
9
|
+
def initialize(email_provider)
|
10
|
+
@mail = email_provider.email
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_html
|
14
|
+
extraction = ExtractionStep.new(@mail)
|
15
|
+
extraction = extraction.next until extraction.finished?
|
16
|
+
html = extraction.to_html
|
17
|
+
html = resolve_cids_from_attachments(html, @mail.all_parts)
|
18
|
+
html = add_mail_metadata_to_html(@mail, html) if display_metadata?
|
19
|
+
html
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def visible_attachments(mail)
|
24
|
+
mail.attachments.select do |attachment|
|
25
|
+
!attachment.inline?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def resolve_cids_from_attachments(html, attachments)
|
30
|
+
cid_list(attachments).inject(html) do |html_text, key_and_value|
|
31
|
+
k, v = key_and_value
|
32
|
+
html_text.gsub!(k, v)
|
33
|
+
html_text
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def cid_list(attachments)
|
38
|
+
@cid_list ||= attachments.inject({}) do |list, attachment|
|
39
|
+
if attachment.content_id && attachment.content_transfer_encoding == "base64"
|
40
|
+
mime_type = attachment.mime_type
|
41
|
+
decoded = attachment.body.raw_source.strip.gsub(/[\n\t\r]/, "")
|
42
|
+
list[attachment.url] = "data:#{mime_type};base64,#{decoded}"
|
43
|
+
end
|
44
|
+
list
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_mail_metadata_to_html(mail, html)
|
49
|
+
context = MetadataContext.new(from: save_extract_header(mail, :from),
|
50
|
+
to: save_extract_header(mail, :to),
|
51
|
+
cc: save_extract_header(mail, :cc),
|
52
|
+
date: mail.date,
|
53
|
+
subject: mail.subject,
|
54
|
+
attachments: visible_attachments(mail))
|
55
|
+
heading = render_template("heading.html.erb", context.get_binding)
|
56
|
+
style = render_template("style.html")
|
57
|
+
html = "<body></body>" if html.empty?
|
58
|
+
doc = Nokogiri::HTML(html)
|
59
|
+
if doc.at_css("body").first_element_child.nil?
|
60
|
+
doc.at_css("body").add_child(heading)
|
61
|
+
else
|
62
|
+
doc.at_css("body").first_element_child.add_previous_sibling(heading)
|
63
|
+
end
|
64
|
+
doc.at_css("body").first_element_child.add_previous_sibling(style)
|
65
|
+
doc.to_html
|
66
|
+
end
|
67
|
+
|
68
|
+
def render_template(filename, binding = nil)
|
69
|
+
template = File.read(TEMPLATES_PATH + filename)
|
70
|
+
renderer = ERB.new(template)
|
71
|
+
renderer.result(binding)
|
72
|
+
end
|
73
|
+
|
74
|
+
def save_extract_header(mail, header)
|
75
|
+
(mail.header[header] && mail.header[header].decoded) || ""
|
76
|
+
end
|
77
|
+
|
78
|
+
def display_metadata?
|
79
|
+
EmlToPdf.configuration.metadata_visible
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module EmlToPdf
|
2
|
+
class ExtractionStep
|
3
|
+
MIME_TYPES = {
|
4
|
+
plain_text: 'text/plain',
|
5
|
+
html: 'text/html',
|
6
|
+
multipart_alternative: 'multipart/alternative'
|
7
|
+
}
|
8
|
+
|
9
|
+
def initialize(mail_or_part)
|
10
|
+
@mail_or_part = mail_or_part
|
11
|
+
end
|
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
|
+
ExtractionStepList.new(@mail_or_part.parts.map { |part| ExtractionStep.new(part) })
|
19
|
+
else
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def finished?
|
25
|
+
!@mail_or_part.multipart?
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_html
|
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)
|
42
|
+
else
|
43
|
+
""
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def extract_best_part(parts)
|
48
|
+
parts.detect(&:multipart?) ||
|
49
|
+
find_body_with_type(parts, :html) ||
|
50
|
+
find_body_with_type(parts, :plain_text) ||
|
51
|
+
EmptyPart.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_body_with_type(parts, type)
|
55
|
+
parts.detect do |part|
|
56
|
+
part.mime_type == MIME_TYPES[type]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def wrap_text_in_pre_tag(text)
|
61
|
+
"<pre>#{text}</pre>"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|