class Giter8::AST

AST represents a set of nodes of a template file

Constants

LEADING_LINEBREAK_REGEXP

Public Class Methods

new() click to toggle source
# File lib/giter8/ast.rb, line 10
def initialize
  @nodes = []
end

Public Instance Methods

clean() click to toggle source

Returns a new AST node containing this node's after cleaning them.

# File lib/giter8/ast.rb, line 61
def clean
  ast = AST.new
  ast.push(*each_with_index.map(&method(:clean_node)).reject(&:nil?))
  ast
end
clean!() click to toggle source

Cleans up this AST's nodes in-place.

# File lib/giter8/ast.rb, line 24
def clean!
  @nodes = clean.instance_variable_get(:@nodes)
end
clean_conditional_ast(node) click to toggle source

Cleans leading linebreaks from the provided node

# File lib/giter8/ast.rb, line 29
def clean_conditional_ast(node)
  return node if node.empty? || !node.first.is_a?(Literal)

  cond = node.first
  cond.value.sub!(LEADING_LINEBREAK_REGEXP, "")
  node.shift
  node.unshift(cond) unless cond.value.empty?
  node
end
clean_node(node, idx) click to toggle source

clean_node attempts to sanitise a provide node under a given index in this AST

# File lib/giter8/ast.rb, line 41
def clean_node(node, idx)
  if node.is_a?(Conditional)
    # Remove leading linebreak from ASTs inside conditionals
    node.cond_then = clean_conditional_ast(node.cond_then.clean)
    node.cond_else = clean_conditional_ast(node.cond_else.clean)

    # cond_else_if contains a list of Condition objects, which
    # need special handling.
    node.cond_else_if.clean!
  end

  if node.is_a?(Literal) && idx.positive? && self[idx - 1].is_a?(Conditional)
    # Remove leading linebreak from Literals following conditionals
    node.value.sub!(LEADING_LINEBREAK_REGEXP, "")
    return if node.value.empty?
  end
  node
end
pure_literal?() click to toggle source

Returns whether this AST node is composed exclusively by Literals

# File lib/giter8/ast.rb, line 19
def pure_literal?
  all? { |v| v.is_a? Literal }
end