mjml-rb 0.2.38 → 0.3.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 +4 -4
- data/README.md +18 -0
- data/lib/mjml-rb/railtie.rb +13 -9
- data/lib/mjml-rb/template_handler.rb +59 -11
- data/lib/mjml-rb/version.rb +1 -1
- data/lib/mjml-rb.rb +9 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cfa6cc54a797f8b4323003dbcfd92fa4ef02b1e0b118e58acd14bb8e901e502d
|
|
4
|
+
data.tar.gz: 12d0fb1d71e03a23811caac5b0417b7f32823b95870e47296ea51fd8bd713018
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 689a1974e1c6e7477e202c3fb070c63eadf3b66617e2ea177feb6b45ae9d58cf9c1472dd7ddbe4e141d6583e67f83c93f76ca91c5f787d4a706241523c313361
|
|
7
|
+
data.tar.gz: ad73e16bd469bdfd98ccff031aca1796f99c460c87ab6435ba272492ffeb4ffff2b1e77f1019c368324286942cc561239e20b4219e6010efcac7d2066a945bb6
|
data/README.md
CHANGED
|
@@ -46,6 +46,24 @@ bundle exec bin/mjml --validate example.mjml
|
|
|
46
46
|
In a Rails app, requiring the gem registers an `ActionView` template handler for
|
|
47
47
|
`.mjml` templates through a `Railtie`.
|
|
48
48
|
|
|
49
|
+
By default, `.mjml` files are treated as raw MJML/XML source.
|
|
50
|
+
|
|
51
|
+
If you want Slim-backed MJML templates, configure it explicitly:
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
config.mjml_rb.rails_template_language = :slim
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Supported values are `:slim` and `:haml`.
|
|
58
|
+
|
|
59
|
+
With a configured `rails_template_language`, `.mjml` templates are rendered
|
|
60
|
+
through that template engine first, so partials and embedded Ruby can assemble
|
|
61
|
+
MJML before the outer template is compiled to HTML. Without that setting,
|
|
62
|
+
non-XML MJML source is rejected instead of being guessed.
|
|
63
|
+
|
|
64
|
+
For `:slim` or `:haml`, the matching Rails template handler must already be
|
|
65
|
+
registered in `ActionView` by the corresponding gem or integration layer.
|
|
66
|
+
|
|
49
67
|
Create a view such as `app/views/user_mailer/welcome.html.mjml`:
|
|
50
68
|
|
|
51
69
|
```mjml
|
data/lib/mjml-rb/railtie.rb
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
if defined?(Rails::Railtie)
|
|
2
|
+
require_relative "template_handler"
|
|
2
3
|
|
|
3
|
-
module MjmlRb
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
module MjmlRb
|
|
5
|
+
class Railtie < Rails::Railtie
|
|
6
|
+
config.mjml_rb = ActiveSupport::OrderedOptions.new
|
|
7
|
+
config.mjml_rb.compiler_options = {validation_level: "strict"}
|
|
8
|
+
config.mjml_rb.rails_template_language = nil
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
initializer "mjml_rb.action_view" do |app|
|
|
11
|
+
MjmlRb.rails_compiler_options = app.config.mjml_rb.compiler_options.to_h
|
|
12
|
+
MjmlRb.rails_template_language = app.config.mjml_rb.rails_template_language
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
ActiveSupport.on_load(:action_view) do
|
|
15
|
+
MjmlRb.register_action_view_template_handler!
|
|
16
|
+
end
|
|
13
17
|
end
|
|
14
18
|
end
|
|
15
19
|
end
|
|
@@ -1,17 +1,65 @@
|
|
|
1
|
+
require "action_view"
|
|
2
|
+
require "action_view/template"
|
|
3
|
+
|
|
1
4
|
module MjmlRb
|
|
2
5
|
class TemplateHandler
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
RUBY
|
|
6
|
+
SUPPORTED_TEMPLATE_LANGUAGES = %w[:slim :haml].freeze
|
|
7
|
+
|
|
8
|
+
def call(template, source = nil)
|
|
9
|
+
compiled_source = compile_source(template, source)
|
|
10
|
+
template_path = template.respond_to?(:virtual_path) ? template.virtual_path : template.identifier
|
|
11
|
+
|
|
12
|
+
if /<mjml.*?>/i.match?(compiled_source)
|
|
13
|
+
"::MjmlRb::TemplateHandler.render_compiled_source(begin;#{compiled_source};end, #{template_path.inspect})"
|
|
14
|
+
else
|
|
15
|
+
"::MjmlRb::TemplateHandler.render_partial_source(begin;#{compiled_source};end)"
|
|
14
16
|
end
|
|
15
17
|
end
|
|
18
|
+
|
|
19
|
+
def self.render_compiled_source(mjml_source, template_path)
|
|
20
|
+
mjml_result = ::MjmlRb::Compiler.new(::MjmlRb.rails_compiler_options || {}).compile(mjml_source.to_s)
|
|
21
|
+
|
|
22
|
+
if mjml_result.errors.any?
|
|
23
|
+
raise "MJML compilation failed for #{template_path}: #{mjml_result.errors.map { |error| error[:formatted_message] || error[:message] }.join(', ')}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
html = mjml_result.html.to_s
|
|
27
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.render_partial_source(compiled_source)
|
|
31
|
+
output = compiled_source.to_s
|
|
32
|
+
output.respond_to?(:html_safe) ? output.html_safe : output
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def template_handler
|
|
38
|
+
language = MjmlRb.rails_template_language
|
|
39
|
+
raise missing_rails_template_language_error if language.nil?
|
|
40
|
+
|
|
41
|
+
handler = ActionView::Template.registered_template_handler(language)
|
|
42
|
+
return handler if handler
|
|
43
|
+
|
|
44
|
+
raise "MJML Rails rails_template_language `#{language}` is not registered with ActionView. Make sure the matching Rails template handler is loaded."
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def compile_source(template, source)
|
|
48
|
+
return template.source.inspect if xml_source?(template.source)
|
|
49
|
+
|
|
50
|
+
template_handler.call(template, source)
|
|
51
|
+
rescue RuntimeError => error
|
|
52
|
+
raise error unless error.message == missing_rails_template_language_error
|
|
53
|
+
|
|
54
|
+
%(raise #{missing_rails_template_language_error.inspect})
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def xml_source?(source)
|
|
58
|
+
source.to_s.lstrip.start_with?("<")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def missing_rails_template_language_error
|
|
62
|
+
"MJML Rails rails_template_language is not configured for non-XML templates. Configure it with one of: #{SUPPORTED_TEMPLATE_LANGUAGES.join(', ')}. Otherwise use raw XML MJML."
|
|
63
|
+
end
|
|
16
64
|
end
|
|
17
65
|
end
|
data/lib/mjml-rb/version.rb
CHANGED
data/lib/mjml-rb.rb
CHANGED
|
@@ -28,11 +28,19 @@ module MjmlRb
|
|
|
28
28
|
@rails_compiler_options = options || {}
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
def rails_template_language
|
|
32
|
+
@rails_template_language
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def rails_template_language=(language)
|
|
36
|
+
@rails_template_language = language&.to_sym
|
|
37
|
+
end
|
|
38
|
+
|
|
31
39
|
def register_action_view_template_handler!
|
|
32
40
|
return unless defined?(ActionView::Template)
|
|
33
41
|
|
|
34
42
|
require_relative "mjml-rb/template_handler"
|
|
35
|
-
ActionView::Template.register_template_handler(:mjml, TemplateHandler)
|
|
43
|
+
ActionView::Template.register_template_handler(:mjml, TemplateHandler.new)
|
|
36
44
|
end
|
|
37
45
|
|
|
38
46
|
def mjml2html(mjml, options = {})
|