Class: Paggio::HTML

Inherits:
BasicObject
Defined in:
lib/paggio/css.rb,
lib/paggio/html.rb,
lib/paggio/script.rb,
lib/paggio/markdown.rb,
lib/paggio/html/element.rb,
lib/paggio/html/helpers.rb,
lib/paggio/html/element/a.rb,
lib/paggio/html/element/td.rb,
lib/paggio/html/element/img.rb,
lib/paggio/html/element/base.rb,
lib/paggio/html/element/link.rb,
lib/paggio/html/element/embed.rb,
lib/paggio/html/element/input.rb,
lib/paggio/html/element/button.rb,
lib/paggio/html/element/canvas.rb,
lib/paggio/html/element/object.rb,
lib/paggio/html/element/option.rb,
lib/paggio/html/element/select.rb,
lib/paggio/html/element/optgroup.rb,
lib/paggio/html/element/blockquote.rb

Defined Under Namespace

Classes: Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HTML.



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

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

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

  @block = block

  build! unless defer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object Also known as: e



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/paggio/html.rb', line 79

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

  unless args.empty? || ::Hash === args.first
    content = ::Paggio::Utils.heredoc(args.shift.to_s)
  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.instance_eval { @inner_html = result }
    end
  end

  self << element

  element
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#<<(what) ⇒ Object



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

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

#build!(force_call: false) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/paggio/html.rb', line 35

def build!(force_call: false)
  if !force_call && @block.arity == 0
    instance_exec(&@block)
  else
    @block.call(self)
  end
  @block = nil
end

#each(&block) ⇒ Object



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

def each(&block)
  @roots.each(&block)
end

#element!Object



52
53
54
# File 'lib/paggio/html.rb', line 52

def element!
  @current
end

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/paggio/html.rb', line 56

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

  result = block.call(self)

  if ::String === result
    @current.instance_eval { @inner_html = result }
  end

  @current = old

  self
end

#inspectObject



110
111
112
113
114
115
116
# File 'lib/paggio/html.rb', line 110

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

#local_variablesObject



37
38
39
# File 'lib/paggio/script.rb', line 37

def local_variables(*)
  []
end

#markdown(string) ⇒ Object



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

def markdown(string)
  (@current || @roots) << ::Kramdown::Document.new(
    ::Paggio::Utils.heredoc(string))
end

#root!Object



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

def root!
  @roots.first
end

#roots!Object



48
49
50
# File 'lib/paggio/html.rb', line 48

def roots!
  @roots
end

#script(*args, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/paggio/script.rb', line 29

def script(*args, &block)
  if block
    (@current || @roots) << Script.new(*args, &block)
  else
    super
  end
end

#style(&block) ⇒ Object



120
121
122
# File 'lib/paggio/css.rb', line 120

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

#text(*fragments, &block) ⇒ Object



74
75
76
77
# File 'lib/paggio/html.rb', line 74

def text(*fragments, &block)
  fragments << yield if block
  fragments.each { |fragment| self << fragment }
end