SubtitleEdit/libse/NetflixQualityCheck/NetflixCheckWhiteSpace.cs

51 lines
2.0 KiB
C#
Raw Normal View History

using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck
{
public class NetflixCheckWhiteSpace : INetflixQualityChecker
{
private static readonly Regex LineEndingSpaceBefore = new Regex(@"^( |\n|\r\n)[^\s]", RegexOptions.Compiled);
private static readonly Regex LineEndingSpaceAfter = new Regex(@"[^\s]( |\n|\r\n)$", RegexOptions.Compiled);
private static readonly Regex SpacesBeforePunctuation = new Regex(@"[^\s]( |\n|\r\n)[!?).,]", RegexOptions.Compiled);
private static readonly Regex TwoPlusConsequentSpaces = new Regex(@"( |\n|\r\n){2,}", RegexOptions.Compiled);
private static void AddWhiteSpaceWarning(Paragraph p, NetflixQualityController report, int pos)
2017-02-22 14:39:08 +01:00
{
string timecode = p.StartTime.ToHHMMSSFF();
string context = NetflixQualityController.StringContext(p.Text, pos, 6);
2017-02-22 14:39:08 +01:00
string comment = string.Format(Configuration.Settings.Language.NetflixQualityCheck.WhiteSpaceCheckReport, pos);
report.AddRecord(p, timecode, context, comment);
2017-02-22 14:39:08 +01:00
}
public void Check(Subtitle subtitle, NetflixQualityController controller)
{
foreach (Paragraph p in subtitle.Paragraphs)
{
2017-02-22 14:39:08 +01:00
// Line endings
if (LineEndingSpaceBefore.IsMatch(p.Text))
2017-02-22 14:39:08 +01:00
{
AddWhiteSpaceWarning(p, controller, 1);
2017-02-22 14:39:08 +01:00
}
if (LineEndingSpaceAfter.IsMatch(p.Text))
{
AddWhiteSpaceWarning(p, controller, p.Text.Length);
2017-02-22 14:39:08 +01:00
}
2017-02-22 14:39:08 +01:00
// Spaces before punctuation
foreach (Match m in SpacesBeforePunctuation.Matches(p.Text))
2017-02-22 14:39:08 +01:00
{
AddWhiteSpaceWarning(p, controller, m.Index + 1);
2017-02-22 14:39:08 +01:00
}
// 2+ consequent spaces
foreach (Match m in TwoPlusConsequentSpaces.Matches(p.Text))
2017-02-22 14:39:08 +01:00
{
AddWhiteSpaceWarning(p, controller, m.Index);
}
}
}
}
}