Sha256: 889c9bf275895d74f236c4c506a14c43b64c91f342affe7f140914dc5b64062b

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Contextual
  class SafeErubis < ::Erubis::Eruby
    BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/

      def add_preamble(src)
        src << "@output_buffer = output_buffer || Erubis::ContextualBuffer.new; "
      end

    def add_text(src, text)
      if !text.empty?
        src << "@output_buffer.concat('" << text.to_s.gsub("'", "\\\\'") << "');"
      end
    end

    def add_expr_literal(src, code)
      if code =~ BLOCK_EXPR
        src << '@output_buffer.append= ' << code
      else
        src << <<-SRC
              val = (#{code.to_s});
              if (val.html_safe?);
                @output_buffer.append=(val);
              else;
                @output_buffer.safe_append=(val);
              end;
        SRC
      end
    end

    def add_expr_escaped(src, code)
      if code =~ BLOCK_EXPR
        src << "@output_buffer.append= " << code
      else
        src << "@output_buffer.append(" << code << ");"
      end
    end

    def add_postamble(src)
      src << "@output_buffer.close \n"
      # src << "p [:CONTEXTUAL,@output_buffer, @output_buffer.to_s, @output_buffer.to_s.html_safe.html_safe?]\n"
      src << "@output_buffer.to_s.html_safe"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contextual-0.0.2-java lib/contextual/safe_erubis.rb