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.



1326
1327
1328
1329
1330
1331
# File 'lib/amrita2/template.rb', line 1326

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

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



1322
1323
1324
# File 'lib/amrita2/template.rb', line 1322

def sections
  @sections
end

Instance Method Details

#process(s) ⇒ Object



1333
1334
1335
1336
1337
1338
1339
# File 'lib/amrita2/template.rb', line 1333

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



1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
# File 'lib/amrita2/template.rb', line 1350

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



1341
1342
1343
1344
1345
1346
1347
1348
# File 'lib/amrita2/template.rb', line 1341

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