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

#amazon_button(asin) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/mill/html_helpers.rb', line 63

def amazon_button(asin)
  html_fragment do |html|
    html.a(href: "http://www.amazon.com/dp/#{asin}") do
      html.img(src: '/images/buy1._V46787834_.gif', alt: 'Buy from Amazon.com')
    end
  end
end

#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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mill/html_helpers.rb', line 97

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


116
117
118
119
120
121
122
123
# File 'lib/mill/html_helpers.rb', line 116

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

#paypal_button(id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mill/html_helpers.rb', line 71

def paypal_button(id)
  html_fragment do |html|
    html.form(action: 'https://www.paypal.com/cgi-bin/webscr', method: 'post') do
      html.input(
        type: 'hidden',
        name: 'cmd',
        value: '_s-xclick')
      html.input(
        type: 'hidden',
        name: 'hosted_button_id',
        value: id)
      html.input(
        type: 'image',
        src: 'https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif',
        name: 'submit',
        alt: 'PayPal - The safer, easier way to pay online!')
      html.img(
        alt: '',
        border: 0,
        width: 1,
        height: 1,
        src: 'https://www.paypalobjects.com/en_US/i/scr/pixel.gif')
    end
  end
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