<commit_before>require 'pstore'
require 'rack/static'

module Rack
  class Reshow
    
    @@store_file = 'reshow.ps'
    
    def initialize( app, opts = {} )
      root = Object::File.expand_path(Object::File.dirname(__FILE__))
      @app = Rack::Static.new(app, :urls => ["/__reshow__"], :root => root)
      @store = PStore.new(@@store_file)
    end
    
    def call( env )
      status, headers, body = @app.call(env)
      request = Request.new(env)
      if request.get?
        path = request.path
        if version = request.params['__reshow__']
          body = @store.transaction(true) do |store|
            store[path][version.to_i-1]
          end
        elsif body.respond_to? :scan
          @store.transaction do |store|
            store[path] ||= []
            content = body.scan(/<body>(.*?)<\/body>/m).flatten.first
            store[path] << content unless content.nil? or store[path].last.eql?(content)
            body.sub! /<body>.*<\/body>/m, "<body></body>"
            store[path][0..-2].each do |b|
              body.sub! /<\/body>/, encapsulate(b) + '</body>'
            end
            body.sub! /<\/body>/, encapsulate(store[path].last, true) + '</body>'
          end
          insert_reshow(body)
        end
      end
      [status, headers, body]
    end
    
    def []( path )
      @store.transaction(true) {|store| store[path] || Array.new}
    end
    
    def purge!
      Object::File.delete(@@store_file) if Object::File.exists?(@@store_file)
    end

    private

    def encapsulate(body, active=false)
      <<-EOF
      <div class="__reshow_body__ #{'__reshow_active__' if active}">
        #{body}
      </div>
      EOF
    end

    def insert_reshow(body)
      # Include the cute toolbar
      body.sub! /<\/body>/, toolbar + '</body>' 
      # Include the styling css
      body.sub! /<\/head>/, style + '</head>'
      # Include the jQuery library<commit_msg>Static content is now correctly served, courtesy of Rack::Bugs source code.<commit_after>require 'pstore'
require 'rack/static'

module Rack
  class Reshow

    class RackStaticBugAvoider
      def initialize(app, static_app)
        @app = app
        @static_app = static_app
      end

      def call(env)
        if env["PATH_INFO"]
          @static_app.call(env)
        else
          @app.call(env)
        end
      end
    end
    
    @@store_file = 'reshow.ps'
    
    def initialize( app, opts = {} )
      root = Object::File.expand_path(Object::File.dirname(__FILE__))
      @app = RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__reshow__"], :root => root))
      @store = PStore.new(@@store_file)
    end
    
    def call( env )
      status, headers, body = @app.call(env)
      response = Rack::Response.new(body, status, headers)
      request = Request.new(env)
      if request.get?
        path = request.path
        if version = request.params['__reshow__']
          body = @store.transaction(true) do |store|
            store[path][version.to_i-1]
          end
        elsif body.respond_to? :join
          body = body.join
          @store.transaction do |store|
            store[path] ||= []
            content = body.scan(/<body>(.*?)<\/body>/m).flatten.first
            store[path] << content unless content.nil? or store[path].last.eql?(content)
            body.sub! /<body>.*<\/body>/m, "<body></body>"
            store[path][0..-2].each do |b|
              body.sub! /<\/body>/, encapsulate(b) + '</body>'
            end
            body.sub! /<\/body>/, encapsulate(store[path].last, true) + '</body>'
          end
          insert_reshow(body)
          response['Content-Length'] = body.size.to_s
          response.body = [body]
        end
      end
      response.to_a
    end
    
    def []( path )
      @store.transaction(true) {|store| store[path] || Array.new}
    end
    
    def purge!
      Object::File.delete(@@store_file) if Object::File.exists?(@@store_file)
    end

    private

    def encapsulate(body, active=false)
      <<-EOF
      <div class="__reshow_body__ #{'__reshow_active__' if active}">
        #{body}
      </div>
      EOF
    end

    def insert_reshow(body)
      # Include the cute toolbar
      body.sub! /<\/body>/, toolbar + '</body>' 
      # Include the styling css
      body.sub! /<\/head>/, style + '</head>'
      # Include the jQuery library
{'fraction_non_alphanumeric': 0.15033127713045466, 'fraction_numerical': 0.0013708019191226869, 'mean_word_length': 2.0213940648723256, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3338756', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1396, 'n_words': 308}