Sha256: 28537702482cbbc59202d0ff7ab56633ab57f7d34ecac8788247d2da20d1bb57

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

module Nitro::Compiler

# A module that expand xhtml tags to ruby code
# according to some tag attributes. (if, unless, each)
#--
# FIXME: nested tags are not handled correctly. We should
# reimplement this using REXML or something.
#++

module Morphing

  def self.transform(input_text)
    text = input_text.dup
    # <tag if="x">..</tag>
    # <tag unless="x">..</tag>
    #
    # example: 
    #
    # <div prop1="one" if="@mycond" prop2="two">@mycond is true</div>
    #
    # becomes
    #
    # <?r if @mycond ?>
    #   <div prop1="one" prop2="two">@mycond is true</div>
    # <?r end ?>

    text.gsub!(/<(\w*?)([^>]*?)(if|unless)=["|'](.*?)["|'](.*?)>(.*?)<\/\1>/m) do |match|
      %{<?r #$3 #$4 ?>
        <#$1#$2#$5>#$6</#$1>
      <?r end ?>}
    end

    # <tag times="x">..</tag>

    text.gsub!(/<(\w*?)([^>]*?)times=["|'](.*?)["|'](.*?)>(.*?)<\/\1>/m) do |match|
      %{<?r #$3.times do ?>
        <#$1#$2#$4>#$5</#$1>
      <?r end ?>}
    end

    # <tag each="x">..</tag>
    #
    # example:
    #
    # <li each="item in array">my item is #{item}</li>
    #
    # becomes
    #
    # <?r for item in array ?>
    #   <li>my item is #{item}</li>
    # <?r end ?>

    text.gsub!(/<(\w*?)([^>]*?)each=["|'](.*?)["|'](.*?)>(.*?)<\/\1>/m) do |match|
      %{<?r for #$3 ?>
        <#$1#$2#$4>#$5</#$1>
      <?r end ?>}
    end
    
    text
  end

end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.24.0 lib/nitro/compiler/morphing.rb