# Copyright (C) 2003, 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

Match = /(<%%[\t ]*)|([\t ]*%%>)|(<%=[\t ]*)|(<%#[\t ]*)|(<%[\t ]*)|([\t ]*%>)|(<\/?[A-Z][A-Za-z_]*:[a-z][A-Za-z_]*\s*(?:[^\/]|"[^"]*"|'[^']*')*\/?>)/

SubMatch = /<\/?[A-Z][A-Za-z_]*:[a-z][A-Za-z_]*\s*(?:[^\/]|"[^"]*"|'[^']*')*\/?>/
SubMatchOpen = /<[A-Z][A-Za-z_]*:[a-z][A-Za-z_]*\s*.*[^\/]>/
SubMatchClose = /<\/[A-Z][A-Za-z_]*:[a-z][A-Za-z_]*\s*.*[^\/]>/
SubMatchAlone = /<[A-Z][A-Za-z_]*:[a-z][A-Za-z_]*\s*.*[^\/]\/>/

OpenASP = /(<%%)|(<%=)|(<%#)|(<%)/
CloseASP = /(%%>)|(%>)/

def makeargs( data ) 
  lxTagData = data.gsub( /[<\/>]/, "" ).gsub( /\s*=\s*/, "=" ).split( " " )
  xOut = lxTagData.shift.gsub( /:/, "::" ).gsub( /-/, "_" ) + "( { "
  xSeparator = ""
  vOldValue = nil
  lxTagData.each { |a|
    xArgKey, xArgValue = a.split( "=" )
    if xArgValue == nil
	  if vOldValue.nil? == false and ( vOldValue[0,1] == '"' or vOldValue[0,1] == "'" )
	    xOut += " " + xArgKey
		vOldValue += " " + xArgKey
        xSeparator = ", "
	  else
	    xOut += xSeparator + "'" + xArgKey + "' => true"
        xSeparator = ", "
	  end
	else
      xOut += xSeparator + "'" + xArgKey + "' => " + xArgValue
      xSeparator = ", "
	  vOldValue = xArgValue
	end
  } 
  xOut += " } )"
  
  return xOut
end

def submatch( data )
  iLine = 0
  bIgnore = false
  bOpen = false
  name = ""
  xCode = ""

  data.each do |line| 
    xKeepLine = line
    line.chomp!
    iLine = iLine + 1

    while res = Match.match( line )
      preTag = res.pre_match
      matchTag = res[0]
      line = res.post_match

      if SubMatch.match( matchTag )
        if bIgnore == true
          xAddCode = matchTag
        else
          if SubMatchOpen.match( matchTag ) and bOpen == false
            name = matchTag.gsub( /[<\/>]/, "" ).split( / / )[0]
            bOpen = true
            xAddCode = "<% "+makeargs( matchTag )+" { %>"
          elsif SubMatchClose.match( matchTag ) and bOpen == true and matchTag.gsub( /[<\/>]/, "" ) == name
            name = ""
            bOpen = false
            xAddCode = "<% } %>"
          elsif SubMatchAlone.match( matchTag ) and bOpen == false
            name = ""
            bOpen = false
            xAddCode = "<% "+makeargs( matchTag )+" %>"
          else
            @error = true
            @error_line = iLine
            @error_code = @init_code
            @error_message = (bOpen == true)?sprintf( "Found tag %s in open tag %s", matchTag, name):sprintf( "Error tag %s", matchTag )
            return nil
          end
        end
      elsif OpenASP.match( matchTag )
        bIgnore = true
        xAddCode = matchTag
      elsif CloseASP.match( matchTag )
        bIgnore = false
        xAddCode = matchTag
      end

      xCode = xCode + preTag + xAddCode
    end
    if line.nil? == false and line.length > 0
      xCode = xCode + line
    end
    xCode = xCode + "\n"
    
  end

  return xCode
end
