FixInvalidItalicTags moved to HmtilUtil

This commit is contained in:
ivandrofly 2015-06-01 20:00:28 +01:00
parent 6ec25ab938
commit 98b9215d89
14 changed files with 271 additions and 269 deletions

View File

@ -366,7 +366,7 @@ namespace Nikse.SubtitleEdit.Core
return s;
if (s.IndexOf("< ", StringComparison.Ordinal) >= 0)
s = Utilities.FixInvalidItalicTags(s);
s = FixInvalidItalicTags(s);
return RemoveOpenCloseTags(s, TagItalic, TagBold, TagUnderline, TagParagraph, TagFont, TagCyrillicI);
}
@ -418,5 +418,253 @@ namespace Nikse.SubtitleEdit.Core
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.CountTagInText(text, Environment.NewLine) + 1;
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)
text = text.Replace(Environment.NewLine, "</i>" + Environment.NewLine) + "</i>";
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();
if (lines.Length == 2 && lines[0].StartsWith("<i>", StringComparison.Ordinal) && lines[0].EndsWith("</i>", StringComparison.Ordinal) &&
lines[1].StartsWith("<i>", StringComparison.Ordinal))
{
text = text.TrimEnd() + "</i>";
}
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;
}
if (text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && text.Contains("</i>" + Environment.NewLine + "<i>"))
{
text = text.Replace("</i>" + Environment.NewLine + "<i>", Environment.NewLine);
}
}
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)
{
var cleanText = HtmlUtil.RemoveOpenCloseTags(text, HtmlUtil.TagItalic, HtmlUtil.TagBold, HtmlUtil.TagUnderline, HtmlUtil.TagCyrillicI);
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.CountTagInText(cleanText, Environment.NewLine) == 1)
{
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;
}
if (italicBeginTagCount == 0 && italicEndTagCount == 2 && text.StartsWith("</i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal))
{
int firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
text = text.Remove(firstIndex, endTag.Length).Insert(firstIndex, "<i>");
}
// <i>Foo</i>
// <i>Bar</i>
if (italicBeginTagCount == 2 && italicEndTagCount == 2 && Utilities.CountTagInText(text, Environment.NewLine) == 1)
{
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(':');
if (colIdx > -1 && Utilities.CountTagInText(text, "<i>") + Utilities.CountTagInText(text, "</i>") == 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();
text = text.Remove(0, idx + 1);
text = FixInvalidItalicTags(text).Trim();
if (text.StartsWith("<i> ", StringComparison.OrdinalIgnoreCase))
text = Utilities.RemoveSpaceBeforeAfterTag(text, "<i>");
text = pre + " " + text;
}
}
}
}
//<i>- You think they're they gone?<i>
//<i>- That can't be.</i>
if ((italicBeginTagCount == 3 && italicEndTagCount == 1) && Utilities.CountTagInText(text, Environment.NewLine) == 1)
{
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("<i>", string.Empty);
text = text.Replace("</i>", string.Empty);
text = text.Replace(" ", " ").Trim();
text = "<i>" + text + "</i>";
}
}
text = text.Replace("<i></i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
}
return text;
}
}
}

View File

@ -1944,7 +1944,7 @@ $DROP=[DROPVALUE]" + Environment.NewLine + Environment.NewLine +
text = text.Replace("<I>", "<i>");
text = text.Replace("</I>", "</i>");
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
text = text.Replace("<B>", "<b>");
text = text.Replace("</B>", "</b>");

View File

