Created
September 17, 2018 21:45
-
-
Save vegetabill/c677d21d9cbf9fc64d0ef45fbc03017b to your computer and use it in GitHub Desktop.
How to augment mjml to output intermediate output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open3' | |
module Mjml | |
class Parser | |
class ParseError < StandardError; end | |
attr_reader :input | |
# Create new parser | |
# | |
# @param input [String] The string to transform in html | |
def initialize input | |
raise Mjml.mjml_binary_error_string unless mjml_bin | |
@input = input | |
end | |
# Render mjml template | |
# | |
# @return [String] | |
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 | |
raise if Mjml.raise_render_exception | |
"" | |
ensure | |
in_tmp_file.unlink | |
end | |
# Exec mjml command | |
# | |
# @return [String] The result as string | |
def run(in_tmp_file) | |
puts "[DEBUG] copying #{in_tmp_file}" | |
`cp #{in_tmp_file} /Users/wdephill/goodreads/Monolith/src/Goodreads/rails-root/tmp/mjml/` | |
Tempfile.create(["out", ".html"]) do |out_tmp_file| | |
command = "#{mjml_bin} -r #{in_tmp_file} -o #{out_tmp_file.path}" | |
_, _, stderr, _ = Open3.popen3(command) | |
raise ParseError.new(stderr.read.chomp) unless stderr.eof? | |
out_tmp_file.read | |
end | |
end | |
private | |
# Get mjml bin path | |
# | |
# @return [String] | |
def mjml_bin | |
Mjml::BIN | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment