<commit_before>module Ki
  module Middleware #:nodoc:
    module BaseMiddleware
      include Helpers::FormatOf
      include Helpers::View
      include Helpers::PublicFile

      def initialize app
        @app = app
      end
    end

    class PublicFileServer
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if public_file_exists? req
          Rack::File.new(Ki::PUBLIC_PATH).call env
        else
          @app.call env
        end
      end
    end

    class InitMiddleware
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if req.root?
          resp = Rack::Response.new
          resp.redirect('/index')
          resp.finish
        else
          env['CONTENT_TYPE'] = 'application/json' if format_of(req) == 'json'
          @app.call env
        end
      end
    end

    class HamlCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if view_exists?(req)
          render_haml view_path(req)
        else
          @app.call env
        end
      end

      def render_haml file_path
        file_contents = File.read(file_path)

        if view_exists? 'layout'
          layout_contents = File.read(view_path('layout'))
        else
          layout_contents = "= yield"
        end

        html = haml(layout_contents).render do
          haml(file_contents).render
        end

        Rack::Response.new(html).finish
      end

      def haml s
        Haml::Engine.new("- extend Ki::Helpers\n" + s)
      end
    end

    class CoffeeCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        coffee_path = req.path.to_s[0...-3] + '.coffee'
        if !public_file_exists?(req) && format_of(req) == 'js' && public_file_exists?(coffee_path)
          js = CoffeeScript.compile(File.read(public_file_path(coffee_path)))
          Rack::Response.new(js).finish
        else
          @app.call env
        end
      end
    end

    class SassCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        sass_path = req.path.to_s[0...-4] + '.sass'
        # if req ends with css and it does not exist, if a sass file exists instead
        if !public_file_exists?(req) && format_of(req) == 'css' && public_file_exists?(sass_path)
          eng = Sass::Engine.new(File.read(public_file_path(sass_path)), :syntax => :sass)
          Rack::Response.new(eng.render).finish
        else<commit_msg>set sass and coffee middleware content type header<commit_after>module Ki
  module Middleware #:nodoc:
    module BaseMiddleware
      include Helpers::FormatOf
      include Helpers::View
      include Helpers::PublicFile

      def initialize app
        @app = app
      end
    end

    class PublicFileServer
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if public_file_exists? req
          Rack::File.new(Ki::PUBLIC_PATH).call env
        else
          @app.call env
        end
      end
    end

    class InitMiddleware
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if req.root?
          resp = Rack::Response.new
          resp.redirect('/index')
          resp.finish
        else
          env['CONTENT_TYPE'] = 'application/json' if format_of(req) == 'json'
          @app.call env
        end
      end
    end

    class HamlCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        if view_exists?(req)
          render_haml view_path(req)
        else
          @app.call env
        end
      end

      def render_haml file_path
        file_contents = File.read(file_path)

        if view_exists? 'layout'
          layout_contents = File.read(view_path('layout'))
        else
          layout_contents = "= yield"
        end

        html = haml(layout_contents).render do
          haml(file_contents).render
        end

        Rack::Response.new(html).finish
      end

      def haml s
        Haml::Engine.new("- extend Ki::Helpers\n" + s)
      end
    end

    class CoffeeCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        coffee_path = req.path.to_s[0...-3] + '.coffee'
        if !public_file_exists?(req) && format_of(req) == 'js' && public_file_exists?(coffee_path)
          js = CoffeeScript.compile(File.read(public_file_path(coffee_path)))
          Rack::Response.new(js, 200, {"Content-Type" => "application/javascript"}).finish
        else
          @app.call env
        end
      end
    end

    class SassCompiler
      include BaseMiddleware

      def call env
        req = BaseRequest.new env
        sass_path = req.path.to_s[0...-4] + '.sass'
        # if req ends with css and it does not exist, if a sass file exists instead
        if !public_file_exists?(req) && format_of(req) == 'css' && public_file_exists?(sass_path)
          eng = Sass::Engine.new(File.read(public_file_path(sass_path)), :syntax => :sass)
          Rack::Response.new(eng.render, 200, {"Content-Type" => "text/css"}).finish
        else
{'fraction_non_alphanumeric': 0.08843406062946514, 'fraction_numerical': 0.0027032245607260088, 'mean_word_length': 1.9133858267716535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3583805', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1525, 'n_words': 380}