Module Kwartz::HandlerHelper
In: kwartz/converter.rb

Methods

Public Instance methods

private

[Source]

# File kwartz/converter.rb, line 273
    def _last_stmt_kind(stmt_list)
      return nil if stmt_list.nil? || stmt_list.empty?
      stmt = stmt_list.last
      return nil unless stmt.is_a?(NativeStatement)
      return stmt.kind
    end

[Source]

# File kwartz/converter.rb, line 431
    def add_foreach_stmts(elem_info, stmt_list, foreach_code, endforeach_code,
                          content_only, counter, toggle, init_code, incr_code, toggle_code)
      stmt_list << stag_stmt(elem_info)               if content_only
      start_code.split(/\n/).each do |code|
        stmt_list << NativeStatement.new(code, kind)
      end if start_code
      stmt_list << stag_stmt(elem_info)               if !content_only
      stmt_list.concat(elem_info.cont_stmts)
      stmt_list << etag_stmt(elem_info)               if !content_only
      end_code.split(/\n/).each do |code|
        stmt_list << NativeStatement.new(code, kind)
      end
      stmt_list << etag_stmt(elem_info)               if content_only
    end

[Source]

# File kwartz/converter.rb, line 404
    def add_native_code(stmt_list, code, kind)
      if code.is_a?(String)
        stmt_list << NativeStatement.new(code, kind)
      elsif code.is_a?(Array)
        stmt_list.concat(code.collect {|line| NativeStatement.new(line, kind)})
      end
    end

[Source]

# File kwartz/converter.rb, line 447
    def add_native_expr_with_default(elem_info, stmt_list,
                                     expr_code, flag_escape,
                                     if_code, else_code, endif_code)
      stmt_list << stag_stmt(elem_info)
      stmt_list << NativeStatement.new_without_newline(if_code, :if)
      stmt_list << PrintStatement.new([ NativeExpression.new(expr_code, flag_escape) ])
      stmt_list << NativeStatement.new_without_newline(else_code, :else)
      stmt_list.concat(elem_info.cont_stmts)
      stmt_list << NativeStatement.new_without_newline(endif_code, :else)
      stmt_list << etag_stmt(elem_info)
    end

create array of String and NativeExpression for PrintStatement

[Source]

# File kwartz/converter.rb, line 289
    def build_print_args(taginfo, attr_info, append_exprs)
      return [] if taginfo.tagname.nil?
      #if taginfo.tagname.nil?
      #  if (!attr_info || attr_info.empty?) && (!append_exprs || append_exprs.empty?)
      #    return []
      #  else
      #    taginfo.tagname = 'span'
      #  end
      #end
      unless attr_info || append_exprs
        return [taginfo.tag_text]
      end
      args = []
      t = taginfo
      sb = "#{t.head_space}<#{t.is_etag ? '/' : ''}#{t.tagname}"
      attr_info.each do |space, aname, avalue|
        sb << "#{space}#{aname}=\""
        if avalue.is_a?(NativeExpression)
          native_expr = expand_attr_vars_in_native_expr(avalue, attr_info)
          args << sb     # TextExpression.new(sb)
          args << native_expr
          sb = ''
        else
          sb << avalue
        end
        sb << '"'
      end if attr_info
      if append_exprs && !append_exprs.empty?
        unless sb.empty?
          args << sb     # TextExpression.new(sb)
          sb = ''
        end
        append_exprs.each do |append_expr|
          native_expr = expand_attr_vars_in_native_expr(append_expr, attr_info)
          args << native_expr
        end
      end
      sb << "#{t.extra_space}#{t.is_empty ? '/' : ''}>#{t.tail_space}"
      args << sb         # TextExpression.new(sb)
      return args
    end

create PrintStatement for NativeExpression

[Source]

# File kwartz/converter.rb, line 340
    def build_print_expr_stmt(native_expr, stag_info, etag_info, attr_info=nil)
      head_space = (stag_info || etag_info).head_space
      tail_space = (etag_info || stag_info).tail_space
      native_expr = expand_attr_vars_in_native_expr(native_expr, attr_info) if attr_info
      args = []
      args << head_space if head_space    # TextExpression.new(head_space)
      args << native_expr
      args << tail_space if tail_space    # TextExpression.new(tail_space)
      return PrintStatement.new(args)
    end

