class MJML::Parser
Constants
- ROOT_TAGS_REGEX
Public Class Methods
new()
click to toggle source
# File lib/mjml/parser.rb, line 11 def initialize raise ExecutableNotFound if MJML.executable_version.nil? end
Public Instance Methods
call(template)
click to toggle source
# File lib/mjml/parser.rb, line 15 def call(template) call!(template) rescue InvalidTemplate nil end
call!(template)
click to toggle source
# File lib/mjml/parser.rb, line 21 def call!(template) @template = template exec! end
Private Instance Methods
cmd(file_path = nil)
click to toggle source
# File lib/mjml/parser.rb, line 55 def cmd(file_path = nil) "#{mjml_bin} #{minify_output} #{validation_level} #{cmd_options}" end
cmd_options()
click to toggle source
# File lib/mjml/parser.rb, line 59 def cmd_options if should_get_outpout_from_file? "-i -o #{@temp_file.path}" else "-is" end end
exec!()
click to toggle source
# File lib/mjml/parser.rb, line 28 def exec! raise InvalidTemplate if @template.empty? return @template if partial? out, err = should_get_outpout_from_file? ? output_from_file : output_from_memory parsed = parse_output(out) MJML.logger.error(err) unless err.empty? MJML.logger.warn(parsed[:warnings]) unless parsed[:warnings].empty? unless err.empty? message = [err, parsed[:warnings]].reject(&:empty?).join("\n") raise InvalidTemplate.new(message) end parsed[:output] end
minify_output()
click to toggle source
# File lib/mjml/parser.rb, line 67 def minify_output '--min' if MJML::Config.minify_output end
mjml_bin()
click to toggle source
# File lib/mjml/parser.rb, line 51 def mjml_bin MJML::Config.bin_path end
output_from_file()
click to toggle source
# File lib/mjml/parser.rb, line 75 def output_from_file @temp_file = Tempfile.new("mjml-template") _out, err, _sts = Open3.capture3(cmd, stdin_data: @template) @temp_file.rewind @temp_file.unlink return @temp_file.read, err end
output_from_memory()
click to toggle source
# File lib/mjml/parser.rb, line 83 def output_from_memory out, err, _sts = Open3.capture3(cmd, stdin_data: @template) return out, err end
parse_output(out)
click to toggle source
# File lib/mjml/parser.rb, line 92 def parse_output(out) warnings = [] output = [] out.lines.each do |l| if l.strip.start_with?('Line') warnings << l else output << l end end { warnings: warnings.join("\n"), output: output.join } end
partial?()
click to toggle source
# File lib/mjml/parser.rb, line 47 def partial? (@template =~ ROOT_TAGS_REGEX).nil? end
should_get_outpout_from_file?()
click to toggle source
# File lib/mjml/parser.rb, line 88 def should_get_outpout_from_file? @template.size > 20_000 end
validation_level()
click to toggle source
# File lib/mjml/parser.rb, line 71 def validation_level "--level=#{MJML::Config.validation_level}" if MJML::Feature.available?(:validation_level) end