Class: Mjml::Parser
- Inherits:
-
Object
- Object
- Mjml::Parser
- Defined in:
- lib/mjml/parser.rb
Defined Under Namespace
Classes: ParseError
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Instance Method Summary collapse
-
#initialize(input) ⇒ Parser
constructor
Create new parser.
-
#render ⇒ String
Render mjml template.
-
#run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict') ⇒ String
Exec mjml command.
Constructor Details
#initialize(input) ⇒ Parser
Create new parser
12 13 14 15 16 |
# File 'lib/mjml/parser.rb', line 12 def initialize(input) raise Mjml.mjml_binary_error_string unless Mjml.valid_mjml_binary @input = input end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/mjml/parser.rb', line 7 def input @input end |
Instance Method Details
#render ⇒ String
Render mjml template
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mjml/parser.rb', line 21 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 StandardError raise if Mjml.raise_render_exception '' ensure in_tmp_file&.unlink end |
#run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict') ⇒ String
Exec mjml command
rubocop:disable Style/OptionalBooleanParameter: Fixing this offense would imply a change in the public API.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mjml/parser.rb', line 39 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) unless status.success? # The process status ist quite helpful in case of dying processes without STDERR output. # Node exit codes are documented here: https://node.readthedocs.io/en/latest/api/process/#exit-codes raise ParseError, "#{stderr.chomp}\n(process status: #{status})" end Mjml.logger.warn(stderr.chomp) if stderr.present? out_tmp_file.read end end |