Module: ThemeBandit::HTMLParser

Included in:
DocumentWriter
Defined in:
lib/theme_bandit/parser/html.rb

Instance Method Summary collapse

Instance Method Details



64
65
66
67
68
# File 'lib/theme_bandit/parser/html.rb', line 64

def inject_link_nodes
  link_nodes.each do |node|
    document.search('head').first.add_child(node)
  end
end

#inject_script_nodesObject



70
71
72
73
74
# File 'lib/theme_bandit/parser/html.rb', line 70

def inject_script_nodes
  script_nodes.each do |node|
    document.search('head').first.add_child(node)
  end
end


43
44
45
46
47
48
49
50
51
52
# File 'lib/theme_bandit/parser/html.rb', line 43

def link_nodes
  [].tap do |arr|
    local_link_names.each do |link_path|
      link = Nokogiri::XML::Node.new 'link', document
      link.set_attribute('rel', 'stylesheet')
      link.set_attribute('href', "#{link_path}")
      arr << link
    end
  end
end


29
30
31
32
33
34
# File 'lib/theme_bandit/parser/html.rb', line 29

def local_link_names
  path = "#{Dir.pwd}/theme/public/css/"
  Dir.entries(path).map do |file_name|
    "#{path}#{file_name}" if file_name['css']
  end.compact
end

#local_script_namesObject



36
37
38
39
40
41
# File 'lib/theme_bandit/parser/html.rb', line 36

def local_script_names
  path = "#{Dir.pwd}/theme/public/js/"
  Dir.entries(path).map do |file_name|
    "#{path}#{file_name}" if file_name['js']
  end.compact.sort
end

#remove_base_tagsObject



11
12
13
# File 'lib/theme_bandit/parser/html.rb', line 11

def remove_base_tags
  document.search('base').remove
end


15
16
17
18
19
20
21
# File 'lib/theme_bandit/parser/html.rb', line 15

def remove_link_tags
  document.search('link').each do |node|
    if node[:rel] == 'stylesheet'
      node.remove
    end
  end
end

#remove_script_tagsObject



23
24
25
26
27
# File 'lib/theme_bandit/parser/html.rb', line 23

def remove_script_tags
  document.search('script').each do |node|
    node.remove if node[:src]
  end
end

#revise_head_tagsObject



3
4
5
6
7
8
9
# File 'lib/theme_bandit/parser/html.rb', line 3

def revise_head_tags
  remove_base_tags
  remove_link_tags
  remove_script_tags
  inject_link_nodes
  inject_script_nodes
end

#script_nodesObject



54
55
56
57
58
59
60
61
62
# File 'lib/theme_bandit/parser/html.rb', line 54

def script_nodes
  [].tap do |arr|
    local_script_names.each do |script_path|
      script = Nokogiri::XML::Node.new 'script', document
      script.set_attribute('src', "#{script_path}")
      arr << script
    end
  end
end