2015-08-26 22:42:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace Nikse.SubtitleEdit.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// HTML specific string manipulations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class HtmlUtil
|
|
|
|
|
{
|
|
|
|
|
public const string TagItalic = "i";
|
|
|
|
|
public const string TagBold = "b";
|
|
|
|
|
public const string TagUnderline = "u";
|
|
|
|
|
public const string TagParagraph = "p";
|
|
|
|
|
public const string TagFont = "font";
|
|
|
|
|
public const string TagCyrillicI = "\u0456"; // Cyrillic Small Letter Byelorussian-Ukrainian i (http://graphemica.com/%D1%96)
|
|
|
|
|
|
|
|
|
|
private static readonly Regex TagOpenRegex = new Regex(@"<\s*(?:/\s*)?(\w+)[^>]*>", RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove all of the specified opening and closing tags from the source HTML string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The source string to search for specified HTML tags.</param>
|
|
|
|
|
/// <param name="tags">The HTML tags to remove.</param>
|
|
|
|
|
/// <returns>A new string without the specified opening and closing tags.</returns>
|
|
|
|
|
public static string RemoveOpenCloseTags(string source, params string[] tags)
|
|
|
|
|
{
|
|
|
|
|
// This pattern matches these tag formats:
|
|
|
|
|
// <tag*>
|
|
|
|
|
// < tag*>
|
|
|
|
|
// </tag*>
|
|
|
|
|
// < /tag*>
|
|
|
|
|
// </ tag*>
|
|
|
|
|
// < / tag*>
|
|
|
|
|
return TagOpenRegex.Replace(
|
|
|
|
|
source,
|
|
|
|
|
m => tags.Contains(m.Groups[1].Value, StringComparer.OrdinalIgnoreCase) ? string.Empty : m.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a string to an HTML-encoded string using named character references.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The string to encode.</param>
|
|
|
|
|
/// <returns>An encoded string.</returns>
|
|
|
|
|
public static string EncodeNamed(string source)
|
|
|
|
|
{
|
2015-09-06 01:45:51 +02:00
|
|
|
|
if (string.IsNullOrEmpty(source))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
var encoded = new StringBuilder(source.Length);
|
|
|
|
|
foreach (var ch in source)
|
|
|
|
|
{
|
|
|
|
|
switch (ch)
|
|
|
|
|
{
|
|
|
|
|
case '<':
|
|
|
|
|
encoded.Append("<");
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
encoded.Append(">");
|
|
|
|
|
break;
|
|
|
|
|
case '"':
|
|
|
|
|
encoded.Append(""");
|
|
|
|
|
break;
|
|
|
|
|
case '&':
|
|
|
|
|
encoded.Append("&");
|
|
|
|
|
break;
|
|
|
|
|
case '\'':
|
|
|
|
|
encoded.Append("'");
|
|
|
|
|
break;
|
|
|
|
|
case ' ':
|
|
|
|
|
encoded.Append(" ");
|
|
|
|
|
break;
|
|
|
|
|
case '–':
|
|
|
|
|
encoded.Append("–");
|
|
|
|
|
break;
|
|
|
|
|
case '—':
|
|
|
|
|
encoded.Append("—");
|
|
|
|
|
break;
|
|
|
|
|
case '¡':
|
|
|
|
|
encoded.Append("¡");
|
|
|
|
|
break;
|
|
|
|
|
case '¿':
|
|
|
|
|
encoded.Append("¿");
|
|
|
|
|
break;
|
|
|
|
|
case '“':
|
|
|
|
|
encoded.Append("“");
|
|
|
|
|
break;
|
|
|
|
|
case '”':
|
|
|
|
|
encoded.Append("”");
|
|
|
|
|
break;
|
|
|
|
|
case '‘':
|
|
|
|
|
encoded.Append("‘");
|
|
|
|
|
break;
|
|
|
|
|
case '’':
|
|
|
|
|
encoded.Append("’");
|
|
|
|
|
break;
|
|
|
|
|
case '«':
|
|
|
|
|
encoded.Append("«");
|
|
|
|
|
break;
|
|
|
|
|
case '»':
|
|
|
|
|
encoded.Append("»");
|
|
|
|
|
break;
|
|
|
|
|
case '¢':
|
|
|
|
|
encoded.Append("¢");
|
|
|
|
|
break;
|
|
|
|
|
case '©':
|
|
|
|
|
encoded.Append("©");
|
|
|
|
|
break;
|
|
|
|
|
case '÷':
|
|
|
|
|
encoded.Append("÷");
|
|
|
|
|
break;
|
|
|
|
|
case 'µ':
|
|
|
|
|
encoded.Append("µ");
|
|
|
|
|
break;
|
|
|
|
|
case '·':
|
|
|
|
|
encoded.Append("·");
|
|
|
|
|
break;
|
|
|
|
|
case '¶':
|
|
|
|
|
encoded.Append("¶");
|
|
|
|
|
break;
|
|
|
|
|
case '±':
|
|
|
|
|
encoded.Append("±");
|
|
|
|
|
break;
|
|
|
|
|
case '€':
|
|
|
|
|
encoded.Append("€");
|
|
|
|
|
break;
|
|
|
|
|
case '£':
|
|
|
|
|
encoded.Append("£");
|
|
|
|
|
break;
|
|
|
|
|
case '®':
|
|
|
|
|
encoded.Append("®");
|
|
|
|
|
break;
|
|
|
|
|
case '§':
|
|
|
|
|
encoded.Append("§");
|
|
|
|
|
break;
|
|
|
|
|
case '™':
|
|
|
|
|
encoded.Append("™");
|
|
|
|
|
break;
|
|
|
|
|
case '¥':
|
|
|
|
|
encoded.Append("¥");
|
|
|
|
|
break;
|
|
|
|
|
case 'á':
|
|
|
|
|
encoded.Append("á");
|
|
|
|
|
break;
|
|
|
|
|
case 'Á':
|
|
|
|
|
encoded.Append("Á");
|
|
|
|
|
break;
|
|
|
|
|
case 'à':
|
|
|
|
|
encoded.Append("à");
|
|
|
|
|
break;
|
|
|
|
|
case 'À':
|
|
|
|
|
encoded.Append("À");
|
|
|
|
|
break;
|
|
|
|
|
case 'â':
|
|
|
|
|
encoded.Append("â");
|
|
|
|
|
break;
|
|
|
|
|
case 'Â':
|
|
|
|
|
encoded.Append("Â");
|
|
|
|
|
break;
|
|
|
|
|
case 'å':
|
|
|
|
|
encoded.Append("å");
|
|
|
|
|
break;
|
|
|
|
|
case 'Å':
|
|
|
|
|
encoded.Append("Å");
|
|
|
|
|
break;
|
|
|
|
|
case 'ã':
|
|
|
|
|
encoded.Append("ã");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ã':
|
|
|
|
|
encoded.Append("Ã");
|
|
|
|
|
break;
|
|
|
|
|
case 'ä':
|
|
|
|
|
encoded.Append("ä");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ä':
|
|
|
|
|
encoded.Append("Ä");
|
|
|
|
|
break;
|
|
|
|
|
case 'æ':
|
|
|
|
|
encoded.Append("æ");
|
|
|
|
|
break;
|
|
|
|
|
case 'Æ':
|
|
|
|
|
encoded.Append("Æ");
|
|
|
|
|
break;
|
|
|
|
|
case 'ç':
|
|
|
|
|
encoded.Append("ç");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ç':
|
|
|
|
|
encoded.Append("Ç");
|
|
|
|
|
break;
|
|
|
|
|
case 'é':
|
|
|
|
|
encoded.Append("é");
|
|
|
|
|
break;
|
|
|
|
|
case 'É':
|
|
|
|
|
encoded.Append("É");
|
|
|
|
|
break;
|
|
|
|
|
case 'è':
|
|
|
|
|
encoded.Append("è");
|
|
|
|
|
break;
|
|
|
|
|
case 'È':
|
|
|
|
|
encoded.Append("È");
|
|
|
|
|
break;
|
|
|
|
|
case 'ê':
|
|
|
|
|
encoded.Append("ê");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ê':
|
|
|
|
|
encoded.Append("Ê");
|
|
|
|
|
break;
|
|
|
|
|
case 'ë':
|
|
|
|
|
encoded.Append("ë");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ë':
|
|
|
|
|
encoded.Append("Ë");
|
|
|
|
|
break;
|
|
|
|
|
case 'í':
|
|
|
|
|
encoded.Append("í");
|
|
|
|
|
break;
|
|
|
|
|
case 'Í':
|
|
|
|
|
encoded.Append("Í");
|
|
|
|
|
break;
|
|
|
|
|
case 'ì':
|
|
|
|
|
encoded.Append("ì");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ì':
|
|
|
|
|
encoded.Append("Ì");
|
|
|
|
|
break;
|
|
|
|
|
case 'î':
|
|
|
|
|
encoded.Append("î");
|
|
|
|
|
break;
|
|
|
|
|
case 'Î':
|
|
|
|
|
encoded.Append("Î");
|
|
|
|
|
break;
|
|
|
|
|
case 'ï':
|
|
|
|
|
encoded.Append("ï");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ï':
|
|
|
|
|
encoded.Append("Ï");
|
|
|
|
|
break;
|
|
|
|
|
case 'ñ':
|
|
|
|
|
encoded.Append("ñ");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ñ':
|
|
|
|
|
encoded.Append("Ñ");
|
|
|
|
|
break;
|
|
|
|
|
case 'ó':
|
|
|
|
|
encoded.Append("ó");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ó':
|
|
|
|
|
encoded.Append("Ó");
|
|
|
|
|
break;
|
|
|
|
|
case 'ò':
|
|
|
|
|
encoded.Append("ò");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ò':
|
|
|
|
|
encoded.Append("Ò");
|
|
|
|
|
break;
|
|
|
|
|
case 'ô':
|
|
|
|
|
encoded.Append("ô");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ô':
|
|
|
|
|
encoded.Append("Ô");
|
|
|
|
|
break;
|
|
|
|
|
case 'ø':
|
|
|
|
|
encoded.Append("ø");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ø':
|
|
|
|
|
encoded.Append("Ø");
|
|
|
|
|
break;
|
|
|
|
|
case 'õ':
|
|
|
|
|
encoded.Append("õ");
|
|
|
|
|
break;
|
|
|
|
|
case 'Õ':
|
|
|
|
|
encoded.Append("Õ");
|
|
|
|
|
break;
|
|
|
|
|
case 'ö':
|
|
|
|
|
encoded.Append("ö");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ö':
|
|
|
|
|
encoded.Append("Ö");
|
|
|
|
|
break;
|
|
|
|
|
case 'ß':
|
|
|
|
|
encoded.Append("ß");
|
|
|
|
|
break;
|
|
|
|
|
case 'ú':
|
|
|
|
|
encoded.Append("ú");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ú':
|
|
|
|
|
encoded.Append("Ú");
|
|
|
|
|
break;
|
|
|
|
|
case 'ù':
|
|
|
|
|
encoded.Append("ù");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ù':
|
|
|
|
|
encoded.Append("Ù");
|
|
|
|
|
break;
|
|
|
|
|
case 'û':
|
|
|
|
|
encoded.Append("û");
|
|
|
|
|
break;
|
|
|
|
|
case 'Û':
|
|
|
|
|
encoded.Append("Û");
|
|
|
|
|
break;
|
|
|
|
|
case 'ü':
|
|
|
|
|
encoded.Append("ü");
|
|
|
|
|
break;
|
|
|
|
|
case 'Ü':
|
|
|
|
|
encoded.Append("Ü");
|
|
|
|
|
break;
|
|
|
|
|
case 'ÿ':
|
|
|
|
|
encoded.Append("ÿ");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (ch > 127)
|
|
|
|
|
encoded.Append("&#" + (int)ch + ";");
|
|
|
|
|
else
|
|
|
|
|
encoded.Append(ch);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return encoded.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a string to an HTML-encoded string using numeric character references.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source">The string to encode.</param>
|
|
|
|
|
/// <returns>An encoded string.</returns>
|
|
|
|
|
public static string EncodeNumeric(string source)
|
|
|
|
|
{
|
2015-09-06 01:45:51 +02:00
|
|
|
|
if (string.IsNullOrEmpty(source))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
var encoded = new StringBuilder(source.Length);
|
|
|
|
|
foreach (var ch in source)
|
|
|
|
|
{
|
|
|
|
|
if (ch == ' ')
|
|
|
|
|
{
|
|
|
|
|
encoded.Append("&#");
|
|
|
|
|
encoded.Append(160); //
|
|
|
|
|
encoded.Append(';');
|
|
|
|
|
}
|
|
|
|
|
else if (ch > 127 || ch == '<' || ch == '>' || ch == '"' || ch == '&' || ch == '\'')
|
|
|
|
|
{
|
|
|
|
|
encoded.Append("&#");
|
|
|
|
|
encoded.Append((int)ch);
|
|
|
|
|
encoded.Append(';');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
encoded.Append(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return encoded.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string RemoveHtmlTags(string s, bool alsoSsaTags = false)
|
|
|
|
|
{
|
|
|
|
|
if (s == null || s.Length < 3)
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
|
|
if (alsoSsaTags)
|
|
|
|
|
s = Utilities.RemoveSsaTags(s);
|
|
|
|
|
|
|
|
|
|
if (!s.Contains('<'))
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
|
|
if (s.Contains("< "))
|
|
|
|
|
s = FixInvalidItalicTags(s);
|
|
|
|
|
|
|
|
|
|
return RemoveOpenCloseTags(s, TagItalic, TagBold, TagUnderline, TagParagraph, TagFont, TagCyrillicI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsUrl(string text)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(text) || text.Length < 6 || !text.Contains('.') || text.Contains(' '))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var allLower = text.ToLower();
|
|
|
|
|
if (allLower.StartsWith("http://", StringComparison.Ordinal) || allLower.StartsWith("https://", StringComparison.Ordinal) ||
|
|
|
|
|
allLower.StartsWith("www.", StringComparison.Ordinal) || allLower.EndsWith(".org", StringComparison.Ordinal) ||
|
|
|
|
|
allLower.EndsWith(".com", StringComparison.Ordinal) || allLower.EndsWith(".net", StringComparison.Ordinal))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (allLower.Contains(".org/") || allLower.Contains(".com/") || allLower.Contains(".net/"))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool StartsWithUrl(string text)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var arr = text.Trim().TrimEnd('.').TrimEnd().Split();
|
|
|
|
|
if (arr.Length == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return IsUrl(arr[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 16:38:58 +02:00
|
|
|
|
private static readonly string[] UppercaseTags = { "<I>", "<U>", "<B>", "<FONT", "</I>", "</U>", "</B>", "</FONT>" };
|
|
|
|
|
|
2015-08-26 22:42:40 +02:00
|
|
|
|
public static string FixUpperTags(string text)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(text))
|
|
|
|
|
return text;
|
2015-08-27 16:38:58 +02:00
|
|
|
|
var idx = text.IndexOfAny(UppercaseTags, StringComparison.Ordinal);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
while (idx >= 0)
|
|
|
|
|
{
|
|
|
|
|
var endIdx = text.IndexOf('>', idx + 2);
|
|
|
|
|
if (endIdx < idx)
|
|
|
|
|
break;
|
|
|
|
|
var tag = text.Substring(idx, endIdx - idx).ToLowerInvariant();
|
|
|
|
|
text = text.Remove(idx, endIdx - idx).Insert(idx, tag);
|
2015-08-27 16:38:58 +02:00
|
|
|
|
idx = text.IndexOfAny(UppercaseTags, StringComparison.Ordinal);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string FixInvalidItalicTags(string text)
|
|
|
|
|
{
|
|
|
|
|
const string beginTag = "<i>";
|
|
|
|
|
const string endTag = "</i>";
|
|
|
|
|
|
|
|
|
|
text = text.Replace("< i >", beginTag);
|
|
|
|
|
text = text.Replace("< i>", beginTag);
|
|
|
|
|
text = text.Replace("<i >", beginTag);
|
|
|
|
|
text = text.Replace("< I>", beginTag);
|
|
|
|
|
text = text.Replace("<I >", beginTag);
|
|
|
|
|
|
|
|
|
|
text = text.Replace("< / i >", endTag);
|
|
|
|
|
text = text.Replace("< /i>", endTag);
|
|
|
|
|
text = text.Replace("</ i>", endTag);
|
|
|
|
|
text = text.Replace("< /i>", endTag);
|
|
|
|
|
text = text.Replace("< /i >", endTag);
|
|
|
|
|
text = text.Replace("</i >", endTag);
|
|
|
|
|
text = text.Replace("</ i >", endTag);
|
|
|
|
|
text = text.Replace("< / i>", endTag);
|
|
|
|
|
text = text.Replace("< /I>", endTag);
|
|
|
|
|
text = text.Replace("</ I>", endTag);
|
|
|
|
|
text = text.Replace("< /I>", endTag);
|
|
|
|
|
text = text.Replace("< / I >", endTag);
|
|
|
|
|
|
|
|
|
|
text = text.Replace("</i> <i>", "_@_");
|
|
|
|
|
text = text.Replace(" _@_", "_@_");
|
|
|
|
|
text = text.Replace(" _@_ ", "_@_");
|
|
|
|
|
text = text.Replace("_@_", " ");
|
|
|
|
|
|
|
|
|
|
if (text.Contains(beginTag))
|
|
|
|
|
text = text.Replace("<i/>", endTag);
|
|
|
|
|
else
|
|
|
|
|
text = text.Replace("<i/>", string.Empty);
|
|
|
|
|
|
|
|
|
|
text = text.Replace(beginTag + beginTag, beginTag);
|
|
|
|
|
text = text.Replace(endTag + endTag, endTag);
|
|
|
|
|
|
|
|
|
|
int italicBeginTagCount = Utilities.CountTagInText(text, beginTag);
|
|
|
|
|
int italicEndTagCount = Utilities.CountTagInText(text, endTag);
|
|
|
|
|
int noOfLines = Utilities.GetNumberOfLines(text);
|
|
|
|
|
if (italicBeginTagCount + italicEndTagCount > 0)
|
|
|
|
|
{
|
|
|
|
|
if (italicBeginTagCount == 1 && italicEndTagCount == 1 && text.IndexOf(beginTag, StringComparison.Ordinal) > text.IndexOf(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
text = text.Replace(beginTag, "___________@");
|
|
|
|
|
text = text.Replace(endTag, beginTag);
|
|
|
|
|
text = text.Replace("___________@", endTag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (italicBeginTagCount == 2 && italicEndTagCount == 0)
|
|
|
|
|
{
|
|
|
|
|
int firstIndex = text.IndexOf(beginTag, StringComparison.Ordinal);
|
|
|
|
|
int lastIndex = text.LastIndexOf(beginTag, StringComparison.Ordinal);
|
|
|
|
|
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag, StringComparison.Ordinal) + Environment.NewLine.Length;
|
|
|
|
|
if (noOfLines == 2 && lastIndex == lastIndexWithNewLine && firstIndex < 2)
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = text.Replace(Environment.NewLine, endTag + Environment.NewLine) + endTag;
|
2015-08-26 22:42:40 +02:00
|
|
|
|
else if (text.Length > lastIndex + endTag.Length)
|
|
|
|
|
text = text.Substring(0, lastIndex) + endTag + text.Substring(lastIndex - 1 + endTag.Length);
|
|
|
|
|
else
|
|
|
|
|
text = text.Substring(0, lastIndex) + endTag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (italicBeginTagCount == 1 && italicEndTagCount == 2)
|
|
|
|
|
{
|
|
|
|
|
int firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
|
|
|
|
|
if (text.StartsWith("</i>-<i>-", StringComparison.Ordinal))
|
|
|
|
|
text = text.Remove(0, 5);
|
|
|
|
|
else if (text.StartsWith("</i>- <i>-", StringComparison.Ordinal))
|
|
|
|
|
text = text.Remove(0, 5);
|
|
|
|
|
else if (text.StartsWith("</i>- <i> -", StringComparison.Ordinal))
|
|
|
|
|
text = text.Remove(0, 5);
|
|
|
|
|
else if (text.StartsWith("</i>-<i> -", StringComparison.Ordinal))
|
|
|
|
|
text = text.Remove(0, 5);
|
|
|
|
|
else if (firstIndex == 0)
|
|
|
|
|
text = text.Remove(0, 4);
|
|
|
|
|
else
|
|
|
|
|
text = text.Substring(0, firstIndex) + text.Substring(firstIndex + endTag.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (italicBeginTagCount == 2 && italicEndTagCount == 1)
|
|
|
|
|
{
|
|
|
|
|
var lines = text.SplitToLines();
|
2015-09-08 12:30:52 +02:00
|
|
|
|
if (lines.Length == 2 && lines[0].StartsWith(beginTag, StringComparison.Ordinal) && lines[0].EndsWith(endTag, StringComparison.Ordinal) &&
|
|
|
|
|
lines[1].StartsWith(beginTag, StringComparison.Ordinal))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = text.TrimEnd() + endTag;
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int lastIndex = text.LastIndexOf(beginTag, StringComparison.Ordinal);
|
|
|
|
|
if (text.Length > lastIndex + endTag.Length)
|
|
|
|
|
text = text.Substring(0, lastIndex) + text.Substring(lastIndex - 1 + endTag.Length);
|
|
|
|
|
else
|
|
|
|
|
text = text.Substring(0, lastIndex - 1) + endTag;
|
|
|
|
|
}
|
2015-09-08 12:30:52 +02:00
|
|
|
|
if (text.StartsWith(beginTag, StringComparison.Ordinal) && text.EndsWith(endTag, StringComparison.Ordinal) && text.Contains(endTag + Environment.NewLine + beginTag))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = text.Replace(endTag + Environment.NewLine + beginTag, Environment.NewLine);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (italicBeginTagCount == 1 && italicEndTagCount == 0)
|
|
|
|
|
{
|
|
|
|
|
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag, StringComparison.Ordinal) + Environment.NewLine.Length;
|
|
|
|
|
int lastIndex = text.LastIndexOf(beginTag, StringComparison.Ordinal);
|
|
|
|
|
|
|
|
|
|
if (text.StartsWith(beginTag, StringComparison.Ordinal))
|
|
|
|
|
text += endTag;
|
|
|
|
|
else if (noOfLines == 2 && lastIndex == lastIndexWithNewLine)
|
|
|
|
|
text += endTag;
|
|
|
|
|
else
|
|
|
|
|
text = text.Replace(beginTag, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (italicBeginTagCount == 0 && italicEndTagCount == 1)
|
|
|
|
|
{
|
2015-12-27 00:06:04 +01:00
|
|
|
|
var cleanText = RemoveOpenCloseTags(text, TagItalic, TagBold, TagUnderline, TagCyrillicI);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
bool isFixed = false;
|
|
|
|
|
|
|
|
|
|
// Foo.</i>
|
|
|
|
|
if (text.EndsWith(endTag, StringComparison.Ordinal) && !cleanText.StartsWith('-') && !cleanText.Contains(Environment.NewLine + "-"))
|
|
|
|
|
{
|
|
|
|
|
text = beginTag + text;
|
|
|
|
|
isFixed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - Foo</i> | - Foo.
|
|
|
|
|
// - Bar. | - Foo.</i>
|
|
|
|
|
if (!isFixed && Utilities.GetNumberOfLines(cleanText) == 2)
|
|
|
|
|
{
|
|
|
|
|
int newLineIndex = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
|
|
|
|
|
if (newLineIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
var firstLine = text.Substring(0, newLineIndex).Trim();
|
|
|
|
|
var secondLine = text.Substring(newLineIndex + 2).Trim();
|
|
|
|
|
if (firstLine.EndsWith(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
firstLine = beginTag + firstLine;
|
|
|
|
|
isFixed = true;
|
|
|
|
|
}
|
|
|
|
|
if (secondLine.EndsWith(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
secondLine = beginTag + secondLine;
|
|
|
|
|
isFixed = true;
|
|
|
|
|
}
|
|
|
|
|
text = firstLine + Environment.NewLine + secondLine;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!isFixed)
|
|
|
|
|
text = text.Replace(endTag, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - foo.</i>
|
|
|
|
|
// - bar.</i>
|
|
|
|
|
if (italicBeginTagCount == 0 && italicEndTagCount == 2 && text.Contains(endTag + Environment.NewLine, StringComparison.Ordinal) && text.EndsWith(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
text = text.Replace(endTag, string.Empty);
|
|
|
|
|
text = beginTag + text + endTag;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 12:30:52 +02:00
|
|
|
|
if (italicBeginTagCount == 0 && italicEndTagCount == 2 && text.StartsWith(endTag, StringComparison.Ordinal) && text.EndsWith(endTag, StringComparison.Ordinal))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
|
|
|
|
int firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = text.Remove(firstIndex, endTag.Length).Insert(firstIndex, beginTag);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <i>Foo</i>
|
|
|
|
|
// <i>Bar</i>
|
|
|
|
|
if (italicBeginTagCount == 2 && italicEndTagCount == 2 && Utilities.GetNumberOfLines(text) == 2)
|
|
|
|
|
{
|
|
|
|
|
int index = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
|
|
|
|
|
if (index > 0 && text.Length > index + (beginTag.Length + endTag.Length))
|
|
|
|
|
{
|
|
|
|
|
var firstLine = text.Substring(0, index).Trim();
|
|
|
|
|
var secondLine = text.Substring(index + 2).Trim();
|
|
|
|
|
|
|
|
|
|
if (firstLine.Length > 10 && firstLine.StartsWith("- <i>", StringComparison.Ordinal) && firstLine.EndsWith(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
text = "<i>- " + firstLine.Remove(0, 5) + Environment.NewLine + secondLine;
|
|
|
|
|
text = text.Replace("<i>- ", "<i>- ");
|
|
|
|
|
index = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
|
|
|
|
|
firstLine = text.Substring(0, index).Trim();
|
|
|
|
|
secondLine = text.Substring(index + 2).Trim();
|
|
|
|
|
}
|
|
|
|
|
if (secondLine.Length > 10 && secondLine.StartsWith("- <i>", StringComparison.Ordinal) && secondLine.EndsWith(endTag, StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
text = firstLine + Environment.NewLine + "<i>- " + secondLine.Remove(0, 5);
|
|
|
|
|
text = text.Replace("<i>- ", "<i>- ");
|
|
|
|
|
index = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
|
|
|
|
|
firstLine = text.Substring(0, index).Trim();
|
|
|
|
|
secondLine = text.Substring(index + 2).Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Utilities.StartsAndEndsWithTag(firstLine, beginTag, endTag) && Utilities.StartsAndEndsWithTag(secondLine, beginTag, endTag))
|
|
|
|
|
{
|
|
|
|
|
text = text.Replace(beginTag, String.Empty).Replace(endTag, String.Empty).Trim();
|
|
|
|
|
text = beginTag + text + endTag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//FALCONE:<i> I didn't think</i><br /><i>it was going to be you,</i>
|
|
|
|
|
var colIdx = text.IndexOf(':');
|
2015-09-08 12:30:52 +02:00
|
|
|
|
if (colIdx > -1 && Utilities.CountTagInText(text, beginTag) + Utilities.CountTagInText(text, endTag) == 4 && text.Length > colIdx + 1 && !char.IsDigit(text[colIdx + 1]))
|
2015-08-26 22:42:40 +02:00
|
|
|
|
{
|
|
|
|
|
var firstLine = text.Substring(0, index);
|
|
|
|
|
var secondLine = text.Substring(index).TrimStart();
|
|
|
|
|
|
|
|
|
|
var secIdxCol = secondLine.IndexOf(':');
|
|
|
|
|
if (secIdxCol < 0 || !Utilities.IsBetweenNumbers(secondLine, secIdxCol))
|
|
|
|
|
{
|
|
|
|
|
var idx = firstLine.IndexOf(':');
|
|
|
|
|
if (idx > 1)
|
|
|
|
|
{
|
|
|
|
|
var pre = text.Substring(0, idx + 1).TrimStart();
|
|
|
|
|
text = text.Remove(0, idx + 1);
|
|
|
|
|
text = FixInvalidItalicTags(text).Trim();
|
|
|
|
|
if (text.StartsWith("<i> ", StringComparison.OrdinalIgnoreCase))
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = Utilities.RemoveSpaceBeforeAfterTag(text, beginTag);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
text = pre + " " + text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<i>- You think they're they gone?<i>
|
|
|
|
|
//<i>- That can't be.</i>
|
|
|
|
|
if ((italicBeginTagCount == 3 && italicEndTagCount == 1) && Utilities.GetNumberOfLines(text) == 2)
|
|
|
|
|
{
|
|
|
|
|
var newLineIdx = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
|
|
|
|
|
var firstLine = text.Substring(0, newLineIdx).Trim();
|
|
|
|
|
var secondLine = text.Substring(newLineIdx).Trim();
|
|
|
|
|
|
|
|
|
|
if ((Utilities.StartsAndEndsWithTag(firstLine, beginTag, beginTag) && Utilities.StartsAndEndsWithTag(secondLine, beginTag, endTag)) ||
|
|
|
|
|
(Utilities.StartsAndEndsWithTag(secondLine, beginTag, beginTag) && Utilities.StartsAndEndsWithTag(firstLine, beginTag, endTag)))
|
|
|
|
|
{
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = text.Replace(beginTag, string.Empty);
|
|
|
|
|
text = text.Replace(endTag, string.Empty);
|
2015-08-26 22:42:40 +02:00
|
|
|
|
text = text.Replace(" ", " ").Trim();
|
2015-09-08 12:30:52 +02:00
|
|
|
|
text = beginTag + text + endTag;
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
text = text.Replace("<i></i>", string.Empty);
|
|
|
|
|
text = text.Replace("<i> </i>", string.Empty);
|
|
|
|
|
text = text.Replace("<i> </i>", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 21:34:58 +02:00
|
|
|
|
public static string ToogleTag(string text, string tag)
|
|
|
|
|
{
|
|
|
|
|
if (text.Contains("<" + tag + ">"))
|
|
|
|
|
{
|
|
|
|
|
text = text.Replace("<" + tag + ">", string.Empty);
|
|
|
|
|
text = text.Replace("</" + tag + ">", string.Empty);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int indexOfEndBracket = text.IndexOf('}');
|
|
|
|
|
if (text.StartsWith("{\\", StringComparison.Ordinal) && indexOfEndBracket > 1 && indexOfEndBracket < 6)
|
|
|
|
|
{
|
2016-01-03 05:15:38 +01:00
|
|
|
|
text = $"{text.Substring(0, indexOfEndBracket + 1)}<{tag}>{text.Remove(0, indexOfEndBracket + 1)}</{tag}>";
|
2015-09-25 21:34:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-03 05:15:38 +01:00
|
|
|
|
text = $"<{tag}>{text}</{tag}>";
|
2015-09-25 21:34:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 22:42:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|