mjml-rails 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9204547c6624e463ff1082518b9952f7583235ce
4
+ data.tar.gz: 0d8da054e7c4741edcb3f0c89568c2d9c2365a5d
5
+ SHA512:
6
+ metadata.gz: db387778f72d62202acf4e3f17b68b24e552b7c16b12dc2fd2b2e973211a6634615e32ab9c38b9d36c5e350a5ec7fa4bf845ae10c99d2ec2f4d0388f91813e6e
7
+ data.tar.gz: 9c8bcb710f5b7ff0ca65c105369c221dbb80cc087641ea76c986ebb2ee0c4a1c73f516317b6b87c09980ea7a42cda99e9c1c02e3e815bb71bc42363cc2b09582
checksums.yaml.gz.sig ADDED
Binary file
data.tar.gz.sig ADDED
@@ -0,0 +1 @@
1
+ -�4�M��:���Q�4�DԂv)���^.��MG��.Q�K|���u�+�3x(��R�Y�V�ܪQdH���"p�&��T�Z�L )3j��E@�@��!��$R '�(^�L ��SE�l�V��d\;И��(�Zc�<�J�P�@u�w��Ԑ��`��):�13UJ�%g"
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011-2015 Plataformatec (http://blog.plataformatec.com.br)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # MJML-Rails
2
+
3
+ [![Build Status](https://api.travis-ci.org/sighmon/mjml-rails.png?branch=master)](http://travis-ci.org/sighmon/mjml-rails)
4
+
5
+ **MJML-Rails** allows you to render HTML e-mails from an [MJML](https://mjml.io) template.
6
+
7
+ An example template might look like `app/views/user_mailer/email.mjml`:
8
+
9
+ ```erb
10
+ <mjml>
11
+ <mj-body>
12
+ <mj-container>
13
+ <mj-section>
14
+ <mj-column>
15
+ <mj-text>Hello World</mj-text>
16
+ <%= render :partial => 'info', :formats => [:html] %>
17
+ </mj-column>
18
+ </mj-section>
19
+ </mj-container>
20
+ </mj-body>
21
+ </mjml>
22
+ ```
23
+
24
+ And the partial `_info.mjml`:
25
+
26
+ ```erb
27
+ <mj-text>This is <%= @user.username %></mj-text>
28
+ ```
29
+
30
+ * Notice you can use ERb and partials inside the template.
31
+
32
+ ## Installation
33
+
34
+ Add it to your Gemfile.
35
+
36
+ ```ruby
37
+ gem 'mjml-rails', git: 'https://github.com/sighmon/mjml-rails.git', require: 'mjml'
38
+ ```
39
+
40
+ Run the following command to install it:
41
+
42
+ ```console
43
+ bundle install
44
+ ```
45
+
46
+ Install the MJML parser
47
+
48
+ ```console
49
+ npm install -g mjml@2.1.1
50
+ ```
51
+
52
+ ## Bug reports
53
+
54
+ If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us fixing the possible bug. We also encourage you to help even more by forking and sending us a pull request.
55
+
56
+ [github.com/sighmon/mjml-rails/issues](https://github.com/sighmon/mjml-rails/issues)
57
+
58
+ ## Maintainers
59
+
60
+ * Simon Loffler [github.com/sighmon](https://github.com/sighmon)
61
+ * Steven Pickles [github.com/thatpixguy](https://github.com/thatpixguy)
62
+
63
+ ## License
64
+
65
+ MIT License. Copyright 2016 Simon Loffler. [sighmon.com](http://sighmon.com)
66
+
67
+ Lovingly built on [github.com/plataformatec/markerb](https://github.com/plataformatec/markerb)
@@ -0,0 +1,23 @@
1
+ require "rails/generators/erb/mailer/mailer_generator"
2
+
3
+ module Mjml
4
+ module Generators
5
+ class MailerGenerator < Erb::Generators::MailerGenerator
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ protected
9
+
10
+ def format
11
+ nil # Our templates have no format
12
+ end
13
+
14
+ def formats
15
+ [format]
16
+ end
17
+
18
+ def handler
19
+ :mjml
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ <%%= yield %>
@@ -0,0 +1,3 @@
1
+ <%= class_name %>#<%= @action %>
2
+
3
+ <%%= @greeting %>, find me in app/views/<%= @path %>
data/lib/mjml.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "action_view"
2
+ require "action_view/template"
3
+ require "mjml/mjmltemplate"
4
+ require "mjml/railtie"
5
+
6
+ module Mjml
7
+ class Handler
8
+ def erb_handler
9
+ @erb_handler ||= ActionView::Template.registered_template_handler(:erb)
10
+ end
11
+
12
+ def call(template)
13
+ compiled_source = erb_handler.call(template)
14
+ if template.formats.include?(:mjml)
15
+ "Mjml::Mjmltemplate.to_html(begin;#{compiled_source};end).html_safe"
16
+ else
17
+ compiled_source
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require "mjml/parser"
2
+
3
+ module Mjml
4
+ mattr_accessor :processing_options, :renderer
5
+ @@processing_options = {}
6
+
7
+ class Mjmltemplate
8
+ def self.to_html(compiled_source)
9
+ Mjml::Parser.new(compiled_source).render.html_safe
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,81 @@
1
+ module Mjml
2
+ class Parser
3
+
4
+ # Create new parser
5
+ #
6
+ # @param input [String] The string to transform in html
7
+ def initialize input
8
+ file = File.open(in_tmp_file, 'w')
9
+ file.write(input)
10
+ file.close
11
+ end
12
+
13
+ # Render mjml template
14
+ #
15
+ # @return [String]
16
+ def render
17
+ result = run
18
+ remove_tmp_files
19
+ result
20
+ rescue
21
+
22
+ ""
23
+ end
24
+
25
+ # Exec mjml command
26
+ #
27
+ # @return [String] The result as string
28
+ def run
29
+ command = "#{mjml_bin} -r #{in_tmp_file} -o #{out_tmp_file}"
30
+ # puts command
31
+ `#{command}`
32
+ file = File.open(out_tmp_file, 'r')
33
+ str = file.read
34
+ file.close
35
+ str
36
+ end
37
+
38
+ private
39
+
40
+ # Remove tmp files
41
+ #
42
+ # @return nil
43
+ def remove_tmp_files
44
+ FileUtils.rm(in_tmp_file)
45
+ FileUtils.rm(out_tmp_file)
46
+ nil
47
+ end
48
+
49
+ # Return tmp dir
50
+ #
51
+ # @return [String]
52
+ def tmp_dir
53
+ "/tmp"
54
+ end
55
+
56
+ # Get parser tpm file to store result
57
+ #
58
+ # @return [String]
59
+ def out_tmp_file
60
+
61
+ @_out_tmp_file ||= "#{tmp_dir}/out_#{(0...8).map { (65 + rand(26)).chr }.join}.html"
62
+ end
63
+
64
+ # Get parser tpm file to get result
65
+ #
66
+ # @return [String]
67
+ def in_tmp_file
68
+
69
+ @_in_tmp_file ||= "#{tmp_dir}/in_#{(0...8).map { (65 + rand(26)).chr }.join}.mjml"
70
+ # puts @_in_tmp_file
71
+ return @_in_tmp_file
72
+ end
73
+
74
+ # Get mjml bin path
75
+ #
76
+ # @return [String]
77
+ def mjml_bin
78
+ `/usr/bin/which mjml`.chomp
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,11 @@
1
+ module Mjml
2
+ class Railtie < Rails::Railtie
3
+ config.mjml = Mjml
4
+ config.app_generators.mailer :template_engine => :mjml
5
+
6
+ initializer "mjml-rails.register_template_handler" do
7
+ ActionView::Template.register_template_handler :mjml, Mjml::Handler.new
8
+ Mime::Type.register "text/html", :mjml
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Mjml
2
+ # Version number matches MJML.io version
3
+ VERSION = "2.1.1"
4
+ end
@@ -0,0 +1,16 @@
1
+ require "test_helper"
2
+ require "generators/mjml/mailer/mailer_generator"
3
+
4
+ class GeneratorTest < Rails::Generators::TestCase
5
+ tests Mjml::Generators::MailerGenerator
6
+ destination File.expand_path("../tmp", __FILE__)
7
+ setup :prepare_destination
8
+
9
+ test "assert all views are properly created with given name" do
10
+ run_generator %w(notifier foo bar baz)
11
+
12
+ assert_file "app/views/notifier/foo.mjml"
13
+ assert_file "app/views/notifier/bar.mjml"
14
+ assert_file "app/views/notifier/baz.mjml"
15
+ end
16
+ end
data/test/mjml_test.rb ADDED
@@ -0,0 +1,127 @@
1
+ require "test_helper"
2
+
3
+ class Notifier < ActionMailer::Base
4
+ self.view_paths = File.expand_path("../views", __FILE__)
5
+
6
+ layout false
7
+
8
+ def contact(recipient, format_type)
9
+ @recipient = recipient
10
+ mail(:to => @recipient, :from => "john.doe@example.com") do |format|
11
+ format.send(format_type)
12
+ end
13
+ end
14
+
15
+ def link(format_type)
16
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
17
+ format.send(format_type)
18
+ end
19
+ end
20
+
21
+ def user(format_type)
22
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
23
+ format.send(format_type)
24
+ end
25
+ end
26
+
27
+ def no_partial(format_type)
28
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
29
+ format.send(format_type)
30
+ end
31
+ end
32
+
33
+ def multiple_format_contact(recipient)
34
+ @recipient = recipient
35
+ mail(:to => @recipient, :from => "john.doe@example.com", :template => "contact") do |format|
36
+ format.text { render 'contact' }
37
+ format.html { render 'contact' }
38
+ end
39
+ end
40
+ end
41
+
42
+ # class TestRenderer < ActionView::PartialRenderer
43
+ # attr_accessor :show_text
44
+ # def initialize(render_options = {})
45
+ # @show_text = render_options.delete(:show_text)
46
+ # super(render_options)
47
+ # end
48
+
49
+ # def normal_text(text)
50
+ # show_text ? "TEST #{text}" : "TEST"
51
+ # end
52
+ # end
53
+
54
+ class MjmlTest < ActiveSupport::TestCase
55
+
56
+ setup do
57
+ @original_renderer = Mjml.renderer
58
+ @original_processing_options = Mjml.processing_options
59
+ end
60
+
61
+ teardown do
62
+ Mjml.renderer = @original_renderer
63
+ Mjml.processing_options = @original_processing_options
64
+ end
65
+
66
+ test "html should be sent as html" do
67
+ email = Notifier.contact("you@example.com", :mjml)
68
+ assert_equal "text/html", email.mime_type
69
+ assert_no_match(/<mj-body>/, email.body.encoded.strip)
70
+ assert_match(/<body/, email.body.encoded.strip)
71
+ assert_match('<p>Hello from <a href="http://www.sighmon.com">sighmon.com</a></p>', email.body.encoded.strip)
72
+ end
73
+
74
+ test 'with partial' do
75
+ email = Notifier.user(:mjml)
76
+ assert_equal "text/html", email.mime_type
77
+ assert_match(/Hello Partial/, email.body.encoded.strip)
78
+ assert_no_match(/mj-text/, email.body.encoded.strip)
79
+ end
80
+
81
+ test 'without a partial' do
82
+ email = Notifier.no_partial(:mjml)
83
+ assert_equal "text/html", email.mime_type
84
+ assert_match(/Hello World/, email.body.encoded.strip)
85
+ assert_no_match(/mj-text/, email.body.encoded.strip)
86
+ end
87
+
88
+ # test "plain text should be sent as a plain text" do
89
+ # email = Notifier.contact("you@example.com", :text)
90
+ # assert_equal "text/plain", email.mime_type
91
+ # assert_equal "<mj-body></mj-body>", email.body.encoded.strip
92
+ # end
93
+
94
+ # test 'dealing with multipart e-mails' do
95
+ # email = Notifier.multiple_format_contact("you@example.com")
96
+ # assert_equal 2, email.parts.size
97
+ # assert_equal "multipart/alternative", email.mime_type
98
+ # assert_equal "text/plain", email.parts[0].mime_type
99
+ # assert_equal "<mj-body></mj-body>",
100
+ # email.parts[0].body.encoded.strip
101
+ # assert_equal "text/html", email.parts[1].mime_type
102
+ # assert_not_equal "<mj-body></mj-body>",
103
+ # email.parts[1].body.encoded.strip
104
+ # end
105
+
106
+ # test "with a custom renderer" do
107
+ # Mjml.renderer = TestRenderer
108
+ # email = Notifier.contact("you@example.com", :html)
109
+ # assert_equal "text/html", email.mime_type
110
+ # assert_equal "<p>TEST<strong>TEST</strong>TEST</p>", email.body.encoded.strip
111
+ # end
112
+
113
+ # test "with a custom renderer and options" do
114
+ # Mjml.renderer = TestRenderer.new(:show_text => true)
115
+ # email = Notifier.contact("you@example.com", :html)
116
+ # assert_equal "text/html", email.mime_type
117
+ # assert_equal "<p>TEST Dual templates <strong>TEST rocks</strong>TEST !</p>", email.body.encoded.strip
118
+ # end
119
+
120
+ # test 'with custom mjml processing options' do
121
+ # Mjml.processing_options = {:autolink => true}
122
+ # email = Notifier.link(:html)
123
+ # assert_equal "text/html", email.mime_type
124
+ # assert_equal '<p>Hello from <a href="http://www.sighmon.com">http://www.sighmon.com</a></p>', email.body.encoded.strip
125
+ # end
126
+
127
+ end
@@ -0,0 +1,9 @@
1
+ require "test_helper"
2
+
3
+ class MjmlTest < ActiveSupport::TestCase
4
+
5
+ test 'with Mjmltemplate processor' do
6
+ assert_not_equal "<mj-body></mj-body>", Mjml::Mjmltemplate.to_html("<mjml><mj-body><mj-container><mj-section><mj-column></mj-column></mj-section></mj-container></mj-body></mjml>").strip
7
+ end
8
+
9
+ end
@@ -0,0 +1,26 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup
4
+
5
+ require "minitest/autorun"
6
+ require "active_support/test_case"
7
+
8
+ require "action_mailer"
9
+ require "rails/railtie"
10
+ require "rails/generators"
11
+ require "rails/generators/test_case"
12
+
13
+ # require "minitest/reporters"
14
+ # Minitest::Reporters.use!
15
+
16
+ $:.unshift File.expand_path("../../lib", __FILE__)
17
+ require "mjml"
18
+ Mjml::Railtie.run_initializers
19
+
20
+ ActiveSupport::TestCase.test_order = :sorted if ActiveSupport::TestCase.respond_to? :test_order=
21
+
22
+ # Avoid annoying warning from I18n.
23
+ I18n.enforce_available_locales = false
24
+
25
+ ActionMailer::Base.delivery_method = :test
26
+ ActionMailer::Base.perform_deliveries = true
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mjml-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Loffler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDdDCCAlygAwIBAgIBATANBgkqhkiG9w0BAQUFADBAMRAwDgYDVQQDDAdzaWdo
14
+ bW9uMRcwFQYKCZImiZPyLGQBGRYHc2lnaG1vbjETMBEGCgmSJomT8ixkARkWA2Nv
15
+ bTAeFw0xNjA1MzAwNzQzMjFaFw0xNzA1MzAwNzQzMjFaMEAxEDAOBgNVBAMMB3Np
16
+ Z2htb24xFzAVBgoJkiaJk/IsZAEZFgdzaWdobW9uMRMwEQYKCZImiZPyLGQBGRYD
17
+ Y29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5NPPme/xXWWsnBPf
18
+ nMZ7IgZVrYxJmztef4+YP85wmmwxle5M8z70KE8zym/6FwuEtmI41t+wUIMRTfuv
19
+ +cvtvJqfVdGJxix528WXfeaezEK3ScsO24CeTBOqiuWbO8uMoNS0k0dg3+O02U9z
20
+ ntAiK1QN0TFQ1iu+fWW5N1GMu03BZHX+bBknsosSSA6Mnc1uyetm58qE8UV6Q8Mw
21
+ BpnsJ35D9uFLyKostYl64WJoAM9XM+xKfkgsCO8obz1lhl0RB0Sl9uI0aD484dYM
22
+ 9WnsdG9DugdECIIXwcz67vGYYln4g3+8hsvnVxKPxVT8liHo1b/rIP7NQMli2aTd
23
+ 0kLBqQIDAQABo3kwdzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU
24
+ veuvCzX+SffuzZn/PYtOpj0ni/0wHgYDVR0RBBcwFYETc2lnaG1vbkBzaWdobW9u
25
+ LmNvbTAeBgNVHRIEFzAVgRNzaWdobW9uQHNpZ2htb24uY29tMA0GCSqGSIb3DQEB
26
+ BQUAA4IBAQBgWEQGX1nB32sNYp1vB1CIBuT3twVkvAss4Vu/rAHyGHM/MLhcCiqD
27
+ Je/5hq3oRy+o8BToILpVV8DZdZLtWVaX42qL/cuWHZqfFhoPbLIjcBphAurOTCmN
28
+ yzGuPoqkEF8moN0hNqY7nNYIf4SLjal/9AJMUlrUuQyK8UaxqOg76fz7XGgJQmbH
29
+ duxe+YhT4pdcfPUw7JXf9NtdyTpaC07r2c1TIn54Q7dEGI2eKby8xvOsWAxa9rfJ
30
+ 5hPZOkUdP3egZ1xAHeFIOhTIaOJezjUEDQVattPkgwl25+Q2EXJ+5ehDU86v+lxH
31
+ E9hMwkxwQntg0fKVFbnVMOg2itaj8fJ7
32
+ -----END CERTIFICATE-----
33
+ date: 2016-05-30 00:00:00.000000000 Z
34
+ dependencies: []
35
+ description: Render MJML + ERb template views in Rails
36
+ email: sighmon@sighmon.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - MIT-LICENSE
42
+ - README.md
43
+ - lib/generators/mjml/mailer/mailer_generator.rb
44
+ - lib/generators/mjml/mailer/templates/layout.mjml
45
+ - lib/generators/mjml/mailer/templates/view.mjml
46
+ - lib/mjml.rb
47
+ - lib/mjml/mjmltemplate.rb
48
+ - lib/mjml/parser.rb
49
+ - lib/mjml/railtie.rb
50
+ - lib/mjml/version.rb
51
+ - test/generator_test.rb
52
+ - test/mjml_test.rb
53
+ - test/template_test.rb
54
+ - test/test_helper.rb
55
+ homepage: https://github.com/sighmon/mjml-rails
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message: "Don't forget to install MJML e.g. \n$ npm install -g mjml"
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: MJML + ERb templates
79
+ test_files:
80
+ - test/generator_test.rb
81
+ - test/mjml_test.rb
82
+ - test/template_test.rb
83
+ - test/test_helper.rb
metadata.gz.sig ADDED
Binary file