opf/openproject

View on GitHub
lib_static/redmine/diff.rb

Summary

Maintainability
A
35 mins
Test Coverage
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module Redmine
  # A line of diff
  class Diff
    include ActionView::Helpers::TagHelper

    attr_accessor :nb_line_left, :line_left, :nb_line_right, :line_right, :type_diff_right, :type_diff_left, :offsets

    def initialize
      self.nb_line_left = ""
      self.nb_line_right = ""
      self.line_left = ""
      self.line_right = ""
      self.type_diff_right = ""
      self.type_diff_left = ""
    end

    def type_diff
      type_diff_right == "diff_in" ? type_diff_right : type_diff_left
    end

    def line
      type_diff_right == "diff_in" ? line_right : line_left
    end

    def html_line_left
      line_to_html(line_left, offsets)
    end

    def html_line_right
      line_to_html(line_right, offsets)
    end

    def html_line
      line_to_html(line, offsets)
    end

    def inspect
      puts "### Start Line Diff ###"
      puts nb_line_left
      puts line_left
      puts nb_line_right
      puts line_right
    end

    private

    def line_to_html(line, offsets)
      line_to_html_raw(line, offsets).tap do |html_str|
        html_str.force_encoding("UTF-8")
      end
    end

    def line_to_html_raw(line, offsets)
      return line unless offsets

      ActiveSupport::SafeBuffer.new.tap do |output|
        if offsets.first != 0
          output << line[0..offsets.first - 1]
        end

        output << content_tag(:span, line[offsets.first..offsets.last])

        unless offsets.last == -1
          output << line[offsets.last + 1..-1]
        end
      end.to_s
    end
  end
end