<commit_before># This file implements redirection support

module Hardwired
    
  module Aliases
    extend Sinatra::Extension

    # set some settings for development
    #configure :development do
    #  set :reload_stuff, true
    #end

    # Add a before filter to perform any redirects requested by individual pages

    before do

      #return if response.status != 404
      this_url = AliasTable.normalize(request.fullpath)
      table = AliasTable.all
      if table.include?(this_url)
        redirect table[this_url], 301 # Always do permanent redirects
      end
    end

    ## Todo - refactor to self.class instance instead of static?
    class AliasTable

      # Cache the redirects table in a static variable
      def self.all
        @@all ||= AliasTable.build_alias_table
      end
      
      def self.build_alias_table
        table = {}
        Index.files.each do |p| 
          dest = p.path
          if p.meta.redirect_to
            dest = p.meta.redirect_to
            table[AliasTable.normalize(p.abspath)] = dest
          end
          p.aliases.each  do |url| 
            table[AliasTable.normalize(url)] = dest
          end
        end
        return table
      end
      
      def self.normalize(url)
        return url.gsub("+"," ").downcase #TODO: Normalize url encoding
      end
    end
  end 
end
<commit_msg>Added cyclic alias detection. Added support for trailing slash normalization on aliases (both URLs with and without are matched)<commit_after># This file implements redirection support

module Hardwired
    
  module Aliases
    extend Sinatra::Extension

    # set some settings for development
    #configure :development do
    #  set :reload_stuff, true
    #end

    # Add a before filter to perform any redirects requested by individual pages

    before do

      #return if response.status != 404
      this_url = AliasTable.normalize(request.fullpath)
      table = AliasTable.all
      if table.include?(this_url)
        redirect table[this_url], 301 # Always do permanent redirects
      end
    end

    ## Todo - refactor to self.class instance instead of static?
    class AliasTable

      # Cache the redirects table in a static variable
      def self.all
        @@all ||= AliasTable.build_alias_table
      end
      
      def self.build_alias_table
        table = {}
        Index.files.each do |p| 
          dest = p.path
          if p.meta.redirect_to
            dest = p.meta.redirect_to
            table[AliasTable.normalize(p.abspath)] = dest
          end
          p.aliases.each  do |url| 
            #raise "Invalid alias #{url} for #{p.path} will cause a cyclic redirect" 
            next if  (AliasTable.normalize(url) == AliasTable.normalize(dest))
            table[AliasTable.normalize(url)] = dest
            table[AliasTable.normalize(url) + '/'] = dest
          end
        end
        return table
      end
      
      def self.normalize(url)
        return url.gsub("+"," ").sub(/(\/)+$/,'').downcase #TODO: Normalize url encoding
      end
    end
  end 
end

{'fraction_non_alphanumeric': 0.07592049527533398, 'fraction_numerical': 0.0039100684261974585, 'mean_word_length': 2.0854271356783918, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5685521', 'n_tokens_mistral': 892, 'n_tokens_neox': 850, 'n_words': 271}