Class: Simple::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-builder/helpers.rb,
lib/simple-builder/parsers.rb,
lib/simple-builder/builders.rb,
lib/simple-builder/fragments.rb

Direct Known Subclasses

HTMLDocument, HTMLFragment

Defined Under Namespace

Classes: HTML4Document, HTML5Document, HTMLDocument, HTMLFragment, ParseError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_html(&block) ⇒ Object



13
14
15
# File 'lib/simple-builder/builders.rb', line 13

def self.build_html(&block)
  HTMLFragment.new.build_html(&block)
end

.build_html4_document(&block) ⇒ Object



5
6
7
# File 'lib/simple-builder/builders.rb', line 5

def self.build_html4_document(&block)
  HTML4Document.new.build_html(&block)
end

.build_html5_document(&block) ⇒ Object



9
10
11
# File 'lib/simple-builder/builders.rb', line 9

def self.build_html5_document(&block)
  HTML5Document.new.build_html(&block)
end

.check_html_errors(doc) ⇒ Object



17
18
19
20
21
# File 'lib/simple-builder/parsers.rb', line 17

def check_html_errors(doc)
  doc.errors.each do |error|
    raise ParseError, "HTML error #{error}"
  end
end

.find_body_element(doc) ⇒ Object



15
16
17
# File 'lib/simple-builder/helpers.rb', line 15

def find_body_element(doc)
  doc.at_xpath('/html/body')
end

.find_head_element(doc) ⇒ Object



11
12
13
# File 'lib/simple-builder/helpers.rb', line 11

def find_head_element(doc)
  doc.at_xpath('/html/head')
end


7
8
9
# File 'lib/simple-builder/helpers.rb', line 7

def find_link_element_attributes(doc)
  doc.xpath('//@href | //@src')
end

.find_meta_info(doc) ⇒ Object



23
24
25
26
27
# File 'lib/simple-builder/helpers.rb', line 23

def find_meta_info(doc)
  find_head_element(doc).xpath('meta[@name]').map do |meta|
    [meta['name'], meta['content']]
  end.to_h
end

.find_p_child_elements(doc) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/simple-builder/helpers.rb', line 29

def find_p_child_elements(doc)
  if (p = doc.at_xpath('p'))
    p.children
  else
    doc
  end
end

.find_title_element(doc) ⇒ Object



19
20
21
# File 'lib/simple-builder/helpers.rb', line 19

def find_title_element(doc)
  find_head_element(doc).at_xpath('title')
end

.parse_html_document(html) ⇒ Object



9
10
11
# File 'lib/simple-builder/parsers.rb', line 9

def parse_html_document(html)
  Nokogiri::HTML5::Document.parse(html).tap { |doc| check_html_errors(doc) }
end

.parse_html_fragment(html) ⇒ Object



13
14
15
# File 'lib/simple-builder/parsers.rb', line 13

def parse_html_fragment(html)
  Nokogiri::HTML5::DocumentFragment.parse(html).tap { |doc| check_html_errors(doc) }
end

.replace_element(doc, xpath, &block) ⇒ Object



37
38
39
40
41
# File 'lib/simple-builder/helpers.rb', line 37

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

.script(src = nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple-builder/fragments.rb', line 25

def script(src=nil, &block)
  build_html do |html|
    if src
      html.script(src: src, type: 'text/javascript')
    elsif block_given?
      html.script(type: 'text/javascript') do
        html << yield
      end
    end
  end
end

.string_to_html(str) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/simple-builder/helpers.rb', line 43

def string_to_html(str)
  find_p_child_elements(
    parse_html_fragment(
      RubyPants.new(str).to_html
    )
  ).to_html
end

.style(href = nil, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple-builder/fragments.rb', line 13

def style(href=nil, &block)
  build_html do |html|
    if href
      html.link(href: href, rel: 'stylesheet', type: 'text/css')
    elsif block_given?
      html.style(type: 'text/css') do
        html << yield
      end
    end
  end
end

.viewportObject



7
8
9
10
11
# File 'lib/simple-builder/fragments.rb', line 7

def viewport
  build_html do |html|
    html.meta(name: 'viewport', content: 'width=device-width, initial-scale=1')
  end
end

Instance Method Details

#build_html(&block) ⇒ Object



17
18
19
20
21
22
# File 'lib/simple-builder/builders.rb', line 17

def build_html(&block)
  Nokogiri::HTML::Builder.with(@root) do |builder|
    yield(builder)
  end
  @root
end