<commit_before>module Jekyll
  class ShowDataGenerator < Jekyll::Generator
    priority :highest

    def generate(site)
      puts "Processing shows..."
      all_shows = site.collections["shows"].docs
      years = site.collections["years"].docs

      # This is here as it's needed here, I expect year.rb to be run after.
      years_by_slug = Hash.new
      for year in years
        years_by_slug[year.data["year"]] = year
      end

      # Compute extra show data attributes
      shows_by_year = Hash.new
      for show, index in all_shows.each_with_index do
        path_split = show.path.split("/")
        year = path_split[path_split.length-2]

        # Set year attributes
        show.data["year"] = year
        show.data["year_page"] = years_by_slug[show.data["year"]]

        # To put content in meta description
        show.data["excerpt"] = show.content

        # Generate the legacy path for 301 redirect re. #142 Make semantic and pretty urls
        legacy_path = "shows/#{show.data["year"]}/#{show.basename_without_ext}.html"
        show.data["redirect_from"] = legacy_path

        if show.data.has_key?("cast") and show.data["cast"].class == Array
          fill_people_reverse_index(show, show.data["cast"], "people_ri_shows", site) end
        if show.data.has_key?("crew") and show.data["crew"].class == Array
          fill_people_reverse_index(show, show.data["crew"], "people_ri_shows", site) end

      end

      # Sort Shows
      sorted_shows = all_shows.sort_by do | show |
        if show.data.has_key?("season_sort")
          # Sort by year, then by season_sort
          [show.data["year_page"].data["sort"], show.data["season_sort"].to_i]
        else
          # If no season_sort, assume 1000
          [show.data["year_page"].data["sort"], 1000]
        end
      end

      for show, index in sorted_shows.each_with_index do
        # Put show into an array as a member of the shows_byt_year hash
        # Create that array if this is the first time we've touched this year
        (shows_by_year[show.data["year"]] ||= []) << show

        # Set sort dependant attributes
        show.data["index"] = index
        show.data["next"] = sorted_shows[index + 1]
        show.data["previous"] = sorted_shows[index - 1]

        # Set the show poster attribute, see #117
        if show.data["assets"]

          # Find assets that fit the criteria for being a display image
          override_assets = show.data["assets"].select { |i| i["display_image"] == true }
          posters = show.data["assets"].select { |i| i["type"] == "poster" }
          flyers = show.data["assets"].select { |i|
            if i.has_key?("page")
              i["type"] == "flyer" and not i["page"] > 1
            else
              i["type"] == "flyer"
            end
          }
          programmes = show.data["assets"].select { |i|
            if i.has_key?("page")
              i["type"] == "programme" and not i["page"] > 1
            else
              i["type"] == "programme"
            end
          }
<commit_msg>Ensure cast/crew reverse index records are sorted like shows<commit_after>module Jekyll
  class ShowDataGenerator < Jekyll::Generator
    priority :highest

    def generate(site)
      puts "Processing shows..."
      all_shows = site.collections["shows"].docs
      years = site.collections["years"].docs

      # This is here as it's needed here, I expect year.rb to be run after.
      years_by_slug = Hash.new
      for year in years
        years_by_slug[year.data["year"]] = year
      end

      # Compute extra show data attributes
      shows_by_year = Hash.new
      for show, index in all_shows.each_with_index do
        path_split = show.path.split("/")
        year = path_split[path_split.length-2]

        # Set year attributes
        show.data["year"] = year
        show.data["year_page"] = years_by_slug[show.data["year"]]

        # To put content in meta description
        show.data["excerpt"] = show.content

        # Generate the legacy path for 301 redirect re. #142 Make semantic and pretty urls
        legacy_path = "shows/#{show.data["year"]}/#{show.basename_without_ext}.html"
        show.data["redirect_from"] = legacy_path

      end

      # Sort Shows
      sorted_shows = all_shows.sort_by do | show |
        if show.data.has_key?("season_sort")
          # Sort by year, then by season_sort
          [show.data["year_page"].data["sort"], show.data["season_sort"].to_i]
        else
          # If no season_sort, assume 1000
          [show.data["year_page"].data["sort"], 1000]
        end
      end

      for show, index in sorted_shows.each_with_index do
        # Put show into an array as a member of the shows_byt_year hash
        # Create that array if this is the first time we've touched this year
        (shows_by_year[show.data["year"]] ||= []) << show

        # Set sort dependant attributes
        show.data["index"] = index
        show.data["next"] = sorted_shows[index + 1]
        show.data["previous"] = sorted_shows[index - 1]

        # Extract cast/crew data for reverse indexing
        if show.data.has_key?("cast") and show.data["cast"].class == Array
          fill_people_reverse_index(show, show.data["cast"], "people_ri_shows", site) end
        if show.data.has_key?("crew") and show.data["crew"].class == Array
          fill_people_reverse_index(show, show.data["crew"], "people_ri_shows", site) end

        # Set the show poster attribute, see #117
        if show.data["assets"]

          # Find assets that fit the criteria for being a display image
          override_assets = show.data["assets"].select { |i| i["display_image"] == true }
          posters = show.data["assets"].select { |i| i["type"] == "poster" }
          flyers = show.data["assets"].select { |i|
            if i.has_key?("page")
              i["type"] == "flyer" and not i["page"] > 1
            else
              i["type"] == "flyer"
            end
          }
          programmes = show.data["assets"].select { |i|
            if i.has_key?("page")
              i["type"] == "programme" and not i["page"] > 1
            else
              i["type"] == "programme"
            end
          }

{'fraction_non_alphanumeric': 0.12429651069303746, 'fraction_numerical': 0.007075092458594629, 'mean_word_length': 2.2736842105263158, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7438623', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1851, 'n_words': 496}