Class: Antwort::PartialBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/antwort/builder/partial.rb

Instance Attribute Summary collapse

Attributes inherited from Builder

#asset_server, #build_dir, #build_id, #css, #css_style, #markup_dir, #scss_dir, #source_dir, #template

Instance Method Summary collapse

Methods inherited from Builder

#build, #build_css!, #compile_scss, #create_build_directories!, #flatten_inlined_css, #initialize, #load_css, #use_asset_server

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

This class inherits a constructor from Antwort::Builder

Instance Attribute Details

#partialsObject (readonly)

Returns the value of attribute partials.



3
4
5
# File 'lib/antwort/builder/partial.rb', line 3

def partials
  @partials
end

Instance Method Details

#adjust_filename(filename) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/antwort/builder/partial.rb', line 48

def adjust_filename(filename)
  filename = @template.name if filename == 'index.html.erb'

  name = filename.gsub('.erb', '')
  name << '.html' unless name[-5, 5] == '.html'
  name = '_' << name unless name[0] == '_'
  name
end

#build!Object



17
18
19
20
# File 'lib/antwort/builder/partial.rb', line 17

def build!
  @css = load_css
  partials.each { |t| build_html t }
end

#build_html(partial_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/antwort/builder/partial.rb', line 22

def build_html(partial_name)
  source_file = "#{source_dir}/#{partial_name}"
  source      = File.read(source_file)
  markup      = preserve_logic(source)
  markup      = preserve_nbsps(markup)
  markup      = preserve_operators_from_nokogiri(markup)
  inlined     = inline(markup)
  inlined     = restore_nbsps(inlined)
  inlined     = flatten_inlined_css(inlined)
  filename    = adjust_filename(partial_name)
  create_file!(content: inlined, path: "#{build_dir}/#{filename}")
end

#cleanup(html = '') ⇒ Object



57
58
59
60
61
62
# File 'lib/antwort/builder/partial.rb', line 57

def cleanup(html = '')
  code = remove_extra_dom(html)
  code = cleanup_logic(code)
  code = restore_variables_in_links(code)
  code
end

#inline(markup) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/antwort/builder/partial.rb', line 35

def inline(markup)
  # Force a complete DOM tree before inlining for nokogiri
  # Otherwise we have random <p> at beginning of document
  html = add_nokogiri_wrapper(markup)

  document = Roadie::Document.new html
  document.add_css(css)
  inlined  = document.transform
  inlined  = cleanup(inlined)

  remove_nokogiri_wrapper(inlined)
end

#post_initializeObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/antwort/builder/partial.rb', line 5

def post_initialize(*)
  @partials = @template.partials.push('index.html.erb')

  if partials.empty?
    say 'Error: ', :red
    puts "No partials found in #{template.name} folder."
    return
  else
    create_build_directories!
  end
end