create PrintStatement for TagInfo

[Source]

# File kwartz/converter.rb, line 333
    def build_print_stmt(taginfo, attr_info, append_exprs)
      args = build_print_args(taginfo, attr_info, append_exprs)
      return PrintStatement.new(args)
    end

create print statement from text

[Source]

# File kwartz/converter.rb, line 282
    def create_text_print_stmt(text)
      return PrintStatement.new([text])
      #return PritnStatement.new([TextExpression.new(text)])
    end

raise error if etag_info is null

[Source]

# File kwartz/converter.rb, line 253
    def error_if_empty_tag(elem_info, directive_str)
      unless elem_info.etag_info
        msg = "'#{directive_str}': directive is not available with empty tag."
        raise convert_error(msg, elem_info.stag_info.linenum)
      end
    end

[Source]

# File kwartz/converter.rb, line 261
    def error_when_last_stmt_is_not_if(elem_info, directive_str, stmt_list)
      kind = _last_stmt_kind(stmt_list)
      unless kind == :if || kind == :elseif
        msg = "'#{directive_str}': previous statement should be 'if' or 'elsif'."
        raise convert_error(msg, elem_info.stag_info.linenum)
      end
    end

build print statemetn of end-tag

[Source]

# File kwartz/converter.rb, line 399
    def etag_stmt(elem_info)
      return build_print_stmt(elem_info.etag_info, nil, nil)
    end

expand attribute variables (such as ’$(rows)’ or ’$(value)’) and return new code

[Source]

# File kwartz/converter.rb, line 353
    def expand_attr_vars(code, attr_info)
      new_code = code.gsub(/\$\((\w+(?::\w+)?)\)/) do |m|
        aname = $1
        #unless attrs.key?(aname)
        #  raise "#{m}: attribute '#{aname}' expected but not found."
        #end
        avalue = attr_info[aname]
        if avalue.is_a?(NativeExpression)
          raise "#{m}: attribute value of '#{aname}' is NativeExpression object."
        end
        avalue
      end
      return new_code
    end

expand attribute variables and return new NativeExpression

[Source]

# File kwartz/converter.rb, line 370
    def expand_attr_vars_in_native_expr(native_expr, attr_info)
      code = expand_attr_vars(native_expr.code, attr_info)
      if code != native_expr.code
        native_expr = NativeExpression.new(code, native_expr.escape)
      end
      return native_expr
    end

expand attribute variables and return new NativeExpression

[Source]

# File kwartz/converter.rb, line 380
    def expand_attr_vars_in_native_stmt(native_stmt, attr_info)
      code = expand_attr_vars(native_stmt.code, attr_info)
      if code != native_stmt.code
        no_newline = native_stmt.no_newline
        native_stmt = NativeStatement.new(code, native_stmt.kind)
        native_stmt.no_newline = no_newline unless no_newline.nil?
      end
      return native_stmt
    end

build print statement of start-tag

[Source]

# File kwartz/converter.rb, line 392
    def stag_stmt(elem_info)
      e = elem_info
      return build_print_stmt(e.stag_info, e.attr_info, e.append_exprs)
    end

[Source]

# File kwartz/converter.rb, line 422
    def wrap_content_with_native_stmt(elem_info, stmt_list, start_code, end_code, kind=nil)
      stmt_list << stag_stmt(elem_info)
      add_native_code(stmt_list, start_code, kind)
      stmt_list.concat(elem_info.cont_stmts)
      add_native_code(stmt_list, end_code, kind)
      stmt_list << etag_stmt(elem_info)
    end

[Source]

# File kwartz/converter.rb, line 413
    def wrap_element_with_native_stmt(elem_info, stmt_list, start_code, end_code, kind=nil)
      add_native_code(stmt_list, start_code, kind)
      stmt_list << stag_stmt(elem_info)
      stmt_list.concat(elem_info.cont_stmts)
      stmt_list << etag_stmt(elem_info)
      add_native_code(stmt_list, end_code, kind)
    end

[Validate]