Module: Mjml

Defined in:
lib/mjml.rb,
lib/mjml/cache.rb,
lib/mjml/parser.rb,
lib/mjml/handler.rb,
lib/mjml/railtie.rb,
lib/mjml/version.rb,
lib/mjml/mrml_parser.rb,
lib/generators/mjml/mailer/mailer_generator.rb,
lib/generators/mjml/install/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Cache, Handler, MrmlParser, Parser, Railtie

Constant Summary collapse

VERSION =

Version number no longer matches MJML.io version

'4.16.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject



145
146
147
148
149
# File 'lib/mjml.rb', line 145

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

Class Method Details

.check_for_bun_mjml_binaryObject



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

def self.check_for_bun_mjml_binary
  return unless Mjml.which('bun')

  # HINT: Bun always prioritizes local bins first and falls back to global installations
  mjml_bin = 'bun run mjml'

  return mjml_bin if check_version(mjml_bin)
end

.check_for_custom_mjml_binaryObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mjml.rb', line 74

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 if mjml_binary.blank?

  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



117
118
119
120
121
122
# File 'lib/mjml.rb', line 117

def self.check_for_global_mjml_binary
  mjml_bin = Mjml.which('mjml')
  return unless mjml_bin

  return mjml_bin if mjml_bin.present? && check_version(mjml_bin)
end

.check_for_mjml_package(package_manager, bin_command, binary_path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mjml.rb', line 90

def self.check_for_mjml_package(package_manager, bin_command, binary_path)
  pm_bin = Mjml.which(package_manager)
  return unless pm_bin

  stdout, _, status = Open3.capture3("#{pm_bin} #{bin_command}")
  return unless status.success?

  mjml_bin = File.join(stdout.chomp, *binary_path)
  return mjml_bin if check_version(mjml_bin)
rescue Errno::ENOENT # package manager is not installed
  nil
end

.check_for_mrml_binaryObject



124
125
126
127
128
129
130
131
# File 'lib/mjml.rb', line 124

def self.check_for_mrml_binary
  return unless Mjml.use_mrml

  MRML.present?
rescue NameError
  Mjml.mjml_binary_error_string = 'Couldn\'t find MRML - did you add \'mrml\' to your Gemfile?'
  false
end

.check_for_package_mjml_binaryObject



112
113
114
115
# File 'lib/mjml.rb', line 112

def self.check_for_package_mjml_binary
  check_for_mjml_package('npm', 'root', ['.bin', 'mjml']) ||
    check_for_mjml_package('yarn', 'bin mjml', [])
end

.check_version(bin) ⇒ Object



39
40
41
42
43
44
# File 'lib/mjml.rb', line 39

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



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

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



46
47
48
# File 'lib/mjml.rb', line 46

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



138
139
140
# File 'lib/mjml.rb', line 138

def self.setup
  yield self if block_given?
end

.valid_mjml_binaryObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mjml.rb', line 61

def self.valid_mjml_binary
  self.valid_mjml_binary = @@valid_mjml_binary ||
                           check_for_custom_mjml_binary ||
                           check_for_mrml_binary ||
                           check_for_bun_mjml_binary ||
                           check_for_package_mjml_binary ||
                           check_for_global_mjml_binary

  return @@valid_mjml_binary if @@valid_mjml_binary

  puts Mjml.mjml_binary_error_string
end

.which(command) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/mjml.rb', line 50

def self.which(command)
  if Gem.win_platform?
    stdout, _, status = Open3.capture3("where #{command}")
  else
    stdout, _, status = Open3.capture3("which #{command}")
  end
  status.success? ? stdout.chomp : nil
rescue Errno::ENOENT
  nil
end