<commit_before>require 'webrick'

module Nanoc

  class AutoCompiler

    ERROR_404 = <<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>404 File Not Found</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>404 File Not Found</h1>
		<p>The file you requested, <i><%=h path %></i>, was not found on this server.</p>
	</body>
</html>
END

    ERROR_500 = <<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>500 Server Error</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>500 Server Error</h1>
		<p>An error occurred while compiling the page you requested, <i><%=h path %></i>.</p>
		<p>If you think this is a bug in nanoc, please do <a href="http://nanoc.stoneship.org/trac/newticket">report it</a>&mdash;thanks!</p>
		<p>Message:</p>
		<blockquote><p><%=h exception.message %></p></blockquote>
		<p>Backtrace:</p>
		<ol>
<% exception.backtrace.each do |line| %>
			<li><%= line %></li>
<% end %>
		</ol>
	</body>
</html>
END

    def initialize(site)
      # Set site
      @site = site
    end

    def start(port)
      nanoc_require('mime/types', "'mime/types' is required to autocompile sites.")
      
      # Create server
      @server = WEBrick::HTTPServer.new(:Port => port || 3000)
      @server.mount_proc("/") { |request, response| handle_request(request, response) }

      # Start server
      trap('INT') { @server.shutdown }
      @server.start
    end

    def handle_request(request, response)
      # Reload site data
      @site.load_data(true)

      # Get page or file
      page      = @site.pages.find { |page| page.path == request.path }
      file_path = @site.config[:output_dir] + request.path

      if page.nil?
        # Serve file
        if File.exist?(file_path)
          serve_file(file_path, response)
        else
          serve_404(request.path, response)
        end
      else
        # Serve page
        serve_page(page, response)
      end
    end

    def h(s)
      ERB::Util.html_escape(s)
    end

    def serve_404(path, response)
      response.status           = 404
      response['Content-Type']  = 'text/html'
      response.body             = ERB.new(ERROR_404).result(binding)
    end

    def serve_500(path, exception, response)
      response.status           = 500
      response['Content-Type']  = 'text/html'
      response.body             = ERB.new(ERROR_500).result(binding)
    end<commit_msg>autocompiler no longer tries to serve directories (only files)<commit_after>require 'webrick'

module Nanoc

  class AutoCompiler

    ERROR_404 = <<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>404 File Not Found</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>404 File Not Found</h1>
		<p>The file you requested, <i><%=h path %></i>, was not found on this server.</p>
	</body>
</html>
END

    ERROR_500 = <<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>500 Server Error</title>
		<style type="text/css">
			body { padding: 10px; border: 10px solid #f00; margin: 10px; font-family: Helvetica, Arial, sans-serif; }
		</style>
	</head>
	<body>
		<h1>500 Server Error</h1>
		<p>An error occurred while compiling the page you requested, <i><%=h path %></i>.</p>
		<p>If you think this is a bug in nanoc, please do <a href="http://nanoc.stoneship.org/trac/newticket">report it</a>&mdash;thanks!</p>
		<p>Message:</p>
		<blockquote><p><%=h exception.message %></p></blockquote>
		<p>Backtrace:</p>
		<ol>
<% exception.backtrace.each do |line| %>
			<li><%= line %></li>
<% end %>
		</ol>
	</body>
</html>
END

    def initialize(site)
      # Set site
      @site = site
    end

    def start(port)
      nanoc_require('mime/types', "'mime/types' is required to autocompile sites.")
      
      # Create server
      @server = WEBrick::HTTPServer.new(:Port => port || 3000)
      @server.mount_proc("/") { |request, response| handle_request(request, response) }

      # Start server
      trap('INT') { @server.shutdown }
      @server.start
    end

    def handle_request(request, response)
      # Reload site data
      @site.load_data(true)

      # Get page or file
      page      = @site.pages.find { |page| page.path == request.path }
      file_path = @site.config[:output_dir] + request.path

      if page.nil?
        # Serve file
        if File.file?(file_path)
          serve_file(file_path, response)
        else
          serve_404(request.path, response)
        end
      else
        # Serve page
        serve_page(page, response)
      end
    end

    def h(s)
      ERB::Util.html_escape(s)
    end

    def serve_404(path, response)
      response.status           = 404
      response['Content-Type']  = 'text/html'
      response.body             = ERB.new(ERROR_404).result(binding)
    end

    def serve_500(path, exception, response)
      response.status           = 500
      response['Content-Type']  = 'text/html'
      response.body             = ERB.new(ERROR_500).result(binding)
    end
{'fraction_non_alphanumeric': 0.15442764578833693, 'fraction_numerical': 0.026997840172786176, 'mean_word_length': 2.6201954397394136, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4016962', 'n_tokens_mistral': 2139, 'n_tokens_neox': 1902, 'n_words': 473}