class MailMatic::Generator

Constants

DEFAULT_LAYOUT_SUBPATH
DEFAULT_PAGE_SUBPATH
EMAILS_SUBDIR
PREMAILER_WARN_LEVEL
STATICMATIC_BUILD_COMMAND
STATICMATIC_OUTPUT_SUBDIR
STATICMATIC_SETUP_COMMAND

Attributes

root_dir[RW]

Public Class Methods

new(root_dir) click to toggle source
# File lib/mailmatic.rb, line 15
def initialize(root_dir)
  @root_dir = root_dir
end

Public Instance Methods

build() click to toggle source
# File lib/mailmatic.rb, line 91
def build
  puts "Building #{root_dir}"

  status = generate_pages
  return status if status != 0

  status = generate_emails

  return status
end
generate_email(infile, outfile) click to toggle source
# File lib/mailmatic.rb, line 19
def generate_email(infile, outfile)
  outdir = File.dirname(outfile)
  Dir.mkdir(outdir) if !File.directory?(outdir)

  premailer = Premailer.new(
    infile,
    :warn_level => PREMAILER_WARN_LEVEL
  )

  File.open(outfile, "wb") do |f|
    f << premailer.to_inline_css
  end
  puts "created #{outfile}"

  if premailer.warnings.any?
    puts
    puts "WARNING: #{outfile}"
    puts "-" * 79
    premailer.warnings.each do |w|
      puts "  [#{w[:level]}] #{w[:message]} may not render properly in #{w[:clients]}"
    end
    puts
  end

  return 0
rescue Exception => e
  puts "failed to create #{outfile}"
  puts e.inspect
  return -1
end
generate_emails() click to toggle source
# File lib/mailmatic.rb, line 50
def generate_emails
  html_dir   = File.expand_path(STATICMATIC_OUTPUT_SUBDIR, root_dir)
  emails_dir = File.expand_path(EMAILS_SUBDIR, root_dir)
  Dir.mkdir(emails_dir) if !File.directory?(emails_dir)
  Dir.chdir(html_dir) do
    Dir.glob("**/*.html").each do |html_file|
      email_file = File.expand_path(html_file, "../#{EMAILS_SUBDIR}")
      status = generate_email(html_file, email_file)
      return status if status != 0
    end
  end
  return 0
end
generate_pages() click to toggle source
# File lib/mailmatic.rb, line 64
def generate_pages
  result = system(STATICMATIC_BUILD_COMMAND % root_dir)
  return result ? 0 : -1
end
setup() click to toggle source
# File lib/mailmatic.rb, line 69
def setup
  # Run StaticMatic setup then hack a few files with sed
  result = system(STATICMATIC_SETUP_COMMAND % root_dir)
  return -1 unless result

  i_opt = "-i"
  if `uname` =~ /darwin/i
    i_opt = '-i ""'
  end

  result = system("sed #{i_opt} -e \"s/StaticMatic/MailMatic/g\" \"#{root_dir}/#{DEFAULT_LAYOUT_SUBPATH}\"")
  return -1 unless result

  result = system("sed #{i_opt} -e \"s/= stylesheets/%link\\{:rel => 'stylesheet', :href => 'stylesheets\\/screen.css'\\}/g\" \"#{root_dir}/#{DEFAULT_LAYOUT_SUBPATH}\"")
  return -1 unless result

  result = system("sed #{i_opt} -e \"s/StaticMatic/MailMatic/g\" \"#{root_dir}/#{DEFAULT_PAGE_SUBPATH}\"")
  return -1 unless result

  return 0
end