mjml-rails 4.14.1 → 4.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae000b23a07869a790fcda678ef39a4647ff5dfa561a012d562d8193c5c5d125
4
- data.tar.gz: 9bb383f3a70b2a71518ee8684aec8a3eb0a7b153714372da01b79901fd9cbfcb
3
+ metadata.gz: 7a768bdc7c72cb474b41346d9f48685d46ac7eb477850f417eac944abcfe3289
4
+ data.tar.gz: ceca1bd32af0d391c92a5ff0a00a80751e34b069571fe57567fc3b1cfee656ff
5
5
  SHA512:
6
- metadata.gz: 7851d43cf196343c277f8653f67007d3ce92e5ef20761f2b078d74acc8982fb47629829cd89eded4e4a18a6dd72e41bf7ab89e4bb6ddc45172ec67410f6eceee
7
- data.tar.gz: 71fc235d2d2f282c7c7e21e91a6e0bdd850bd8fe8bcd80054ae16a8148624b589586ba26039dd735735d5c60b6a1ffd1e5f6a535e9f2c76f42f5aa4653d0a41b
6
+ metadata.gz: a2a6098dadd2d9004b7043903b61056da28d2d061bebb391c7ed9ab537f24879dac30b46d08388c41871a74c7a7e4e703d1b206e08f0980aeaeb006e3674ed7c
7
+ data.tar.gz: f626fc4c6141133b42cba395e137f557ed3259198a40ab7b445a8ec5b3521cbb74a5a499df5d3e0a176f5f3a8c10e5f933e37fa000d45957784c1d9869b4a778
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -68,6 +68,14 @@ Run the following command to install it:
68
68
  bundle install
69
69
  ```
70
70
 
71
+ After installation, you can generate the MJML initializer file:
72
+
73
+ ```console
74
+ rails generate mjml:install
75
+ ```
76
+
77
+ This will create `config/initializers/mjml.rb` with default configuration options.
78
+
71
79
  Add the MJML parser to your project with your favourite package manager:
72
80
 
73
81
  ```console
@@ -124,7 +132,7 @@ MJML-Rails has the following settings with defaults:
124
132
 
125
133
  ERB can be used inside MJML templates by default. Possible values are all template languages that you have installed, e.g. `:haml` or `:slim`.
126
134
 
