Module: HTMLHelpers

Included in:
Mill::Resource::Feed, Mill::Resource::Image, Mill::Resource::Index, Mill::Resource::Text
Defined in:
lib/mill/html_helpers.rb

Defined Under Namespace

Classes: PreText

Constant Summary collapse

LinkElementsXPath =
'//@href | //@src'

Instance Method Summary collapse

Instance Method Details

#check_errors(html) ⇒ Object



47
48
49
50
51
# File 'lib/mill/html_helpers.rb', line 47

def check_errors(html)
  html.errors.each do |error|
    raise Mill::Error, "HTML error #{error}" unless error.message =~ /Tag .+? invalid$/
  end
end


53
54
55
# File 'lib/mill/html_helpers.rb', line 53

def find_link_elements(html)
  html.xpath(LinkElementsXPath)
end

#google_analytics(tracker_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mill/html_helpers.rb', line 63

def google_analytics(tracker_id)
  html_fragment do |html|
    html.script(type: 'text/javascript') do
      html << %Q{
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
      }
    end
    html.script(type: 'text/javascript') do
      html << %Q{
        try {
          var pageTracker = _gat._getTracker("#{tracker_id}");
          pageTracker._trackPageview();
        } catch(err) {}
      }
    end
  end
end

#html_document(type = :html4_transitional, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mill/html_helpers.rb', line 5

def html_document(type=:html4_transitional, &block)
  doc = Nokogiri::HTML::Document.new
  doc.encoding = 'UTF-8'
  doc.internal_subset.remove
  case type
  when :html4_transitional
    doc.create_internal_subset('html', '-//W3C//DTD HTML 4.01 Transitional//EN', 'http://www.w3.org/TR/html4/loose.dtd')
  when :html5
    doc.create_internal_subset('html', nil, nil)
  else
    raise "Unknown HTML type: #{type.inspect}"
  end
  Nokogiri::HTML::Builder.with(doc) do |doc|
    yield(doc)
  end
  doc
end

#html_fragment(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mill/html_helpers.rb', line 23

def html_fragment(&block)
  html = Nokogiri::HTML::DocumentFragment.parse('')
  Nokogiri::HTML::Builder.with(html) do |html|
    yield(html) if block_given?
  end
  html
end


82
83
84
85
86
87
88
89
# File 'lib/mill/html_helpers.rb', line 82

def link_if(state, html, &block)
  elem = html_fragment { |h| yield(h) }
  if state
    html.a(href: uri) { html << elem.to_html }
  else
    html << elem.to_html
  end
end

#parse_html(str) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/mill/html_helpers.rb', line 31

def parse_html(str)
  if str.strip.empty?
    html = html_fragment
  else
    html = Nokogiri::HTML::Document.parse(str) { |config| config.strict }
    check_errors(html)
  end
  html
end

#parse_html_fragment(str) ⇒ Object



41
42
43
44
45
# File 'lib/mill/html_helpers.rb', line 41

def parse_html_fragment(str)
  html = Nokogiri::HTML::DocumentFragment.parse(str) { |config| config.strict }
  check_errors(html)
  html
end

#replace_element(html, xpath, &block) ⇒ Object



57
58
59
60
61
# File 'lib/mill/html_helpers.rb', line 57

def replace_element(html, xpath, &block)
  html.xpath(xpath).each do |elem|
    elem.replace(yield(elem))
  end
end