Module: Mjml

Defined in:
lib/mjml.rb,
lib/mjml/parser.rb,
lib/mjml/railtie.rb,
lib/mjml/version.rb,
lib/mjml/mjmltemplate.rb,
lib/generators/mjml/mailer/mailer_generator.rb

Defined Under Namespace

Modules: Generators Classes: Handler, Mjmltemplate, Parser, Railtie

Constant Summary collapse

BIN =
discover_mjml_bin
VERSION =

Version number no longer matches MJML.io version

"4.5.0"
@@template_language =
:erb
@@raise_render_exception =
true
@@mjml_binary_version_supported =
"4."
@@mjml_binary_error_string =
"Couldn't find the MJML #{Mjml.mjml_binary_version_supported} binary.. have you run $ npm install mjml?"
@@beautify =
true
@@minify =
false
@@validation_level =
"strict"
@@processing_options =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject



106
107
108
109
110
# File 'lib/mjml.rb', line 106

def logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.progname = self.name
  end
end

Class Method Details

.bin_path_from(package_manager) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/mjml.rb', line 56

def self.bin_path_from(package_manager)
  stdout, _, status = Open3.capture3("#{package_manager} bin")

  return unless status.success?

  stdout.chomp
rescue Errno::ENOENT # package manager is not installed
  nil
end

.check_version(bin) ⇒ Object



20
21
22
23
24
25
# File 'lib/mjml.rb', line 20

def self.check_version(bin)
  stdout, _, status = run_mjml('--version', mjml_bin: bin)
  status.success? && stdout.include?("mjml-core: #{Mjml.mjml_binary_version_supported}")
rescue StandardError
  false
end

.discover_mjml_binObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mjml.rb', line 33

def self.discover_mjml_bin
  # Check for local install of MJML with yarn
  yarn_bin = `which yarn`.chomp
  if yarn_bin.present?
    mjml_bin = "#{yarn_bin} run mjml"
    return mjml_bin if check_version(mjml_bin)
  end

  # Check for a local install of MJML with npm
  npm_bin = `which npm`.chomp
  if npm_bin.present? && (installer_path = bin_path_from(npm_bin)).present?
    mjml_bin = File.join(installer_path, 'mjml')
    return mjml_bin if check_version(mjml_bin)
  end

  # Check for a global install of MJML
  mjml_bin = `which mjml`.chomp
  return mjml_bin if mjml_bin.present? && check_version(mjml_bin)

  puts Mjml.mjml_binary_error_string
  nil
end

.run_mjml(args, mjml_bin: nil) ⇒ Object



27
28
29
30
31
# File 'lib/mjml.rb', line 27

def self.run_mjml(args, mjml_bin: nil)
  mjml_bin ||= BIN

  Open3.capture3("#{mjml_bin} #{args}")
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Mjml)

    the object that the method was called on



99
100
101
# File 'lib/mjml.rb', line 99

def self.setup
  yield self if block_given?
end