@ -1066,7 +1066,7 @@ namespace Nikse.SubtitleEdit.Forms
text = text.Replace(beginTag.ToUpper(), beginTag).Replace(endTag.ToUpper(), endTag);
string oldText = text;
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
if (text != oldText)
{
Subtitle.Paragraphs[i].Text = text;

View File

@ -7701,7 +7701,7 @@ namespace Nikse.SubtitleEdit.Forms
Paragraph currentParagraph = _subtitle.Paragraphs[firstIndex];
string text = sb.ToString();
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
text = ChangeAllLinesItalictoSingleItalic(text);
text = Utilities.AutoBreakLine(text, Utilities.AutoDetectGoogleLanguage(_subtitle));
currentParagraph.Text = text;

View File

@ -166,7 +166,7 @@ https://github.com/SubtitleEdit/subtitleedit
private static void MostUsedWordsAdd(Dictionary<string, string> hashtable, string lastLine)
{
if (lastLine.Contains("< "))
lastLine = Utilities.FixInvalidItalicTags(lastLine);
lastLine = HtmlUtil.FixInvalidItalicTags(lastLine);
lastLine = lastLine.Trim('\'');
lastLine = lastLine.Replace("\"", "");
lastLine = lastLine.Replace("<i>", "");

View File

@ -6466,7 +6466,7 @@ namespace Nikse.SubtitleEdit.Forms
s = "<i>" + HtmlUtil.RemoveOpenCloseTags(s, HtmlUtil.TagItalic) + "</i>";
s = s.Replace("</i>" + Environment.NewLine + "<i>", Environment.NewLine);
return Utilities.FixInvalidItalicTags(s);
return HtmlUtil.FixInvalidItalicTags(s);
}
private void LogUnknownWords()

View File

@ -107,7 +107,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
text = text.Remove(index, "@Italic@".Length).Insert(index, italicTag);
italicOn = !italicOn;
}
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
}
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
subtitle.Paragraphs.Add(p);

View File

@ -102,7 +102,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
text = text.Remove(index, "@Italic@".Length).Insert(index, italicTag);
italicOn = !italicOn;
}
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
}
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
subtitle.Paragraphs.Add(p);

View File

@ -322,7 +322,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
s = "{\\an5}" + s.Remove(0, 20).Trim();
if (s.StartsWith("/STYLE VERTICAL(-10)" + Environment.NewLine))
s = "{\\an5}" + s.Remove(0, 20).Trim();
s = Utilities.FixInvalidItalicTags(s);
s = HtmlUtil.FixInvalidItalicTags(s);
return s;
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Core;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
@ -177,7 +178,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
res = res.Replace("♪♪", "♪");
res = res.Replace("'''", "'");
res = res.Replace(" ", " ").Replace(" ", " ").Replace(Environment.NewLine + " ", Environment.NewLine).Trim();
return Utilities.FixInvalidItalicTags(res);
return HtmlUtil.FixInvalidItalicTags(res);
}
private static List<string> ExecuteReplacesAndGetParts(string[] parts)

View File

@ -1767,7 +1767,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
res += "</i>";
//res = res.Replace("aã", "ã");
//res = res.Replace("oõ", "õ");
return Utilities.FixInvalidItalicTags(res);
return HtmlUtil.FixInvalidItalicTags(res);
}
private static string GetLetter(string hexCode)

View File

@ -98,7 +98,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
text = text.Remove(index, "@Italic@".Length).Insert(index, italicTag);
italicOn = !italicOn;
}
text = Utilities.FixInvalidItalicTags(text);
text = HtmlUtil.FixInvalidItalicTags(text);
}
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
subtitle.Paragraphs.Add(p);

View File

