Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b8eec5ac
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b8eec5ac
编写于
9月 21, 2008
作者:
M
Manfred Stienstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove special 1.9 version of excerpt helper.
上级
bfc73852
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
45 deletion
+45
-45
actionpack/lib/action_view/helpers/text_helper.rb
actionpack/lib/action_view/helpers/text_helper.rb
+45
-45
未找到文件。
actionpack/lib/action_view/helpers/text_helper.rb
浏览文件 @
b8eec5ac
...
...
@@ -121,57 +121,57 @@ def highlight(text, phrases, *args)
end
end
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
# The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
# defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
# then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
# will be stripped in any case. If the +phrase+ isn't found, nil is returned.
#
# ==== Examples
# excerpt('This is an example', 'an', :radius => 5)
# # => ...s is an exam...
#
# excerpt('This is an example', 'is', :radius => 5)
# # => This is a...
#
# excerpt('This is an example', 'is')
# # => This is an example
#
# excerpt('This next thing is an example', 'ex', :radius => 2)
# # => ...next...
#
# excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
# # => <chop> is also an example
#
# You can still use <tt>excerpt</tt> with the old API that accepts the
# +radius+ as its optional third and the +ellipsis+ as its
# optional forth parameter:
# excerpt('This is an example', 'an', 5) # => ...s is an exam...
# excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
def
excerpt
(
text
,
phrase
,
*
args
)
options
=
args
.
extract_options!
unless
args
.
empty?
options
[
:radius
]
=
args
[
0
]
||
100
options
[
:omission
]
=
args
[
1
]
||
"..."
end
options
.
reverse_merge!
(
:radius
=>
100
,
:omission
=>
"..."
)
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
# The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
# defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
# then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. The resulting string
# will be stripped in any case. If the +phrase+ isn't found, nil is returned.
#
# ==== Examples
# excerpt('This is an example', 'an', :radius => 5)
# # => ...s is an exam...
#
# excerpt('This is an example', 'is', :radius => 5)
# # => This is a...
#
# excerpt('This is an example', 'is')
# # => This is an example
#
# excerpt('This next thing is an example', 'ex', :radius => 2)
# # => ...next...
#
# excerpt('This is also an example', 'an', :radius => 8, :omission => '<chop> ')
# # => <chop> is also an example
#
# You can still use <tt>excerpt</tt> with the old API that accepts the
# +radius+ as its optional third and the +ellipsis+ as its
# optional forth parameter:
# excerpt('This is an example', 'an', 5) # => ...s is an exam...
# excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
def
excerpt
(
text
,
phrase
,
*
args
)
options
=
args
.
extract_options!
unless
args
.
empty?
options
[
:radius
]
=
args
[
0
]
||
100
options
[
:omission
]
=
args
[
1
]
||
"..."
end
options
.
reverse_merge!
(
:radius
=>
100
,
:omission
=>
"..."
)
if
text
&&
phrase
phrase
=
Regexp
.
escape
(
phrase
)
if
text
&&
phrase
phrase
=
Regexp
.
escape
(
phrase
)
if
found_pos
=
text
.
mb_chars
=~
/(
#{
phrase
}
)/i
start_pos
=
[
found_pos
-
options
[
:radius
],
0
].
max
end_pos
=
[
[
found_pos
+
phrase
.
mb_chars
.
length
+
options
[
:radius
]
-
1
,
0
].
max
,
text
.
mb_chars
.
length
].
min
if
found_pos
=
text
.
mb_chars
=~
/(
#{
phrase
}
)/i
start_pos
=
[
found_pos
-
options
[
:radius
],
0
].
max
end_pos
=
[
[
found_pos
+
phrase
.
mb_chars
.
length
+
options
[
:radius
]
-
1
,
0
].
max
,
text
.
mb_chars
.
length
].
min
prefix
=
start_pos
>
0
?
options
[
:omission
]
:
""
postfix
=
end_pos
<
text
.
mb_chars
.
length
-
1
?
options
[
:omission
]
:
""
prefix
=
start_pos
>
0
?
options
[
:omission
]
:
""
postfix
=
end_pos
<
text
.
mb_chars
.
length
-
1
?
options
[
:omission
]
:
""
prefix
+
text
.
mb_chars
[
start_pos
..
end_pos
].
strip
+
postfix
else
nil
end
prefix
+
text
.
mb_chars
[
start_pos
..
end_pos
].
strip
+
postfix
else
nil
end
end
end
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
# +plural+ is supplied, it will use that when count is > 1, otherwise
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录