Class: Paggio::HTML

Inherits:
BasicObject
Defined in:
lib/paggio/html.rb,
lib/paggio/format.rb,
lib/paggio/html/element.rb

Defined Under Namespace

Classes: Element

Constant Summary collapse

Format =
::Paggio::Format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = 5, &block) ⇒ HTML

Returns a new instance of HTML.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/paggio/html.rb', line 18

def initialize(version = 5, &block)
  ::Kernel.raise ::ArgumentError, 'no block given' unless block

  @version = version
  @roots   = []
  @current = nil

  if block.arity == 0
    instance_exec(&block)
  else
    block.call(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/paggio/html.rb', line 74

def method_missing(name, *args, &block)
  if name.to_s.end_with? ?!
    return super
  end

  if ::String === args.first
    content = args.shift
  end

  element = Element.new(self, name, *args)
  element << content if content

  if block
    parent   = @current
    @current = element
    result   = block.call(self)
    @current = parent

    if ::String === result
      element.inner_html = result
    end
  end

  (@current || @roots) << element

  element
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/paggio/html.rb', line 16

def version
  @version
end

Instance Method Details

#<<(what) ⇒ Object



32
33
34
# File 'lib/paggio/html.rb', line 32

def <<(what)
  @current << what
end

#each(&block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/paggio/html.rb', line 62

def each(&block)
  return enum_for :each unless block

  @roots.each(&block)

  self
end

#element!Object



44
45
46
# File 'lib/paggio/html.rb', line 44

def element!
  @current
end

#extend!(element = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/paggio/html.rb', line 48

def extend!(element = nil, &block)
  old, @current = @current, element

  result = block.call(self)

  if ::String === result
    @current.inner_html = result
  end

  @current = old

  self
end

#format(io = ::StringIO.new, options = { indent: 0 }) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/paggio/format.rb', line 30

def format(io = ::StringIO.new, options = { indent: 0 })
  case @version
  when 5
    Format.puts io, "<!DOCTYPE html>", options[:indent]
  end

  Format.puts io, "<html>", options[:indent]

  each {|root|
    root.format(io, indent: options[:indent] + 1)
  }

  Format.puts io, "</html>", options[:indent]

  io
end

#inspectObject



102
103
104
105
106
107
108
# File 'lib/paggio/html.rb', line 102

def inspect
  if @roots.empty?
    "#<HTML(#@version)>"
  else
    "#<HTML(#@version): #{@roots.inspect[1 .. -2]}>"
  end
end

#root!Object



36
37
38
# File 'lib/paggio/html.rb', line 36

def root!
  @roots.first
end

#roots!Object



40
41
42
# File 'lib/paggio/html.rb', line 40

def roots!
  @roots
end

#style(&block) ⇒ Object



70
71
72
# File 'lib/paggio/html.rb', line 70

def style(&block)
  (@current || @roots) << CSS.new(&block)
end