##// END OF EJS Templates
Extract parsing of inline attachments, wiki links and redmine links....
Jean-Philippe Lang -
r3474:a179f261cde4
parent child
Show More
@@ -405,11 +405,19 module ApplicationHelper
405 405 raise ArgumentError, 'invalid arguments to textilizable'
406 406 end
407 407 return '' if text.blank?
408 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
409 only_path = options.delete(:only_path) == false ? false : true
408 410
409 411 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
410 412
411 only_path = options.delete(:only_path) == false ? false : true
412
413 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
414 send method_name, text, project, obj, attr, only_path, options
415 end
416
417 text
418 end
419
420 def parse_inline_attachments(text, project, obj, attr, only_path, options)
413 421 # when using an image link, try to use an attachment, if possible
414 422 if options[:attachments] || (obj && obj.respond_to?(:attachments))
415 423 attachments = nil
@@ -429,33 +437,20 module ApplicationHelper
429 437 end
430 438 end
431 439 end
440 end
432 441
433
434 # different methods for formatting wiki links
435 case options[:wiki_links]
436 when :local
437 # used for local links to html files
438 format_wiki_link = Proc.new {|project, title, anchor| "#{title}.html" }
439 when :anchor
440 # used for single-file wiki export
441 format_wiki_link = Proc.new {|project, title, anchor| "##{title}" }
442 else
443 format_wiki_link = Proc.new {|project, title, anchor| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title, :anchor => anchor) }
444 end
445
446 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
447
448 # Wiki links
449 #
450 # Examples:
451 # [[mypage]]
452 # [[mypage|mytext]]
453 # wiki links can refer other project wikis, using project name or identifier:
454 # [[project:]] -> wiki starting page
455 # [[project:|mytext]]
456 # [[project:mypage]]
457 # [[project:mypage|mytext]]
458 text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
442 # Wiki links
443 #
444 # Examples:
445 # [[mypage]]
446 # [[mypage|mytext]]
447 # wiki links can refer other project wikis, using project name or identifier:
448 # [[project:]] -> wiki starting page
449 # [[project:|mytext]]
450 # [[project:mypage]]
451 # [[project:mypage|mytext]]
452 def parse_wiki_links(text, project, obj, attr, only_path, options)
453 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
459 454 link_project = project
460 455 esc, all, page, title = $1, $2, $3, $5
461 456 if esc.nil?
@@ -473,8 +468,13 module ApplicationHelper
473 468 end
474 469 # check if page exists
475 470 wiki_page = link_project.wiki.find_page(page)
476 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor),
477 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
471 url = case options[:wiki_links]
472 when :local; "#{title}.html"
473 when :anchor; "##{title}" # used for single-file wiki export
474 else
475 url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => link_project, :page => Wiki.titleize(page), :anchor => anchor)
476 end
477 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
478 478 else
479 479 # project or wiki doesn't exist
480 480 all
@@ -483,34 +483,36 module ApplicationHelper
483 483 all
484 484 end
485 485 end
486
487 # Redmine links
488 #
489 # Examples:
490 # Issues:
491 # #52 -> Link to issue #52
492 # Changesets:
493 # r52 -> Link to revision 52
494 # commit:a85130f -> Link to scmid starting with a85130f
495 # Documents:
496 # document#17 -> Link to document with id 17
497 # document:Greetings -> Link to the document with title "Greetings"
498 # document:"Some document" -> Link to the document with title "Some document"
499 # Versions:
500 # version#3 -> Link to version with id 3
501 # version:1.0.0 -> Link to version named "1.0.0"
502 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
503 # Attachments:
504 # attachment:file.zip -> Link to the attachment of the current object named file.zip
505 # Source files:
506 # source:some/file -> Link to the file located at /some/file in the project's repository
507 # source:some/file@52 -> Link to the file's revision 52
508 # source:some/file#L120 -> Link to line 120 of the file
509 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
510 # export:some/file -> Force the download of the file
511 # Forum messages:
512 # message#1218 -> Link to message with id 1218
513 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
486 end
487
488 # Redmine links
489 #
490 # Examples:
491 # Issues:
492 # #52 -> Link to issue #52
493 # Changesets:
494 # r52 -> Link to revision 52
495 # commit:a85130f -> Link to scmid starting with a85130f
496 # Documents:
497 # document#17 -> Link to document with id 17
498 # document:Greetings -> Link to the document with title "Greetings"
499 # document:"Some document" -> Link to the document with title "Some document"
500 # Versions:
501 # version#3 -> Link to version with id 3
502 # version:1.0.0 -> Link to version named "1.0.0"
503 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
504 # Attachments:
505 # attachment:file.zip -> Link to the attachment of the current object named file.zip
506 # Source files:
507 # source:some/file -> Link to the file located at /some/file in the project's repository
508 # source:some/file@52 -> Link to the file's revision 52
509 # source:some/file#L120 -> Link to line 120 of the file
510 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
511 # export:some/file -> Force the download of the file
512 # Forum messages:
513 # message#1218 -> Link to message with id 1218
514 def parse_redmine_links(text, project, obj, attr, only_path, options)
515 text.gsub!(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
514 516 leading, esc, prefix, sep, identifier = $1, $2, $3, $5 || $7, $6 || $8
515 517 link = nil
516 518 if esc.nil?
@@ -602,8 +604,6 module ApplicationHelper
602 604 end
603 605 leading + (link || "#{prefix}#{sep}#{identifier}")
604 606 end
605
606 text
607 607 end
608 608
609 609 # Same as Rails' simple_format helper without using paragraphs
General Comments 0
You need to be logged in to leave comments. Login now