Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

wiki_annotate.rb 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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. class WikiAnnotate
  19. attr_reader :lines, :content
  20. def initialize(content)
  21. @content = content
  22. current = content
  23. current_lines = current.text.split(/\r?\n/)
  24. @lines = current_lines.collect {|t| [nil, nil, t]}
  25. positions = []
  26. current_lines.size.times {|i| positions << i}
  27. while current.previous
  28. d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
  29. d.each_slice(3) do |s|
  30. sign, line = s[0], s[1]
  31. if sign == '+' && positions[line] && positions[line] != -1
  32. if @lines[positions[line]][0].nil?
  33. @lines[positions[line]][0] = current.version
  34. @lines[positions[line]][1] = current.author
  35. end
  36. end
  37. end
  38. d.each_slice(3) do |s|
  39. sign, line = s[0], s[1]
  40. if sign == '-'
  41. positions.insert(line, -1)
  42. else
  43. positions[line] = nil
  44. end
  45. end
  46. positions.compact!
  47. # Stop if every line is annotated
  48. break unless @lines.detect {|line| line[0].nil?}
  49. current = current.previous
  50. end
  51. @lines.each do |line|
  52. line[0] ||= current.version
  53. # if the last known version is > 1 (eg. history was cleared), we don't know the author
  54. line[1] ||= current.author if current.version == 1
  55. end
  56. end
  57. end