<commit_before># Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include RailsRinku

  def known_languages(user, group)
    return group.languages unless logged_in?
    languages = user.preferred_languages & group.languages
    (languages.empty?)? group.languages : languages
  end

  def multiauth_dropdown(title)
    render 'shared/login_menu', :title => title
  end

  def with_facebook?
    return true if current_group.share.fb_active

    if request.host =~ Regexp.new("#{AppConfig.domain}$", Regexp::IGNORECASE)
      AppConfig.facebook["activate"]
    else
      false
    end
  end

  def language_json
    languages = []
    I18n.t('languages').keys.each do |k| languages << {:caption => I18n.t("languages.#{k}"),
        :value=>I18n.t("languages.#{k}"), :code => k} end
    languages.to_json
  end

  def preferred_languages_code(entity, language_method)
    if logged_in?
      entity.send(language_method).map do |code|
        I18n.t("languages.#{code}")+":#{code}"
      end
    else
      if I18n.locale.to_s != current_group.language &&
          current_group.languages.include?(I18n.locale.to_s)
        return [I18n.t("languages.#{I18n.locale}")+":#{I18n.locale}"]
      end
      return []
    end
  end

  def language_desc(langs)
    (langs.kind_of?(Array) ? langs : [langs]).map do |lang|
      I18n.t("languages.#{lang}", :default => lang).capitalize
    end.join(', ')
  end

  def language_select(f, question, opts = {})
    languages = current_group.languages

    selected = question.language

    f.select :language, languages_options(languages), {:selected => selected}, {:class => "select"}.merge(opts)
  end

  def language_select_tag(name = "language", value = nil, opts = {})
    languages = logged_in? ? current_user.preferred_languages : current_group.languages
    select_tag name, options_for_select(languages_options(languages)), {:value => value, :class => "select"}.merge(opts)
  end

  def languages_options(languages=nil, current_languages = [])
    languages = AVAILABLE_LANGUAGES-current_languages if languages.blank?
    locales_options(languages)
  end

  def locales_options(languages=nil)
    languages = AVAILABLE_LOCALES if languages.blank?

    languages.collect do |lang|
      [language_desc(lang), lang]
    end
  end

  def locales_roles
    roles = []
    Membership::ROLES.each do |role|
      roles << [I18n.t("roles.#{role}"), role]
    end
    roles
  end

  def tag_cloud(tags = [], options = {}, limit = 15, style = "tag_cloud")
    if tags.empty?
      tags = Tag.desc(:count).where({:group_id => current_group.id}).limit(limit).entries
    end

    return '' if tags.size <= 2 #tags.count return all tags instead of using .limit

    tag_class = options.delete(:tag_class) || "tag"
    if style == "tag_cloud"
      # Sizes: xxs xs s l xl xxl
      css = {1 => "xxs", 2 => "xs", 3 => "s", 4 => "l", 5 => "xl" }
      max_size = 5
      min_size = 1
      lowest_value = tags[tags.size-1] #tags.last returns the last tags without taking the .limit into account (mongoid bug?)
      highest_value = tags.first

      return '' if highest_value.nil? || lowest_value.nil?

      spread = (highest_value.count - lowest_value.count)
      spread = 1 if spread == 0
      ratio = (max_size - min_size) / spread

      render 'shared/tag_cloud', :tags => tags, :css => css,
                                :lowest_value => lowest_value, :ratio => ratio,
                                :min_size => min_size, :tag_class => tag_class, :style => style
    else
      render 'shared/tag_list', :tags => tags, :tag_class => tag_class, :style => style
    end
  end

  def country_flag(code, name)
    if code
      image_tag("flags/flag_#{code.downcase}.gif", :title => name, :alt => "")
    end
  end

  def markdown(txt, options = {})
    raw = options.delete(:raw)
    body = render_page_links(txt.to_s, options)
    txt = if raw
            (defined?(RDiscount) ? RDiscount.new(body, :protect_math) :
             Maruku.new(body)).to_html
          else
            (defined?(RDiscount) ? RDiscount.new(body, :smart, :strict, :protect_math) :
             Maruku.new(sanitize(body))).to_html
    end

    if options[:sanitize] != false
      txt = defined?(Sanitize) ? Sanitize.clean(txt, SANITIZE_CONFIG) : sanitize(txt)
    end
    txt.html_safe
  end

  def render_page_links(text, options = {})<commit_msg>wrap mathjax post in <div> tags<commit_after># Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include RailsRinku

  def known_languages(user, group)
    return group.languages unless logged_in?
    languages = user.preferred_languages & group.languages
    (languages.empty?)? group.languages : languages
  end

  def multiauth_dropdown(title)
    render 'shared/login_menu', :title => title
  end

  def with_facebook?
    return true if current_group.share.fb_active

    if request.host =~ Regexp.new("#{AppConfig.domain}$", Regexp::IGNORECASE)
      AppConfig.facebook["activate"]
    else
      false
    end
  end

  def language_json
    languages = []
    I18n.t('languages').keys.each do |k| languages << {:caption => I18n.t("languages.#{k}"),
        :value=>I18n.t("languages.#{k}"), :code => k} end
    languages.to_json
  end

  def preferred_languages_code(entity, language_method)
    if logged_in?
      entity.send(language_method).map do |code|
        I18n.t("languages.#{code}")+":#{code}"
      end
    else
      if I18n.locale.to_s != current_group.language &&
          current_group.languages.include?(I18n.locale.to_s)
        return [I18n.t("languages.#{I18n.locale}")+":#{I18n.locale}"]
      end
      return []
    end
  end

  def language_desc(langs)
    (langs.kind_of?(Array) ? langs : [langs]).map do |lang|
      I18n.t("languages.#{lang}", :default => lang).capitalize
    end.join(', ')
  end

  def language_select(f, question, opts = {})
    languages = current_group.languages

    selected = question.language

    f.select :language, languages_options(languages), {:selected => selected}, {:class => "select"}.merge(opts)
  end

  def language_select_tag(name = "language", value = nil, opts = {})
    languages = logged_in? ? current_user.preferred_languages : current_group.languages
    select_tag name, options_for_select(languages_options(languages)), {:value => value, :class => "select"}.merge(opts)
  end

  def languages_options(languages=nil, current_languages = [])
    languages = AVAILABLE_LANGUAGES-current_languages if languages.blank?
    locales_options(languages)
  end

  def locales_options(languages=nil)
    languages = AVAILABLE_LOCALES if languages.blank?

    languages.collect do |lang|
      [language_desc(lang), lang]
    end
  end

  def locales_roles
    roles = []
    Membership::ROLES.each do |role|
      roles << [I18n.t("roles.#{role}"), role]
    end
    roles
  end

  def tag_cloud(tags = [], options = {}, limit = 15, style = "tag_cloud")
    if tags.empty?
      tags = Tag.desc(:count).where({:group_id => current_group.id}).limit(limit).entries
    end

    return '' if tags.size <= 2 #tags.count return all tags instead of using .limit

    tag_class = options.delete(:tag_class) || "tag"
    if style == "tag_cloud"
      # Sizes: xxs xs s l xl xxl
      css = {1 => "xxs", 2 => "xs", 3 => "s", 4 => "l", 5 => "xl" }
      max_size = 5
      min_size = 1
      lowest_value = tags[tags.size-1] #tags.last returns the last tags without taking the .limit into account (mongoid bug?)
      highest_value = tags.first

      return '' if highest_value.nil? || lowest_value.nil?

      spread = (highest_value.count - lowest_value.count)
      spread = 1 if spread == 0
      ratio = (max_size - min_size) / spread

      render 'shared/tag_cloud', :tags => tags, :css => css,
                                :lowest_value => lowest_value, :ratio => ratio,
                                :min_size => min_size, :tag_class => tag_class, :style => style
    else
      render 'shared/tag_list', :tags => tags, :tag_class => tag_class, :style => style
    end
  end

  def country_flag(code, name)
    if code
      image_tag("flags/flag_#{code.downcase}.gif", :title => name, :alt => "")
    end
  end

  def markdown(txt, options = {})
    raw = options.delete(:raw)
    body = render_page_links(txt.to_s, options)
    if current_group.enable_mathjax
      body = "<div>#{body}</div>"
    end
    txt = if raw
            (defined?(RDiscount) ? RDiscount.new(body, :protect_math) :
             Maruku.new(body)).to_html
          else
            (defined?(RDiscount) ? RDiscount.new(body, :smart, :strict, :protect_math) :
             Maruku.new(sanitize(body))).to_html
    end

    if options[:sanitize] != false
      txt = defined?(Sanitize) ? Sanitize.clean(txt, SANITIZE_CONFIG) : sanitize(txt)
    end
    txt.html_safe
  end

  def render_page_links(text, options = {})
{'fraction_non_alphanumeric': 0.13233653202410176, 'fraction_numerical': 0.007810756527560812, 'mean_word_length': 3.015681003584229, 'pattern_counts': {'":': 4, '<': 16, '<?xml version=': 0, '>': 60, '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': '6323568', 'n_tokens_mistral': 3011, 'n_tokens_neox': 2947, 'n_words': 674}