module Paru::PandocFilter::ASTManipulation

ASTManipulation is a mixin for Node with some standard tree manipulation methods such as inserting or removing nodes, replacing nodes, and so on.

Public Instance Methods

<<(child)
Alias for: append
append(child) click to toggle source

Append a child to the list with this node’s children.

@param child [Node] the child to append.

# File lib/paru/filter/ast_manipulation.rb, line 70
def append(child)
    @children.push child
end
Also aliased as: <<
delete(child) click to toggle source

Delete child from this node’s children.

@param child [Node] the child node to delete.

# File lib/paru/filter/ast_manipulation.rb, line 56
def delete(child)
    @children.delete child
end
each_depth_first() { |self| ... } click to toggle source

Walk the node tree starting at this node, depth first, and apply block to each node in the tree

@param block [Proc] the block to apply to each node in this node

tree

@yield [Node]

# File lib/paru/filter/ast_manipulation.rb, line 109
def each_depth_first(&block)    
    yield self

    if has_been_replaced?
        node = get_replacement
    else
        node = self
    end

    node.each {|child| child.each_depth_first(&block)} if node.has_children?
end
find_index(child) click to toggle source

Find index of child

@param child [Node] the child to find the index for

@return [Number] the index of child or nil

# File lib/paru/filter/ast_manipulation.rb, line 31
def find_index(child)
    @children.find_index child
end
get(index) click to toggle source

Get the child node at index

@param index [Number] the index of the child to get

@return [Node] the child at index

# File lib/paru/filter/ast_manipulation.rb, line 41
def get(index)
    @children[index]
end
insert(index, child) click to toggle source

Insert child node among this node’s children at position index.

@param index [Integer] the position to insert the child @param child [Node] the child to insert

# File lib/paru/filter/ast_manipulation.rb, line 49
def insert(index, child)
    @children.insert index, child
end
prepend(child) click to toggle source

Prepend a child to the list with this node’s children.

@param child [Node] the child to prepend.

# File lib/paru/filter/ast_manipulation.rb, line 78
def prepend(child)
    insert 0, child
end
remove_at(index) click to toggle source

Remove the child at position index from this node’s children

@param index [Integer] the position of the child to remove

# File lib/paru/filter/ast_manipulation.rb, line 63
def remove_at(index)
    @children.delete_at index
end
replace(old_child, new_child) click to toggle source

Replace a child from this node’s children with a new child.

@param old_child [Node] the child to replace @param new_child [Node] the replacement child

# File lib/paru/filter/ast_manipulation.rb, line 86
def replace(old_child, new_child)
    old_child_index = find_index old_child
    if old_child_index then
        replace_at old_child_index, new_child
    end
end
replace_at(index, new_child) click to toggle source

Replace the child at position index from this node’s children with a new child.

@param index [Integer] the position of the child to replace @param new_child [Node] the replacement child

# File lib/paru/filter/ast_manipulation.rb, line 98
def replace_at(index, new_child)
    @children[index] = new_child
end