diff --git a/wiki/app/helpers/application_helper.rb b/wiki/app/helpers/application_helper.rb index 46eb12dd4..f9010df15 100644 --- a/wiki/app/helpers/application_helper.rb +++ b/wiki/app/helpers/application_helper.rb @@ -94,23 +94,40 @@ module ApplicationHelper # textilize text according to system settings and RedCloth availability def textilizable(text, options = {}) + # different methods for formatting wiki links case options[:wiki_links] when :local - wiki_link_format = 'Wiki.titleize($1) + ".html"' + # used for local links to html files + format_wiki_link = Proc.new {|title| "#{title}.html" } when :anchor - wiki_link_format = '"#" + Wiki.titleize($1)' + # used for single-file wiki export + format_wiki_link = Proc.new {|title| "##{title}" } else - if @project.nil? - wiki_link_format = 'Wiki.titleize($1)' + if @project + format_wiki_link = Proc.new {|title| url_for :controller => 'wiki', :action => 'index', :id => @project, :page => title } else - wiki_link_format = '"/wiki/#{@project.id}/#{Wiki.titleize($1)}"' + format_wiki_link = Proc.new {|title| title } end end - # turn wiki links in textile links: "text":url - text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) {|m| "\"#{$3 || $1}\":" + (eval wiki_link_format) } + + # turn wiki links into textile links: + # example: + # [[link]] -> "link":link + # [[link|title]] -> "title":link + text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) {|m| "\"#{$3 || $1}\":" + format_wiki_link.call(Wiki.titleize($1)) } + + # turn issue ids to textile links + # example: + # #52 -> "#52":/issues/show/52 + text = text.gsub(/#(\d+)([\s\.\(\)\-,:;])/) {|m| "\"##{$1}\":" + url_for(:controller => 'issues', :action => 'show', :id => $1) + $2 } + + # turn revision ids to textile links (@project needed) + # example: + # r52 -> "r52":/repositories/revision/6?rev=52 (@project.id is 6) + text = text.gsub(/r(\d+)([\s\.\(\)\-,:;])/) {|m| "\"r#{$1}\":" + url_for(:controller => 'repositories', :action => 'revision', :id => @project.id, :rev => $1) + $2 } if @project + + # finally textilize text text = (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") ? auto_link(RedCloth.new(text, [:filter_html]).to_html) : simple_format(auto_link(h(text))) - # turn "#id" patterns into links to issues - text = text.gsub(/#(\d+)([^;\d])/, "#\\1\\2") end def error_messages_for(object_name, options = {})