class Mjml::Parser

Attributes

input[R]

Public Class Methods

new(input) click to toggle source

Create new parser

@param input [String] The string to transform in html

# File lib/mjml/parser.rb, line 12
def initialize(input)
  raise Mjml.mjml_binary_error_string unless Mjml.valid_mjml_binary

  @input = input
end

Public Instance Methods

build_command(in_file, out_file) click to toggle source

Build command string from config variables

@return [String] Command string

# File lib/mjml/parser.rb, line 56
def build_command(in_file, out_file)
  command = "-r #{in_file} -o #{out_file.path} " \
            "--config.beautify #{Mjml.beautify} " \
            "--config.minify #{Mjml.minify} " \
            "--config.validationLevel #{Mjml.validation_level}"
  command += " --config.fonts '#{Mjml.fonts.to_json}'" unless Mjml.fonts.nil?
  command
end
render() click to toggle source

Render mjml template

@return [String]

# File lib/mjml/parser.rb, line 21
def render
  in_tmp_file = Tempfile.open(['in', '.mjml']) do |file|
    file.write(input)
    file # return tempfile from block so #unlink works later
  end
  run(in_tmp_file.path)
rescue StandardError
  raise if Mjml.raise_render_exception

  ''
ensure
  in_tmp_file&.unlink
end
run(in_tmp_file) click to toggle source

Exec mjml command

@return [String] The result as string

# File lib/mjml/parser.rb, line 38
def run(in_tmp_file)
  Tempfile.create(['out', '.html']) do |out_tmp_file|
    _, stderr, status = Mjml.run_mjml(build_command(in_tmp_file, out_tmp_file))

    unless status.success?
      # The process status ist quite helpful in case of dying processes without STDERR output.
      # Node exit codes are documented here: https://node.readthedocs.io/en/latest/api/process/#exit-codes
      raise ParseError, "#{stderr.chomp}\n(process status: #{status})"
    end

    Mjml.logger.warn(stderr.chomp) if stderr.present?
    out_tmp_file.read
  end
end