"Merge selected lines" can now merge more than two lines (will not merge text where result is longer than 200 chars though)

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@230 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2011-01-08 18:46:15 +00:00
parent e7d0ab769f
commit 3f18536052
5 changed files with 215 additions and 117 deletions

View File

@ -574,19 +574,7 @@ namespace Nikse.SubtitleEdit.Forms
if (noOfShortDisplayTimes > 0)
LogStatus(fixAction, string.Format(_language.XDisplayTimesProlonged, noOfShortDisplayTimes));
}
public static int CountTagInText(string text, string tag)
{
int count = 0;
int index = text.IndexOf(tag);
while (index >= 0)
{
count++;
index = text.IndexOf(tag, index + 1);
}
return count;
}
public void FixInvalidItalicTags()
{
const string beginTag = "<i>";
@ -598,100 +586,18 @@ namespace Nikse.SubtitleEdit.Forms
string text = _subtitle.Paragraphs[i].Text.Replace(beginTag.ToUpper(), beginTag).Replace(endTag.ToUpper(), endTag);
string oldText = text;
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);
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)
text = Utilities.FixInvalidItalicTags(text);
if (text != oldText)
{
if (italicBeginTagCount == 1 && italicEndTagCount == 1)
if (AllowFix(i + 1, fixAction))
{
if (text.IndexOf(beginTag) > text.IndexOf(endTag))
{
text = text.Replace(beginTag, "___________@");
text = text.Replace(endTag, beginTag);
text = text.Replace("___________@", endTag);
}
}
if (italicBeginTagCount == 2 && italicEndTagCount == 0)
{
int firstIndex = text.IndexOf(beginTag);
int lastIndex = text.LastIndexOf(beginTag);
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine+ beginTag) + 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);
text = text.Substring(0, firstIndex - 1) + text.Substring(firstIndex + endTag.Length);
}
if (italicBeginTagCount == 2 && italicEndTagCount == 1)
{
int lastIndex = text.LastIndexOf(beginTag);
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 (italicBeginTagCount == 1 && italicEndTagCount == 0)
{
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + Environment.NewLine.Length;
int lastIndex = text.LastIndexOf(beginTag);
if (text.StartsWith(beginTag))
text += endTag;
else if (noOfLines == 2 && lastIndex == lastIndexWithNewLine)
text += endTag;
else
text = text.Replace(beginTag, string.Empty);
}
if (italicBeginTagCount == 0 && italicEndTagCount == 1)
{
text = text.Replace(endTag, string.Empty);
}
text = text.Replace("<i></i>", string.Empty);
if (text != oldText)
{
if (AllowFix(i + 1, fixAction))
{
_subtitle.Paragraphs[i].Text = text;
_totalFixes++;
noOfInvalidHtmlTags++;
AddFixToListView(_subtitle.Paragraphs[i], i + 1, fixAction, oldText, text);
}
_subtitle.Paragraphs[i].Text = text;
_totalFixes++;
noOfInvalidHtmlTags++;
AddFixToListView(_subtitle.Paragraphs[i], i + 1, fixAction, oldText, text);
}
}
}
if (noOfInvalidHtmlTags > 0)
LogStatus(_language.FixInvalidItalicTags, string.Format(_language.XInvalidHtmlTagsFixed, noOfInvalidHtmlTags));
@ -1210,8 +1116,8 @@ namespace Nikse.SubtitleEdit.Forms
int noOfFixes = 0;
for (int i = 0; i < _subtitle.Paragraphs.Count; i++)
{
Paragraph p = _subtitle.Paragraphs[i];
if (CountTagInText(p.Text, "\"") == 1)
Paragraph p = _subtitle.Paragraphs[i];
if (Utilities.CountTagInText(p.Text, "\"") == 1)
{
string oldText = p.Text;
if (p.Text.StartsWith("\""))
@ -1956,7 +1862,7 @@ namespace Nikse.SubtitleEdit.Forms
if (prev == null || !Utilities.RemoveHtmlTags(prev.Text).Trim().EndsWith("-"))
{
if (CountTagInText(text, "-") == 1)
if (Utilities.CountTagInText(text, "-") == 1)
{
string oldText = p.Text;
@ -1992,7 +1898,7 @@ namespace Nikse.SubtitleEdit.Forms
Paragraph p = _subtitle.Paragraphs[i];
string text = p.Text;
if (CountTagInText(text, Environment.NewLine) > 1)
if (Utilities.CountTagInText(text, Environment.NewLine) > 1)
{
string oldText = p.Text;
text = Utilities.AutoBreakLine(text);

View File

@ -1558,7 +1558,7 @@
this.fixCommonErrorsInSelectedLinesToolStripMenuItem,
this.changeCasingForSelectedLinesToolStripMenuItem});
this.contextMenuStripListview.Name = "contextMenuStripListview";
this.contextMenuStripListview.Size = new System.Drawing.Size(276, 562);
this.contextMenuStripListview.Size = new System.Drawing.Size(276, 584);
this.contextMenuStripListview.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStripListviewOpening);
//
// toolStripMenuItemDelete

View File

@ -2952,10 +2952,8 @@ namespace Nikse.SubtitleEdit.Forms
toolStripMenuItemInsertAfter.Visible = false;
toolStripMenuItemInsertSubtitle.Visible = false;
splitLineToolStripMenuItem.Visible = false;
toolStripMenuItemMergeLines.Visible = false;
mergeAfterToolStripMenuItem.Visible = false;
mergeBeforeToolStripMenuItem.Visible = false;
toolStripMenuItemMergeLines.Visible = false;
typeEffectToolStripMenuItem.Visible = false;
toolStripSeparator7.Visible = false;
}
@ -3575,10 +3573,95 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private void MergeSelectedLines()
{
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 1)
{
StringBuilder sb = new StringBuilder();
List<Paragraph> mergeParagraphs = new List<Paragraph>();
List<int> deleteIndices = new List<int>();
bool first = true;
int firstIndex = 0;
foreach (int index in SubtitleListview1.SelectedIndices)
{
if (first)
firstIndex = index;
else
deleteIndices.Add(index);
sb.AppendLine(_subtitle.Paragraphs[index].Text);
first = false;
}
if (sb.Length > 200)
return;
SubtitleListview1.SelectedIndexChanged -= SubtitleListview1_SelectedIndexChanged;
MakeHistoryForUndo(_language.BeforeMergeLines);
Paragraph currentParagraph = _subtitle.Paragraphs[firstIndex];
string text = sb.ToString();
text = Utilities.FixInvalidItalicTags(text);
text = ChangeAllLinesItalictoSingleItalic(text);
text = Utilities.AutoBreakLine(text);
currentParagraph.Text = text;
//display time
currentParagraph.EndTime.TotalMilliseconds = currentParagraph.StartTime.TotalMilliseconds + Utilities.GetDisplayMillisecondsFromText(text);
Paragraph nextParagraph = _subtitle.GetParagraphOrDefault(_subtitle.GetIndex(currentParagraph) + 1);
if (nextParagraph != null && currentParagraph.EndTime.TotalMilliseconds > nextParagraph.StartTime.TotalMilliseconds && currentParagraph.StartTime.TotalMilliseconds < nextParagraph.StartTime.TotalMilliseconds)
{
currentParagraph.EndTime.TotalMilliseconds = nextParagraph.StartTime.TotalMilliseconds - 1;
}
if (_networkSession != null)
{
_networkSession.TimerStop();
_networkSession.UpdateLine(firstIndex, currentParagraph);
NetworkGetSendUpdates(deleteIndices, 0, null);
}
else
{
for (int i = deleteIndices.Count - 1; i >= 0; i--)
_subtitle.Paragraphs.RemoveAt(deleteIndices[i]);
_subtitle.Renumber(1);
SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
}
ShowSource();
ShowStatus(_language.LinesMerged);
SubtitleListview1.SelectIndexAndEnsureVisible(firstIndex);
SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged;
RefreshSelectedParagraph();
_change = true;
}
}
private static string ChangeAllLinesItalictoSingleItalic(string text)
{
bool allLinesStartAndEndsWithItalic = true;
foreach (string line in text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
if (!line.Trim().StartsWith("<i>") || !line.Trim().EndsWith("</i>"))
allLinesStartAndEndsWithItalic = false;
}
if (allLinesStartAndEndsWithItalic)
{
text = text.Replace("<i>", string.Empty).Replace("</i>", string.Empty).Trim();
text = "<i>" + text + "</i>";
}
return text;
}
private void MergeAfterToolStripMenuItemClick(object sender, EventArgs e)
{
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 0)
{
if (SubtitleListview1.SelectedItems.Count > 2)
{
MergeSelectedLines();
return;
}
int startNumber = _subtitle.Paragraphs[0].Number;
int firstSelectedIndex = SubtitleListview1.SelectedItems[0].Index;
@ -3592,8 +3675,9 @@ namespace Nikse.SubtitleEdit.Forms
currentParagraph.Text = currentParagraph.Text.Replace(Environment.NewLine, " ");
currentParagraph.Text += Environment.NewLine + nextParagraph.Text.Replace(Environment.NewLine, " ");
currentParagraph.Text = ChangeAllLinesItalictoSingleItalic(currentParagraph.Text);
currentParagraph.Text = Utilities.AutoBreakLine(currentParagraph.Text);
//currentParagraph.EndTime.TotalMilliseconds = currentParagraph.EndTime.TotalMilliseconds + nextParagraph.Duration.TotalMilliseconds; //nextParagraph.EndTime;
currentParagraph.EndTime.TotalMilliseconds = nextParagraph.EndTime.TotalMilliseconds;
@ -4605,7 +4689,7 @@ namespace Nikse.SubtitleEdit.Forms
private void ToolStripMenuItemMergeLinesClick(object sender, EventArgs e)
{
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1 && SubtitleListview1.SelectedItems.Count <= 2)
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1)
MergeAfterToolStripMenuItemClick(null, null);
}
@ -4750,10 +4834,13 @@ namespace Nikse.SubtitleEdit.Forms
}
else if (e.Modifiers == (Keys.Control | Keys.Shift) && e.KeyCode == Keys.M)
{
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1 && SubtitleListview1.SelectedItems.Count <= 2)
if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count >= 1)
{
e.SuppressKeyPress = true;
MergeAfterToolStripMenuItemClick(null, null);
if (SubtitleListview1.SelectedItems.Count == 2)
MergeAfterToolStripMenuItemClick(null, null);
else
MergeSelectedLines();
}
}
else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.U)

