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