<commit_before>module Middleman
  module MetaTags
    module Helpers
      def meta_tags
        @meta_tags ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def set_meta_tags(meta_tags = {})
        self.meta_tags.merge! meta_tags
      end

      def description(description = nil)
        set_meta_tags(description: description) unless description.nil?
      end

      def keywords(keywords = nil)
        set_meta_tags(keywords: keywords) unless keywords.nil?
      end

      def title(title = nil)
        set_meta_tags(title: title) unless title.nil?
      end

      def display_meta_tags(default = {})
        result    = []
        meta_tags = default.merge(self.meta_tags).with_indifferent_access

        title = full_title(meta_tags)
        meta_tags.delete(:title)
        meta_tags.delete(:separator)
        result << content_tag(:title, title) unless title.blank?

        description = safe_description(meta_tags.delete(:description))
        result << tag(:meta, name: :description, content: description) unless description.blank?

        keywords = meta_tags.delete(:keywords)
        result << tag(:meta, name: :keywords, content: keywords) unless keywords.blank?

        meta_tags.each do |name, content|
          if name.start_with?('og:')
            result << tag(:meta, property: name, content: content ) unless content.blank?
          else
            result << tag(:meta, name: name, content: content ) unless content.blank?
          end
        end

        result = result.join("\n")
        result.html_safe
      end

      def auto_display_meta_tags(default = {})
        auto_tag

        display_meta_tags default
      end

      def auto_tag
        site_data = (data['site'] || {}).with_indifferent_access

        set_meta_tags site: site_data['site']
        set_meta_tags 'og:site_name' => site_data['site']

        fall_through(site_data, :title, 'title')
        fall_through(site_data, :description, 'description')
        fall_through(site_data, :keywords, 'keywords')

        # Twitter cards
        fall_through(site_data, 'twitter:card', 'twitter_card', 'summary_large_image')
        fall_through(site_data, 'twitter:creator', 'twitter_author')
        fall_through(site_data, 'twitter:description', 'description')
        fall_through(site_data, 'twitter:image:src', 'pull_image')
        fall_through(site_data, 'twitter:site', 'publisher_twitter')
        fall_through(site_data, 'twitter:title', 'title')

        # Open Graph
        fall_through(site_data, 'og:description', 'description')
        fall_through(site_data, 'og:image', 'pull_image')
        fall_through(site_data, 'og:title', 'title')
      end

    private

      def fall_through(site_data, name, key, default = nil)
        need_customized = site_data[:customize_by_frontmatter]
        value = self.meta_tags[key] ||
                (need_customized && current_page.data[key]) ||
                site_data[key] ||
                default
        set_meta_tags name => value unless value.blank?
        value
      end

      def full_title(meta_tags)
        separator   = meta_tags[:separator] || '|'
        full_title  = ''
        title       = safe_title(meta_tags[:title])

        (full_title << title) unless title.blank?
        (full_title << " #{separator} ") unless title.blank? || meta_tags[:site].blank?
        (full_title << meta_tags[:site]) unless meta_tags[:site].blank?
        full_title
      end

      def safe_description(description)
        truncate(strip_tags(description), length: 200)
      end

      def safe_title(title)
        title = strip_tags(title)
      end
    end
  end
end
<commit_msg>Keywords can be an array<commit_after>module Middleman
  module MetaTags
    module Helpers
      def meta_tags
        @meta_tags ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      def set_meta_tags(meta_tags = {})
        self.meta_tags.merge! meta_tags
      end

      def description(description = nil)
        set_meta_tags(description: description) unless description.nil?
      end

      def keywords(keywords = nil)
        set_meta_tags(keywords: keywords) unless keywords.nil?
      end

      def title(title = nil)
        set_meta_tags(title: title) unless title.nil?
      end

      def display_meta_tags(default = {})
        result    = []
        meta_tags = default.merge(self.meta_tags).with_indifferent_access

        title = full_title(meta_tags)
        meta_tags.delete(:title)
        meta_tags.delete(:separator)
        result << content_tag(:title, title) unless title.blank?

        description = safe_description(meta_tags.delete(:description))
        result << tag(:meta, name: :description, content: description) unless description.blank?

        keywords = meta_tags.delete(:keywords)
        keywords = keywords.join(', ') if keywords.is_a?(Array)
        result << tag(:meta, name: :keywords, content: keywords) unless keywords.blank?

        meta_tags.each do |name, content|
          if name.start_with?('og:')
            result << tag(:meta, property: name, content: content ) unless content.blank?
          else
            result << tag(:meta, name: name, content: content ) unless content.blank?
          end
        end

        result = result.join("\n")
        result.html_safe
      end

      def auto_display_meta_tags(default = {})
        auto_tag

        display_meta_tags default
      end

      def auto_tag
        site_data = (data['site'] || {}).with_indifferent_access

        set_meta_tags site: site_data['site']
        set_meta_tags 'og:site_name' => site_data['site']

        fall_through(site_data, :title, 'title')
        fall_through(site_data, :description, 'description')
        fall_through(site_data, :keywords, 'keywords')

        # Twitter cards
        fall_through(site_data, 'twitter:card', 'twitter_card', 'summary_large_image')
        fall_through(site_data, 'twitter:creator', 'twitter_author')
        fall_through(site_data, 'twitter:description', 'description')
        fall_through(site_data, 'twitter:image:src', 'pull_image')
        fall_through(site_data, 'twitter:site', 'publisher_twitter')
        fall_through(site_data, 'twitter:title', 'title')

        # Open Graph
        fall_through(site_data, 'og:description', 'description')
        fall_through(site_data, 'og:image', 'pull_image')
        fall_through(site_data, 'og:title', 'title')
      end

    private

      def fall_through(site_data, name, key, default = nil)
        need_customized = site_data[:customize_by_frontmatter]
        value = self.meta_tags[key] ||
                (need_customized && current_page.data[key]) ||
                site_data[key] ||
                default
        set_meta_tags name => value unless value.blank?
        value
      end

      def full_title(meta_tags)
        separator   = meta_tags[:separator] || '|'
        full_title  = ''
        title       = safe_title(meta_tags[:title])

        (full_title << title) unless title.blank?
        (full_title << " #{separator} ") unless title.blank? || meta_tags[:site].blank?
        (full_title << meta_tags[:site]) unless meta_tags[:site].blank?
        full_title
      end

      def safe_description(description)
        truncate(strip_tags(description), length: 200)
      end

      def safe_title(title)
        title = strip_tags(title)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1072533979276006, 'fraction_numerical': 0.0008074283407347598, 'mean_word_length': 2.6556812592228236, 'pattern_counts': {'":': 0, '<': 35, '<?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': '953920', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2111, 'n_words': 448}