Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string FixInvalidItalicTags(string input)
- {
- var text = input;
- var preTags = string.Empty;
- if (text.StartsWith("{\\", StringComparison.Ordinal))
- {
- var endIdx = text.IndexOf('}', 2);
- if (endIdx > 2)
- {
- preTags = text.Substring(0, endIdx + 1);
- text = text.Remove(0, endIdx + 1);
- }
- }
- const string beginTag = "<i>";
- const string endTag = "</i>";
- foreach (var beginTagVariation in BeginTagVariations)
- {
- text = text.Replace(beginTagVariation, beginTag);
- }
- foreach (var endTagVariation in EndTagVariations)
- {
- text = text.Replace(endTagVariation, endTag);
- }
- text = text.Replace("</i> <i>", "_@_");
- text = text.Replace(" _@_", "_@_");
- text = text.Replace(" _@_ ", "_@_");
- text = text.Replace("_@_", " ");
- var tagsToCheck = new HashSet<string>(text.Length / 3);
- var l = 0;
- for (var r = 0; r < text.Length; r++)
- {
- if (text[r] == '<')
- {
- l = r;
- }
- else if (text[r] == '>' && text[l] == '<') // expecting: <i/>
- {
- if (text[r - 1] == '/')
- {
- tagsToCheck.Add(text.Substring(l, r - l + 1));
- }
- else
- {
- l = r + 1;
- }
- }
- else if (text[l] != '<')
- {
- l = r;
- }
- }
- foreach (string tag in tagsToCheck)
- {
- var hasOpeningVariant = text.Contains(tag.Replace("/", string.Empty));
- if (hasOpeningVariant)
- {
- text = text.Replace(tag, tag
- .Replace("/", string.Empty)
- .Replace("<", "</"));
- }
- else // remove
- {
- text = text.Replace(tag, string.Empty);
- }
- }
- var replacement = text.Contains(beginTag) ? endTag : string.Empty;
- foreach (var invalidTag in new[] { "<i/>", "<I/>" })
- {
- text = text.Replace(invalidTag, replacement);
- }
- text = text.Replace("]<i> ", "] <i>");
- text = text.Replace(")<i> ", ") <i>");
- text = text.Replace("] </i>", "] </i>");
- text = text.Replace(") </i>", ") </i>");
- text = text.Replace(beginTag + beginTag, beginTag);
- text = text.Replace(endTag + endTag, endTag);
- var italicBeginTagCount = Utilities.CountTagInText(text, beginTag);
- var italicEndTagCount = Utilities.CountTagInText(text, endTag);
- var noOfLines = Utilities.GetNumberOfLines(text);
- if (italicBeginTagCount + italicEndTagCount == 0)
- {
- return preTags + text;
- }
- if (italicBeginTagCount == 1 && italicEndTagCount == 1 && text.IndexOf(beginTag, StringComparison.Ordinal) > text.IndexOf(endTag, StringComparison.Ordinal))
- {
- const string pattern = "___________@";
- text = text.Replace(beginTag, pattern);
- text = text.Replace(endTag, beginTag);
- text = text.Replace(pattern, endTag);
- }
- if (italicBeginTagCount == 2 && italicEndTagCount == 0)
- {
- var firstIndex = text.IndexOf(beginTag, StringComparison.Ordinal);
- var lastIndex = text.LastIndexOf(beginTag, StringComparison.Ordinal);
- var lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag, StringComparison.Ordinal) + Environment.NewLine.Length;
- if (noOfLines == 2 && lastIndex == lastIndexWithNewLine && firstIndex < 2)
- {
- text = text.Replace(Environment.NewLine, endTag + Environment.NewLine) + endTag;
- }
- else
- {
- text = text.Remove(lastIndex, beginTag.Length).Insert(lastIndex, endTag);
- }
- }
- if (italicBeginTagCount == 1 && italicEndTagCount == 2)
- {
- var firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
- if (text.StartsWith("</i>-<i>-", StringComparison.Ordinal) ||
- text.StartsWith("</i>- <i>-", StringComparison.Ordinal) ||
- text.StartsWith("</i>- <i> -", StringComparison.Ordinal) ||
- 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();
- if (lines.Count == 2 && lines[0].StartsWith(beginTag, StringComparison.Ordinal) && lines[0].EndsWith(endTag, StringComparison.Ordinal) &&
- lines[1].StartsWith(beginTag, StringComparison.Ordinal))
- {
- text = text.TrimEnd() + endTag;
- }
- else
- {
- var 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;
- }
- }
- if (text.StartsWith(beginTag, StringComparison.Ordinal) && text.EndsWith(endTag, StringComparison.Ordinal) && text.Contains(endTag + Environment.NewLine + beginTag))
- {
- text = text.Replace(endTag + Environment.NewLine + beginTag, Environment.NewLine);
- }
- }
- if (italicBeginTagCount == 1 && italicEndTagCount == 0)
- {
- var lines = text.SplitToLines();
- var sc = StringComparison.Ordinal;
- for (int i = 0; i < lines.Count; i++)
- {
- var line = lines[i];
- var italicIndex = line.LastIndexOf(beginTag, StringComparison.Ordinal);
- // no italic in current 'i' line, try next
- if (italicIndex < 0)
- {
- continue;
- }
- // try earlier insert if possible e.g: <b><i>foobar</b> => <b><i>foobar</i></b>
- lines[i] = IsTextFormattable(line.Substring(italicIndex + 3))
- ? line.Insert(CalculateEarlyInsertIndex(line), endTag)
- : line.Replace(beginTag, string.Empty);
- break; // break as soon as we reach here since italicBeginTagCount == 1
- int CalculateEarlyInsertIndex(string s)
- {
- var len = s.Length;
- var lastClosingTagIndex = s.LastIndexOf("</", len - 1, len - italicIndex - 3, sc);
- while (lastClosingTagIndex > italicIndex + 3)
- {
- var tempClosingIdx = s.LastIndexOf("</", lastClosingTagIndex, lastClosingTagIndex - italicIndex - 3, sc);
- if (tempClosingIdx < 0) break;
- lastClosingTagIndex = tempClosingIdx;
- }
- // try finding the first closing tag index and insert the new closing there
- // to avoid having text with closed tags like <b><i>foo</b></i>
- return lastClosingTagIndex > italicIndex ? lastClosingTagIndex : len;
- }
- }
- // reconstruct the text from lines
- text = string.Join(Environment.NewLine, lines);
- }
- if (italicBeginTagCount == 0 && italicEndTagCount == 1)
- {
- var cleanText = RemoveOpenCloseTags(text, TagItalic, TagBold, TagUnderline, TagCyrillicI);
- var 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)
- {
- var 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;
- }
- if (italicBeginTagCount == 0 && italicEndTagCount == 2)
- {
- var firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
- text = text.Remove(firstIndex, endTag.Length).Insert(firstIndex, beginTag);
- }
- // <i>Foo</i>
- // <i>Bar</i>
- if (italicBeginTagCount == 2 && italicEndTagCount == 2 && noOfLines == 2)
- {
- var 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 + Environment.NewLine.Length).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 + Environment.NewLine.Length).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 + Environment.NewLine.Length).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(':');
- if (colIdx >= 0 && Utilities.CountTagInText(text, beginTag) + Utilities.CountTagInText(text, endTag) == 4 && text.Length > colIdx + 1 && !char.IsDigit(text[colIdx + 1]))
- {
- 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();
- var tempText = text.Remove(0, idx + 1);
- if (!tempText.StartsWith(']') && !tempText.StartsWith(')'))
- {
- text = tempText;
- text = FixInvalidItalicTags(text).Trim();
- if (text.StartsWith("<i> ", StringComparison.OrdinalIgnoreCase))
- {
- text = Utilities.RemoveSpaceBeforeAfterTag(text, beginTag);
- }
- text = pre + " " + text;
- }
- }
- }
- }
- }
- //<i>- You think they're they gone?<i>
- //<i>- That can't be.</i>
- if (italicBeginTagCount == 3 && italicEndTagCount == 1 && noOfLines == 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)))
- {
- text = text.Replace(beginTag, string.Empty);
- text = text.Replace(endTag, string.Empty);
- text = text.Replace(" ", " ").Trim();
- text = beginTag + text + endTag;
- }
- }
- if (noOfLines == 3)
- {
- var lines = text.SplitToLines();
- if ((italicBeginTagCount == 3 && italicEndTagCount == 2) || (italicBeginTagCount == 2 && italicEndTagCount == 3))
- {
- var numberOfItalics = 0;
- foreach (var line in lines)
- {
- if (line.StartsWith(beginTag, StringComparison.Ordinal))
- {
- numberOfItalics++;
- }
- if (line.EndsWith(endTag, StringComparison.Ordinal))
- {
- numberOfItalics++;
- }
- }
- if (numberOfItalics == 5)
- { // fix missing tag
- text = "<i>" + text.Replace("<i>", string.Empty).Replace("</i>", string.Empty) + "</i>";
- }
- }
- }
- text = text.Replace("<i></i>", string.Empty);
- text = text.Replace("</i><i>", string.Empty);
- if (text.IndexOf('@') < 0)
- {
- text = text.Replace("</i> <i>", "@");
- text = text.Replace("<i> </i>", "@");
- text = text.Replace("<i> </i>", "@");
- text = text.Replace("@ ", " ");
- text = text.Replace("@ ", " ");
- text = text.Replace(" @", " ");
- text = text.Replace(" @", " ");
- text = text.Replace("@", " ");
- }
- else
- {
- text = text.Replace("</i> <i>", " ");
- text = text.Replace("<i> </i>", " ");
- text = text.Replace("<i> </i>", " ");
- text = text.Replace(" ", " ");
- text = text.Replace(" ", " ");
- }
- return preTags + text;
- }
Add Comment
Please, Sign In to add comment