class Malvolio::Compiler

Attributes

compiler[R]
config[R]
no_warnings[R]
path[R]

Public Class Methods

new(path = nil, no_warnings = false) click to toggle source
# File lib/malvolio/compiler.rb, line 8
def initialize(path = nil, no_warnings = false)
        @path = File.expand_path(path || ".")
        @config = YAML.load_file(File.join(@path, "config.yaml"))
        @compiler = Sass::Plugin::Compiler.new
        @compiler.on_compilation_error do |error, template, _css|
                raise Malvolio::CompilationError, compilation_error(error, template)
        end
        @no_warnings = no_warnings
end

Public Instance Methods

run!() click to toggle source
# File lib/malvolio/compiler.rb, line 18
def run!
        convert_html                 
        compile_sass_to_css
        inline_styles
        clean_tmp_folder
        print_colorized("New HTML email build was a success!", 32)
end

Private Instance Methods

clean_tmp_folder() click to toggle source
# File lib/malvolio/compiler.rb, line 61
def clean_tmp_folder
        remove_path = Dir[File.join(path, "tmp", "*")]
        FileUtils.rm_rf(remove_path)
end
compilation_error(error, template) click to toggle source
# File lib/malvolio/compiler.rb, line 66
def compilation_error(error, template)
        "Sass failed to compile. Error found in #{template}\n#{error}"
end
compile_sass_to_css() click to toggle source
# File lib/malvolio/compiler.rb, line 42
def compile_sass_to_css
        src_path = File.join(path, "src", "scss", "index.scss")
        dest_path = File.join(path, "tmp", "index.css")
        compiler.update_stylesheets([[src_path, dest_path]])
end
convert_html() click to toggle source
# File lib/malvolio/compiler.rb, line 30
def convert_html
        src_path = File.join(path, "src", "index.html")
        dest_path = File.join(path, "tmp", "index.html")
        if config["inky"]
                html_string = File.read(src_path)
                html_output = Inky::Core.new.release_the_kraken(html_string)
                File.write(dest_path, html_output)
        else
                FileUtils.cp(src_path, dest_path)
        end
end
inline_styles() click to toggle source
# File lib/malvolio/compiler.rb, line 48
def inline_styles
        src_path = File.join(path, "tmp", "index.html")
        dist_path = File.join(path, "dist", "index.html")
        warn_level = no_warnings ? Premailer::Warnings::NONE : Premailer::Warnings::SAFE
        premailer = Premailer.new(src_path, warn_level: warn_level)
        File.open(dist_path, "w") do |file|
                file.puts premailer.to_inline_css
        end
        premailer.warnings.each do |warning|
                print_premailer_warning(warning)
        end
end
premailer_risk_color_codes() click to toggle source
# File lib/malvolio/compiler.rb, line 81
def premailer_risk_color_codes
        {"SAFE" => 32, "NONE" => 32, "POOR" => 33, "RISKY" => 31}
end
print_colorized(string, code) click to toggle source
print_premailer_warning(warning) click to toggle source