Module: YART::Parser

Included in:
YART
Defined in:
lib/yart/parser.rb

Constant Summary collapse

CUSTOM_ATTRIBUTES =
[:close].freeze

Instance Method Summary collapse

Instance Method Details

#br(**attributes, &block) ⇒ Object



57
58
59
60
# File 'lib/yart/parser.rb', line 57

def br(**attributes, &block)
  attributes[:close] = true unless attributes[:close] == false
  element("br", **attributes, &block)
end

#doctypeObject

Renders a element, for convenience.



37
38
39
# File 'lib/yart/parser.rb', line 37

def doctype
  element("!DOCTYPE", html: true, close: true)
end

#element(name, **attributes, &block) ⇒ Object

Renders an element with the raw name (case insensitive).



24
25
26
# File 'lib/yart/parser.rb', line 24

def element(name, **attributes, &block)
  render(name, attributes, &block)
end

#javascript(src = nil, &block) ⇒ Object

Renders a JS

Overrides Ruby's p method to render the element instead of printing.



63
64
65
# File 'lib/yart/parser.rb', line 63

def p(**attributes, &block)
  element("p", **attributes, &block)
end

#parse(&block) ⇒ Object

Parses a block of Ruby, rendering and returning a HTML String.



8
9
10
11
12
13
14
# File 'lib/yart/parser.rb', line 8

def parse(&block)
  raise "Must pass a block to parse" unless block_given?

  @@yart_buffer = []
  instance_eval(&block)
  @@yart_buffer.join
end

#stylesheet(href = nil, &block) ⇒ Object

Renders a CSS element, for convenience.



50
51
52
53
54
55
# File 'lib/yart/parser.rb', line 50

def stylesheet(href = nil, &block)
  raise "Must pass a String param or a block returning a String" unless href || block_given?

  href ||= block.call
  element("link", href: href, rel: :stylesheet, type: "text/css", close: true)
end

#text(str = nil, &block) ⇒ Object

Sets the innerText of the element being rendered.



29
30
31
32
33
34
# File 'lib/yart/parser.rb', line 29

def text(str = nil, &block)
  raise "Must pass a String param or a block returning a String" unless str || block_given?

  str ||= block.call
  buffer(str)
end