module WabisabiTemplateModule

Public Instance Methods

apply(data) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 111
def apply(data)
  self.each_tag {|e|
    next e unless e[0].is_a?(Symbol)
    next e unless e[1].is_a?(Hash)
    attr = e[1]
    eid = attr[:id]
    next e if eid.nil?
    eid = eid.intern
    d = data[eid]
    next nil if d.nil?
    next e if d == true
    if d.is_a? Hash
      next e.clone_with(d)
    elsif d.is_a? Array
      next e.clone_with(d)
    end
    e.clone_with(d)
  }
end
clone_with(*ar) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 84
def clone_with(*ar)
  name = self[0]
  nar = [name]

  offset = 1
  attr = {}
  while self[offset].is_a?(Hash)
    attr.update(self[offset])
    offset += 1
  end
  if 0 < attr.length
    nar << attr
  end

  nar += self[offset...self.length].dup
  ar.each {|e|
    if e.is_a?(Hash)
      attr.update(e)
    elsif e.nil?
      # do nothing
    else
      nar << e
    end
  }
  nar
end
deep_copy() click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 9
def deep_copy
  map {|a|
    a.is_a?(Array) ? a.deep_copy : a
  }
end
each_tag(*tags) { |w| ... } click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 36
def each_tag(*tags, &block)
  raise 'block not given' unless block_given?

  ar = []
  deleted = false
  self.each {|w|
    if ! w.is_a?(Array)
      ar << w
      next
    end

    if tags.length == 0 || tags.include?(w[0])
      y = yield(w)

      if y.nil?      # do nothing, go next
        deleted = true
        next
      end

      if y != w      # not equal
        if y.is_a?(Array) && !y[0].is_a?(Symbol)
          ar += y
        else
          ar << y
        end
        next
      else
        # do nothing, run through
      end
    end

    ar << w.each_tag(*tags, &block)   # recursive
  }

  if ar[0].is_a?(Symbol) && deleted
    num = 0
    ar.each {|a|
      next if a.is_a?(Symbol) || a.is_a?(Hash)
      num += 1
    }
    if num == 0
      ar << ''
    end
  end

  ar
end
prepare() click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 15
def prepare
  h_class = {}
  h_id = {}
  self.each_tag {|e|
    attr = self.attr
    next if attr.nil?
    klass = attr[:class]
    h_class[klass] = self if klass
    tag_id = attr[:id]
    h_id[tag_id] = self if tag_id
  }
  @h_class = h_class
  @h_id = h_id
end
remove_comment() click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 131
def remove_comment
  self.each_tag(:"!--") {|x|
    nil
  }
end
set_to_class(klass, w) click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-template.rb, line 30
def set_to_class(klass, w)
  prepare if @h_class
  e = @h_class[klass]
  e << w
end