class Biruda::HTML

Constants

EMPTY_TAG_CLOSER

The default HTML version of this class is 5

OpeningTag
VALID_TAGS

Public Class Methods

create(options = {}, &block) click to toggle source
# File lib/biruda/html.rb, line 20
def self.create(options = {}, &block)
  new.build(options, &block)
end
new() click to toggle source
# File lib/biruda/html.rb, line 16
def initialize
  @page = '<!DOCTYPE html>'
end

Public Instance Methods

build(options = {}, &block) click to toggle source
# File lib/biruda/html.rb, line 28
def build(options = {}, &block)
  @context = options[:context]
  html(&block)
  self
end
build_tag(options = {}, &block) click to toggle source
# File lib/biruda/html.rb, line 34
def build_tag(options = {}, &block)
  @context = options[:context]
  instance_eval(&block)
  self
end
to_s() click to toggle source
# File lib/biruda/html.rb, line 24
def to_s
  @page
end

Private Instance Methods

adapt_tag_params(content, attributes) click to toggle source
# File lib/biruda/html.rb, line 94
def adapt_tag_params(content, attributes)
  # In case the caller does something like `script src: 'some js'`
  return [nil, content] if content.is_a?(Hash)
  [content, attributes]
end
build_array_elements(array) click to toggle source
# File lib/biruda/html.rb, line 65
def build_array_elements(array)
  array.each do |cont|
    # This is a workaround because Ruby does not have lazy evaluation, so if the caller sends
    # a block with an element that is a tag, it will be evaluated before calling the method,
    # and will add that tag before the rest of the elements in the array
    if cont.respond_to?(:call)
      cont.call
    else
      @page << cont
    end
  end
end
built_tag_is_empty(name, content, attributes, block_given) click to toggle source
# File lib/biruda/html.rb, line 50
def built_tag_is_empty(name, content, attributes, block_given)
  opening_tag = "<#{name}"

  attributes.each { |key, value| opening_tag << " #{key}=\"#{value}\"" }

  opening_tag << if content.nil? && !block_given
                   EMPTY_TAG_CLOSER
                 else
                   '>'
                 end

  @page << opening_tag
  opening_tag.include?(EMPTY_TAG_CLOSER)
end
tag(name, content = nil, attributes = {}, &block) click to toggle source
# File lib/biruda/html.rb, line 78
def tag(name, content = nil, attributes = {}, &block)
  content, attributes = adapt_tag_params(content, attributes)

  return if built_tag_is_empty(name, content, attributes, block_given?)

  if content.is_a?(Array)
    build_array_elements(content)
  elsif block_given?
    instance_eval(&block)
  else
    @page << content
  end

  @page << "</#{name}>"
end