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 10
def initialize input
  raise Mjml.mjml_binary_error_string unless Mjml.valid_mjml_binary
  @input = input
end

Public Instance Methods

render() click to toggle source

Render mjml template

@return [String]

# File lib/mjml/parser.rb, line 18
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, Mjml.beautify, Mjml.minify, Mjml.validation_level)
rescue
  raise if Mjml.raise_render_exception
  ""
ensure
  in_tmp_file.unlink
end
run(in_tmp_file, beautify=true, minify=false, validation_level="strict") click to toggle source

Exec mjml command

@return [String] The result as string

# File lib/mjml/parser.rb, line 34
def run(in_tmp_file, beautify=true, minify=false, validation_level="strict")
  Tempfile.create(["out", ".html"]) do |out_tmp_file|
    command = "-r #{in_tmp_file} -o #{out_tmp_file.path} --config.beautify #{beautify} --config.minify #{minify} --config.validationLevel #{validation_level}"
    _, stderr, status = Mjml.run_mjml(command)
    raise ParseError.new(stderr.chomp) unless status.success?
    Mjml.logger.warn(stderr.chomp) unless stderr.blank?
    out_tmp_file.read
  end
end