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

VERSION =

Version number no longer matches MJML.io version

"4.6.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



140
141
142
143
144
# File 'lib/mjml.rb', line 140

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

Class Method Details

.bin_path_from(package_manager) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/mjml.rb', line 88

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_for_custom_mjml_binaryObject



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

def self.check_for_custom_mjml_binary
  if const_defined?('BIN') && Mjml::BIN.present?
    logger.warn('Setting `Mjml::BIN` is deprecated and will be removed in a future version! Please use `Mjml.mjml_binary=` instead.')
    self.mjml_binary = Mjml::BIN
    remove_const 'BIN'
  end

  return unless mjml_binary.present?

  return mjml_binary if check_version(mjml_binary)

  raise "MJML.mjml_binary is set to '#{mjml_binary}' but MJML-Rails could not validate that it is a valid MJML binary. Please check your configuration."
end

.check_for_global_mjml_binaryObject



83
84
85
86
# File 'lib/mjml.rb', line 83

def self.check_for_global_mjml_binary
  mjml_bin = `which mjml`.chomp
  return mjml_bin if mjml_bin.present? && check_version(mjml_bin)
end

.check_for_npm_mjml_binaryObject



75
76
77
78
79
80
81
# File 'lib/mjml.rb', line 75

def self.check_for_npm_mjml_binary
  npm_bin = `which npm`.chomp
  return unless 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_yarn_mjml_binaryObject



67
68
69
70
71
72
73
# File 'lib/mjml.rb', line 67

def self.check_for_yarn_mjml_binary
  yarn_bin = `which yarn`.chomp
  return unless yarn_bin.present?

  mjml_bin = "#{yarn_bin} run mjml"
  return mjml_bin if check_version(mjml_bin)
end

.check_version(bin) ⇒ Object



30
31
32
33
34
35
# File 'lib/mjml.rb', line 30

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



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

def self.discover_mjml_bin
  logger.warn('`Mjml.discover_mjml_bin` is deprecated and has no effect anymore! Please use `Mjml.mjml_binary=` to set a custom MJML binary.')
end

.run_mjml(args, mjml_bin: valid_mjml_binary) ⇒ Object



37
38
39
# File 'lib/mjml.rb', line 37

def self.run_mjml(args, mjml_bin: valid_mjml_binary)
  Open3.capture3("#{mjml_bin} #{args}")
end

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Mjml)

    the object that the method was called on



133
134
135
# File 'lib/mjml.rb', line 133

def self.setup
  yield self if block_given?
end

.valid_mjml_binaryObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mjml.rb', line 41

def self.valid_mjml_binary
  @@valid_mjml_binary ||=
    check_for_custom_mjml_binary ||
    check_for_yarn_mjml_binary ||
    check_for_npm_mjml_binary ||
    check_for_global_mjml_binary

  return @@valid_mjml_binary if @@valid_mjml_binary

  puts Mjml.mjml_binary_error_string
end