mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding word boundary option to Ellipsis extension method
--HG-- branch : 1.x
This commit is contained in:
@@ -20,14 +20,20 @@ namespace Orchard.Utility.Extensions {
|
||||
return text.Ellipsize(characterCount, " …");
|
||||
}
|
||||
|
||||
public static string Ellipsize(this string text, int characterCount, string ellipsis) {
|
||||
public static string Ellipsize(this string text, int characterCount, string ellipsis, bool wordBoundary = false) {
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return "";
|
||||
|
||||
if (characterCount < 0 || text.Length <= characterCount)
|
||||
return text;
|
||||
|
||||
return Regex.Replace(text.Substring(0, characterCount + 1), @"\s+\S*$", "") + ellipsis;
|
||||
var trimmed = Regex.Replace(text.Substring(0, characterCount + 1), @"\s+\S*$", "") ;
|
||||
|
||||
if(wordBoundary) {
|
||||
trimmed = Regex.Replace(trimmed + ".", @"\W*\w*$", "");
|
||||
}
|
||||
|
||||
return trimmed + ellipsis;
|
||||
}
|
||||
|
||||
public static string HtmlClassify(this string text) {
|
||||
|
||||
Reference in New Issue
Block a user