<commit_before># coding: utf-8
# This module encapsulates the chessboard-specific helper methods that
# are available in the main sinatra router and the views.
module Chessboard::Helpers

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !!session["user"]
  end

  # If a user is logged in (see #logged_in?), returns the User
  # instance representing the user. Otherwise returns nil.
  def logged_in_user
    return nil unless logged_in?

    Chessboard::User[session["user"].to_i]
  end

  # Convenience method for calling Sinatra's halt method
  # with an argument of 400 (authentication error).
  def require_log_in!
    halt 400 unless logged_in?
  end

  # Escape HTML content in +str+.
  def h(str)
    CGI.escapeHTML(str)
  end

  # If vaue is trueish, return the string 'checked="checked"', otherwise
  # return an empty string. This is useful when building forms with
  # checkboxes to tick them based on a model value.
  def checked(value)
    value ? 'checked="checked"' : ""
  end

  # Store +str+ in the user session. Returns it again
  # if +str+ is ommitted, clearing it from the session.
  def message(str = nil)
    if str
      session[:message] = str
    else
      session.delete(:message)
    end
  end

  # Checks if #message without an argument would return something
  # other than nil.
  def message?
    !!session[:message]
  end

  # Like #message, but for other messages, namely alerts.
  def alert(str = nil)
    if str
      session[:alert] = str
    else
      session.delete(:alert)
    end
  end

  # Like #message?, but for alerts.
  def alert?
    !!session[:alert]
  end

  # Process +str+ as markdown and return the corresponding HTML.
  def process_markup(str)
    Chessboard::EmailDocument.debug_preprocessor = true
    k = Chessboard::EmailDocument.new(str, :enable_coderay => true)
    k.to_html
  end

  # A much more unobtrusive version of #process_markup
  # that returns a <pre> element.
  def process_raw(str)
    r = Chessboard::RawDocument.new(str)
    r.to_html
  end

  # Return a URL to this post, adapting to the user's configuration
  # view mode if available, otherwise defaulting to the global default
  # view mode.
  # If +honor_view_mode+ is true (default), then the link is made to
  # the logged in user's configured view mode (thread or topic view).
  # Otherwise it is made to the default view mode.
  def post_url(post, honor_view_mode = true)
    view_mode = Chessboard::Configuration[:default_view_mode]

    if honor_view_mode && logged_in?
      view_mode = logged_in_user.view_mode
    end

    "/forums/#{post.forum_id}/#{view_mode}/#{post.id}"
  end

  # Append the given key-value pair to this URL's query string
  # and return the resulting full path such that it can be
  # used in an anchor tag's HREF attribute.
  def add_to_querystr(key, value)
    str = "?#{request.query_string.dup}"

    unless str == "?"
      str << "&"<commit_msg>If running in production, swallow crashes in the message parser.<commit_after># coding: utf-8
# This module encapsulates the chessboard-specific helper methods that
# are available in the main sinatra router and the views.
module Chessboard::Helpers

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !!session["user"]
  end

  # If a user is logged in (see #logged_in?), returns the User
  # instance representing the user. Otherwise returns nil.
  def logged_in_user
    return nil unless logged_in?

    Chessboard::User[session["user"].to_i]
  end

  # Convenience method for calling Sinatra's halt method
  # with an argument of 400 (authentication error).
  def require_log_in!
    halt 400 unless logged_in?
  end

  # Escape HTML content in +str+.
  def h(str)
    CGI.escapeHTML(str)
  end

  # If vaue is trueish, return the string 'checked="checked"', otherwise
  # return an empty string. This is useful when building forms with
  # checkboxes to tick them based on a model value.
  def checked(value)
    value ? 'checked="checked"' : ""
  end

  # Store +str+ in the user session. Returns it again
  # if +str+ is ommitted, clearing it from the session.
  def message(str = nil)
    if str
      session[:message] = str
    else
      session.delete(:message)
    end
  end

  # Checks if #message without an argument would return something
  # other than nil.
  def message?
    !!session[:message]
  end

  # Like #message, but for other messages, namely alerts.
  def alert(str = nil)
    if str
      session[:alert] = str
    else
      session.delete(:alert)
    end
  end

  # Like #message?, but for alerts.
  def alert?
    !!session[:alert]
  end

  # Process +str+ as markdown and return the corresponding HTML.
  def process_markup(str)
    Chessboard::EmailDocument.debug_preprocessor = true
    k = Chessboard::EmailDocument.new(str, :enable_coderay => true)
    k.to_html
  end

  # A much more unobtrusive version of #process_markup
  # that returns a <pre> element.
  def process_raw(str)
    if ENV["RACK_ENV"] == "production"
      begin
        r = Chessboard::RawDocument.new(str)
        r.to_html
      rescue => e
        # This is an archive, it must not be unable to display a message just
        # because some weird message fails to parse. Log the incident still so
        # that the parser can later be improved.
        Chessboard::Application.logger.error("#{e.class.name}: #{e.message}: #{e.backtrace.join("\n")}")
        Chessboard::Application.logger.error("Failed to process this, returning raw unprocessed text")
        str
      end
    else
      # If not running in production, crash it so the problem
      # can more easily be debugged.
      r = Chessboard::RawDocument.new(str)
      r.to_html
    end
  end

  # Return a URL to this post, adapting to the user's configuration
  # view mode if available, otherwise defaulting to the global default
  # view mode.
  # If +honor_view_mode+ is true (default), then the link is made to
  # the logged in user's configured view mode (thread or topic view).
  # Otherwise it is made to the default view mode.
  def post_url(post, honor_view_mode = true)
    view_mode = Chessboard::Configuration[:default_view_mode]

    if honor_view_mode && logged_in?
      view_mode = logged_in_user.view_mode
    end

    "/forums/#{post.forum_id}/#{view_mode}/#{post.id}"
  end

  # Append the given key-value pair to this URL's query string
  # and return the resulting full path such that it can be
  # used in an anchor tag's HREF attribute.
  def add_to_querystr(key, value)
    str = "?#{request.query_string.dup}"

    unless str == "?"
      str << "&"
{'fraction_non_alphanumeric': 0.08704178609141651, 'fraction_numerical': 0.0021119324181626186, 'mean_word_length': 3.1750629722921913, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '3483293', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1984, 'n_words': 763}