71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using System;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace C4IT.API
|
||
{
|
||
public static class ConverterHelper
|
||
{
|
||
public static string Html2Plaintext(string html)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(html))
|
||
return string.Empty;
|
||
|
||
// 1. HTML‐Entities dekodieren (z.B. ü → ü)
|
||
string decoded = WebUtility.HtmlDecode(html);
|
||
|
||
// 2. </p> und <br> durch Zeilenumbruch ersetzen
|
||
string withBreaks = Regex.Replace(
|
||
decoded,
|
||
@"</(?:p|div|li|blockquote|pre|h[1-6]|tr)\s*> # schließende Block-Tags
|
||
|<(?:br|hr)\b[^>]*> # <br>, <hr>
|
||
|<(?:ul|ol|table|thead|tbody|tfoot|article # öffnende Block-Container
|
||
|section|nav|aside|header|footer
|
||
|figure|figcaption|details|summary)[^>]*>",
|
||
" ",
|
||
RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
|
||
|
||
// 3. Alle übrigen Tags entfernen
|
||
string noTags = Regex.Replace(withBreaks, @"<[^>]+>", string.Empty);
|
||
|
||
return noTags.Trim();
|
||
}
|
||
|
||
public static bool ObjectToBoolConverter(object source)
|
||
{
|
||
try
|
||
{
|
||
if (source.Equals("1"))
|
||
{
|
||
source = 1;
|
||
}
|
||
return Convert.ToBoolean(source);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static int ObjectToIntConverter(object source)
|
||
{
|
||
try
|
||
{
|
||
return Convert.ToInt32(source);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
public static string ObjectToStringConverter(this object value)
|
||
{
|
||
return (value ?? string.Empty).ToString();
|
||
}
|
||
|
||
|
||
}
|
||
}
|