Class: Antwort::Builder

Inherits:
Object
  • Object
show all
Includes:
FileHelpers, Helpers, LogicHelpers, MarkupSanitizers, Thor::Shell
Defined in:
lib/antwort/builder/builder.rb

Direct Known Subclasses

EmailBuilder, PartialBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MarkupSanitizers

#add_included_css, #remove_excessive_newlines, #remove_extra_dom, #remove_livereload, #remove_roadie_flags

Methods included from LogicHelpers

#cleanup_logic, #convert_helper_wrappers, #convert_partials_to_includes, #preserve_assignments, #preserve_comments, #preserve_conditionals, #preserve_leftover_statements, #preserve_logic, #preserve_loops, #preserve_nbsps, #preserve_variables, #restore_nbsps, #restore_variables_in_links

Methods included from FileHelpers

#count_files, #create_file!, #email_id_from_folder_name, #list_folders

Methods included from Helpers

#symbolize_keys!

Constructor Details

#initialize(attrs = {}) ⇒ Builder

Returns a new instance of Builder.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/antwort/builder/builder.rb', line 16

def initialize(attrs = {})
  attrs = symbolize_keys!(attrs)

  @build_id = attrs[:id]
  @template = EmailTemplate.new(attrs[:email])

  @build_dir     = "./build/#{template.name}-#{build_id}"
  @markup_dir    = "#{build_dir}/source"
  @source_dir    = "./emails/#{template.name}"
  @scss_dir      = "./assets/css/#{template.name}"
  @css_style     = attrs[:'css-style'].to_sym
  @asset_server  = ENV['ASSET_SERVER'] || '/assets'

  post_initialize(attrs)
end

Instance Attribute Details

#asset_serverObject (readonly)

Returns the value of attribute asset_server.



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

def asset_server
  @asset_server
end

#build_dirObject (readonly)

Returns the value of attribute build_dir.



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

def build_dir
  @build_dir
end

#build_idObject (readonly)

Returns the value of attribute build_id.



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

def build_id
  @build_id
end

#cssObject (readonly)

Returns the value of attribute css.



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

def css
  @css
end

#css_styleObject (readonly)

Returns the value of attribute css_style.



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

def css_style
  @css_style
end

#markup_dirObject (readonly)

Returns the value of attribute markup_dir.



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

def markup_dir
  @markup_dir
end

#scss_dirObject (readonly)

Returns the value of attribute scss_dir.



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

def scss_dir
  @scss_dir
end

#source_dirObject (readonly)

Returns the value of attribute source_dir.



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

def source_dir
  @source_dir
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#buildObject



43
44
# File 'lib/antwort/builder/builder.rb', line 43

def build
end

#build_css!Object



53
54
55
56
57
# File 'lib/antwort/builder/builder.rb', line 53

def build_css!
  compile_scss(source: "#{scss_dir}/inline.scss", destination: "#{markup_dir}/inline.css")
  compile_scss(source: "#{scss_dir}/include.scss", destination: "#{markup_dir}/include.css")
  @css = load_css
end

#compile_scss(attrs = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/antwort/builder/builder.rb', line 59

def compile_scss(attrs = {})
  source_file      = attrs[:source]
  destination_file = attrs[:destination]

  if File.file? source_file
    content = Tilt::ScssTemplate.new(source_file, style: @css_style).render
    create_file!(content: content, path: destination_file)
  else
    say 'Build failed. ', :red
    say "#{source_file}.scss for #{template.name} not found."
  end
end

#create_build_directories!Object



36
37
38
39
40
41
# File 'lib/antwort/builder/builder.rb', line 36

def create_build_directories!
  return if Dir.exist? build_dir
  Dir.mkdir 'build' unless Dir.exist? './build'
  Dir.mkdir(build_dir)
  Dir.mkdir("#{build_dir}/source")
end

#flatten_inlined_css(markup) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/antwort/builder/builder.rb', line 78

def flatten_inlined_css(markup)
  copy = ''

  # loop through lines so we have the line number
  markup.lines.each_with_index do |line, i|
    f = Flattener.new(line).flatten
    if f.flattened?
      say " flattened  CSS #{f.flattened_keys} ", :yellow
      say "#{i}: #{f.source.strip}"
      copy << f.flattened
    else
      copy << f.source
    end
  end
  copy
end

#load_cssObject



46
47
48
49
50
51
# File 'lib/antwort/builder/builder.rb', line 46

def load_css
  css_file = "#{markup_dir}/inline.css"
  build_css unless File.file? css_file
  css = File.read(css_file)
  css
end

#post_initializeObject



32
33
34
# File 'lib/antwort/builder/builder.rb', line 32

def post_initialize(*)
  nil
end

#use_asset_server(markup = '') ⇒ Object



72
73
74
75
76
# File 'lib/antwort/builder/builder.rb', line 72

def use_asset_server(markup = '')
  replaced = "#{asset_server}/"
  output = markup.gsub('/assets/', replaced)
  output
end