Skip to content

Commit

Permalink
added Text.Zip
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Jan 10, 2019
1 parent fcbd03f commit a7bb964
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Blade/Blade/Tags/LineBreaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public static Regex Replacer(string names, bool open = true, bool close = true)
return new Regex("<" + closer + names + "[^>]*>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}

private static readonly Regex NewLine = new Regex(@"[\r\n]");
/// <summary>
/// Convert \n into line-breaks
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string Nl2Br(string value)
{
return NewLine.Replace(value, "<br>");
return Text.Nl2X(value, "<br>");
}


private static readonly Regex Br = Replacer("br");

/// <summary>
/// Convert <br> and <br/> into line-breaks
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions Blade/Blade/Tags/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public static string Remove(string original)
sanitizedText = sanitizedText.Replace("<", " ").Replace(">", " ");

// combine resulting multi-spaces
sanitizedText = Regex.Replace(sanitizedText, "\\s{2,}", " ");
sanitizedText = Text.ShrinkSpaces(sanitizedText);// Regex.Replace(sanitizedText, "\\s{2,}", " ");

return sanitizedText.Trim();
}



}
}
21 changes: 21 additions & 0 deletions Blade/Blade/Text/Crop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Connect.Razor.Internals;

namespace Connect.Razor.Blade
{
public static partial class Text
{
/// <summary>
/// Cut off a text at the best possible place with a max-length.
/// This will count html-entities like &amp; &nbsp; or umlauts as 1 character,
/// and will try to cut off between words if possible.
/// </summary>
/// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param>
/// <param name="length">length to cut off at</param>
/// <returns></returns>
public static string Crop(string value, int length)
{
return Truncator.SafeTruncate(value, length);
}

}
}
12 changes: 0 additions & 12 deletions Blade/Blade/Text/Ellipsis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ namespace Connect.Razor.Blade
{
public static partial class Text
{
/// <summary>
/// Cut off a text at the best possible place with a max-length.
/// This will count html-entities like &amp; &nbsp; or umlauts as 1 character,
/// and will try to cut off between words if possible.
/// </summary>
/// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param>
/// <param name="length">length to cut off at</param>
/// <returns></returns>
public static string Crop(string value, int length)
{
return Truncator.SafeTruncate(value, length);
}

/// <summary>
/// Crop a text if too long, add in that case, also add an ellipsis or a custom suffix
Expand Down
9 changes: 5 additions & 4 deletions Blade/Blade/Text/HasText.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Connect.Razor.Internals;
using System;
using System.Text.RegularExpressions;
using Connect.Razor.Internals;

namespace Connect.Razor.Blade
{
Expand All @@ -25,7 +27,7 @@ public static bool Has(object value, bool handleHtmlWhitespaces = true)
public static bool Has(string value, bool handleHtmlWhitespaces = true)
{
// do quick-check, as this will usually be all it needs
if(string.IsNullOrWhiteSpace(value))
if(String.IsNullOrWhiteSpace(value))
return false;

// if it got here and we don't want to re-check for html-whitespace, then we do have text
Expand All @@ -36,8 +38,7 @@ public static bool Has(string value, bool handleHtmlWhitespaces = true)
foreach (var whitespace in Defaults.HtmlNonBreakingSpaces)
value = value.Replace(whitespace, " ");

return !string.IsNullOrWhiteSpace(value);
return !String.IsNullOrWhiteSpace(value);
}

}
}
32 changes: 32 additions & 0 deletions Blade/Blade/Text/Zip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text.RegularExpressions;
using Connect.Razor.Internals;

namespace Connect.Razor.Blade
{
public static partial class Text
{
/// <summary>
/// Will remove all new-lines from a string and merge multiple spaces together
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string Zip(string value)
{
return string.IsNullOrEmpty(value)
? value
: ShrinkSpaces(Nl2X(value, " "));
}

internal static string ShrinkSpaces(string value)
{
return Regex.Replace(value, @"\s{2,}", " ");
}

internal static readonly Regex NewLine = new Regex(@"[\r\n]");

internal static string Nl2X(string value, string replacement)
{
return NewLine.Replace(value, replacement);
}
}
}
2 changes: 2 additions & 0 deletions Blade/RazorBlade.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Blade\Tags\LineBreaks.cs" />
<Compile Include="Blade\Text\Zip.cs" />
<Compile Include="Blade\Text\Crop.cs" />
<Compile Include="Internals\Defaults.cs" />
<Compile Include="Blade\Dic\SafeGet.cs" />
<Compile Include="Blade\Dic\Dynamic.cs" />
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ The goal is to provide helpers for very common code snippets or functions, which
_Sometimes you need the first 100 characters followed by an ellipsis (if truncated), but umlauts like `&uuml;` will mess up your count or might even be cut off. This is automatically handled by:_

```razor
@* just cut it off at the visible character count, not splitting words *@
@Text.Crop(someText, 100)
@* truncate a text and if necessary, add ellipsis character *@
@Text.Ellipsis(longText, 100)
```

_Or sometimes you need a value, but if it's empty, you need another one. So instead of writing:_
Expand All @@ -37,9 +41,6 @@ _Note that HTML whitespace like `&nbsp;` will also be treated as empty, unless y
@* remove html from a wysiwyg-string *@
@Tags.Remove(formattedText)
@* truncate a text and if necessary, add ellipsis character *@
@Text.Ellipsis(longText, 100)
@* the same with a custom ending *@
@Text.Ellipsis(longText, 100, "...more")
Expand Down Expand Up @@ -72,6 +73,7 @@ This is a short summary of the most used variations of the helpers. Further deta
1. `Text.Ellipsis(value, length)`
1. `Text.Has(value)`
1. `Text.First(value, value[, moreValues, ...])`
1. `Text.Zip(value)`

## Commands in v0.1 to Shorten Texts Correctly

Expand All @@ -95,6 +97,10 @@ This is a short summary of the most used variations of the helpers. Further deta

1. `Text.First(intendedValue, next-value, next-value, [up to 5 values], false)` - same behavior as above, values will be checked in the order given. By ending with `false` html-whitespace will not be cleaned but treated as text.

### Commands to Clean up Text

1. `Text.Zip(value)` will remove line-breaks and shrink all multiple-spaces into one single space.

## Commands in v0.1 to Convert Html to Text or Back

1. `Tags.Remove(htmlText)` - strips the html from an string, ensuring that all tags will cause 1 spaces between words, but only one (multiple spaces are shortened to 1 again)
Expand Down
29 changes: 29 additions & 0 deletions Razor Blades Tests/Test_Blades_Zip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Connect.Razor.Internals;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Connect.Razor.Blade;


namespace Razor_Blades_Tests
{
[TestClass]
public class Test_Blades_Zip
{

[TestMethod]
public void Test_Zip_Basic()
{
var message = "This is a teaser for something";
var expected = "This is a teaser for something";
Assert.AreEqual(expected, Text.Zip(message), "multiple spaces must go");
}

[TestMethod]
public void Test_Zip_NewLine()
{
var message = "This is a \n teaser\n for something";
var expected = "This is a teaser for something";
Assert.AreEqual(expected, Text.Zip(message), "multiple spaces must go");
}

}
}
1 change: 1 addition & 0 deletions Razor Blades Tests/Tests for RazorBlade.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Test_Blades_Zip.cs" />
<Compile Include="Test_Blades_Html.cs" />
<Compile Include="Test_Blades_HasText.cs" />
<Compile Include="Test_Blades_FirstText.cs" />
Expand Down

0 comments on commit a7bb964

Please sign in to comment.