class Bytewise::Ast::Body

This is something like a syntax tree. It can hold other nodes like tags, chunks, etc.

Attributes

blocks[RW]
context[RW]
current_child[RW]
nodes[RW]

Public Class Methods

new() click to toggle source
# File lib/brace_markup/ast/body.rb, line 10
def initialize
  @nodes = []
  # Used while rendering, so that the node knows "where" it is.
  @current_child = nil
end

Public Instance Methods

'+='(content)
Alias for: add_chunk
<<(node, before: nil, after: nil)
Alias for: insert
add_chunk(content) click to toggle source

Adds chunk to the body

# File lib/brace_markup/ast/body.rb, line 47
def add_chunk(content)
  content = content.to_s unless content.is_a?(String)

  unless content.empty?
    if content == "\n"
      @nodes << Bytewise::Ast::Chars::NewLine.new
    elsif @nodes.last.class == Bytewise::Ast::Chunk
      # Test if the last element was a chunk and append it
      @nodes.last << content
    else
      @nodes << Bytewise::Ast::Chunk.new(content)
    end
  end
end
Also aliased as: '+='
children?() click to toggle source
# File lib/brace_markup/ast/body.rb, line 126
def children?
  @nodes.length > 0
end
find_blocks(name, exclude: nil) click to toggle source

Finds the last block with the given name

# File lib/brace_markup/ast/body.rb, line 102
def find_blocks(name, exclude: nil)
  found = []

  @nodes.each do |node|
    if node.is_a? ::Bytewise::Ast::Body
      blocks = node.find_blocks(name, exclude: exclude)
      blocks.each { |b| found << b }
    elsif node.is_a? ::BraceMarkup::Ast::Tags::Block
      if node.name == name && node != exclude
        found << node
      end
    end
  end

  # Find in parent, if parent is a body
  if @parent.is_a? Body
    @parent.find_blocks(name, exclude: exclude).each { |n| found << n }
  end



  found
end
import_body(body) click to toggle source

Imports the nodes of another body

@param body ::Bytewise::Ast::Body

# File lib/brace_markup/ast/body.rb, line 90
def import_body(body)
  body.nodes.each do |node|
    newnode = node.clone
    newnode.parent = self

    self << newnode
  end
end
insert(node, before: nil, after: nil) click to toggle source

Adds a node or some chunk

@param node String|Node

# File lib/brace_markup/ast/body.rb, line 21
def insert(node, before: nil, after: nil)
  if node.is_a? Node
    if before.nil? && !after.nil?
      # Insert after
      @nodes.insert(@nodes.index(after) + 1, node)
    elsif after.nil? && !before.nil?
      # Insert before
      @nodes.insert(@nodes.index(before), node)
    else
      # Add to the end
      @nodes << node
    end

    node.parent = self
  elsif node.is_a? String
    add_chunk(node)
  elsif node.is_a? Array
    node.each { |n| insert n, before: before, after: after }
  end
end
Also aliased as: <<
remove_child(child) click to toggle source
# File lib/brace_markup/ast/body.rb, line 130
def remove_child(child)
  @nodes.delete(child)
end
render(*args) click to toggle source

Called by the evaluator

# File lib/brace_markup/ast/body.rb, line 74
def render(*args)
  result = ''

  @nodes.each_with_index do |node, index|
    @current_child = index
    result += node.render(*args).to_s
  end

  result
end
to_s() click to toggle source
# File lib/brace_markup/ast/body.rb, line 63
def to_s
  string = ''

  @nodes.each do |node|
    string += node.to_s
  end
end