<commit_before>require 'sinatra/base'
require 'haml'

module Hstatic
  BASEDIR = File.dirname(__FILE__) + '/../../'

  class App < Sinatra::Base
    configure do
      set :views, BASEDIR + 'res'
    end

    helpers do
      def dynamic_size(size)
        if size > Math.ldexp(1, 40)
          sprintf("%5.2f", size / Math.ldexp(1, 40)) + ' GB'
        elsif (size / Math.ldexp(1, 20)) > 0.1
          sprintf("%5.2f", size / Math.ldexp(1, 20)).to_s + ' MB'
        else
          sprintf("%5.2f", size / Math.ldexp(1, 10)).to_s + ' kB'
        end
      end
    end

    # Going around bootstrap
    get '/.res/bootstrap.min.css' do
      send_file BASEDIR + 'res/bootstrap.min.css'
    end
    get '/.res/style.css' do
      send_file BASEDIR + 'res/style.css'
    end
    get '/.res/jquery.min.js' do
      send_file BASEDIR + 'res/jquery.min.js'
    end
    get '/fonts/glyphicons-halflings-regular.woff' do
      send_file BASEDIR + 'res/glyphicons-halflings-regular.woff'
    end
    get '/fonts/glyphicons-halflings-regular.ttf' do
      send_file BASEDIR + 'res/glyphicons-halflings-regular.ttf'
    end
    get '/fonts/glyphicons-halflings-regular.svg' do
      send_file BASEDIR + 'res/glyphicons-halflings-regular.svg'
    end

    get '*' do
      path = File.expand_path(Dir.pwd + unescape(request.path_info))
      if File.file? path
        send_file path
      elsif File.exists? File.expand_path(path + '/index.html')
        redirect request.path_info + '/index.html'
      elsif File.directory? path
        @folders = Array.new
        @files = Array.new
        @parent = File.dirname request.path_info
        Dir.foreach(path) do |entry|
          next if entry == '.' or entry == '..'
          url_base = ""
          url_base = request.path_info unless request.path_info == '/'
          if File.directory? File.expand_path(path + '/' + entry)
            @folders << {name: entry, href: url_base + '/' + entry}
          else
            @files << {name: unescape(entry),
                       size: dynamic_size(File.size(File.expand_path(path + '/' + entry))),
                       href: url_base + '/' + entry}
          end
        end
        haml :index
      else
        'not found'
      end
    end
  end
end
<commit_msg>app: improved file paths handling<commit_after>require 'sinatra/base'
require 'haml'

module Hstatic
  BASEDIR = File.join File.dirname(__FILE__), '..', '..'

  class App < Sinatra::Base
    configure do
      set :views, File.join(BASEDIR, 'res')
    end

    helpers do
      def dynamic_size(size)
        if size > Math.ldexp(1, 40)
          sprintf("%5.2f", size / Math.ldexp(1, 40)) + ' GB'
        elsif (size / Math.ldexp(1, 20)) > 0.1
          sprintf("%5.2f", size / Math.ldexp(1, 20)).to_s + ' MB'
        else
          sprintf("%5.2f", size / Math.ldexp(1, 10)).to_s + ' kB'
        end
      end
    end

    # Going around bootstrap
    get '/.res/bootstrap.min.css' do
      send_file(File.join BASEDIR, 'res/bootstrap.min.css')
    end
    get '/.res/style.css' do
      send_file(File.join BASEDIR, 'res/style.css')
    end
    get '/.res/jquery.min.js' do
      send_file(File.join BASEDIR, 'res/jquery.min.js')
    end
    get '/fonts/glyphicons-halflings-regular.woff' do
      send_file(File.join BASEDIR, 'res/glyphicons-halflings-regular.woff')
    end
    get '/fonts/glyphicons-halflings-regular.ttf' do
      send_file(File.join BASEDIR, 'res/glyphicons-halflings-regular.ttf')
    end
    get '/fonts/glyphicons-halflings-regular.svg' do
      send_file(File.join BASEDIR, 'res/glyphicons-halflings-regular.svg')
    end

    get '*' do
      path = File.expand_path(File.join Dir.pwd, unescape(request.path_info))
      if File.file? path
        send_file path
      elsif File.exists? File.expand_path(File.join path, 'index.html')
        redirect(File.join request.path_info, 'index.html')
      elsif File.directory? path
        @folders = Array.new
        @files = Array.new
        @parent = File.dirname request.path_info
        Dir.foreach(path) do |entry|
          next if entry == '.' or entry == '..'
          url_base = File.join "/", request.path_info
          if File.directory? File.expand_path(File.join path, entry)
            @folders << {name: entry, href: File.join(url_base, entry)}
          else
            @files << {name: unescape(entry),
                       size: dynamic_size(File.size(File.expand_path(File.join path, entry))),
                       href: File.join(url_base, entry)}
          end
        end
        haml :index
      else
        'not found'
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1319489508976855, 'fraction_numerical': 0.009950248756218905, 'mean_word_length': 2.0602250165453344, 'pattern_counts': {'":': 0, '<': 13, '<?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': '7013778', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1554, 'n_words': 339}