<commit_before>module Middleman
  
  WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)

  module PreviewServer
    
    DEFAULT_PORT = 4567
    
    class << self
      
      # Start an instance of Middleman::Application
      # @return [void]
      def start(options={})
        require "webrick"
        
        app = ::Middleman::Application.server.inst do
          if options[:environment]
            set :environment, options[:environment].to_sym
          end
          
          if options[:debug]
            set :logging, true
          end
        end
    
        puts "== The Middleman is standing watch on port #{options[:port]||4567}"

        @webrick ||= setup_webrick(
          options[:host]  || "0.0.0.0",
          options[:port]  || DEFAULT_PORT,
          options[:debug] || false
        )
        
        mount_instance(app)
        
        @initialized ||= false
        unless @initialized
          @initialized = true
          
          register_signal_handlers unless ::Middleman::WINDOWS
          
          start_file_watcher unless options[:"disable-watcher"]

          # Save the last-used options so it may be re-used when
          # reloading later on.
          @last_options = options
          
          @webrick.start
        end
      end

      # Detach the current Middleman::Application instance
      # @return [void]
      def stop
        puts "== The Middleman is shutting down"
        unmount_instance
      end
    
      # Simply stop, then start the server
      # @return [void]
      def reload
        stop
        start @last_options
      end

      # Stop the current instance, exit Webrick
      # @return [void]
      def shutdown
        stop
        @webrick.shutdown
      end
      
    private
      
      def start_file_watcher
        # Watcher Library
        require "listen"
    
        listener = Listen.to(Dir.pwd, :relative_paths => true)
      
        listener.change do |modified, added, removed|
          added_and_modified = (modified + added)

          if added_and_modified.length > 0
            # See if the changed file is config.rb or lib/*.rb
            return reload if needs_to_reload?(added_and_modified)

            # Otherwise forward to Middleman
            added_and_modified.each do |path|
              @app.files.did_change(@app.root_path + path)
            end
          end
      
          if removed.length > 0
            # See if the changed file is config.rb or lib/*.rb
            return reload if needs_to_reload?(removed)

            # Otherwise forward to Middleman
            removed.each do |path|
              @app.files.did_delete(@app.root_path + path)
            end
          end
        end
    
        # Don't block this thread
        listener.start(false)
      end
      
      # Trap the interupt signal and shut down smoothly
      # @return [void]
      def register_signal_handlers
        trap("INT")  { shutdown }
        trap("TERM") { shutdown }
        trap("QUIT") { shutdown }
      end
      <commit_msg>Restart the listener when reloading the server<commit_after>module Middleman
  
  WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)

  module PreviewServer
    
    DEFAULT_PORT = 4567
    
    class << self
      
      # Start an instance of Middleman::Application
      # @return [void]
      def start(options={})
        require "webrick"
        
        app = ::Middleman::Application.server.inst do
          if options[:environment]
            set :environment, options[:environment].to_sym
          end
          
          if options[:debug]
            set :logging, true
          end
        end
    
        puts "== The Middleman is standing watch on port #{options[:port]||4567}"

        @webrick ||= setup_webrick(
          options[:host]  || "0.0.0.0",
          options[:port]  || DEFAULT_PORT,
          options[:debug] || false
        )
        
        mount_instance(app)

        start_file_watcher unless options[:"disable-watcher"]
        
        @initialized ||= false
        unless @initialized
          @initialized = true
          
          register_signal_handlers unless ::Middleman::WINDOWS
          
          # Save the last-used options so it may be re-used when
          # reloading later on.
          @last_options = options
          
          @webrick.start
        end
      end

      # Detach the current Middleman::Application instance
      # @return [void]
      def stop
        puts "== The Middleman is shutting down"
        unmount_instance
      end
    
      # Simply stop, then start the server
      # @return [void]
      def reload
        stop
        start @last_options
      end

      # Stop the current instance, exit Webrick
      # @return [void]
      def shutdown
        stop
        @webrick.shutdown
      end
      
    private
      
      def start_file_watcher
        # Watcher Library
        require "listen"
    
        listener = Listen.to(Dir.pwd, :relative_paths => true)
      
        listener.change do |modified, added, removed|
          added_and_modified = (modified + added)

          if added_and_modified.length > 0
            # See if the changed file is config.rb or lib/*.rb
            if needs_to_reload?(added_and_modified)
              reload
              return listener.stop
            end

            # Otherwise forward to Middleman
            added_and_modified.each do |path|
              @app.files.did_change(@app.root_path + path)
            end
          end
      
          if removed.length > 0
            # See if the changed file is config.rb or lib/*.rb
            if needs_to_reload?(removed)
              reload
              return listener.stop
            end

            # Otherwise forward to Middleman
            removed.each do |path|
              @app.files.did_delete(@app.root_path + path)
            end
          end
        end
    
        # Don't block this thread
        listener.start(false)
      end
      
      # Trap the interupt signal and shut down smoothly
      # @return [void]
      def register_signal_handlers
        trap("INT")  { shutdown }
        trap("TERM") { shutdown }
        trap("QUIT") { shutdown }
      end
      
{'fraction_non_alphanumeric': 0.07766074516585443, 'fraction_numerical': 0.005030655557302311, 'mean_word_length': 1.5684295518772708, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2007788', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1706, 'n_words': 472}