127
- **Note:** If youre using Haml/Slim layouts, please dont put `<mjml>` in comments in your partial. Read more at [#34](https://github.com/sighmon/mjml-rails/issues/34).
135
+ **Note:** If you're using Haml/Slim layouts, please don't put `<mjml>` in comments in your partial. Read more at [#34](https://github.com/sighmon/mjml-rails/issues/34).
128
136
 
129
137
  - `raise_render_exception: true`
130
138
 
@@ -155,6 +163,9 @@ MJML-Rails has the following settings with defaults:
155
163
  - `use_mrml: false`
156
164
  Enabling this will allow you to use Rust implementation of MJML via the `mrml` gem. It comes with prebuilt binaries instead of having to install MJML along with Node. When enabled the options `mjml_binary_version_supported`, `mjml_binary`, `minify`, `beautify` and `validation_level` are ignored.
157
165
 
166
+ - `cache_mjml: false`
167
+ By default, MJML-Rails does not cache compiled templates. Setting this to `true` will cache compiled templates in `tmp/mjml_cache` to improve performance for frequently used templates.
168
+
158
169
  - `fonts`
159
170
  By default, MJML-Rails uses MJML default fonts, but enables you to override it.
160
171
  Example : `config.fonts = { Raleway: 'https://fonts.googleapis.com/css?family=Raleway }`
@@ -185,6 +196,9 @@ Mjml.setup do |config|
185
196
 
186
197
  # Use default system fonts instead of google fonts
187
198
  config.fonts = {}
199
+
200
+ # Uncomment this to enable template caching
201
+ # config.cache_mjml = true
188
202
  end
189
203
  ```
190
204
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Mjml
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('templates', __dir__)
9
+ desc 'Creates MJML initializer for your application'
10
+
11
+ def copy_initializer
12
+ template 'mjml.rb', 'config/initializers/mjml.rb'
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MJML configuration
4
+ Mjml.setup do |config|
5
+ # Use :erb as a template language
6
+ config.template_language = :erb
7
+
8
+ # Raise exceptions for template errors
9
+ config.raise_render_exception = true
10
+
11
+ # Beautify the output HTML
12
+ config.beautify = true
13
+
14
+ # Minify the output HTML
15
+ config.minify = false
16
+
17
+ # Validation level for MJML templates
18
+ # Possible values: 'strict', 'soft'
19
+ config.validation_level = 'strict'
20
+
21
+ # Use MRML instead of MJML (requires mrml gem)
22
+ config.use_mrml = false
23
+
24
+ # Cache compiled templates for better performance
25
+ config.cache_mjml = false
26
+
27
+ # Custom fonts configuration
28
+ # Example: config.fonts = { Raleway: 'https://fonts.googleapis.com/css?family=Raleway' }
29
+ config.fonts = nil
30
+
31
+ # Uncomment this to enable template caching
32
+ # config.cache_mjml = true
33
+ end
data/lib/mjml/cache.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mjml
4
+ class Cache
5
+ attr_reader :template_path
6
+
7
+ def initialize(template_path)
8
+ @template_path = template_path
9
+ end
10
+
11
+ # @yield [] -> String
12
+ # @return [String]
13
+ def cache(&block)
14
+ return yield if !Mjml.cache_mjml && block
15
+
16
+ cached_path = cached_file_path
17
+ if File.exist?(cached_path)
18
+ File.read(cached_path)
19
+ else
20
+ html_content = yield if block
21
+ File.write(cached_path, html_content)
22
+ html_content
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def cached_file_path
29
+ File.join(cache_directory, "#{fingerprint}.html")
30
+ end
31
+
32
+ def fingerprint
33
+ full_path = File.join(Dir.pwd, 'app', 'views', "#{template_path}.mjml")
34
+ raise "Template file not found: #{full_path}" unless File.exist?(full_path)
35
+
36
+ Digest::SHA256.hexdigest(File.read(full_path))
37
+ end
38
+
39
+ def cache_directory
40
+ dir = File.join(Dir.pwd, 'tmp', 'mjml_cache')
41
+ FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
42
+ dir
43
+ end
44
+ end
45
+ end
data/lib/mjml/handler.rb CHANGED
@@ -17,6 +17,7 @@ module Mjml
17
17
  compiled_source = compile_source(source, template)
18
18
 
19
19
  parser_class = Mjml.use_mrml ? 'MrmlParser' : 'Parser'
20
+ template_path = template.virtual_path
20
21
  # Per MJML v4 syntax documentation[0] valid/render'able document MUST start with <mjml> root tag
21
22
  # If we get here and template source doesn't start with one it means
22
23
  # that we are rendering partial named according to legacy naming convention (partials ending with '.mjml')
@@ -25,7 +26,7 @@ module Mjml
25
26
  #
26
27
  # [0] - https://github.com/mjmlio/mjml/blob/master/doc/components_1.md#mjml
27
28
  if /<mjml.*?>/i.match?(compiled_source)
28
- "Mjml::#{parser_class}.new(begin;#{compiled_source};end).render.html_safe"
29
+ "Mjml::#{parser_class}.new('#{template_path}', begin;#{compiled_source};end).render.html_safe"
29
30
  else
30
31
  compiled_source
31
32
  end
data/lib/mjml/parser.rb CHANGED
@@ -1,36 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'digest'
4
+ require_relative 'cache'
5
+
3
6
  module Mjml
4
7
  class Parser
5
8
  class ParseError < StandardError; end
6
9
 
7
- attr_reader :input
10
+ attr_reader :template_path, :input
8
11
 
9
12
  # Create new parser
10
13
  #
11
- # @param input [String] The string to transform in html
12
- def initialize(input)
14
+ # @param template_path [String] The path to the .mjml file
15
+ # @param input [String] The content of the .mjml file
16
+ def initialize(template_path, input)
13
17
  raise Mjml.mjml_binary_error_string unless Mjml.valid_mjml_binary
14
18
 
15
- @input = input
19
+ @template_path = template_path
20
+ @input = input
21
+ @with_cache = Cache.new(template_path)
16
22
  end
17
23
 
18
- # Render mjml template
24
+ # rubocop:disable Metrics/MethodLength
25
+ # Render MJML template
19
26
  #
20
27
  # @return [String]
21
28
  def render
22
- in_tmp_file = Tempfile.open(['in', '.mjml']) do |file|
23
- file.write(input)
24
- file # return tempfile from block so #unlink works later
25
- end
26
- run(in_tmp_file.path)
27
- rescue StandardError
28
- raise if Mjml.raise_render_exception
29
+ @with_cache.cache do
30
+ in_tmp_file = Tempfile.open(['in', '.mjml']) do |file|
31
+ file.write(input)
32
+ file # return tempfile from block so #unlink works later
33
+ end
34
+ run(in_tmp_file.path)
35
+ rescue StandardError
36
+ raise if Mjml.raise_render_exception
29
37
 
30
- ''
31
- ensure
32
- in_tmp_file&.unlink
38
+ ''
39
+ ensure
40
+ in_tmp_file&.unlink
41
+ end
33
42
  end
43
+ # rubocop:enable Metrics/MethodLength
34
44
 
35
45
  # Exec mjml command
36
46
  #
data/lib/mjml/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Mjml
4
4
  # Version number no longer matches MJML.io version
5
- VERSION = '4.14.1'
5
+ VERSION = '4.15.0'
6
6
  end
data/lib/mjml.rb CHANGED
@@ -19,7 +19,8 @@ module Mjml
19
19
  :raise_render_exception,
20
20
  :template_language,
21
21
  :validation_level,
22
- :use_mrml
22
+ :use_mrml,
23
+ :cache_mjml
23
24
 
24
25
  mattr_writer :valid_mjml_binary
25
26
 
@@ -33,6 +34,7 @@ module Mjml
33
34
  self.validation_level = 'strict'
34
35
  self.use_mrml = false
35
36
  self.fonts = nil
37
+ self.cache_mjml = false
36
38
 
37
39
  def self.check_version(bin)
38
40
  stdout, _, status = run_mjml('--version', mjml_bin: bin)
data/test/parser_test.rb CHANGED
@@ -4,7 +4,7 @@ require 'test_helper'
4
4
 
5
5
  describe Mjml::Parser do
6
6
  let(:input) { mock('input') }
7
- let(:parser) { Mjml::Parser.new(input) }
7
+ let(:parser) { Mjml::Parser.new('test_template', input) }
8
8
 
9
9
  describe '#render' do
10
10
  describe 'when exception is raised' do
@@ -42,7 +42,7 @@ describe Mjml::Parser do
42
42
  expect(Mjml.beautify).must_equal(true)
43
43
  expect(Mjml.minify).must_equal(false)
44
44
  expect(Mjml.validation_level).must_equal('strict')
45
- expect(Mjml.fonts).must_equal(nil)
45
+ assert_nil(Mjml.fonts)
46
46
  end
47
47
 
48
48
  it 'uses setup config' do
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mjml-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.14.1
4
+ version: 4.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Loffler
8
8
  - Steven Pickles
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain:
12
11
  - |
@@ -36,7 +35,7 @@ cert_chain:
36
35
  Qqk2dn/fFE9vdcl9OaOw2Zizne/1VFL/jkzXLqnLgbKD/q129mVCBgw2CKYnQfMN
37
36
  4RwBjPyqnMxWnSq6Ycn7HdFEkgyf2cAxFfH5QtDsjEuca+/LAJMeAQ==
38
37
  -----END CERTIFICATE-----
39
- date: 2025-02-20 00:00:00.000000000 Z
38
+ date: 2025-04-05 00:00:00.000000000 Z
40
39
  dependencies:
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: byebug
@@ -158,11 +157,14 @@ extra_rdoc_files: []
158
157
  files:
159
158
  - MIT-LICENSE
160
159
  - README.md
160
+ - lib/generators/mjml/install/install_generator.rb
161
+ - lib/generators/mjml/install/templates/mjml.rb
161
162
  - lib/generators/mjml/mailer/mailer_generator.rb
162
163
  - lib/generators/mjml/mailer/templates/layout.html.mjml
163
164
  - lib/generators/mjml/mailer/templates/view.html.erb
164
165
  - lib/mjml-rails.rb
165
166
  - lib/mjml.rb
167
+ - lib/mjml/cache.rb
166
168
  - lib/mjml/handler.rb
167
169
  - lib/mjml/mrml_parser.rb
168
170
  - lib/mjml/parser.rb
@@ -192,8 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
194
  - !ruby/object:Gem::Version
193
195
  version: '0'
194
196
  requirements: []
195
- rubygems_version: 3.5.22
196
- signing_key:
197
+ rubygems_version: 3.6.2
197
198
  specification_version: 4
198
199
  summary: MJML + ERb templates
199
200
  test_files:
metadata.gz.sig CHANGED
Binary file