Class: MailMatic::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/mailmatic.rb

Constant Summary collapse

STATICMATIC_SETUP_COMMAND =
"staticmatic setup \"%s\""
STATICMATIC_BUILD_COMMAND =
"staticmatic build \"%s\""
STATICMATIC_OUTPUT_SUBDIR =
"site"
EMAILS_SUBDIR =
"emails"
DEFAULT_LAYOUT_SUBPATH =
"src/layouts/default.haml"
DEFAULT_PAGE_SUBPATH =
"src/pages/index.haml"
PREMAILER_WARN_LEVEL =
Premailer::Warnings::SAFE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Generator

Returns a new instance of Generator.



15
16
17
# File 'lib/mailmatic.rb', line 15

def initialize(root_dir)
  @root_dir = root_dir
end

Instance Attribute Details

#root_dirObject

Returns the value of attribute root_dir.



14
15
16
# File 'lib/mailmatic.rb', line 14

def root_dir
  @root_dir
end

Instance Method Details

#buildObject



91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_emailsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# 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_pagesObject



64
65
66
67
# File 'lib/mailmatic.rb', line 64

def generate_pages
  result = system(STATICMATIC_BUILD_COMMAND % root_dir)
  return result ? 0 : -1
end

#setupObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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