# # HTMLTemplate class # # Copyright (C) 2003-, All right reserved by Tasuku Suzuki # You can redistribute it and/or modify it under GPL2. # class HTMLTemplate def HTMLTemplate.open( template ) new( File::open(template, 'r').read ) end def initialize( template ) @template = template.dup end def dup HTMLTemplate.new( @template ) end def delete!( str ) @template.gsub!( str, "" ) end def gsub!( key, str ) @template.gsub!( Regexp.escape( key ), str.to_s ) self end def block( str ) str = Regexp.escape( str ) pattern = /(.*?)/m while @template =~ pattern @template.sub!( pattern ){ yield HTMLTemplate.new( $1 ) } end end def to_s @template.dup end end if $0 == __FILE__ str = "abcdef " str += "あいうえお" tmpl = HTMLTemplate.new(str) tmpl.gsub!( "bcd", "!!!" ) tmpl.block("a") do |block| block.delete!( "あ" ) print block.to_s end end