Class: Amrita2::Core::PreProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/amrita2/template.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: AmXml, LineProcessor

Constant Summary collapse

CDataStart =
%r[(.*?)(<!\[CDATA\[)(.*)]m
CDataEnd =
%r[(.*?)(\]\]>)(.*)]m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ PreProcessor

Returns a new instance of PreProcessor.



1442
1443
1444
1445
1446
1447
# File 'lib/amrita2/template.rb', line 1442

def initialize(opt={})
  @opt = opt
  init()
  @trace = false
  #@trace = true
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



1438
1439
1440
# File 'lib/amrita2/template.rb', line 1438

def sections
  @sections
end

Instance Method Details

#process(s) ⇒ Object



1449
1450
1451
1452
1453
1454
1455
# File 'lib/amrita2/template.rb', line 1449

def process(s)
  init
  ret = process_block(s)
  ret = process_erb_and_inline_amxml(ret)
  ret << pop_tag(0)
  ret
end

#process_block(s) ⇒ Object



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
# File 'lib/amrita2/template.rb', line 1466

def process_block(s)
  ret = ""
  lines = s.to_a
  while line = lines.shift
    next if line =~ /^\s*$/
    current_indent = line[/^ */].size
    ret << pop_tag(current_indent)

    case line
    when %r[^\s*#]
      # It's a comment. do nothing
    when AmXml::R_AMXML_BLOCK_START
      amxml = $1
      while true
        l = lines.shift
        case l
        when %r[(.*)<(?: |-)*$]
          amxml << $1
          break
        when nil
          raise "incomplete amxml block start #{amxml}"
        else
          amxml << l
        end
      end
      a = AmXml.parse(amxml, @opt)
      raise "illeagal amxml multi line #{amxml.inspect}" unless a
      push_tag(current_indent, a.tag)
      ret << a.result(false)
    when AmXml::R_AMXML_ERB_LINE
      mark, src =  $1,$2
      ret << "<![CDATA[<% "
      while true
        case mark
        when '='
          ret << "\n _erbout.concat(eval(%{#{src}}).to_s) "
        when String
          ret << "\n#{src}"
        else
          break
        end
        case l = lines.shift
        when AmXml::R_AMXML_ERB_LINE
          mark, src =  $1,$2
        else
          mark = nil
          lines.unshift l
        end
      end
      ret << "\n%>]]>\n"
    when AmXml::R_AMXML_BLOCK_LINE
      a = AmXml.parse($1, {:trline=>$2.include?("-")}.merge(@opt))
      raise "illeagal amxml block line #{line.inspect}" unless a
      ret << a.result(false)
      push_tag(current_indent, a.tag)
    when %r[(.*)^\s*>>>\s*$]m
      ret << $1
      raise "unmatched >>> " unless @stack.size > 0
      ret << pop_tag(current_indent)
    when %r[^\s*\|\s*([\w:]*)\s*\|\|]
      lp = LineProcessor.new do |cell_text|
        PreProcessor.new.process_block(cell_text)
      end
      while lp.parse_line(line)
        line = lines.shift
      end
      lines.unshift(line) unless line =~ /^( |-)*$/
      ret << lp.get_result_xml
      ret
    else
      ret << line
    end
  end
  ret << pop_tag(0)
  ret
end

#process_erb_and_inline_amxml(s) ⇒ Object



1457
1458
1459
1460
1461
1462
1463
1464
# File 'lib/amrita2/template.rb', line 1457

def process_erb_and_inline_amxml(s)
  if s =~ CDataStart
    out_of_cdata, cdata, in_cdata = $1, $2, $3
    process_out_of_cdata(out_of_cdata) + cdata + parse_in_cdata(in_cdata)
  else
    process_out_of_cdata(s)
  end
end