Project

General

Profile

Feature #18257 » 18257.diff

Jens Krämer, 2014-11-14 03:49

View differences:

lib/redmine/wiki_formatting/macros.rb
154 154
        end
155 155

  
156 156
        # Sets description for the next macro to be defined
157
        def desc(txt)
158
          @@desc = txt
157
        # For complex descriptions that use textile syntax, you may also
158
        # provide a markdown equivalent using the :markdown option.
159
        def desc(*args)
160
          options = args.extract_options!
161
          options.symbolize_keys!
162
          if default = args.shift
163
            options[:default] = default
164
          end
165
          @@desc = ->(){
166
            options[Setting.text_formatting.to_sym] || options[:default]
167
          }
159 168
        end
160 169
      end
161 170

  
......
173 182
        out = ''.html_safe
174 183
        @@available_macros.each do |macro, options|
175 184
          out << content_tag('dt', content_tag('code', macro.to_s))
176
          out << content_tag('dd', textilizable(options[:desc]))
185
          desc = options[:desc]
186
          desc = desc.call if desc.respond_to?(:call)
187
          out << content_tag('dd', textilizable(desc.to_s))
177 188
        end
178 189
        content_tag('dl', out)
179 190
      end
180 191

  
181 192
      desc "Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" +
182 193
             "  !{{child_pages}} -- can be used from a wiki page only\n" +
183
             "  !{{child_pages(depth=2)}} -- display 2 levels nesting only\n"
194
             "  !{{child_pages(depth=2)}} -- display 2 levels nesting only\n" +
184 195
             "  !{{child_pages(Foo)}} -- lists all children of page Foo\n" +
185
             "  !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo"
196
             "  !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo",
197
           markdown:("Displays a list of child pages. With no argument, it displays the child pages of the current wiki page. Examples:\n\n" +
198
             "~~~~\n" +
199
             "!{{child_pages}} -- can be used from a wiki page only\n" +
200
             "!{{child_pages(depth=2)}} -- display 2 levels nesting only\n" +
201
             "!{{child_pages(Foo)}} -- lists all children of page Foo\n" +
202
             "!{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo\n" +
203
             "~~~~\n\n")
186 204
      macro :child_pages do |obj, args|
187 205
        args, options = extract_macro_options(args, :parent, :depth)
188 206
        options[:depth] = options[:depth].to_i if options[:depth].present?
......
200 218
        render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id)
201 219
      end
202 220

  
203
      desc "Include a wiki page. Example:\n\n  !{{include(Foo)}}\n\nor to include a page of a specific project wiki:\n\n  !{{include(projectname:Foo)}}"
221
      desc "Include a wiki page. Example:\n\n  !{{include(Foo)}}\n\nor to include a page of a specific project wiki:\n\n  !{{include(projectname:Foo)}}",
222
        markdown: "Include a wiki page. Example:\n\n~~~~\n!{{include(Foo)}}\n~~~~\n\nor to include a page of a specific project wiki:\n\n~~~~\n!{{include(projectname:Foo)}}\n~~~~\n"
204 223
      macro :include do |obj, args|
205 224
        page = Wiki.find_page(args.first.to_s, :project => @project)
206 225
        raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
......
212 231
        out
213 232
      end
214 233

  
215
      desc "Inserts of collapsed block of text. Example:\n\n  {{collapse(View details...)\nThis is a block of text that is collapsed by default.\nIt can be expanded by clicking a link.\n}}"
234
      desc "Inserts of collapsed block of text. Example:\n\n  {{collapse(View details...)\nThis is a block of text that is collapsed by default.\nIt can be expanded by clicking a link.\n}}",
235
        markdown: "Inserts of collapsed block of text. Example:\n\n~~~~\n{{collapse(View details...)\nThis is a block of text that is collapsed by default.\nIt can be expanded by clicking a link.\n}}\n~~~~\n"
216 236
      macro :collapse do |obj, args, text|
217 237
        html_id = "collapse-#{Redmine::Utils.random_hex(4)}"
218 238
        show_label = args[0] || l(:button_show)
......
225 245
        out
226 246
      end
227 247

  
228
      desc "Displays a clickable thumbnail of an attached image. Examples:\n\n<pre>{{thumbnail(image.png)}}\n{{thumbnail(image.png, size=300, title=Thumbnail)}}</pre>"
248
      desc "Displays a clickable thumbnail of an attached image. Examples:\n\n<pre>{{thumbnail(image.png)}}\n{{thumbnail(image.png, size=300, title=Thumbnail)}}</pre>", markdown: "Displays a clickable thumbnail of an attached image. Examples:\n\n~~~~\n{{thumbnail(image.png)}}\n{{thumbnail(image.png, size=300, title=Thumbnail)}}\n~~~~\n"
229 249
      macro :thumbnail do |obj, args|
230 250
        args, options = extract_macro_options(args, :size, :title)
231 251
        filename = args.first
......
246 266
        end
247 267
      end
248 268

  
249
      desc <<DESC
269
      desc "Wraps the enclosed image/thumbnail/text in a box with an optional caption. Example:
270

  
271
<pre>
272
{{figure(This is a caption)
273
!some-image.jpg!
274
}}
275
</pre>
276
", markdown: "
250 277
Wraps the enclosed image/thumbnail/text in a box with an optional caption. Example:
251 278

  
252
<pre>{{figure(This is a caption)
279
~~~~
280
{{figure(This is a caption)
253 281
!some-image.jpg!
254
}}</pre>
255
DESC
282
}}
283
~~~~
284
"
256 285
      macro :figure do |obj, args, text|
257
        puts obj.inspect
258 286
        args, options = extract_macro_options(args, :size)
259 287
        caption = args.first
260 288
        content_tag :figure do
test/unit/lib/redmine/wiki_formatting/macros_test.rb
38 38
  end
39 39

  
40 40
  def teardown
41
    Setting.text_formatting = @original_formatting if @original_formatting
42
  end
43

  
44
  def test_macro_description_markup
45
    Redmine::WikiFormatting::Macros.register do
46
      desc 'textile default docs', markdown: 'markdown docs'
47
      macro :foo do |obj, args|
48
        "Foo: #{args.size} (#{args.join(',')}) (#{args.class.name})"
49
      end
50
    end
51
    @original_formatting = Setting.text_formatting
52
    Setting.text_formatting = 'plain'
53
    assert_match /textile default docs/, textilizable('{{macro_list}}')
54
    Setting.text_formatting = 'textile'
55
    assert_match /textile default docs/, textilizable('{{macro_list}}')
56
    Setting.text_formatting = 'markdown'
57
    assert_match /markdown docs/, textilizable('{{macro_list}}')
41 58
  end
42 59

  
43 60
  def test_macro_registration
(2-2/2)