class Paggio::HTML

Attributes

version[R]

Public Class Methods

new(version = 5, defer: false, &block) click to toggle source
# 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

Public Instance Methods

<<(what) click to toggle source
# File lib/paggio/html.rb, line 31
def <<(what)
  (@current || @roots) << what
end
build!(force_call: false) click to toggle source
# 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
e(name, *args, &block)

Support for custom elements

Alias for: method_missing
each(&block) click to toggle source
# File lib/paggio/html.rb, line 70
def each(&block)
  @roots.each(&block)
end
element!() click to toggle source
# File lib/paggio/html.rb, line 52
def element!
  @current
end
extend!(element = nil, &block) click to toggle source
# 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
inspect() click to toggle source
# File lib/paggio/html.rb, line 110
def inspect
  if @roots.empty?
    "#<HTML(#@version)>"
  else
    "#<HTML(#@version): #{@roots.inspect[1 .. -2]}>"
  end
end
local_variables(*) click to toggle source
# File lib/paggio/script.rb, line 37
def local_variables(*)
  []
end
markdown(string) click to toggle source
# File lib/paggio/markdown.rb, line 16
def markdown(string)
  (@current || @roots) << ::Kramdown::Document.new(
    ::Paggio::Utils.heredoc(string))
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# 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
Also aliased as: e
root!() click to toggle source
# File lib/paggio/html.rb, line 44
def root!
  @roots.first
end
roots!() click to toggle source
# File lib/paggio/html.rb, line 48
def roots!
  @roots
end
script(*args, &block) click to toggle source
Calls superclass method
# 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) click to toggle source
# File lib/paggio/css.rb, line 120
def style(&block)
  (@current || @roots) << CSS.new(&block)
end
text(*fragments) { || ... } click to toggle source
# File lib/paggio/html.rb, line 74
def text(*fragments, &block)
  fragments << yield if block
  fragments.each { |fragment| self << fragment }
end