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_AMVARS =
%[(#{S_FOR}|#{S_COND}|#{S_VALUE})?]
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.



1684
1685
1686
1687
1688
1689
1690
# File 'lib/amrita2/template.rb', line 1684

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.



1659
1660
1661
# File 'lib/amrita2/template.rb', line 1659

def attr
  @attr
end

#filtersObject (readonly)

Returns the value of attribute filters.



1659
1660
1661
# File 'lib/amrita2/template.rb', line 1659

def filters
  @filters
end

#forObject (readonly)

Returns the value of attribute for.



1660
1661
1662
# File 'lib/amrita2/template.rb', line 1660

def for
  @for
end

#skipifObject (readonly)

Returns the value of attribute skipif.



1660
1661
1662
# File 'lib/amrita2/template.rb', line 1660

def skipif
  @skipif
end

#skipunlessObject (readonly)

Returns the value of attribute skipunless.



1660
1661
1662
# File 'lib/amrita2/template.rb', line 1660

def skipunless
  @skipunless
end

#src_nameObject (readonly)

Returns the value of attribute src_name.



1659
1660
1661
# File 'lib/amrita2/template.rb', line 1659

def src_name
  @src_name
end

#tagObject (readonly)

Returns the value of attribute tag.



1659
1660
1661
# File 'lib/amrita2/template.rb', line 1659

def tag
  @tag
end

#valueObject (readonly)

Returns the value of attribute value.



1660
1661
1662
# File 'lib/amrita2/template.rb', line 1660

def value
  @value
end

Class Method Details

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



1680
1681
1682
# File 'lib/amrita2/template.rb', line 1680

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

Instance Method Details

#has_amxml_attrs?Boolean

Returns:

  • (Boolean)


1692
1693
1694
# File 'lib/amrita2/template.rb', line 1692

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

#make_attr(key, val) ⇒ Object



1837
1838
1839
1840
1841
1842
1843
# File 'lib/amrita2/template.rb', line 1837

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

#parse(s, trline = false) ⇒ Object



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
1731
1732
# File 'lib/amrita2/template.rb', line 1696

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



1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
# File 'lib/amrita2/template.rb', line 1753

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
  else
    raise "can't happen #{s}"
  end
end

#parse_css_selecter_likeObject



1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# File 'lib/amrita2/template.rb', line 1734

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



1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
# File 'lib/amrita2/template.rb', line 1772

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_skipif] || "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 single_tag
    ret << " />"
  else
    ret << " >"
  end
  ret
end