Skip to content

Commit

Permalink
Merge pull request #8 from menunextdoor/mnd
Browse files Browse the repository at this point in the history
Add the ability to compile very large template without stdout
  • Loading branch information
m-basov authored Apr 26, 2017
2 parents 748f614 + d95c559 commit 325601e
Show file tree
Hide file tree
Showing 4 changed files with 648 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end
task default: :test

TEMPLATE_PATH = "#{Dir.pwd}/spec/fixtures"
OUTPUT_FILES = ['hello.html', 'hello.min.html']
OUTPUT_FILES = ['hello.html', 'hello-big.html', 'hello.min.html']

def mjml_path
local_path = File.expand_path('node_modules/.bin/mjml', Dir.pwd)
Expand All @@ -22,6 +22,7 @@ end
# Prepare env for tests
task :prepare => [:clear] do
`#{mjml_path} #{TEMPLATE_PATH}/hello.mjml -o #{TEMPLATE_PATH}/hello.html`
`#{mjml_path} #{TEMPLATE_PATH}/hello-big.mjml -o #{TEMPLATE_PATH}/hello-big.html`
`#{mjml_path} #{TEMPLATE_PATH}/hello.mjml -o #{TEMPLATE_PATH}/hello.min.html --min`
end

Expand Down
53 changes: 40 additions & 13 deletions lib/mjml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ def initialize
end

def call(template)
exec!(template)
@template = template
exec!
rescue InvalidTemplate
nil
end

def call!(template)
exec!(template)
@template = template
exec!
end

private

def exec!(template)
MJML.logger.debug("Template:\n #{template}")
raise InvalidTemplate if template.empty?
def exec!
MJML.logger.debug("Template:\n #{@template}")
raise InvalidTemplate if @template.empty?

MJML.logger.debug("Partial: #{partial?(template)}")
return template if partial?(template)
MJML.logger.debug("Partial: #{partial?}")
return @template if partial?

out, err, _sts = Open3.capture3(cmd, stdin_data: template)
out, err = should_get_outpout_from_file? ? output_from_file : output_from_memory
parsed = parse_output(out)

MJML.logger.debug("Output:\n #{parsed[:output]}")
Expand All @@ -44,16 +46,24 @@ def exec!(template)
parsed[:output]
end

def partial?(template)
(template =~ ROOT_TAGS_REGEX).nil?
def partial?
(@template =~ ROOT_TAGS_REGEX).nil?
end

def mjml_bin
MJML.config.bin_path
end

def cmd
"#{mjml_bin} #{minify_output} #{validation_level} -is"
def cmd(file_path = nil)
"#{mjml_bin} #{minify_output} #{validation_level} #{cmd_options}"
end

def cmd_options
if should_get_outpout_from_file?
"-i -o #{@temp_file.path}"
else
"-is"
end
end

def minify_output
Expand All @@ -64,11 +74,28 @@ def validation_level
"--level=#{MJML.config.validation_level}" if MJML::Feature.available?(:validation_level)
end

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

def output_from_memory
out, err, _sts = Open3.capture3(cmd, stdin_data: @template)
return out, err
end

def should_get_outpout_from_file?
@template.size > 20_000
end

def parse_output(out)
warnings = []
output = []

out.lines.each do |l|
out.lines.each do |l|
if l.strip.start_with?('Line')
warnings << l
else
Expand Down
Loading

0 comments on commit 325601e

Please sign in to comment.