Class: Amrita2::Core::PreProcessor::AmXml

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

Constant Summary collapse

NCNAME_STR =

Regexps borrowed from REXML rexml/baseparser.rb

'[\w:][\-\w\d.#]*?'
NAME_STR =
"(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
R_TAG_ATTR =

%r[\s*(#{NAME_STR})((?:\s*#{NAME_STR}\s*=\s*(["'])(?:.*?)\3)*)]um
S_NO_SRC =
%[\s*]
S_SRC_AND_FILTERS =
%q[(:([\\w\\d]*))\\s*(\\|.*?)*]
S_FOR =
%q[( \\[(.+)\\] )]
S_COND =
%q[\\?(\\!)?\\[(.+)\\]]
S_VALUE =
%q[\\{(.+)\\}]
S_TRAILER =
%q[((?: |\\-)*)]
S_ERB_IN_AMXML =
%q[%=(.*)]
S_AMVARS =
%[(#{S_FOR}|#{S_COND}|#{S_VALUE}|#{S_ERB_IN_AMXML})?]
R_AMXML_WITHOUT_TAG =
%r[\A\s*(?:\/)?\s*(?:#{S_NO_SRC}|#{S_SRC_AND_FILTERS})*\s*#{S_AMVARS}\s*\Z]um
R_AMXML =
%r[\A#{R_TAG_ATTR}\s*(?:\/)?\s*(?:#{S_NO_SRC}|#{S_SRC_AND_FILTERS})*\s*#{S_AMVARS}\s*\Z]um
R_AMXML_BLOCK_LINE =
%r[^\s*<<\s*?(.*)\s*?<#{S_TRAILER}$]
R_AMXML_BLOCK_START =
%r[^\s*<<\s*((#{R_TAG_ATTR})?[^<>]*)\s*$]
R_AMXML_ERB_LINE =
%r[^\s*\%([ =])(.*)$]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ AmXml

Returns a new instance of AmXml.



1569
1570
1571
1572
1573
1574
1575
# File 'lib/amrita2/template.rb', line 1569

def initialize(opt)
  @opt = opt
  @tag = "_"
  @attr = ""
  @src_name = @filters = nil
  @skipif = @skipunless = @for = @value = nil
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



1543
1544
1545
# File 'lib/amrita2/template.rb', line 1543

def attr
  @attr
end

#filtersObject (readonly)

Returns the value of attribute filters.



1543
1544
1545
# File 'lib/amrita2/template.rb', line 1543

def filters
  @filters
end

#forObject (readonly)

Returns the value of attribute for.



1544
1545
1546
# File 'lib/amrita2/template.rb', line 1544

def for
  @for
end

#skipifObject (readonly)

Returns the value of attribute skipif.



1544
1545
1546
# File 'lib/amrita2/template.rb', line 1544

def skipif
  @skipif
end

#skipunlessObject (readonly)

Returns the value of attribute skipunless.



1544
1545
1546
# File 'lib/amrita2/template.rb', line 1544

def skipunless
  @skipunless
end

#src_nameObject (readonly)

Returns the value of attribute src_name.



1543
1544
1545
# File 'lib/amrita2/template.rb', line 1543

def src_name
  @src_name
end

#tagObject (readonly)

Returns the value of attribute tag.



1543
1544
1545
# File 'lib/amrita2/template.rb', line 1543

def tag
  @tag
end

#valueObject (readonly)

Returns the value of attribute value.



1544
1545
1546
# File 'lib/amrita2/template.rb', line 1544

def value
  @value
end

Class Method Details

.parse(s, opt = {}) ⇒ Object



1565
1566
1567
# File 'lib/amrita2/template.rb', line 1565

def self.parse(s, opt={})
  self.new(opt).parse(s, opt[:trline])
end

Instance Method Details

#has_amxml_attrs?Boolean

Returns:

  • (Boolean)


1577
1578
1579
# File 'lib/amrita2/template.rb', line 1577

def has_amxml_attrs?
  @src_name or @filters or @skipif or @skipunless or @for or @value
end

#make_attr(key, val) ⇒ Object



1732
1733
1734
1735
1736
1737
1738
# File 'lib/amrita2/template.rb', line 1732

def make_attr(key, val)
  if val.include?(?')
    " #{key}=\"#{val}\""
  else
    " #{key}='#{val}'"
  end
end

#parse(s, trline = false) ⇒ Object



1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'lib/amrita2/template.rb', line 1581

def parse(s, trline=false)
  case s
  when  R_AMXML_WITHOUT_TAG
    if trline
      @tag = "tr"
    else
      @tag = "_"
    end
    @src_name = $2.strip if $2
    @filters = $3[1..-1].strip if $3
    parse_am_vars($4)
  when %r[\A\s*([\w]+):([\-\w\d.]*)\s*(/?)\s*\Z] # <<aaa:xxx>>
    if $3 == "/"
      @tag = "#$1:#$2"
    else
      @tag, @src_name = $1, $2
    end
    @tag = "_" if @tag == nil or @tag == ""
    @attr = ""
  when R_AMXML
    @tag, @attr = $1.to_s, $2.to_s
    src_start = $4
    @src_name = $5
    @filters = $6[1..-1].strip if $6
    parse_am_vars($7)
  when "%"
    @tag = :cdata
  when /=\s*(\w+)\s*/
    @tag = [:put_stream,$1.intern]
  when /\s*(\w+)\s*=/
    @tag = [:change_stream,$1.intern]
  else
    return nil
  end
  parse_css_selecter_like
  self
end

#parse_am_vars(s) ⇒ Object



1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
# File 'lib/amrita2/template.rb', line 1638

def parse_am_vars(s)
  case s
  when nil
    # do nothing
  when %r[#{S_FOR}]m
    @for = $2.strip
  when %r[#{S_COND}]m
    if $1 == "!"
      @skipif = $2.strip
    else
      @skipunless = $2.strip
    end
  when %r[#{S_VALUE}]m
    @value = $1.strip
  when %r[#{S_ERB_IN_AMXML}]m
    @erb = $1.strip
  else
    raise "can't happen #{s}"
  end
end

#parse_css_selecter_likeObject



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
# File 'lib/amrita2/template.rb', line 1619

def parse_css_selecter_like
  while true
    case @tag
    when /^(\w+)([#\.])([\w-]+)(.*)$/
      @tag, cls = $1 + $4, $3
      case $2
      when "."
        @attr += " class=\"#{$3}\""
      when "#"
        @attr += " id=\"#{$3}\""
      else
        break
      end
    else
      break
    end
  end
end

#result(single_tag) ⇒ Object



1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
# File 'lib/amrita2/template.rb', line 1659

def result(single_tag)
  case @tag
  when :cdata
    return "<![CDATA["
  when Array
    case @tag[0]
    when :change_stream
      ret = "<_ am:filter='ChangeStream[#{@tag[1].inspect}]'"
    when :put_stream
      ret = "<_ am:filter='PutStream[#{@tag[1].inspect}]'"
    else
      raise "can't happen #{@tag.inspect}"
    end
    if single_tag
      ret << " />"
    else
      ret << " >"
      @tag = "_"
    end
    return ret
  end
  ret = "<"
  if @tag
    ret << @tag
  else
    ret << "_"
  end
  if @src_name and @src_name.size > 0
    am_src = @opt[:am_src] || "am:src"
    if @filters
      ret << make_attr(am_src, "#@src_name|#@filters")
    else
      ret << make_attr(am_src, @src_name)
    end
  else
    if @filters
      am_filter = @opt[:am_filter] || "am:filter"
      ret << make_attr(am_filter, @filters)
    end
  end
  if @for
    am_for = @opt[:am_for] || "am:for"
    ret << make_attr(am_for, @for)
  end
  if @skipif
    am_skipif = @opt[:am_skipif] || "am:skipif"
    ret << make_attr(am_skipif, @skipif)
  end
  if @skipunless
    am_skipif = @opt[:am_skipunless] || "am:skipif"
    ret << make_attr(am_skipif, "not(#@skipunless)")
  end
  if @value
    am_v = @opt[:am_v] || "am:v"
    ret << make_attr(am_v, "HashDelegator.new($_) { {#@value} }")
  end
  ret << @attr.to_s
  if @erb
    if single_tag
      ret << "><![CDATA[<%= #{@erb} %>]]></#@tag>"
    else
      raise "not implemented"
    end
  else
    if single_tag
      ret << " />"
    else
      ret << " >"
    end
  end
  ret
end