View File

@ -127,17 +127,17 @@ Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
text = text.Replace(@"{\i1}", "<i>");
text = text.Replace(@"{\i0}", "</i>");
if (FixCommonErrors.CountTagInText(text, "<i>") > FixCommonErrors.CountTagInText(text, "</i>"))
if (Utilities.CountTagInText(text, "<i>") > Utilities.CountTagInText(text, "</i>"))
text += "</i>";
text = text.Replace(@"{\u1}", "<u>");
text = text.Replace(@"{\u0}", "</u>");
if (FixCommonErrors.CountTagInText(text, "<u>") > FixCommonErrors.CountTagInText(text, "</u>"))
if (Utilities.CountTagInText(text, "<u>") > Utilities.CountTagInText(text, "</u>"))
text += "</u>";
text = text.Replace(@"{\b1}", "<b>");
text = text.Replace(@"{\b0}", "</b>");
if (FixCommonErrors.CountTagInText(text, "<b>") > FixCommonErrors.CountTagInText(text, "</b>"))
if (Utilities.CountTagInText(text, "<b>") > Utilities.CountTagInText(text, "</b>"))
text += "</b>";
for (int i = 0; i < 5; i++) // just look five times...

View File

@ -1538,6 +1538,111 @@ namespace Nikse.SubtitleEdit.Logic
else if (control is Button)
control.Height = newHeight;
}
internal static int CountTagInText(string text, string tag)
{
int count = 0;
int index = text.IndexOf(tag);
while (index >= 0)
{
count++;
index = text.IndexOf(tag, index + 1);
}
return count;
}
internal 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>", 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);
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)
{
if (text.IndexOf(beginTag) > text.IndexOf(endTag))
{
text = text.Replace(beginTag, "___________@");
text = text.Replace(endTag, beginTag);
text = text.Replace("___________@", endTag);
}
}
if (italicBeginTagCount == 2 && italicEndTagCount == 0)
{
int firstIndex = text.IndexOf(beginTag);
int lastIndex = text.LastIndexOf(beginTag);
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + 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);
text = text.Substring(0, firstIndex - 1) + text.Substring(firstIndex + endTag.Length);
}
if (italicBeginTagCount == 2 && italicEndTagCount == 1)
{
int lastIndex = text.LastIndexOf(beginTag);
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 (italicBeginTagCount == 1 && italicEndTagCount == 0)
{
int lastIndexWithNewLine = text.LastIndexOf(Environment.NewLine + beginTag) + Environment.NewLine.Length;
int lastIndex = text.LastIndexOf(beginTag);
if (text.StartsWith(beginTag))
text += endTag;
else if (noOfLines == 2 && lastIndex == lastIndexWithNewLine)
text += endTag;
else
text = text.Replace(beginTag, string.Empty);
}
if (italicBeginTagCount == 0 && italicEndTagCount == 1)
{
text = text.Replace(endTag, string.Empty);
}
text = text.Replace("<i></i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
}
return text;
}
}
}