Skip to content

Commit

Permalink
Merge pull request #1 from sighmon/clairezed-add-fonts-config
Browse files Browse the repository at this point in the history
#111 Suggestion for handling fonts
  • Loading branch information
clairezed authored Apr 4, 2024
2 parents 3e6b629 + d8a57db commit 77815ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/mjml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Mjml
self.minify = false
self.validation_level = 'strict'
self.use_mrml = false
self.fonts = nil

def self.check_version(bin)
stdout, _, status = run_mjml('--version', mjml_bin: bin)
Expand Down
29 changes: 15 additions & 14 deletions lib/mjml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def render
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)
run(in_tmp_file.path)
rescue StandardError
raise if Mjml.raise_render_exception

Expand All @@ -35,17 +35,9 @@ def render
# Exec mjml command
#
# @return [String] The result as string
# rubocop:disable Style/OptionalBooleanParameter: Fixing this offense would imply a change in the public API.
# rubocop:disable Metrics/MethodLength:
# rubocop:disable Metrics/ParameterLists:
def run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict', options = {})
def run(in_tmp_file)
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}"
command += " --config.fonts '#{options[:fonts].to_json}'" if options[:fonts].present?

_, stderr, status = Mjml.run_mjml(command)
_, 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.
Expand All @@ -57,8 +49,17 @@ def run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict
out_tmp_file.read
end
end
# rubocop:enable Metrics/ParameterLists
# rubocop:enable Metrics/MethodLength
# rubocop:enable Style/OptionalBooleanParameter

# Build command string from config variables
#
# @return [String] Command string
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
end
end
2 changes: 2 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
expect(Mjml.beautify).must_equal(true)
expect(Mjml.minify).must_equal(false)
expect(Mjml.validation_level).must_equal('strict')
expect(Mjml.fonts).must_equal(nil)
end

it 'uses setup config' do
Expand All @@ -61,6 +62,7 @@
config.beautify = true
config.minify = false
config.validation_level = 'strict'
config.fonts = nil
end
end
end
Expand Down

0 comments on commit 77815ea

Please sign in to comment.