<commit_before># encoding: utf-8

module DiffHelper
  # Takes a unified diff as input and renders it as html
  def render_diff(udiff, display_mode = "inline")
    return if udiff.blank?

    udiff = force_utf8(udiff)

    if sidebyside_diff?
      render_sidebyside_diff(udiff)
    else
      render_inline_diff(udiff)
    end
  end

  def sidebyside_diff?
    @diffmode == "sidebyside"
  end

  def render_diffs(diffs)
    if sidebyside_diff?
      diffs.map do |file|
        out = %Q{<a name="#{h(force_utf8(file.a_path))}"></a>}
        out << "<h4>"
        out << link_to(h(file.a_path), file_path(@repository, file.a_path, @commit.id))
        out << "</h4>"
        out << force_utf8(render_diff(file.diff))
        out
      end.join("\n").html_safe
    else
      ('<div class="clear"></div><div id="commit-diff-container">' +
        render_inline_diffs_controls("commits") +
        render_inline_diffs_with_stats(diffs, :open) + "</div>").html_safe
    end
  end

  # Supply a block that fetches an array of comments with the file path as parameter
  def render_inline_diffs_with_stats(file_diffs, state = :closed)
    file_diffs.map do |file|
      diff_renderer = Diff::Display::Unified.new(file.diff)
      out =  %Q{<div class="file-diff" data-diff-path="#{file.a_path}">}
      out << %Q{<div class="header round-top-10 #{state == :closed ? 'closed' : 'open'}">}
      out << %Q{<span class="title"><span class="icon"></span>#{h(file.a_path)}</span>}
      out << %Q{<div class="diff-stats">}
      out << render_compact_diff_stats(diff_renderer.stats)
      out << "</div></div>"
      out << %Q{<div class="diff-hunks" #{state == :closed ? 'style="display:none"' : ''}>}

      if file.diff[0..256].include?("\000")
        out << "Binary files differ"
      else
        diff_options = {}
        diff_options[:comments] = if block_given?
                                    yield(file)
                end
        out << force_utf8(render_inline_diff(file.diff, diff_renderer, diff_options))
      end
      out << "</div></div>"
      out
    end.join("\n").html_safe
  end

  def render_inline_diffs_controls(cookie_prefix)
    %Q{<div class="file-diff-controls">
         <small>
           <a href="#" id="expand-all" gts:cookie-prefix="#{cookie_prefix}">expand all</a> /
           <a href="#" id="collapse-all" gts:cookie-prefix="#{cookie_prefix}">collapse all</a>
         </small>
       </div>}
  end

  def render_inline_diff(udiff, differ = nil, options = {})
    differ ||= Diff::Display::Unified.new(udiff)
    out = %Q{<table class="codediff inline">\n}
    out << "<thead>\n"
    out << %Q{<tr><td class="inline_comments">&nbsp;</td>}<commit_msg>Comment out what breaks in diff_helper [#174]<commit_after># encoding: utf-8

module DiffHelper
  # Takes a unified diff as input and renders it as html
  def render_diff(udiff, display_mode = "inline")
    return if udiff.blank?

    udiff = force_utf8(udiff)

    if sidebyside_diff?
      render_sidebyside_diff(udiff)
    else
      render_inline_diff(udiff)
    end
  end

  def sidebyside_diff?
    @diffmode == "sidebyside"
  end

  def render_diffs(diffs)
    if sidebyside_diff?
      diffs.map do |file|
        out = %Q{<a name="#{h(force_utf8(file.a_path))}"></a>}
        out << "<h4>"
        url_to_file = "" # TODO: Fix, LabHasGroup might provide some helper
        out << link_to(h(file.a_path), url_to_file)
        out << "</h4>"
        out << force_utf8(render_diff(file.diff))
        out
      end.join("\n").html_safe
    else
      ('<div class="clear"></div><div id="commit-diff-container">' +
        render_inline_diffs_controls("commits") +
        render_inline_diffs_with_stats(diffs, :open) + "</div>").html_safe
    end
  end

  # Supply a block that fetches an array of comments with the file path as parameter
  def render_inline_diffs_with_stats(file_diffs, state = :closed)
    file_diffs.map do |file|
      diff_renderer = Diff::Display::Unified.new(file.diff)
      out =  %Q{<div class="file-diff" data-diff-path="#{file.a_path}">}
      out << %Q{<div class="header round-top-10 #{state == :closed ? 'closed' : 'open'}">}
      out << %Q{<span class="title"><span class="icon"></span>#{h(file.a_path)}</span>}
      out << %Q{<div class="diff-stats">}
      # out << render_compact_diff_stats(diff_renderer.stats) # TODO
      out << "</div></div>"
      out << %Q{<div class="diff-hunks" #{state == :closed ? 'style="display:none"' : ''}>}

      if file.diff[0..256].include?("\000")
        out << "Binary files differ"
      else
        diff_options = {}
        diff_options[:comments] = if block_given?
                                    yield(file)
                end
        out << force_utf8(render_inline_diff(file.diff, diff_renderer, diff_options))
      end
      out << "</div></div>"
      out
    end.join("\n").html_safe
  end

  def render_inline_diffs_controls(cookie_prefix)
    %Q{<div class="file-diff-controls">
         <small>
           <a href="#" id="expand-all" gts:cookie-prefix="#{cookie_prefix}">expand all</a> /
           <a href="#" id="collapse-all" gts:cookie-prefix="#{cookie_prefix}">collapse all</a>
         </small>
       </div>}
  end

  def render_inline_diff(udiff, differ = nil, options = {})
    differ ||= Diff::Display::Unified.new(udiff)
    out = %Q{<table class="codediff inline">\n}
    out << "<thead>\n"
    out << %Q{<tr><td class="inline_comments">&nbsp;</td>}
{'fraction_non_alphanumeric': 0.1628419313383514, 'fraction_numerical': 0.006425555351569671, 'mean_word_length': 2.899785254115963, 'pattern_counts': {'":': 0, '<': 129, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4301602', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1853, 'n_words': 372}