Class: Lotus::Helpers::HtmlHelper::HtmlBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/helpers/html_helper/html_builder.rb

Overview

HTML Builder

Since:

  • 0.1.0

Constant Summary collapse

CONTENT_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 content tags

[
  'a',
  'abbr',
  'address',
  'article',
  'aside',
  'audio',
  'b',
  'bdi',
  'bdo',
  'blockquote',
  'body',
  'button',
  'canvas',
  'caption',
  'cite',
  'code',
  'colgroup',
  'data',
  'datalist',
  'del',
  'details',
  'dfn',
  'div',
  'dl',
  'dt',
  'em',
  'fieldset',
  'figcaption',
  'figure',
  'footer',
  'form',
  'h1',
  'h2',
  'h3',
  'h4',
  'h5',
  'h6',
  'head',
  'header',
  'i',
  'iframe',
  'ins',
  'kbd',
  'label',
  'legend',
  'li',
  'link',
  'main',
  'map',
  'mark',
  'math',
  'menu',
  'meter',
  'nav',
  'noscript',
  'object',
  'ol',
  'optgroup',
  'option',
  'output',
  'p',
  'pre',
  'progress',
  'q',
  'rp',
  'rt',
  'ruby',
  's',
  'samp',
  'script',
  'section',
  'select',
  'small',
  'span',
  'strong',
  'style',
  'sub',
  'summary',
  'sup',
  'svg',
  'table',
  'tbody',
  'td',
  'template',
  'textarea',
  'tfoot',
  'th',
  'thead',
  'time',
  'title',
  'tr',
  'u',
  'ul',
  'video',
].freeze
EMPTY_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 empty tags

[
  'area',
  'base',
  'br',
  'col',
  'embed',
  'hr',
  'img',
  'input',
  'keygen',
  'link',
  'menuitem',
  'meta',
  'param',
  'source',
  'track',
  'wbr',
].freeze
NEWLINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

New line separator

Since:

  • 0.1.0

"\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeLotus::Helpers::HtmlHelper::HtmlBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new builder

Since:

  • 0.1.0


173
174
175
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 173

def initialize
  @nodes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Forward missing methods to the current context. This allows to access views local variables from nested content blocks.

Since:

  • 0.1.0


290
291
292
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 290

def method_missing(m, *args, &blk)
  @context.__send__(m, *args, &blk)
end

Instance Method Details

#empty_tag(name, attributes = nil) ⇒ self

Defines a custom empty tag

Examples:

html.empty_tag(:xr) # => <xr>

html.empty_tag(:xr, id: 'foo') # => <xr id="foo">

html.empty_tag(:xr, id: 'foo', 'data-xyz': 'bar') # => <xr id="foo" data-xyz="bar">

Parameters:

  • name (Symbol, String)

    the name of the tag

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

Returns:

  • (self)

See Also:

Since:

  • 0.1.0


242
243
244
245
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 242

def empty_tag(name, attributes = nil)
  @nodes << EmptyHtmlNode.new(name, attributes)
  self
end

#nested?TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if there are nested nodes

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0


265
266
267
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 265

def nested?
  @nodes.any?
end

#resolve(&blk) ⇒ Object

Since:

  • 0.1.0


274
275
276
277
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 274

def resolve(&blk)
  @context = blk.binding.receiver
  instance_exec(&blk)
end

#tag(name, content = nil, attributes = nil, &blk) ⇒ self

Define a custom tag

Examples:

html.tag(:custom) # => <custom></custom>

html.tag(:custom, 'foo') # => <custom>foo</custom>

html.tag(:custom, html.p('hello')) # => <custom><p>hello</p></custom>

html.tag(:custom) { 'foo' }
# =>
#<custom>
#  foo
#</custom>

html.tag(:custom) do
  p 'hello'
end
# =>
#<custom>
#  <p>hello</p>
#</custom>

html.tag(:custom, 'hello', id: 'foo', 'data-xyz': 'bar') # => <custom id="foo" data-xyz="bar">hello</custom>

html.tag(:custom, id: 'foo') { 'hello' }
# =>
#<custom id="foo">
#  hello
#</custom>

Parameters:

  • name (Symbol, String)

    the name of the tag

  • content (String, Lotus::Helpers::HtmlHelper::HtmlBuilder, NilClass) (defaults to: nil)

    the optional content

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

  • blk (Proc)

    the optional nested content espressed as a block

Returns:

  • (self)

See Also:

Since:

  • 0.1.0


219
220
221
222
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 219

def tag(name, content = nil, attributes = nil, &blk)
  @nodes << HtmlNode.new(name, blk || content, attributes || content)
  self
end

#to_sLotus::Utils::Escape::SafeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves all the nodes and generates the markup

Returns:

  • (Lotus::Utils::Escape::SafeString)

    the output

See Also:

Since:

  • 0.1.0


255
256
257
# File 'lib/lotus/helpers/html_helper/html_builder.rb', line 255

def to_s
  Utils::Escape::SafeString.new(@nodes.map(&:to_s).join(NEWLINE))
end