Project

General

Profile

Feature #37119 » 0001-Move-methods-related-to-section-to-a-helper-file-and.patch

Marius BĂLTEANU, 2022-05-18 00:12

View differences:

lib/redmine/wiki_formatting/common_mark/formatter.rb
61 61
        TaskList::Filter
62 62
      ], PIPELINE_CONFIG
63 63

  
64
      class Formatter < Redmine::WikiFormatting::Markdown::Formatter
64
      class Formatter
65
        include Redmine::WikiFormatting::SectionHelper
66

  
67
        def initialize(text)
68
          @text = text
69
        end
70

  
65 71
        def to_html(*args)
66 72
          result = MarkdownPipeline.call @text
67 73
          result[:output].to_s
lib/redmine/wiki_formatting/markdown/formatter.rb
57 57

  
58 58
      class Formatter
59 59
        include Redmine::WikiFormatting::LinksHelper
60
        include Redmine::WikiFormatting::SectionHelper
60 61
        alias :inline_restore_redmine_links :restore_redmine_links
61 62

  
62 63
        def initialize(text)
......
69 70
          html
70 71
        end
71 72

  
72
        def get_section(index)
73
          section = extract_sections(index)[1]
74
          hash = Digest::MD5.hexdigest(section)
75
          return section, hash
76
        end
77

  
78
        def update_section(index, update, hash=nil)
79
          t = extract_sections(index)
80
          if hash.present? && hash != Digest::MD5.hexdigest(t[1])
81
            raise Redmine::WikiFormatting::StaleSectionError
82
          end
83

  
84
          t[1] = update unless t[1].blank?
85
          t.reject(&:blank?).join "\n\n"
86
        end
87

  
88
        def extract_sections(index)
89
          sections = [+'', +'', +'']
90
          offset = 0
91
          i = 0
92
          l = 1
93
          inside_pre = false
94
          @text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|
95
            level = nil
96
            if part =~ /\A(~{3,}|`{3,})(\s*\S+)?\s*$/
97
              if !inside_pre
98
                inside_pre = true
99
              elsif !$2
100
                inside_pre = false
101
              end
102
            elsif inside_pre
103
              # nop
104
            elsif part =~ /\A(#+).+/
105
              level = $1.size
106
            elsif part =~ /\A.+\r?\n\r?(\=+|\-+)\s*$/
107
              level = $1.include?('=') ? 1 : 2
108
            end
109
            if level
110
              i += 1
111
              if offset == 0 && i == index
112
                # entering the requested section
113
                offset = 1
114
                l = level
115
              elsif offset == 1 && i > index && level <= l
116
                # leaving the requested section
117
                offset = 2
118
              end
119
            end
120
            sections[offset] << part
121
          end
122
          sections.map(&:strip)
123
        end
124

  
125 73
        private
126 74

  
127 75
        def formatter
lib/redmine/wiki_formatting/section_helper.rb
1
module Redmine
2
  module WikiFormatting
3
    module SectionHelper
4

  
5
      def get_section(index)
6
        section = extract_sections(index)[1]
7
        hash = Digest::MD5.hexdigest(section)
8
        return section, hash
9
      end
10

  
11
      def update_section(index, update, hash=nil)
12
        t = extract_sections(index)
13
        if hash.present? && hash != Digest::MD5.hexdigest(t[1])
14
          raise Redmine::WikiFormatting::StaleSectionError
15
        end
16

  
17
        t[1] = update unless t[1].blank?
18
        t.reject(&:blank?).join "\n\n"
19
      end
20

  
21
      def extract_sections(index)
22
        sections = [+'', +'', +'']
23
        offset = 0
24
        i = 0
25
        l = 1
26
        inside_pre = false
27
        @text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|
28
          level = nil
29
          if part =~ /\A(~{3,}|`{3,})(\s*\S+)?\s*$/
30
            if !inside_pre
31
              inside_pre = true
32
            elsif !$2
33
              inside_pre = false
34
            end
35
          elsif inside_pre
36
            # nop
37
          elsif part =~ /\A(#+).+/
38
            level = $1.size
39
          elsif part =~ /\A.+\r?\n\r?(\=+|\-+)\s*$/
40
            level = $1.include?('=') ? 1 : 2
41
          end
42
          if level
43
            i += 1
44
            if offset == 0 && i == index
45
              # entering the requested section
46
              offset = 1
47
              l = level
48
            elsif offset == 1 && i > index && level <= l
49
              # leaving the requested section
50
              offset = 2
51
            end
52
          end
53
          sections[offset] << part
54
        end
55
        sections.map(&:strip)
56
      end
57
    end
58
  end
59
end
lib/redmine/wiki_formatting/textile/formatter.rb
25 25
      class Formatter < RedCloth3
26 26
        include ActionView::Helpers::TagHelper
27 27
        include Redmine::WikiFormatting::LinksHelper
28
        include Redmine::WikiFormatting::SectionHelper
28 29

  
29 30
        alias :inline_auto_link :auto_link!
30 31
        alias :inline_auto_mailto :auto_mailto!
......
45 46
          super(*RULES).to_s
46 47
        end
47 48

  
48
        def get_section(index)
49
          section = extract_sections(index)[1]
50
          hash = Digest::MD5.hexdigest(section)
51
          return section, hash
52
        end
53

  
54
        def update_section(index, update, hash=nil)
55
          t = extract_sections(index)
56
          if hash.present? && hash != Digest::MD5.hexdigest(t[1])
57
            raise Redmine::WikiFormatting::StaleSectionError
58
          end
59

  
60
          t[1] = update unless t[1].blank?
61
          t.reject(&:blank?).join "\n\n"
62
        end
63

  
64 49
        def extract_sections(index)
65 50
          @pre_list = []
66 51
          text = self.dup
(2-2/2)