halorgium-actionpack 3.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +5179 -0
- data/MIT-LICENSE +21 -0
- data/README +409 -0
- data/lib/abstract_controller.rb +16 -0
- data/lib/abstract_controller/base.rb +158 -0
- data/lib/abstract_controller/callbacks.rb +113 -0
- data/lib/abstract_controller/exceptions.rb +12 -0
- data/lib/abstract_controller/helpers.rb +151 -0
- data/lib/abstract_controller/layouts.rb +250 -0
- data/lib/abstract_controller/localized_cache.rb +49 -0
- data/lib/abstract_controller/logger.rb +61 -0
- data/lib/abstract_controller/rendering_controller.rb +188 -0
- data/lib/action_controller.rb +72 -0
- data/lib/action_controller/base.rb +168 -0
- data/lib/action_controller/caching.rb +80 -0
- data/lib/action_controller/caching/actions.rb +163 -0
- data/lib/action_controller/caching/fragments.rb +116 -0
- data/lib/action_controller/caching/pages.rb +154 -0
- data/lib/action_controller/caching/sweeping.rb +97 -0
- data/lib/action_controller/deprecated.rb +4 -0
- data/lib/action_controller/deprecated/integration_test.rb +2 -0
- data/lib/action_controller/deprecated/performance_test.rb +1 -0
- data/lib/action_controller/dispatch/dispatcher.rb +57 -0
- data/lib/action_controller/metal.rb +129 -0
- data/lib/action_controller/metal/benchmarking.rb +73 -0
- data/lib/action_controller/metal/compatibility.rb +145 -0
- data/lib/action_controller/metal/conditional_get.rb +86 -0
- data/lib/action_controller/metal/configuration.rb +28 -0
- data/lib/action_controller/metal/cookies.rb +105 -0
- data/lib/action_controller/metal/exceptions.rb +55 -0
- data/lib/action_controller/metal/filter_parameter_logging.rb +77 -0
- data/lib/action_controller/metal/flash.rb +162 -0
- data/lib/action_controller/metal/head.rb +27 -0
- data/lib/action_controller/metal/helpers.rb +115 -0
- data/lib/action_controller/metal/hide_actions.rb +47 -0
- data/lib/action_controller/metal/http_authentication.rb +312 -0
- data/lib/action_controller/metal/layouts.rb +171 -0
- data/lib/action_controller/metal/mime_responds.rb +317 -0
- data/lib/action_controller/metal/rack_convenience.rb +27 -0
- data/lib/action_controller/metal/redirector.rb +22 -0
- data/lib/action_controller/metal/render_options.rb +103 -0
- data/lib/action_controller/metal/rendering_controller.rb +57 -0
- data/lib/action_controller/metal/request_forgery_protection.rb +108 -0
- data/lib/action_controller/metal/rescuable.rb +13 -0
- data/lib/action_controller/metal/responder.rb +200 -0
- data/lib/action_controller/metal/session.rb +15 -0
- data/lib/action_controller/metal/session_management.rb +45 -0
- data/lib/action_controller/metal/streaming.rb +188 -0
- data/lib/action_controller/metal/testing.rb +39 -0
- data/lib/action_controller/metal/url_for.rb +41 -0
- data/lib/action_controller/metal/verification.rb +130 -0
- data/lib/action_controller/middleware.rb +38 -0
- data/lib/action_controller/notifications.rb +10 -0
- data/lib/action_controller/polymorphic_routes.rb +183 -0
- data/lib/action_controller/record_identifier.rb +91 -0
- data/lib/action_controller/testing/process.rb +111 -0
- data/lib/action_controller/testing/test_case.rb +345 -0
- data/lib/action_controller/translation.rb +13 -0
- data/lib/action_controller/url_rewriter.rb +204 -0
- data/lib/action_controller/vendor/html-scanner.rb +16 -0
- data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
- data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
- data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +176 -0
- data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
- data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
- data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
- data/lib/action_dispatch.rb +70 -0
- data/lib/action_dispatch/http/headers.rb +33 -0
- data/lib/action_dispatch/http/mime_type.rb +231 -0
- data/lib/action_dispatch/http/mime_types.rb +23 -0
- data/lib/action_dispatch/http/request.rb +539 -0
- data/lib/action_dispatch/http/response.rb +290 -0
- data/lib/action_dispatch/http/status_codes.rb +42 -0
- data/lib/action_dispatch/http/utils.rb +20 -0
- data/lib/action_dispatch/middleware/callbacks.rb +50 -0
- data/lib/action_dispatch/middleware/params_parser.rb +79 -0
- data/lib/action_dispatch/middleware/rescue.rb +26 -0
- data/lib/action_dispatch/middleware/session/abstract_store.rb +208 -0
- data/lib/action_dispatch/middleware/session/cookie_store.rb +235 -0
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +47 -0
- data/lib/action_dispatch/middleware/show_exceptions.rb +143 -0
- data/lib/action_dispatch/middleware/stack.rb +116 -0
- data/lib/action_dispatch/middleware/static.rb +44 -0
- data/lib/action_dispatch/middleware/string_coercion.rb +29 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +24 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +26 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +10 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +29 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +2 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +10 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +21 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +2 -0
- data/lib/action_dispatch/routing.rb +381 -0
- data/lib/action_dispatch/routing/deprecated_mapper.rb +878 -0
- data/lib/action_dispatch/routing/mapper.rb +327 -0
- data/lib/action_dispatch/routing/route.rb +49 -0
- data/lib/action_dispatch/routing/route_set.rb +497 -0
- data/lib/action_dispatch/testing/assertions.rb +8 -0
- data/lib/action_dispatch/testing/assertions/dom.rb +35 -0
- data/lib/action_dispatch/testing/assertions/model.rb +19 -0
- data/lib/action_dispatch/testing/assertions/response.rb +145 -0
- data/lib/action_dispatch/testing/assertions/routing.rb +144 -0
- data/lib/action_dispatch/testing/assertions/selector.rb +639 -0
- data/lib/action_dispatch/testing/assertions/tag.rb +123 -0
- data/lib/action_dispatch/testing/integration.rb +504 -0
- data/lib/action_dispatch/testing/performance_test.rb +15 -0
- data/lib/action_dispatch/testing/test_request.rb +83 -0
- data/lib/action_dispatch/testing/test_response.rb +131 -0
- data/lib/action_pack.rb +24 -0
- data/lib/action_pack/version.rb +9 -0
- data/lib/action_view.rb +58 -0
- data/lib/action_view/base.rb +308 -0
- data/lib/action_view/context.rb +44 -0
- data/lib/action_view/erb/util.rb +48 -0
- data/lib/action_view/helpers.rb +62 -0
- data/lib/action_view/helpers/active_model_helper.rb +306 -0
- data/lib/action_view/helpers/ajax_helper.rb +68 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +830 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +198 -0
- data/lib/action_view/helpers/cache_helper.rb +39 -0
- data/lib/action_view/helpers/capture_helper.rb +168 -0
- data/lib/action_view/helpers/date_helper.rb +988 -0
- data/lib/action_view/helpers/debug_helper.rb +38 -0
- data/lib/action_view/helpers/form_helper.rb +1102 -0
- data/lib/action_view/helpers/form_options_helper.rb +600 -0
- data/lib/action_view/helpers/form_tag_helper.rb +495 -0
- data/lib/action_view/helpers/javascript_helper.rb +208 -0
- data/lib/action_view/helpers/number_helper.rb +311 -0
- data/lib/action_view/helpers/prototype_helper.rb +1309 -0
- data/lib/action_view/helpers/raw_output_helper.rb +9 -0
- data/lib/action_view/helpers/record_identification_helper.rb +20 -0
- data/lib/action_view/helpers/record_tag_helper.rb +58 -0
- data/lib/action_view/helpers/sanitize_helper.rb +259 -0
- data/lib/action_view/helpers/scriptaculous_helper.rb +226 -0
- data/lib/action_view/helpers/tag_helper.rb +151 -0
- data/lib/action_view/helpers/text_helper.rb +594 -0
- data/lib/action_view/helpers/translation_helper.rb +39 -0
- data/lib/action_view/helpers/url_helper.rb +639 -0
- data/lib/action_view/locale/en.yml +117 -0
- data/lib/action_view/paths.rb +80 -0
- data/lib/action_view/render/partials.rb +342 -0
- data/lib/action_view/render/rendering.rb +134 -0
- data/lib/action_view/safe_buffer.rb +28 -0
- data/lib/action_view/template/error.rb +101 -0
- data/lib/action_view/template/handler.rb +36 -0
- data/lib/action_view/template/handlers.rb +52 -0
- data/lib/action_view/template/handlers/builder.rb +17 -0
- data/lib/action_view/template/handlers/erb.rb +53 -0
- data/lib/action_view/template/handlers/rjs.rb +18 -0
- data/lib/action_view/template/resolver.rb +165 -0
- data/lib/action_view/template/template.rb +131 -0
- data/lib/action_view/template/text.rb +38 -0
- data/lib/action_view/test_case.rb +163 -0
- metadata +236 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
require 'action_view/erb/util'
|
|
2
|
+
require 'set'
|
|
3
|
+
|
|
4
|
+
module ActionView
|
|
5
|
+
module Helpers #:nodoc:
|
|
6
|
+
# Provides methods to generate HTML tags programmatically when you can't use
|
|
7
|
+
# a Builder. By default, they output XHTML compliant tags.
|
|
8
|
+
module TagHelper
|
|
9
|
+
include ERB::Util
|
|
10
|
+
|
|
11
|
+
BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer
|
|
12
|
+
autoplay controls loop selected hidden scoped async
|
|
13
|
+
defer reversed ismap seemless muted required
|
|
14
|
+
autofocus novalidate formnovalidate open).to_set
|
|
15
|
+
BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attr| attr.to_sym })
|
|
16
|
+
|
|
17
|
+
# Returns an empty HTML tag of type +name+ which by default is XHTML
|
|
18
|
+
# compliant. Set +open+ to true to create an open tag compatible
|
|
19
|
+
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
|
|
20
|
+
# hash to +options+. Set +escape+ to false to disable attribute value
|
|
21
|
+
# escaping.
|
|
22
|
+
#
|
|
23
|
+
# ==== Options
|
|
24
|
+
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
|
25
|
+
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
|
|
26
|
+
# symbols or strings for the attribute names.
|
|
27
|
+
#
|
|
28
|
+
# ==== Examples
|
|
29
|
+
# tag("br")
|
|
30
|
+
# # => <br />
|
|
31
|
+
#
|
|
32
|
+
# tag("br", nil, true)
|
|
33
|
+
# # => <br>
|
|
34
|
+
#
|
|
35
|
+
# tag("input", { :type => 'text', :disabled => true })
|
|
36
|
+
# # => <input type="text" disabled="disabled" />
|
|
37
|
+
#
|
|
38
|
+
# tag("img", { :src => "open & shut.png" })
|
|
39
|
+
# # => <img src="open & shut.png" />
|
|
40
|
+
#
|
|
41
|
+
# tag("img", { :src => "open & shut.png" }, false, false)
|
|
42
|
+
# # => <img src="open & shut.png" />
|
|
43
|
+
def tag(name, options = nil, open = false, escape = true)
|
|
44
|
+
"<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe!
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Returns an HTML block tag of type +name+ surrounding the +content+. Add
|
|
48
|
+
# HTML attributes by passing an attributes hash to +options+.
|
|
49
|
+
# Instead of passing the content as an argument, you can also use a block
|
|
50
|
+
# in which case, you pass your +options+ as the second parameter.
|
|
51
|
+
# Set escape to false to disable attribute value escaping.
|
|
52
|
+
#
|
|
53
|
+
# ==== Options
|
|
54
|
+
# The +options+ hash is used with attributes with no value like (<tt>disabled</tt> and
|
|
55
|
+
# <tt>readonly</tt>), which you can give a value of true in the +options+ hash. You can use
|
|
56
|
+
# symbols or strings for the attribute names.
|
|
57
|
+
#
|
|
58
|
+
# ==== Examples
|
|
59
|
+
# content_tag(:p, "Hello world!")
|
|
60
|
+
# # => <p>Hello world!</p>
|
|
61
|
+
# content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
|
|
62
|
+
# # => <div class="strong"><p>Hello world!</p></div>
|
|
63
|
+
# content_tag("select", options, :multiple => true)
|
|
64
|
+
# # => <select multiple="multiple">...options...</select>
|
|
65
|
+
#
|
|
66
|
+
# <% content_tag :div, :class => "strong" do -%>
|
|
67
|
+
# Hello world!
|
|
68
|
+
# <% end -%>
|
|
69
|
+
# # => <div class="strong">Hello world!</div>
|
|
70
|
+
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
|
|
71
|
+
if block_given?
|
|
72
|
+
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
|
|
73
|
+
content_tag = content_tag_string(name, capture(&block), options, escape)
|
|
74
|
+
|
|
75
|
+
if block_called_from_erb?(block)
|
|
76
|
+
concat(content_tag)
|
|
77
|
+
else
|
|
78
|
+
content_tag
|
|
79
|
+
end
|
|
80
|
+
else
|
|
81
|
+
content_tag_string(name, content_or_options_with_block, options, escape)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Returns a CDATA section with the given +content+. CDATA sections
|
|
86
|
+
# are used to escape blocks of text containing characters which would
|
|
87
|
+
# otherwise be recognized as markup. CDATA sections begin with the string
|
|
88
|
+
# <tt><![CDATA[</tt> and end with (and may not contain) the string <tt>]]></tt>.
|
|
89
|
+
#
|
|
90
|
+
# ==== Examples
|
|
91
|
+
# cdata_section("<hello world>")
|
|
92
|
+
# # => <![CDATA[<hello world>]]>
|
|
93
|
+
#
|
|
94
|
+
# cdata_section(File.read("hello_world.txt"))
|
|
95
|
+
# # => <![CDATA[<hello from a text file]]>
|
|
96
|
+
def cdata_section(content)
|
|
97
|
+
"<![CDATA[#{content}]]>".html_safe!
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Returns an escaped version of +html+ without affecting existing escaped entities.
|
|
101
|
+
#
|
|
102
|
+
# ==== Examples
|
|
103
|
+
# escape_once("1 < 2 & 3")
|
|
104
|
+
# # => "1 < 2 & 3"
|
|
105
|
+
#
|
|
106
|
+
# escape_once("<< Accept & Checkout")
|
|
107
|
+
# # => "<< Accept & Checkout"
|
|
108
|
+
def escape_once(html)
|
|
109
|
+
ActiveSupport::Multibyte.clean(html.to_s).gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| ERB::Util::HTML_ESCAPE[special] }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
|
|
114
|
+
|
|
115
|
+
if RUBY_VERSION < '1.9.0'
|
|
116
|
+
# Check whether we're called from an erb template.
|
|
117
|
+
# We'd return a string in any other case, but erb <%= ... %>
|
|
118
|
+
# can't take an <% end %> later on, so we have to use <% ... %>
|
|
119
|
+
# and implicitly concat.
|
|
120
|
+
def block_called_from_erb?(block)
|
|
121
|
+
block && eval(BLOCK_CALLED_FROM_ERB, block)
|
|
122
|
+
end
|
|
123
|
+
else
|
|
124
|
+
def block_called_from_erb?(block)
|
|
125
|
+
block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def content_tag_string(name, content, options, escape = true)
|
|
130
|
+
tag_options = tag_options(options, escape) if options
|
|
131
|
+
"<#{name}#{tag_options}>#{content}</#{name}>".html_safe!
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def tag_options(options, escape = true)
|
|
135
|
+
unless options.blank?
|
|
136
|
+
attrs = []
|
|
137
|
+
options.each_pair do |key, value|
|
|
138
|
+
if BOOLEAN_ATTRIBUTES.include?(key)
|
|
139
|
+
attrs << %(#{key}="#{key}") if value
|
|
140
|
+
elsif !value.nil?
|
|
141
|
+
final_value = value.is_a?(Array) ? value.join(" ") : value
|
|
142
|
+
final_value = escape_once(final_value) if escape
|
|
143
|
+
attrs << %(#{key}="#{final_value}")
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
" #{attrs.sort * ' '}".html_safe! unless attrs.empty?
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
require 'action_view/helpers/tag_helper'
|
|
2
|
+
|
|
3
|
+
module ActionView
|
|
4
|
+
module Helpers #:nodoc:
|
|
5
|
+
# The TextHelper module provides a set of methods for filtering, formatting
|
|
6
|
+
# and transforming strings, which can reduce the amount of inline Ruby code in
|
|
7
|
+
# your views. These helper methods extend ActionView making them callable
|
|
8
|
+
# within your template files.
|
|
9
|
+
module TextHelper
|
|
10
|
+
# The preferred method of outputting text in your views is to use the
|
|
11
|
+
# <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
|
|
12
|
+
# do not operate as expected in an eRuby code block. If you absolutely must
|
|
13
|
+
# output text within a non-output code block (i.e., <% %>), you can use the concat method.
|
|
14
|
+
#
|
|
15
|
+
# ==== Examples
|
|
16
|
+
# <%
|
|
17
|
+
# concat "hello"
|
|
18
|
+
# # is the equivalent of <%= "hello" %>
|
|
19
|
+
#
|
|
20
|
+
# if (logged_in == true):
|
|
21
|
+
# concat "Logged in!"
|
|
22
|
+
# else
|
|
23
|
+
# concat link_to('login', :action => login)
|
|
24
|
+
# end
|
|
25
|
+
# # will either display "Logged in!" or a login link
|
|
26
|
+
# %>
|
|
27
|
+
def concat(string, unused_binding = nil)
|
|
28
|
+
if unused_binding
|
|
29
|
+
ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.", caller)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
output_buffer << string
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
|
|
36
|
+
# (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...")
|
|
37
|
+
# for a total length not exceeding <tt>:length</tt>.
|
|
38
|
+
#
|
|
39
|
+
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break.
|
|
40
|
+
#
|
|
41
|
+
# ==== Examples
|
|
42
|
+
#
|
|
43
|
+
# truncate("Once upon a time in a world far far away")
|
|
44
|
+
# # => Once upon a time in a world...
|
|
45
|
+
#
|
|
46
|
+
# truncate("Once upon a time in a world far far away", :separator => ' ')
|
|
47
|
+
# # => Once upon a time in a world...
|
|
48
|
+
#
|
|
49
|
+
# truncate("Once upon a time in a world far far away", :length => 14)
|
|
50
|
+
# # => Once upon a...
|
|
51
|
+
#
|
|
52
|
+
# truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
|
|
53
|
+
# # => And they found t(clipped)
|
|
54
|
+
#
|
|
55
|
+
# truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25)
|
|
56
|
+
# # => And they f... (continued)
|
|
57
|
+
#
|
|
58
|
+
# You can still use <tt>truncate</tt> with the old API that accepts the
|
|
59
|
+
# +length+ as its optional second and the +ellipsis+ as its
|
|
60
|
+
# optional third parameter:
|
|
61
|
+
# truncate("Once upon a time in a world far far away", 14)
|
|
62
|
+
# # => Once upon a...
|
|
63
|
+
#
|
|
64
|
+
# truncate("And they found that many people were sleeping better.", 25, "... (continued)")
|
|
65
|
+
# # => And they f... (continued)
|
|
66
|
+
def truncate(text, *args)
|
|
67
|
+
options = args.extract_options!
|
|
68
|
+
unless args.empty?
|
|
69
|
+
ActiveSupport::Deprecation.warn('truncate takes an option hash instead of separate ' +
|
|
70
|
+
'length and omission arguments', caller)
|
|
71
|
+
|
|
72
|
+
options[:length] = args[0] || 30
|
|
73
|
+
options[:omission] = args[1] || "..."
|
|
74
|
+
end
|
|
75
|
+
options.reverse_merge!(:length => 30, :omission => "...")
|
|
76
|
+
|
|
77
|
+
if text
|
|
78
|
+
l = options[:length] - options[:omission].mb_chars.length
|
|
79
|
+
chars = text.mb_chars
|
|
80
|
+
stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l
|
|
81
|
+
(chars.length > options[:length] ? chars[0...stop] + options[:omission] : text).to_s
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
|
|
86
|
+
# a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
|
|
87
|
+
# as a single-quoted string with \1 where the phrase is to be inserted (defaults to
|
|
88
|
+
# '<strong class="highlight">\1</strong>')
|
|
89
|
+
#
|
|
90
|
+
# ==== Examples
|
|
91
|
+
# highlight('You searched for: rails', 'rails')
|
|
92
|
+
# # => You searched for: <strong class="highlight">rails</strong>
|
|
93
|
+
#
|
|
94
|
+
# highlight('You searched for: ruby, rails, dhh', 'actionpack')
|
|
95
|
+
# # => You searched for: ruby, rails, dhh
|
|
96
|
+
#
|
|
97
|
+
# highlight('You searched for: rails', ['for', 'rails'], :highlighter => '<em>\1</em>')
|
|
98
|
+
# # => You searched <em>for</em>: <em>rails</em>
|
|
99
|
+
#
|
|
100
|
+
# highlight('You searched for: rails', 'rails', :highlighter => '<a href="search?q=\1">\1</a>')
|
|
101
|
+
# # => You searched for: <a href="search?q=rails">rails</a>
|
|
102
|
+
#
|
|
103
|
+
# You can still use <tt>highlight</tt> with the old API that accepts the
|
|
104
|
+
# +highlighter+ as its optional third parameter:
|
|
105
|
+
# highlight('You searched for: rails', 'rails', '<a href="search?q=\1">\1</a>') # => You searched for: <a href="search?q=rails">rails</a>
|
|
106
|
+
def highlight(text, phrases, *args)
|
|
107
|
+
options = args.extract_options!
|
|
108
|
+
unless args.empty?
|
|
109
|
+
options[:highlighter] = args[0] || '<strong class="highlight">\1</strong>'
|
|
110
|
+
end
|
|
111
|
+
options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
|
|
112
|
+
|
|
113
|
+
if text.blank? || phrases.blank?
|
|
114
|
+
text
|
|
115
|
+
else
|
|
116
|
+
match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
|
|
117
|
+
text.gsub(/(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i, options[:highlighter])
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
|
|
122
|
+
# The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
|
|
123
|
+
# defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
|
|
124
|
+
# then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
|
|
125
|
+
# will be stripped in any case. If the +phrase+ isn't found, nil is returned.
|
|
126
|
+
#
|
|
127
|
+
# ==== Examples
|
|
128
|
+
# excerpt('This is an example', 'an', :radius => 5)
|
|
129
|
+
# # => ...s is an exam...
|
|
130
|
+
#
|
|
131
|
+
# excerpt('This is an example', 'is', :radius => 5)
|
|
132
|
+
# # => This is a...
|
|
133
|
+
#
|
|
134
|
+
# excerpt('This is an example', 'is')
|
|
135
|
+
# # => This is an example
|
|
136
|
+
#
|
|
137
|
+
# excerpt('This next thing is an example', 'ex', :radius => 2)
|
|
138
|
+
# # => ...next...
|
|
139
|
+
#
|
|
140
|
+
# excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
|
|
141
|
+
# # => <chop> is also an example
|
|
142
|
+
#
|
|
143
|
+
# You can still use <tt>excerpt</tt> with the old API that accepts the
|
|
144
|
+
# +radius+ as its optional third and the +ellipsis+ as its
|
|
145
|
+
# optional forth parameter:
|
|
146
|
+
# excerpt('This is an example', 'an', 5) # => ...s is an exam...
|
|
147
|
+
# excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
|
|
148
|
+
def excerpt(text, phrase, *args)
|
|
149
|
+
options = args.extract_options!
|
|
150
|
+
unless args.empty?
|
|
151
|
+
options[:radius] = args[0] || 100
|
|
152
|
+
options[:omission] = args[1] || "..."
|
|
153
|
+
end
|
|
154
|
+
options.reverse_merge!(:radius => 100, :omission => "...")
|
|
155
|
+
|
|
156
|
+
if text && phrase
|
|
157
|
+
phrase = Regexp.escape(phrase)
|
|
158
|
+
|
|
159
|
+
if found_pos = text.mb_chars =~ /(#{phrase})/i
|
|
160
|
+
start_pos = [ found_pos - options[:radius], 0 ].max
|
|
161
|
+
end_pos = [ [ found_pos + phrase.mb_chars.length + options[:radius] - 1, 0].max, text.mb_chars.length ].min
|
|
162
|
+
|
|
163
|
+
prefix = start_pos > 0 ? options[:omission] : ""
|
|
164
|
+
postfix = end_pos < text.mb_chars.length - 1 ? options[:omission] : ""
|
|
165
|
+
|
|
166
|
+
prefix + text.mb_chars[start_pos..end_pos].strip + postfix
|
|
167
|
+
else
|
|
168
|
+
nil
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
|
|
174
|
+
# +plural+ is supplied, it will use that when count is > 1, otherwise
|
|
175
|
+
# it will use the Inflector to determine the plural form
|
|
176
|
+
#
|
|
177
|
+
# ==== Examples
|
|
178
|
+
# pluralize(1, 'person')
|
|
179
|
+
# # => 1 person
|
|
180
|
+
#
|
|
181
|
+
# pluralize(2, 'person')
|
|
182
|
+
# # => 2 people
|
|
183
|
+
#
|
|
184
|
+
# pluralize(3, 'person', 'users')
|
|
185
|
+
# # => 3 users
|
|
186
|
+
#
|
|
187
|
+
# pluralize(0, 'person')
|
|
188
|
+
# # => 0 people
|
|
189
|
+
def pluralize(count, singular, plural = nil)
|
|
190
|
+
"#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Wraps the +text+ into lines no longer than +line_width+ width. This method
|
|
194
|
+
# breaks on the first whitespace character that does not exceed +line_width+
|
|
195
|
+
# (which is 80 by default).
|
|
196
|
+
#
|
|
197
|
+
# ==== Examples
|
|
198
|
+
#
|
|
199
|
+
# word_wrap('Once upon a time')
|
|
200
|
+
# # => Once upon a time
|
|
201
|
+
#
|
|
202
|
+
# word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
|
|
203
|
+
# # => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\n a successor to the throne turned out to be more trouble than anyone could have\n imagined...
|
|
204
|
+
#
|
|
205
|
+
# word_wrap('Once upon a time', :line_width => 8)
|
|
206
|
+
# # => Once upon\na time
|
|
207
|
+
#
|
|
208
|
+
# word_wrap('Once upon a time', :line_width => 1)
|
|
209
|
+
# # => Once\nupon\na\ntime
|
|
210
|
+
#
|
|
211
|
+
# You can still use <tt>word_wrap</tt> with the old API that accepts the
|
|
212
|
+
# +line_width+ as its optional second parameter:
|
|
213
|
+
# word_wrap('Once upon a time', 8) # => Once upon\na time
|
|
214
|
+
def word_wrap(text, *args)
|
|
215
|
+
options = args.extract_options!
|
|
216
|
+
unless args.blank?
|
|
217
|
+
options[:line_width] = args[0] || 80
|
|
218
|
+
end
|
|
219
|
+
options.reverse_merge!(:line_width => 80)
|
|
220
|
+
|
|
221
|
+
text.split("\n").collect do |line|
|
|
222
|
+
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
|
|
223
|
+
end * "\n"
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags.
|
|
227
|
+
#
|
|
228
|
+
# You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
|
|
229
|
+
# <i>This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/]
|
|
230
|
+
# is available</i>.
|
|
231
|
+
#
|
|
232
|
+
# ==== Examples
|
|
233
|
+
# textilize("*This is Textile!* Rejoice!")
|
|
234
|
+
# # => "<p><strong>This is Textile!</strong> Rejoice!</p>"
|
|
235
|
+
#
|
|
236
|
+
# textilize("I _love_ ROR(Ruby on Rails)!")
|
|
237
|
+
# # => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>"
|
|
238
|
+
#
|
|
239
|
+
# textilize("h2. Textile makes markup -easy- simple!")
|
|
240
|
+
# # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
|
|
241
|
+
#
|
|
242
|
+
# textilize("Visit the Rails website "here":http://www.rubyonrails.org/.)
|
|
243
|
+
# # => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>"
|
|
244
|
+
#
|
|
245
|
+
# textilize("This is worded <strong>strongly</strong>")
|
|
246
|
+
# # => "<p>This is worded <strong>strongly</strong></p>"
|
|
247
|
+
#
|
|
248
|
+
# textilize("This is worded <strong>strongly</strong>", :filter_html)
|
|
249
|
+
# # => "<p>This is worded <strong>strongly</strong></p>"
|
|
250
|
+
#
|
|
251
|
+
def textilize(text, *options)
|
|
252
|
+
options ||= [:hard_breaks]
|
|
253
|
+
|
|
254
|
+
if text.blank?
|
|
255
|
+
""
|
|
256
|
+
else
|
|
257
|
+
textilized = RedCloth.new(text, options)
|
|
258
|
+
textilized.to_html
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# Returns the text with all the Textile codes turned into HTML tags,
|
|
263
|
+
# but without the bounding <p> tag that RedCloth adds.
|
|
264
|
+
#
|
|
265
|
+
# You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
|
|
266
|
+
# <i>This method is requires RedCloth[http://whytheluckystiff.net/ruby/redcloth/]
|
|
267
|
+
# to be available</i>.
|
|
268
|
+
#
|
|
269
|
+
# ==== Examples
|
|
270
|
+
# textilize_without_paragraph("*This is Textile!* Rejoice!")
|
|
271
|
+
# # => "<strong>This is Textile!</strong> Rejoice!"
|
|
272
|
+
#
|
|
273
|
+
# textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!")
|
|
274
|
+
# # => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!"
|
|
275
|
+
#
|
|
276
|
+
# textilize_without_paragraph("h2. Textile makes markup -easy- simple!")
|
|
277
|
+
# # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
|
|
278
|
+
#
|
|
279
|
+
# textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.)
|
|
280
|
+
# # => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>."
|
|
281
|
+
def textilize_without_paragraph(text)
|
|
282
|
+
textiled = textilize(text)
|
|
283
|
+
if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
|
|
284
|
+
if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
|
|
285
|
+
return textiled
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Returns the text with all the Markdown codes turned into HTML tags.
|
|
289
|
+
# <i>This method requires BlueCloth[http://www.deveiate.org/projects/BlueCloth]
|
|
290
|
+
# to be available</i>.
|
|
291
|
+
#
|
|
292
|
+
# ==== Examples
|
|
293
|
+
# markdown("We are using __Markdown__ now!")
|
|
294
|
+
# # => "<p>We are using <strong>Markdown</strong> now!</p>"
|
|
295
|
+
#
|
|
296
|
+
# markdown("We like to _write_ `code`, not just _read_ it!")
|
|
297
|
+
# # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>"
|
|
298
|
+
#
|
|
299
|
+
# markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.")
|
|
300
|
+
# # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
|
|
301
|
+
# # has more information.</p>"
|
|
302
|
+
#
|
|
303
|
+
# markdown('')
|
|
304
|
+
# # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'
|
|
305
|
+
def markdown(text)
|
|
306
|
+
text.blank? ? "" : BlueCloth.new(text).to_html
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Returns +text+ transformed into HTML using simple formatting rules.
|
|
310
|
+
# Two or more consecutive newlines(<tt>\n\n</tt>) are considered as a
|
|
311
|
+
# paragraph and wrapped in <tt><p></tt> tags. One newline (<tt>\n</tt>) is
|
|
312
|
+
# considered as a linebreak and a <tt><br /></tt> tag is appended. This
|
|
313
|
+
# method does not remove the newlines from the +text+.
|
|
314
|
+
#
|
|
315
|
+
# You can pass any HTML attributes into <tt>html_options</tt>. These
|
|
316
|
+
# will be added to all created paragraphs.
|
|
317
|
+
# ==== Examples
|
|
318
|
+
# my_text = "Here is some basic text...\n...with a line break."
|
|
319
|
+
#
|
|
320
|
+
# simple_format(my_text)
|
|
321
|
+
# # => "<p>Here is some basic text...\n<br />...with a line break.</p>"
|
|
322
|
+
#
|
|
323
|
+
# more_text = "We want to put a paragraph...\n\n...right there."
|
|
324
|
+
#
|
|
325
|
+
# simple_format(more_text)
|
|
326
|
+
# # => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
|
|
327
|
+
#
|
|
328
|
+
# simple_format("Look ma! A class!", :class => 'description')
|
|
329
|
+
# # => "<p class='description'>Look ma! A class!</p>"
|
|
330
|
+
def simple_format(text, html_options={})
|
|
331
|
+
start_tag = tag('p', html_options, true)
|
|
332
|
+
text = text.to_s.dup
|
|
333
|
+
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
|
|
334
|
+
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
|
|
335
|
+
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
|
|
336
|
+
text.insert 0, start_tag
|
|
337
|
+
text << "</p>"
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# Turns all URLs and e-mail addresses into clickable links. The <tt>:link</tt> option
|
|
341
|
+
# will limit what should be linked. You can add HTML attributes to the links using
|
|
342
|
+
# <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
|
|
343
|
+
# <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
|
|
344
|
+
# e-mail address is yielded and the result is used as the link text.
|
|
345
|
+
#
|
|
346
|
+
# ==== Examples
|
|
347
|
+
# auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
|
|
348
|
+
# # => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
|
|
349
|
+
# # say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
|
|
350
|
+
#
|
|
351
|
+
# auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :urls)
|
|
352
|
+
# # => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
|
|
353
|
+
# # or e-mail david@loudthinking.com"
|
|
354
|
+
#
|
|
355
|
+
# auto_link("Visit http://www.loudthinking.com/ or e-mail david@loudthinking.com", :link => :email_addresses)
|
|
356
|
+
# # => "Visit http://www.loudthinking.com/ or e-mail <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
|
|
357
|
+
#
|
|
358
|
+
# post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
|
|
359
|
+
# auto_link(post_body, :html => { :target => '_blank' }) do |text|
|
|
360
|
+
# truncate(text, 15)
|
|
361
|
+
# end
|
|
362
|
+
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.m...</a>.
|
|
363
|
+
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
|
|
364
|
+
#
|
|
365
|
+
#
|
|
366
|
+
# You can still use <tt>auto_link</tt> with the old API that accepts the
|
|
367
|
+
# +link+ as its optional second parameter and the +html_options+ hash
|
|
368
|
+
# as its optional third parameter:
|
|
369
|
+
# post_body = "Welcome to my new blog at http://www.myblog.com/. Please e-mail me at me@email.com."
|
|
370
|
+
# auto_link(post_body, :urls) # => Once upon\na time
|
|
371
|
+
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\">http://www.myblog.com</a>.
|
|
372
|
+
# Please e-mail me at me@email.com."
|
|
373
|
+
#
|
|
374
|
+
# auto_link(post_body, :all, :target => "_blank") # => Once upon\na time
|
|
375
|
+
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
|
|
376
|
+
# Please e-mail me at <a href=\"mailto:me@email.com\">me@email.com</a>."
|
|
377
|
+
def auto_link(text, *args, &block)#link = :all, html = {}, &block)
|
|
378
|
+
return '' if text.blank?
|
|
379
|
+
|
|
380
|
+
options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
|
|
381
|
+
unless args.empty?
|
|
382
|
+
options[:link] = args[0] || :all
|
|
383
|
+
options[:html] = args[1] || {}
|
|
384
|
+
end
|
|
385
|
+
options.reverse_merge!(:link => :all, :html => {})
|
|
386
|
+
|
|
387
|
+
case options[:link].to_sym
|
|
388
|
+
when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], &block), options[:html], &block)
|
|
389
|
+
when :email_addresses then auto_link_email_addresses(text, options[:html], &block)
|
|
390
|
+
when :urls then auto_link_urls(text, options[:html], &block)
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# Creates a Cycle object whose _to_s_ method cycles through elements of an
|
|
395
|
+
# array every time it is called. This can be used for example, to alternate
|
|
396
|
+
# classes for table rows. You can use named cycles to allow nesting in loops.
|
|
397
|
+
# Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
|
|
398
|
+
# named cycle. The default name for a cycle without a +:name+ key is
|
|
399
|
+
# <tt>"default"</tt>. You can manually reset a cycle by calling reset_cycle
|
|
400
|
+
# and passing the name of the cycle. The current cycle string can be obtained
|
|
401
|
+
# anytime using the current_cycle method.
|
|
402
|
+
#
|
|
403
|
+
# ==== Examples
|
|
404
|
+
# # Alternate CSS classes for even and odd numbers...
|
|
405
|
+
# @items = [1,2,3,4]
|
|
406
|
+
# <table>
|
|
407
|
+
# <% @items.each do |item| %>
|
|
408
|
+
# <tr class="<%= cycle("even", "odd") -%>">
|
|
409
|
+
# <td>item</td>
|
|
410
|
+
# </tr>
|
|
411
|
+
# <% end %>
|
|
412
|
+
# </table>
|
|
413
|
+
#
|
|
414
|
+
#
|
|
415
|
+
# # Cycle CSS classes for rows, and text colors for values within each row
|
|
416
|
+
# @items = x = [{:first => 'Robert', :middle => 'Daniel', :last => 'James'},
|
|
417
|
+
# {:first => 'Emily', :middle => 'Shannon', :maiden => 'Pike', :last => 'Hicks'},
|
|
418
|
+
# {:first => 'June', :middle => 'Dae', :last => 'Jones'}]
|
|
419
|
+
# <% @items.each do |item| %>
|
|
420
|
+
# <tr class="<%= cycle("even", "odd", :name => "row_class") -%>">
|
|
421
|
+
# <td>
|
|
422
|
+
# <% item.values.each do |value| %>
|
|
423
|
+
# <%# Create a named cycle "colors" %>
|
|
424
|
+
# <span style="color:<%= cycle("red", "green", "blue", :name => "colors") -%>">
|
|
425
|
+
# <%= value %>
|
|
426
|
+
# </span>
|
|
427
|
+
# <% end %>
|
|
428
|
+
# <% reset_cycle("colors") %>
|
|
429
|
+
# </td>
|
|
430
|
+
# </tr>
|
|
431
|
+
# <% end %>
|
|
432
|
+
def cycle(first_value, *values)
|
|
433
|
+
if (values.last.instance_of? Hash)
|
|
434
|
+
params = values.pop
|
|
435
|
+
name = params[:name]
|
|
436
|
+
else
|
|
437
|
+
name = "default"
|
|
438
|
+
end
|
|
439
|
+
values.unshift(first_value)
|
|
440
|
+
|
|
441
|
+
cycle = get_cycle(name)
|
|
442
|
+
if (cycle.nil? || cycle.values != values)
|
|
443
|
+
cycle = set_cycle(name, Cycle.new(*values))
|
|
444
|
+
end
|
|
445
|
+
return cycle.to_s
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# Returns the current cycle string after a cycle has been started. Useful
|
|
449
|
+
# for complex table highlighting or any other design need which requires
|
|
450
|
+
# the current cycle string in more than one place.
|
|
451
|
+
#
|
|
452
|
+
# ==== Example
|
|
453
|
+
# # Alternate background colors
|
|
454
|
+
# @items = [1,2,3,4]
|
|
455
|
+
# <% @items.each do |item| %>
|
|
456
|
+
# <div style="background-color:<%= cycle("red","white","blue") %>">
|
|
457
|
+
# <span style="background-color:<%= current_cycle %>"><%= item %></span>
|
|
458
|
+
# </div>
|
|
459
|
+
# <% end %>
|
|
460
|
+
def current_cycle(name = "default")
|
|
461
|
+
cycle = get_cycle(name)
|
|
462
|
+
cycle.current_value unless cycle.nil?
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# Resets a cycle so that it starts from the first element the next time
|
|
466
|
+
# it is called. Pass in +name+ to reset a named cycle.
|
|
467
|
+
#
|
|
468
|
+
# ==== Example
|
|
469
|
+
# # Alternate CSS classes for even and odd numbers...
|
|
470
|
+
# @items = [[1,2,3,4], [5,6,3], [3,4,5,6,7,4]]
|
|
471
|
+
# <table>
|
|
472
|
+
# <% @items.each do |item| %>
|
|
473
|
+
# <tr class="<%= cycle("even", "odd") -%>">
|
|
474
|
+
# <% item.each do |value| %>
|
|
475
|
+
# <span style="color:<%= cycle("#333", "#666", "#999", :name => "colors") -%>">
|
|
476
|
+
# <%= value %>
|
|
477
|
+
# </span>
|
|
478
|
+
# <% end %>
|
|
479
|
+
#
|
|
480
|
+
# <% reset_cycle("colors") %>
|
|
481
|
+
# </tr>
|
|
482
|
+
# <% end %>
|
|
483
|
+
# </table>
|
|
484
|
+
def reset_cycle(name = "default")
|
|
485
|
+
cycle = get_cycle(name)
|
|
486
|
+
cycle.reset unless cycle.nil?
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
class Cycle #:nodoc:
|
|
490
|
+
attr_reader :values
|
|
491
|
+
|
|
492
|
+
def initialize(first_value, *values)
|
|
493
|
+
@values = values.unshift(first_value)
|
|
494
|
+
reset
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
def reset
|
|
498
|
+
@index = 0
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
def current_value
|
|
502
|
+
@values[previous_index].to_s
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
def to_s
|
|
506
|
+
value = @values[@index].to_s
|
|
507
|
+
@index = next_index
|
|
508
|
+
return value
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
private
|
|
512
|
+
|
|
513
|
+
def next_index
|
|
514
|
+
step_index(1)
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
def previous_index
|
|
518
|
+
step_index(-1)
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
def step_index(n)
|
|
522
|
+
(@index + n) % @values.size
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
private
|
|
527
|
+
# The cycle helpers need to store the cycles in a place that is
|
|
528
|
+
# guaranteed to be reset every time a page is rendered, so it
|
|
529
|
+
# uses an instance variable of ActionView::Base.
|
|
530
|
+
def get_cycle(name)
|
|
531
|
+
@_cycles = Hash.new unless defined?(@_cycles)
|
|
532
|
+
return @_cycles[name]
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def set_cycle(name, cycle_object)
|
|
536
|
+
@_cycles = Hash.new unless defined?(@_cycles)
|
|
537
|
+
@_cycles[name] = cycle_object
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
AUTO_LINK_RE = %r{
|
|
541
|
+
( https?:// | www\. )
|
|
542
|
+
[^\s<]+
|
|
543
|
+
}x unless const_defined?(:AUTO_LINK_RE)
|
|
544
|
+
|
|
545
|
+
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }
|
|
546
|
+
|
|
547
|
+
# Turns all urls into clickable links. If a block is given, each url
|
|
548
|
+
# is yielded and the result is used as the link text.
|
|
549
|
+
def auto_link_urls(text, html_options = {})
|
|
550
|
+
link_attributes = html_options.stringify_keys
|
|
551
|
+
text.gsub(AUTO_LINK_RE) do
|
|
552
|
+
href = $&
|
|
553
|
+
punctuation = []
|
|
554
|
+
left, right = $`, $'
|
|
555
|
+
# detect already linked URLs and URLs in the middle of a tag
|
|
556
|
+
if left =~ /<[^>]+$/ && right =~ /^[^>]*>/
|
|
557
|
+
# do not change string; URL is already linked
|
|
558
|
+
href
|
|
559
|
+
else
|
|
560
|
+
# don't include trailing punctuation character as part of the URL
|
|
561
|
+
while href.sub!(/[^\w\/-]$/, '')
|
|
562
|
+
punctuation.push $&
|
|
563
|
+
if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
|
|
564
|
+
href << punctuation.pop
|
|
565
|
+
break
|
|
566
|
+
end
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
link_text = block_given?? yield(href) : href
|
|
570
|
+
href = 'http://' + href unless href.index('http') == 0
|
|
571
|
+
|
|
572
|
+
content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation.reverse.join('')
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
# Turns all email addresses into clickable links. If a block is given,
|
|
578
|
+
# each email is yielded and the result is used as the link text.
|
|
579
|
+
def auto_link_email_addresses(text, html_options = {})
|
|
580
|
+
body = text.dup
|
|
581
|
+
text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
|
|
582
|
+
text = $1
|
|
583
|
+
|
|
584
|
+
if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/)
|
|
585
|
+
text
|
|
586
|
+
else
|
|
587
|
+
display_text = (block_given?) ? yield(text) : text
|
|
588
|
+
mail_to text, display_text, html_options
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
end
|
|
592
|
+
end
|
|
593
|
+
end
|
|
594
|
+
end
|