You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

diff.rb 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. # A line of diff
  20. class Diff
  21. attr_accessor :nb_line_left
  22. attr_accessor :line_left
  23. attr_accessor :nb_line_right
  24. attr_accessor :line_right
  25. attr_accessor :type_diff_right
  26. attr_accessor :type_diff_left
  27. attr_accessor :offsets
  28. def initialize
  29. self.nb_line_left = ''
  30. self.nb_line_right = ''
  31. self.line_left = ''
  32. self.line_right = ''
  33. self.type_diff_right = ''
  34. self.type_diff_left = ''
  35. end
  36. def type_diff
  37. type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
  38. end
  39. def line
  40. type_diff_right == 'diff_in' ? line_right : line_left
  41. end
  42. def html_line_left
  43. line_to_html(line_left, offsets)
  44. end
  45. def html_line_right
  46. line_to_html(line_right, offsets)
  47. end
  48. def html_line
  49. line_to_html(line, offsets)
  50. end
  51. def inspect
  52. puts '### Start Line Diff ###'
  53. puts self.nb_line_left
  54. puts self.line_left
  55. puts self.nb_line_right
  56. puts self.line_right
  57. end
  58. private
  59. def line_to_html(line, offsets)
  60. html = line_to_html_raw(line, offsets)
  61. html.force_encoding('UTF-8')
  62. html
  63. end
  64. def line_to_html_raw(line, offsets)
  65. if offsets
  66. s = +''
  67. unless offsets.first == 0
  68. s << CGI.escapeHTML(line[0..offsets.first-1])
  69. end
  70. s << '<span>' + CGI.escapeHTML(line[offsets.first..offsets.last]) + '</span>'
  71. unless offsets.last == -1
  72. s << CGI.escapeHTML(line[offsets.last+1..-1])
  73. end
  74. s
  75. else
  76. CGI.escapeHTML(line)
  77. end
  78. end
  79. end
  80. end