Class: Scarpe::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/scarpe/wasm/html.rb

Constant Summary collapse

CONTENT_TAGS =
[:div, :p, :button, :ul, :li, :textarea, :a, :video, :strong, :style, :em, :code, :u, :line, :span, :svg].freeze
VOID_TAGS =
[:input, :img, :polygon, :source, :link, :path].freeze
TAGS =
(CONTENT_TAGS + VOID_TAGS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HTML

Returns a new instance of HTML.



16
17
18
19
# File 'lib/scarpe/wasm/html.rb', line 16

def initialize(&block)
  @buffer = ""
  block.call(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises:

  • (NoMethodError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/scarpe/wasm/html.rb', line 63

def method_missing(name, *args, &block)
  raise NoMethodError, "no method #{name} for #{self.class.name}" unless TAGS.include?(name)

  if VOID_TAGS.include?(name)
    raise ArgumentError, "void tag #{name} cannot have content" if block_given?

    @buffer += "<#{name}#{render_attributes(*args)} />"
  else
    @buffer += "<#{name}#{render_attributes(*args)}>"

    if block_given?
      result = block.call(self)
    else
      result = args.first
      @buffer += result if result.is_a?(String)
    end
    @buffer += result if result.is_a?(String)

    @buffer += "</#{name}>"
  end

  nil
end

Class Method Details

.render(&block) ⇒ Object



11
12
13
# File 'lib/scarpe/wasm/html.rb', line 11

def render(&block)
  new(&block).value
end

Instance Method Details

#option(**attrs, &block) ⇒ Object



33
34
35
# File 'lib/scarpe/wasm/html.rb', line 33

def option(**attrs, &block)
  tag(:option, **attrs, &block)
end

#p(*args, &block) ⇒ Object



29
30
31
# File 'lib/scarpe/wasm/html.rb', line 29

def p(*args, &block)
  method_missing(:p, *args, &block)
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/scarpe/wasm/html.rb', line 25

def respond_to_missing?(name, include_all = false)
  TAGS.include?(name) || super(name, include_all)
end

#select(**attrs, &block) ⇒ Object



59
60
61
# File 'lib/scarpe/wasm/html.rb', line 59

def select(**attrs, &block)
  tag(:select, **attrs, &block)
end

#tag(name, **attrs, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scarpe/wasm/html.rb', line 37

def tag(name, **attrs, &block)
  if VOID_TAGS.include?(name)
    raise ArgumentError, "void tag #{name} cannot have content" if block_given?

    @buffer += "<#{name}#{render_attributes(attrs)} />"
  else
    @buffer += "<#{name}#{render_attributes(attrs)}>"

    if block_given?
      result = block.call(self)
    else
      result = attrs[:content]
      @buffer += result if result.is_a?(String)
    end
    @buffer += result if result.is_a?(String)

    @buffer += "</#{name}>"
  end

  nil
end

#valueObject



21
22
23
# File 'lib/scarpe/wasm/html.rb', line 21

def value
  @buffer
end