Work on WebVTT

This commit is contained in:
niksedk 2023-06-25 19:09:12 +02:00
parent 6206d0dc1e
commit b18ccc8273
4 changed files with 80 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System.Globalization;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Nikse.SubtitleEdit.Core.Common namespace Nikse.SubtitleEdit.Core.Common
{ {
@ -454,5 +455,58 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
public static List<string> GetParagraphStyles(Paragraph paragraph)
{
var list = new List<string>();
if (paragraph == null || string.IsNullOrEmpty(paragraph.Text))
{
return list;
}
var regex = new Regex(@"<c\.[\.a-zA-Z\d#_-]+>");
foreach (Match match in regex.Matches(paragraph.Text))
{
var styles = match.Value.Remove(0, 3).Trim('>', ' ').Split('.');
foreach (var styleName in styles)
{
if (!string.IsNullOrEmpty(styleName) && !list.Contains(styleName))
{
list.Add("." + styleName);
}
}
}
return list;
}
public static string SetParagraphStyles(Paragraph p, List<WebVttStyle> styles)
{
if (string.IsNullOrEmpty(p.Text) ||
!p.Text.Contains("<c.", StringComparison.Ordinal))
{
return p.Text;
}
var text = p.Text;
var regex = new Regex(@"<c\.[\.a-zA-Z\d#_-]+>");
var match = regex.Match(text);
while (match.Success)
{
text = text.Remove(match.Index, match.Value.Length);
match = regex.Match(text);
}
text = text.Replace("</c>", string.Empty);
if (styles.Count == 0)
{
return text;
}
var prefix = "<c" + string.Join("", styles.Select(s=>s.Name)) +">";
return prefix + text + "</c>";
}
} }
} }

View File

@ -33003,6 +33003,11 @@ namespace Nikse.SubtitleEdit.Forms
toolStripButtonAssAttachments.Visible = true; toolStripButtonAssAttachments.Visible = true;
} }
if (formatType == typeof(AdvancedSubStationAlpha) || formatType == typeof(SubStationAlpha))
{
TryLoadIcon(toolStripButtonAssStyleManager, "AssaStyle");
}
toolStripButtonXProperties.Visible = formatType == typeof(ItunesTimedText); toolStripButtonXProperties.Visible = formatType == typeof(ItunesTimedText);
if (toolStripButtonXProperties.Visible) if (toolStripButtonXProperties.Visible)
{ {
@ -35223,8 +35228,20 @@ namespace Nikse.SubtitleEdit.Forms
var styles = WebVttHelper.GetStyles(_subtitle.Header); var styles = WebVttHelper.GetStyles(_subtitle.Header);
using (var form = new WebVttStylePicker(styles, _subtitle.GetParagraphOrDefault(idx))) using (var form = new WebVttStylePicker(styles, _subtitle.GetParagraphOrDefault(idx)))
{ {
form.ShowDialog(this); if (form.ShowDialog(this) != DialogResult.OK)
} {
return;
}
foreach (int index in SubtitleListview1.SelectedIndices)
{
var p = _subtitle.Paragraphs[index];
p.Text = WebVttHelper.SetParagraphStyles(p, form.ImportExportStyles);
SubtitleListview1.SetText(index, p.Text);
}
}
RefreshSelectedParagraph();
} }
} }
} }

View File

@ -8,20 +8,20 @@ namespace Nikse.SubtitleEdit.Forms.VTT
{ {
public sealed partial class WebVttStylePicker : Form public sealed partial class WebVttStylePicker : Form
{ {
private readonly List<WebVttStyle> _styles;
public List<WebVttStyle> ImportExportStyles { get; set; } public List<WebVttStyle> ImportExportStyles { get; set; }
public WebVttStylePicker(List<WebVttStyle> styles, Paragraph getParagraphOrDefault) public WebVttStylePicker(List<WebVttStyle> styles, Paragraph paragraph)
{ {
InitializeComponent(); InitializeComponent();
UiUtil.FixFonts(this); UiUtil.FixFonts(this);
ImportExportStyles = new List<WebVttStyle>(); ImportExportStyles = new List<WebVttStyle>();
_styles = styles;
listViewExportStyles.Columns[0].Width = listViewExportStyles.Width - 20; listViewExportStyles.Columns[0].Width = listViewExportStyles.Width - 20;
foreach (var style in _styles)
var paragraphStyles = WebVttHelper.GetParagraphStyles(paragraph);
foreach (var style in styles)
{ {
listViewExportStyles.Items.Add(new ListViewItem(style.Name) { Checked = true, Tag = style }); listViewExportStyles.Items.Add(new ListViewItem(style.Name) { Checked = paragraphStyles.Contains(style.Name), Tag = style });
} }
Text = LanguageSettings.Current.SubStationAlphaStyles.Export; Text = LanguageSettings.Current.SubStationAlphaStyles.Export;
@ -41,6 +41,8 @@ namespace Nikse.SubtitleEdit.Forms.VTT
ImportExportStyles.Add((WebVttStyle)item.Tag); ImportExportStyles.Add((WebVttStyle)item.Tag);
} }
} }
DialogResult = ImportExportStyles.Count == 0 ? DialogResult.Cancel : DialogResult.OK;
} }
private void WebVttImportExport_KeyDown(object sender, KeyEventArgs e) private void WebVttImportExport_KeyDown(object sender, KeyEventArgs e)

View File

@ -99,10 +99,7 @@ namespace Nikse.SubtitleEdit.Logic
{ {
_regEx = new Regex(FindText, RegexOptions.None, TimeSpan.FromSeconds(5)); _regEx = new Regex(FindText, RegexOptions.None, TimeSpan.FromSeconds(5));
} }
else
{
}
match = _regEx.Match(text, startIndex); match = _regEx.Match(text, startIndex);
} }
catch (RegexMatchTimeoutException exception) catch (RegexMatchTimeoutException exception)