23216.patch

Takenori TAKAKI, 2019-02-05 09:26

Download (3.58 KB)

View differences:

lib/redmine/wiki_formatting/macros.rb
202 202

  
203 203
      desc "Includes a wiki page. Examples:\n\n" +
204 204
             "{{include(Foo)}}\n" +
205
             "{{include(Foo, section)}} -- to include a spacific section of Foo page\n" +
205 206
             "{{include(projectname:Foo)}} -- to include a page of a specific project wiki"
206 207
      macro :include do |obj, args|
207
        page = Wiki.find_page(args.first.to_s, :project => @project)
208
        title, section_name = args[0], args[1]
209
        page = Wiki.find_page(title, :project => @project)
208 210
        raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
209 211
        @included_wiki_pages ||= []
210
        raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.id)
211
        @included_wiki_pages << page.id
212
        out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false)
212

  
213
        if section_name
214
          regex = nil
215
          case Setting.text_formatting
216
          when "textile"
217
            regex = '(?:\A|\r?\n\s*\r?\n)h\d+\.[ \t]+(.*?)(?=\r?\n\s*\r?\n|\z)'
218
          when "markdown"
219
            regex = '(?:\A|\r?\n)#+ +(.*?)(?=\r?\n|\z)'
220
          end
221

  
222
          section_index = 0
223
          if regex
224
            page.content.text.scan(/#{regex}/m).each.with_index(1) do |matched, i|
225
              if matched.first.gsub(/[\r?\n]/, '') == section_name
226
                section_index = i
227
                break
228
              end
229
            end
230
          end
231

  
232
          section_text = nil
233
          if section_index > 0 && Redmine::WikiFormatting.supports_section_edit?
234
            section_text, _hash = Redmine::WikiFormatting.formatter.new(page.content.text).get_section(section_index)
235
          end
236

  
237
          raise 'Section not found' if section_text.nil?
238
          included_key = "#{page.id}:#{section_index}"
239
          raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.id) || @included_wiki_pages.include?(included_key)
240
          @included_wiki_pages << included_key
241
          out = textilizable(section_text, :attachments => page.attachments, :headings => false)
242
        else
243
          raise 'Circular inclusion detected' if @included_wiki_pages.include?(page.id)
244
          @included_wiki_pages << page.id
245
          out = textilizable(page.content, :text, :attachments => page.attachments, :headings => false)
246
        end
213 247
        @included_wiki_pages.pop
214 248
        out
215 249
      end
test/unit/lib/redmine/wiki_formatting/macros_test.rb
194 194
    text = "{{include(Another page)}}"
195 195
    assert_include 'This is a link to a ticket', textilizable(text)
196 196

  
197
    # include a content of a spacific section
198
    text = "{{include(Page_with_sections, Heading 1)}}"
199
    assert_not_include 'Title', textilizable(text)
200
    assert_include 'Heading 1', textilizable(text)
201
    assert_not_include 'Heading 2', textilizable(text)
202

  
203
    text = "{{include(Page_with_sections, Heading 3)}}"
204
    assert_include 'Section not found', textilizable(text)
205

  
197 206
    @project = nil
198 207
    # include a page of a specific project wiki
199 208
    text = "{{include(ecookbook:Another page)}}"