From 5dfb162fb6b97e24aab9a6eebd93096d91e2018f Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 9 Mar 2007 20:03:25 +0000 Subject: [PATCH] modified textilizable helper method: * no more eval statement * r12 in text will be turned into a link to the revision 12 in the repository * #12 in text will be turned into a link to the issue 12 git-svn-id: http://redmine.rubyforge.org/svn/branches/work@320 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- wiki/app/helpers/application_helper.rb | 35 +++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) 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 = {})