Class: FrontEndTasks::Documents::HtmlDocument

Inherits:
BaseDocument
  • Object
show all
Defined in:
lib/front_end_tasks/documents/html_document.rb

Instance Attribute Summary

Attributes inherited from BaseDocument

#compiled_path

Instance Method Summary collapse

Constructor Details

#initialize(public_root, content) ⇒ HtmlDocument

Returns a new instance of HtmlDocument.



10
11
12
13
# File 'lib/front_end_tasks/documents/html_document.rb', line 10

def initialize(public_root, content)
  super(public_root, content)
  @doc = Nokogiri::HTML(content)
end

Instance Method Details

#compileObject



15
16
17
18
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/front_end_tasks/documents/html_document.rb', line 15

def compile
  path_content_pairs = {}

  script_groups.each do |group|
    combined_content = group[:files].inject('') { |content, file| content << File.read(file) }
    combined_file_path = group[:combined_file_path]
    js_document = JsDocument.new(@public_root, combined_content)
    js_document.compiled_path = combined_file_path
    new_files = js_document.compile

    script_node = Nokogiri::XML::Node.new("script", @doc)
    script_node[:src] = combined_file_path
    replace_group(group, script_node)

    path_content_pairs.merge!(new_files)
  end

  style_groups.each do |group|
    combined_file_path = group[:combined_file_path]
    combined_content = ''

    group[:files].each do |file|
      content = File.read(file)
      updated_content, assets = CssDocument.find_and_update_url_references(File.dirname(file), File.dirname(combined_file_path), content)

      assets.each do |asset|
        new_files = asset.compile
        path_content_pairs.merge!(new_files)
      end

      combined_content << updated_content
    end

    css_document = CssDocument.new(@public_root, combined_content)
    css_document.compiled_path = combined_file_path
    new_files = css_document.compile

    link_node = Nokogiri::XML::Node.new("link", @doc)
    link_node[:href] = combined_file_path
    link_node[:rel] = "stylesheet"
    replace_group(group, link_node)

    path_content_pairs.merge!(new_files)
  end

  comments.each { |c| c.remove }

  path_content_pairs.merge!({
    @compiled_path => @doc.to_html.gsub(/\n\s*\n/, "\n")
  })

  path_content_pairs
end

#included_scripts(public_root = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/front_end_tasks/documents/html_document.rb', line 69

def included_scripts(public_root = nil)
  script_nodes = @doc.xpath('//script')
  script_nodes.map { |n| n[:src] }
  script_nodes.map do |node|
    if public_root
      File.expand_path(File.join(public_root, node[:src]))
    else
      node[:src]
    end
  end
end