##// END OF EJS Templates
Fixed 10061 problem with textilize and :hard_breaks (Pavol Murin)...
Jean-Philippe Lang -
r461:32de29ea35c1
parent child
Show More
@@ -1,241 +1,249
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 class RedCloth
19 # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet.
20 # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a>
21 def hard_break( text )
22 text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
23 end
24 end
25
18 26 module ApplicationHelper
19 27
20 28 # Return current logged in user or nil
21 29 def loggedin?
22 30 @logged_in_user
23 31 end
24 32
25 33 # Return true if user is logged in and is admin, otherwise false
26 34 def admin_loggedin?
27 35 @logged_in_user and @logged_in_user.admin?
28 36 end
29 37
30 38 # Return true if user is authorized for controller/action, otherwise false
31 39 def authorize_for(controller, action)
32 40 # check if action is allowed on public projects
33 41 if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ]
34 42 return true
35 43 end
36 44 # check if user is authorized
37 45 if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project) ) )
38 46 return true
39 47 end
40 48 return false
41 49 end
42 50
43 51 # Display a link if user is authorized
44 52 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
45 53 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action])
46 54 end
47 55
48 56 # Display a link to user's account page
49 57 def link_to_user(user)
50 58 link_to user.display_name, :controller => 'account', :action => 'show', :id => user
51 59 end
52 60
53 61 def link_to_issue(issue)
54 62 link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue
55 63 end
56 64
57 65 def toggle_link(name, id, options={})
58 66 onclick = "Element.toggle('#{id}'); "
59 67 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
60 68 onclick << "return false;"
61 69 link_to(name, "#", :onclick => onclick)
62 70 end
63 71
64 72 def image_to_function(name, function, html_options = {})
65 73 html_options.symbolize_keys!
66 74 tag(:input, html_options.merge({
67 75 :type => "image", :src => image_path(name),
68 76 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
69 77 }))
70 78 end
71 79
72 80 def format_date(date)
73 81 l_date(date) if date
74 82 end
75 83
76 84 def format_time(time)
77 85 l_datetime((time.is_a? String) ? time.to_time : time) if time
78 86 end
79 87
80 88 def day_name(day)
81 89 l(:general_day_names).split(',')[day-1]
82 90 end
83 91
84 92 def month_name(month)
85 93 l(:actionview_datehelper_select_month_names).split(',')[month-1]
86 94 end
87 95
88 96 def pagination_links_full(paginator, options={}, html_options={})
89 97 html = ''
90 98 html << link_to_remote(('&#171; ' + l(:label_previous)),
91 99 {:update => "content", :url => options.merge(:page => paginator.current.previous)},
92 100 {:href => url_for(:params => options.merge(:page => paginator.current.previous))}) + ' ' if paginator.current.previous
93 101
94 102 html << (pagination_links_each(paginator, options) do |n|
95 103 link_to_remote(n.to_s,
96 104 {:url => {:params => options.merge(:page => n)}, :update => 'content'},
97 105 {:href => url_for(:params => options.merge(:page => n))})
98 106 end || '')
99 107
100 108 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
101 109 {:update => "content", :url => options.merge(:page => paginator.current.next)},
102 110 {:href => url_for(:params => options.merge(:page => paginator.current.next))}) if paginator.current.next
103 111 html
104 112 end
105 113
106 114 # textilize text according to system settings and RedCloth availability
107 115 def textilizable(text, options = {})
108 116 # different methods for formatting wiki links
109 117 case options[:wiki_links]
110 118 when :local
111 119 # used for local links to html files
112 120 format_wiki_link = Proc.new {|title| "#{title}.html" }
113 121 when :anchor
114 122 # used for single-file wiki export
115 123 format_wiki_link = Proc.new {|title| "##{title}" }
116 124 else
117 125 if @project
118 126 format_wiki_link = Proc.new {|title| url_for :controller => 'wiki', :action => 'index', :id => @project, :page => title }
119 127 else
120 128 format_wiki_link = Proc.new {|title| title }
121 129 end
122 130 end
123 131
124 132 # turn wiki links into textile links:
125 133 # example:
126 134 # [[link]] -> "link":link
127 135 # [[link|title]] -> "title":link
128 136 text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) {|m| "\"#{$3 || $1}\":" + format_wiki_link.call(Wiki.titleize($1)) }
129 137
130 138 # turn issue ids to textile links
131 139 # example:
132 140 # #52 -> "#52":/issues/show/52
133 141 text = text.gsub(/#(\d+)(?=\b)/) {|m| "\"##{$1}\":" + url_for(:controller => 'issues', :action => 'show', :id => $1) }
134 142
135 143 # turn revision ids to textile links (@project needed)
136 144 # example:
137 145 # r52 -> "r52":/repositories/revision/6?rev=52 (@project.id is 6)
138 146 text = text.gsub(/(?=\b)r(\d+)(?=\b)/) {|m| "\"r#{$1}\":" + url_for(:controller => 'repositories', :action => 'revision', :id => @project.id, :rev => $1) } if @project
139 147
140 148 # finally textilize text
141 149 @do_textilize ||= (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize")
142 text = @do_textilize ? auto_link(RedCloth.new(text).to_html) : simple_format(auto_link(h(text)))
150 text = @do_textilize ? auto_link(RedCloth.new(text, [:hard_breaks]).to_html) : simple_format(auto_link(h(text)))
143 151 end
144 152
145 153 def error_messages_for(object_name, options = {})
146 154 options = options.symbolize_keys
147 155 object = instance_variable_get("@#{object_name}")
148 156 if object && !object.errors.empty?
149 157 # build full_messages here with controller current language
150 158 full_messages = []
151 159 object.errors.each do |attr, msg|
152 160 next if msg.nil?
153 161 msg = msg.first if msg.is_a? Array
154 162 if attr == "base"
155 163 full_messages << l(msg)
156 164 else
157 165 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
158 166 end
159 167 end
160 168 # retrieve custom values error messages
161 169 if object.errors[:custom_values]
162 170 object.custom_values.each do |v|
163 171 v.errors.each do |attr, msg|
164 172 next if msg.nil?
165 173 msg = msg.first if msg.is_a? Array
166 174 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
167 175 end
168 176 end
169 177 end
170 178 content_tag("div",
171 179 content_tag(
172 180 options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :"
173 181 ) +
174 182 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
175 183 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
176 184 )
177 185 else
178 186 ""
179 187 end
180 188 end
181 189
182 190 def lang_options_for_select(blank=true)
183 191 (blank ? [["(auto)", ""]] : []) +
184 192 (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]}
185 193 end
186 194
187 195 def label_tag_for(name, option_tags = nil, options = {})
188 196 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
189 197 content_tag("label", label_text)
190 198 end
191 199
192 200 def labelled_tabular_form_for(name, object, options, &proc)
193 201 options[:html] ||= {}
194 202 options[:html].store :class, "tabular"
195 203 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
196 204 end
197 205
198 206 def check_all_links(form_name)
199 207 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
200 208 " | " +
201 209 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
202 210 end
203 211
204 212 def calendar_for(field_id)
205 213 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
206 214 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
207 215 end
208 216 end
209 217
210 218 class TabularFormBuilder < ActionView::Helpers::FormBuilder
211 219 include GLoc
212 220
213 221 def initialize(object_name, object, template, options, proc)
214 222 set_language_if_valid options.delete(:lang)
215 223 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
216 224 end
217 225
218 226 (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
219 227 src = <<-END_SRC
220 228 def #{selector}(field, options = {})
221 229 return super if options.delete :no_label
222 230 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
223 231 label = @template.content_tag("label", label_text,
224 232 :class => (@object && @object.errors[field] ? "error" : nil),
225 233 :for => (@object_name.to_s + "_" + field.to_s))
226 234 label + super
227 235 end
228 236 END_SRC
229 237 class_eval src, __FILE__, __LINE__
230 238 end
231 239
232 240 def select(field, choices, options = {}, html_options = {})
233 241 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
234 242 label = @template.content_tag("label", label_text,
235 243 :class => (@object && @object.errors[field] ? "error" : nil),
236 244 :for => (@object_name.to_s + "_" + field.to_s))
237 245 label + super
238 246 end
239 247
240 248 end
241 249
General Comments 0
You need to be logged in to leave comments. Login now