Module: Kwartz::HandlerHelper

Included in:
Handler
Defined in:
lib/kwartz/converter.rb

Instance Method Summary collapse

Instance Method Details

#_last_stmt_kind(stmt_list) ⇒ Object



273
274
275
276
277
278
# File 'lib/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

#add_foreach_stmts(elem_info, stmt_list, foreach_code, endforeach_code, content_only, counter, toggle, init_code, incr_code, toggle_code) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/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

#add_native_code(stmt_list, code, kind) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/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

#add_native_expr_with_default(elem_info, stmt_list, expr_code, flag_escape, if_code, else_code, endif_code) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
# File 'lib/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

#build_print_args(taginfo, attr_info, append_exprs) ⇒ Object

create array of String and NativeExpression for PrintStatement



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/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

#build_print_expr_stmt(native_expr, stag_info, etag_info, attr_info = nil) ⇒ Object

create PrintStatement for NativeExpression



340
341
342
343
344
345
346
347
348
349
# File 'lib/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

#build_print_stmt(taginfo, attr_info, append_exprs) ⇒ Object

create PrintStatement for TagInfo



333
334
335
336
# File 'lib/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_text_print_stmt(text) ⇒ Object

create print statement from text



282
283
284
285
# File 'lib/kwartz/converter.rb', line 282

def create_text_print_stmt(text)
  return PrintStatement.new([text])
  #return PritnStatement.new([TextExpression.new(text)])
end

#error_if_empty_tag(elem_info, directive_str) ⇒ Object

raise error if etag_info is null



253
254
255
256
257
258
# File 'lib/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

#error_when_last_stmt_is_not_if(elem_info, directive_str, stmt_list) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/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

#etag_stmt(elem_info) ⇒ Object

build print statemetn of end-tag



399
400
401
# File 'lib/kwartz/converter.rb', line 399

def etag_stmt(elem_info)
  return build_print_stmt(elem_info.etag_info, nil, nil)
end

#expand_attr_vars(code, attr_info) ⇒ Object

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



353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/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_attr_vars_in_native_expr(native_expr, attr_info) ⇒ Object

expand attribute variables and return new NativeExpression



370
371
372
373
374
375
376
# File 'lib/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_attr_vars_in_native_stmt(native_stmt, attr_info) ⇒ Object

expand attribute variables and return new NativeExpression



380
381
382
383
384
385
386
387
388
# File 'lib/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

#stag_stmt(elem_info) ⇒ Object

build print statement of start-tag



392
393
394
395
# File 'lib/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

#wrap_content_with_native_stmt(elem_info, stmt_list, start_code, end_code, kind = nil) ⇒ Object



422
423
424
425
426
427
428
# File 'lib/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

#wrap_element_with_native_stmt(elem_info, stmt_list, start_code, end_code, kind = nil) ⇒ Object



413
414
415
416
417
418
419
# File 'lib/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