<commit_before>module ApplicationHelper

  def current_user_stylesheet
    if @current_user.present?
      theme_stylesheet(@current_user.theme)
    else
      theme_stylesheet(:classic)
    end
  end
  
  def theme_stylesheet(name)
    "theme-#{name}.css"
  end

  def maybe_you(name, capitalize = false)
    if name == @current_user.real_name
      capitalize ? 'You' : 'you'
    else
      name
    end
  end

  def gravatar_image(email, size)
    image_tag 'https://secure.gravatar.com/avatar/' +
      Digest::MD5.hexdigest(email.gsub(/\s+/, '').downcase) +
      "?size=#{size}&default=mm&rating=pg",
      :alt => '', :size => "#{size}x#{size}", :class => 'gravatar'
  end

  def next_unread_href
    if @next_unread_post
      '#!' + post_path(@next_unread_post.newsgroup.name, @next_unread_post.number)
    else
      '#'
    end
  end
  
  def home_page_title
    if @current_user and @current_user.unread_count > 0
      "(#{@current_user.unread_count}) CSH WebNews"
    else
      'CSH WebNews'
    end
  end
  
  def home_unread_line
    unread = @current_user.unread_count
    unread_thread = @current_user.unread_count_in_thread
    unread_reply = @current_user.unread_count_in_reply
    
    if unread == 0
      unread_line = 'You have no unread posts.'
    else
      unread_line = "You have <span class=\"unread\">#{pluralize(unread, 'unread post')}</span>"
      if unread_thread + unread_reply > 0
        unread_line += ' ('
        if unread_thread > 0
          unread_line += "<span class=\"mine_in_thread\">#{unread_thread} in threads you've posted in</span>"
          unread_line += ', ' if unread_reply > 0
        end
        if unread_reply > 0
          unread_line += "<span class=\"mine_reply\">#{unread_reply} in reply to your posts</span>"
        end
        unread_line += ')'
      end
      unread_line += '.'
    end
    
    return unread_line
  end
  
  def activity_breakout(activity)
    now = Time.now
    Hash[
      {
        'Stickies' => ->(item) { item[:thread_parent].sticky? },
        'Today' => ->(item) { item[:newest_post].date > now.beginning_of_day },
        'Yesterday' => ->(item) { item[:newest_post].date > (now - 1.day).beginning_of_day },
        'This Week' => ->(item) { item[:newest_post].date > now.beginning_of_week },
        'Last Week' => ->(item) { item[:newest_post].date > (now - 1.week).beginning_of_week },
        'This Month' => ->(item) { item[:newest_post].date > now.beginning_of_month },
        'Last Month' => ->(item) { true }
      }.map{ |heading, proc|
        if activity.empty?
          nil
        else
          items = activity.select(&proc)
          activity -= items
          if activity.empty?
            ['Earlier', items]
          else
            [heading, items]
          end
        end
      }
    ].reject{ |_, items| items.empty? }
  end
  
  def post_html_body(post, quote_collapse = true)
    pre_body = post.body.dup
    parent = post.parent
    html_body = ''
    quote_collapse &&= parent
    
    # Warning: Even I barely understand this, and I wrote it. --grantovich
    
    if quote_collapse and @current_user.thread_mode == :normal
      [parent.body, parent.sigless_body].each do |parent_body|<commit_msg>Fix dashboard showing 'Earlier' when it shouldn't<commit_after>module ApplicationHelper

  def current_user_stylesheet
    if @current_user.present?
      theme_stylesheet(@current_user.theme)
    else
      theme_stylesheet(:classic)
    end
  end
  
  def theme_stylesheet(name)
    "theme-#{name}.css"
  end

  def maybe_you(name, capitalize = false)
    if name == @current_user.real_name
      capitalize ? 'You' : 'you'
    else
      name
    end
  end

  def gravatar_image(email, size)
    image_tag 'https://secure.gravatar.com/avatar/' +
      Digest::MD5.hexdigest(email.gsub(/\s+/, '').downcase) +
      "?size=#{size}&default=mm&rating=pg",
      :alt => '', :size => "#{size}x#{size}", :class => 'gravatar'
  end

  def next_unread_href
    if @next_unread_post
      '#!' + post_path(@next_unread_post.newsgroup.name, @next_unread_post.number)
    else
      '#'
    end
  end
  
  def home_page_title
    if @current_user and @current_user.unread_count > 0
      "(#{@current_user.unread_count}) CSH WebNews"
    else
      'CSH WebNews'
    end
  end
  
  def home_unread_line
    unread = @current_user.unread_count
    unread_thread = @current_user.unread_count_in_thread
    unread_reply = @current_user.unread_count_in_reply
    
    if unread == 0
      unread_line = 'You have no unread posts.'
    else
      unread_line = "You have <span class=\"unread\">#{pluralize(unread, 'unread post')}</span>"
      if unread_thread + unread_reply > 0
        unread_line += ' ('
        if unread_thread > 0
          unread_line += "<span class=\"mine_in_thread\">#{unread_thread} in threads you've posted in</span>"
          unread_line += ', ' if unread_reply > 0
        end
        if unread_reply > 0
          unread_line += "<span class=\"mine_reply\">#{unread_reply} in reply to your posts</span>"
        end
        unread_line += ')'
      end
      unread_line += '.'
    end
    
    return unread_line
  end
  
  def activity_breakout(activity)
    now = Time.now
    
    stickies = activity.select{ |item| item[:thread_parent].sticky? }
    activity -= stickies
    
    breakout = {}
    
    {
      'Today' => ->(item) { item[:newest_post].date > now.beginning_of_day },
      'Yesterday' => ->(item) { item[:newest_post].date > (now - 1.day).beginning_of_day },
      'This Week' => ->(item) { item[:newest_post].date > now.beginning_of_week },
      'Last Week' => ->(item) { item[:newest_post].date > (now - 1.week).beginning_of_week },
      'This Month' => ->(item) { item[:newest_post].date > now.beginning_of_month },
      'Last Month' => ->(item) { true }
    }.each do |heading, proc|
      break if activity.empty?
      
      items = activity.select(&proc)
      activity -= items
      
      if items.any?
        if activity.any?
          breakout.merge!({ heading => items })
        else
          if breakout.any?
            breakout.merge!({ 'Earlier' => items })
          else
            breakout.merge!({ 'Recent Activity' => items })
          end
        end
      end
    end
    
    { 'Stickies' => stickies }.merge(breakout.reject{ |_, items| items.empty? })
  end
  
  def post_html_body(post, quote_collapse = true)
    pre_body = post.body.dup
    parent = post.parent
    html_body = ''
    quote_collapse &&= parent
    
    # Warning: Even I barely understand this, and I wrote it. --grantovich
    
    if quote_collapse and @current_user.thread_mode == :normal
      [parent.body, parent.sigless_body].each do |parent_body|
{'fraction_non_alphanumeric': 0.13048001188883934, 'fraction_numerical': 0.0026749888542131075, 'mean_word_length': 2.4780361757105944, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3376808', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2247, 'n_words': 471}