@ -2127,254 +2127,6 @@ namespace Nikse.SubtitleEdit.Logic
return count;
}
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 = CountTagInText(text, beginTag);
int italicEndTagCount = CountTagInText(text, endTag);
int noOfLines = CountTagInText(text, Environment.NewLine) + 1;
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)
text = text.Replace(Environment.NewLine, "</i>" + Environment.NewLine) + "</i>";
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();
if (lines.Length == 2 && lines[0].StartsWith("<i>", StringComparison.Ordinal) && lines[0].EndsWith("</i>", StringComparison.Ordinal) &&
lines[1].StartsWith("<i>", StringComparison.Ordinal))
{
text = text.TrimEnd() + "</i>";
}
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;
}
if (text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && text.Contains("</i>" + Environment.NewLine + "<i>"))
{
text = text.Replace("</i>" + Environment.NewLine + "<i>", Environment.NewLine);
}
}
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)
{
var cleanText = HtmlUtil.RemoveOpenCloseTags(text, HtmlUtil.TagItalic, HtmlUtil.TagBold, HtmlUtil.TagUnderline, HtmlUtil.TagCyrillicI);
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 && CountTagInText(cleanText, Environment.NewLine) == 1)
{
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;
}
if (italicBeginTagCount == 0 && italicEndTagCount == 2 && text.StartsWith("</i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal))
{
int firstIndex = text.IndexOf(endTag, StringComparison.Ordinal);
text = text.Remove(firstIndex, endTag.Length).Insert(firstIndex, "<i>");
}
// <i>Foo</i>
// <i>Bar</i>
if (italicBeginTagCount == 2 && italicEndTagCount == 2 && CountTagInText(text, Environment.NewLine) == 1)
{
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 (StartsAndEndsWithTag(firstLine, beginTag, endTag) && 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 > -1 && Utilities.CountTagInText(text, "<i>") + Utilities.CountTagInText(text, "</i>") == 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 || !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))
text = RemoveSpaceBeforeAfterTag(text, "<i>");
text = pre + " " + text;
}
}
}
}
//<i>- You think they're they gone?<i>
//<i>- That can't be.</i>
if ((italicBeginTagCount == 3 && italicEndTagCount == 1) && CountTagInText(text, Environment.NewLine) == 1)
{
var newLineIdx = text.IndexOf(Environment.NewLine, StringComparison.Ordinal);
var firstLine = text.Substring(0, newLineIdx).Trim();
var secondLine = text.Substring(newLineIdx).Trim();
if (StartsAndEndsWithTag(firstLine, beginTag, beginTag) && StartsAndEndsWithTag(secondLine, beginTag, endTag) ||
StartsAndEndsWithTag(secondLine, beginTag, beginTag) && StartsAndEndsWithTag(firstLine, beginTag, endTag))
{
text = text.Replace("<i>", string.Empty);
text = text.Replace("</i>", string.Empty);
text = text.Replace(" ", " ").Trim();
text = "<i>" + text + "</i>";
}
}
text = text.Replace("<i></i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
}
return text;
}
public static bool StartsAndEndsWithTag(string text, string startTag, string endTag)
{
if (string.IsNullOrWhiteSpace(text))
@ -3157,7 +2909,7 @@ namespace Nikse.SubtitleEdit.Logic
return text;
}
private static string RemoveSpaceBeforeAfterTag(string text, string openTag)
public static string RemoveSpaceBeforeAfterTag(string text, string openTag)
{
text = HtmlUtil.FixUpperTags(text);
var closeTag = string.Empty;

View File

@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.Forms;
using Nikse.SubtitleEdit.Core;
namespace Test.Logic
{
@ -90,7 +91,7 @@ namespace Test.Logic
public void FixInvalidItalicTags2()
{
const string s1 = "Gledaj prema kameri i rici <i>zdravo!";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "Gledaj prema kameri i rici zdravo!");
}
@ -99,7 +100,7 @@ namespace Test.Logic
public void FixInvalidItalicTags3()
{
string s1 = "<i>Line 1.</i>" + Environment.NewLine + "<i>Line 2.";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "<i>Line 1." + Environment.NewLine + "Line 2.</i>");
}
@ -108,7 +109,7 @@ namespace Test.Logic
public void FixInvalidItalicTags4()
{
string s1 = "It <i>is</i> a telegram," + Environment.NewLine + "it <i>is</i> ordering an advance,";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, s1);
}
@ -117,7 +118,7 @@ namespace Test.Logic
public void FixInvalidItalicTags5()
{
string s1 = "- <i>It is a telegram?</i>" + Environment.NewLine + "<i>- It is.</i>";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "<i>- It is a telegram?" + Environment.NewLine + "- It is.</i>");
}
@ -126,7 +127,7 @@ namespace Test.Logic
public void FixInvalidItalicTags6()
{
string s1 = "- <i>Text1!</i>" + Environment.NewLine + "- <i>Text2.</i>";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "<i>- Text1!" + Environment.NewLine + "- Text2.</i>");
}
@ -135,7 +136,7 @@ namespace Test.Logic
public void FixInvalidItalicTags7()
{
string s1 = "<i>- You think they're they gone?<i>" + Environment.NewLine + "<i>- That can't be.</i>";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "<i>- You think they're they gone?" + Environment.NewLine + "- That can't be.</i>");
}
@ -144,7 +145,7 @@ namespace Test.Logic
public void FixInvalidItalicTags8()
{
string s1 = "<i>- You think they're they gone?</i>" + Environment.NewLine + "<i>- That can't be.<i>";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "<i>- You think they're they gone?" + Environment.NewLine + "- That can't be.</i>");
}
@ -153,7 +154,7 @@ namespace Test.Logic
public void FixInvalidItalicTags9()
{
const string s1 = "FALCONE:<i> I didn't think</i>\r\n<i>it was going to be you,</i>";
string s2 = Utilities.FixInvalidItalicTags(s1);
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "FALCONE: <i>I didn't think\r\nit was going to be you,</i>");
}