Google

"DTD/xhtml1-strict.dtd">
Module Amrita::Node
In: lib/amrita/node_expand.rb
lib/amrita/node.rb
lib/amrita/format.rb
lib/amrita/compiler.rb

Base module for HTML elements

Methods
Included modules
Enumerable
Public Instance methods
expand(data, context=DefaultContext.clone)

expand self as a template with a model data.

# File lib/amrita/node_expand.rb, line 267
    def expand(data, context=DefaultContext.clone)
      case data
      when true
        self
      when nil, false
        Null
      when DictionaryData
        expand1(data, context)
      else
        raise "Amrita::Node#expand accepts only Hash or ExpandByMember as model data (#{data.type} was passed)"
      end
    end
expand1(data, context=DefaultContext.clone)
# File lib/amrita/node_expand.rb, line 280
    def expand1(data, context=DefaultContext.clone)
      data.amrita_expand_node(self, context)
    end
amrita_expand_node(n, context)
# File lib/amrita/node_expand.rb, line 288
    def amrita_expand_node(n, context)
      self
    end
apply_to_children(&block)
# File lib/amrita/node_expand.rb, line 292
    def apply_to_children(&block)
      self
    end
init_body(&block) {|| ...}

set the block 's result to body

# File lib/amrita/node.rb, line 179
    def init_body(&block)
      if block_given?
        @body = to_node(yield)
      else
        @body = Null
      end
    end
body()

a Node has NullNode as body before init_body was called.

# File lib/amrita/node.rb, line 188
    def body
      if defined? @body
        @body
      else
        Null
      end
    end
no_child?()

test if it has any children

# File lib/amrita/node.rb, line 197
    def no_child?
      body.kind_of?(NullNode)
    end
children()

return an Array of child Node or an empty Array if it does not have a body

# File lib/amrita/node.rb, line 202
    def children
      if no_child?
        []
      else
        [ body ]
      end
    end
to_node(n)

generate a Node object

# File lib/amrita/node.rb, line 211
    def to_node(n)
      case n
      when nil, false
        Null
      when Node
        n
      when Array
        case n.size()
        when 0
          Null
        when 1
          to_node(n[0])
        else
          NodeArray.new(*n)
        end
      else
        TextElement.new(n.to_s) 
      end
    end
hid()
# File lib/amrita/node.rb, line 232
    def hid 
      nil
    end
inspect()
# File lib/amrita/node.rb, line 236
    def inspect
      to_ruby
    end
+(node)

Node can be added and they become NodeArray

# File lib/amrita/node.rb, line 241
    def +(node)
      NodeArray.new(self, to_node(node))
    end
*(n)

Copy a Node n times and generate NodeArray

# File lib/amrita/node.rb, line 246
    def *(n)
      raise "can't #{self.class} * #{n}(#{n.class})" unless n.kind_of?(Integer)
      a = (0...n).collect { |i| self }
      NodeArray.new(*a)
    end
each_node(&block) {|self| ...}

iterate on self and children

# File lib/amrita/node.rb, line 253
    def each_node(&block)
      c = children # save children before yield
      yield(self)
      c.each do |n|
        n.each_node(&block)
      end
    end
each_element(&block) {|node| ...}

iterate on child Elements

# File lib/amrita/node.rb, line 262
    def each_element(&block)
      each_node do |node|
        yield(node) if node.kind_of?(Element)
      end
    end
each_element_with_id(recursive=false, &block)

iterate on child Elements with id. If recursive == false, don't go to children of an Element with id.

# File lib/amrita/node.rb, line 271
    def each_element_with_id(recursive=false, &block)
      children.each do |node|
        node.each_element_with_id(recursive, &block)
      end
    end
has_id_element?()

test if an Element or children has any id

# File lib/amrita/node.rb, line 278
    def has_id_element?
      each_node do |n|
        next unless n.kind_of?(Element)
        next unless n.hid
        return true
      end
      false
    end
to_s()
# File lib/amrita/format.rb, line 516
    def to_s
      ret = ""
      SingleLineFormatter.new(ret).format(self)
      ret
    end
pre_format(formatter, expand_attr=false)

converts an Element without id to TextElement to make tree low for performance.

A pre-formatted Node tree will be expanded faster than original. But, it produces the same output .

# File lib/amrita/format.rb, line 527
    def pre_format(formatter, expand_attr=false)
      raise "pre_format dose not suport pretty-print" if formatter.kind_of?(PrettyPrintFormatter)
      prf = PreFormatter.new(formatter, expand_attr)
      prf.pre_format(self)
      prf
    end
pre_format1(prf)
# File lib/amrita/format.rb, line 534
    def pre_format1(prf)
      prf << self
    end
compile(compiler)
# File lib/amrita/compiler.rb, line 68
    def compile(compiler)
      compiler << to_s
    end
generate_hint_from_template()
# File lib/amrita/compiler.rb, line 72
    def generate_hint_from_template
      hash = {}
      each_element_with_id do |e|
        hash[e.hid.intern] = HtmlCompiler::AnyData
      end
      HtmlCompiler::DictionaryHint.new(hash)
    end