Skip to content

Commit 4d13eaa

Browse files
committed
Update HIColorer plugin for improved functionality and clarity
Refactored code to enhance subtitles handling, configuration management, and color application for hearing-impaired annotations. Introduced new helper methods, centralized configuration logic, updated dependencies, and streamlined regex and utility usage for improved maintainability. Bumped version to 2.0 to reflect changes.
1 parent 240a1af commit 4d13eaa

File tree

9 files changed

+138
-80
lines changed

9 files changed

+138
-80
lines changed

source/HIColorer/Artists/Artist.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Drawing;
3+
using Nikse.SubtitleEdit.Core.Common;
34

45
namespace Nikse.SubtitleEdit.PluginLogic
56
{
@@ -11,7 +12,7 @@ public abstract class Artist
1112

1213
protected bool ContainsColor(string text) => text.Contains("<font", StringComparison.OrdinalIgnoreCase);
1314

14-
protected static string ApplyColor(Color color, string text) => string.Format(WriteFormat, HtmlUtils.ColorToHtml(color), text.Trim());
15+
protected static string ApplyColor(Color color, string text) => string.Format(WriteFormat, Utilities.ColorToHex(color), text.Trim());
1516

1617
}
1718
}

source/HIColorer/Artists/MoodsArtist.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Nikse.SubtitleEdit.PluginLogic
1+
using Nikse.SubtitleEdit.Core.Common;
2+
3+
namespace Nikse.SubtitleEdit.PluginLogic
24
{
35
public class MoodsArtist : Artist
46
{

source/HIColorer/Artists/MusicArtist.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Nikse.SubtitleEdit.Core.Common;
23

34
namespace Nikse.SubtitleEdit.PluginLogic
45
{

source/HIColorer/Artists/NarratorArtist.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
using System.Linq;
55
using System.Text;
66
using System.Text.RegularExpressions;
7+
using Nikse.SubtitleEdit.Core.Common;
78

89
namespace Nikse.SubtitleEdit.PluginLogic
910
{
1011
public class NarratorArtist : Artist
1112
{
12-
private static readonly Regex _regexColonNonWord = new Regex(":\\B", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
13+
private static readonly Regex RegexColonNonWord = new(":\\B", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
1314

1415
/// <summary>
1516
/// This regex pattern is written to not capture characters inside html tags
1617
/// </summary>
17-
private static readonly Regex _regexFirtCharNotTag = new Regex("(?<!<)\\w", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
18+
private static readonly Regex RegexFirtCharNotTag = new("(?<!<)\\w", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
1819

1920
/// <summary>
2021
/// Narrator ignore words
@@ -35,10 +36,11 @@ public override void Paint(Subtitle subtitle)
3536
{
3637
var p = paragraphs[i];
3738
string text = p.Text;
38-
if (_regexColonNonWord.IsMatch(text))
39+
if (RegexColonNonWord.IsMatch(text))
3940
{
4041
text = ProcessText(text);
4142
}
43+
4244
if (text.Length != p.Text.Length)
4345
{
4446
p.Text = text;
@@ -49,13 +51,13 @@ public override void Paint(Subtitle subtitle)
4951
private string ProcessText(string text)
5052
{
5153
char[] trimChars = { '"', '\\' };
52-
string[] lines = text.SplitToLines();
53-
for (int i = 0; i < lines.Length; i++)
54+
var lines = text.SplitToLines();
55+
for (int i = 0; i < lines.Count; i++)
5456
{
5557
string line = lines[i];
5658

5759
//TODO: if text contains 2 hearing text
58-
string lineNoTags = HtmlUtils.RemoveTags(line, true).TrimEnd(trimChars).TrimEnd();
60+
string lineNoTags = HtmlUtil.RemoveHtmlTags(line, true).TrimEnd(trimChars).TrimEnd();
5961

6062
if (!ShouldApplyColor(lineNoTags, isFirstLine: i == 0))
6163
{
@@ -68,7 +70,7 @@ private string ProcessText(string text)
6870

6971
string preText = line.Substring(0, colonIdx);
7072

71-
string firstChr = _regexFirtCharNotTag.Match(preText).Value;
73+
string firstChr = RegexFirtCharNotTag.Match(preText).Value;
7274
if (string.IsNullOrEmpty(firstChr))
7375
{
7476
continue;
@@ -87,8 +89,8 @@ private string ProcessText(string text)
8789
line = line.Insert(0, preText);
8890

8991
lines[i] = line;
90-
9192
}
93+
9294
// re-construct text
9395
return string.Join(Environment.NewLine, lines);
9496
}
@@ -105,7 +107,7 @@ private bool ShouldApplyColor(string textNoTags, bool isFirstLine)
105107

106108
// (foobar) foobar: hello world!
107109
string pre = textNoTags.Substring(0, colonIdx).TrimStart();
108-
if (pre.ContainsAny(new[] { ')', ']' }))
110+
if (pre.Contains(new[] { ')', ']' }))
109111
{
110112
return false;
111113
}
@@ -137,4 +139,4 @@ private bool ShouldApplyColor(string textNoTags, bool isFirstLine)
137139
return true;
138140
}
139141
}
140-
}
142+
}

source/HIColorer/ColorConfig.cs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
using System.Drawing;
1+
using System;
2+
using System.Drawing;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Xml.Linq;
6+
using Nikse.SubtitleEdit.Core.Common;
27

38
namespace Nikse.SubtitleEdit.PluginLogic
49
{
5-
public class ColorConfig : Configuration<ColorConfig>
10+
public class ColorConfig // : Configuration<ColorConfig>
611
{
712
/// <summary>
813
/// Narrators color.
@@ -13,17 +18,60 @@ public class ColorConfig : Configuration<ColorConfig>
1318
/// Moods color.
1419
/// </summary>
1520
public int Moods { get; set; }
16-
21+
1722
/// <summary>
1823
/// Music paragraph color
1924
/// </summary>
2025
public int Music { get; set; }
2126

22-
public ColorConfig(string configFile) : base(configFile)
27+
private ColorConfig()
28+
{
29+
}
30+
31+
public static ColorConfig LoadOrCreateConfigurations()
32+
{
33+
// old config file
34+
try
35+
{
36+
File.Delete("hicolor.xml");
37+
}
38+
catch
39+
{
40+
}
41+
42+
var configFile = GetConfigFile();
43+
if (File.Exists(configFile))
44+
{
45+
var xdoc = XDocument.Load(configFile);
46+
var colorConfig = new ColorConfig()
47+
{
48+
Narrator = Convert.ToInt32(xdoc.Root.Element("Narrator").Value),
49+
Moods = Convert.ToInt32(xdoc.Root.Element("Moods").Value),
50+
Music = Convert.ToInt32(xdoc.Root.Element("Music").Value)
51+
};
52+
return colorConfig;
53+
}
54+
55+
return new ColorConfig();
56+
}
57+
58+
public void Save()
2359
{
24-
Narrator = Color.Blue.ToArgb();
25-
Moods = Color.Maroon.ToArgb();
26-
Moods = Color.Maroon.ToArgb();
60+
try
61+
{
62+
var configFile = GetConfigFile();
63+
XDocument xdoc = File.Exists(configFile) ? XDocument.Load(configFile) : new XDocument(new XElement("HIColorer"));
64+
xdoc.Root.Element("Narrator").Value = Narrator.ToString();
65+
xdoc.Root.Element("Moods").Value = Moods.ToString();
66+
xdoc.Root.Element("Music").Value = Music.ToString();
67+
xdoc.Save(configFile);
68+
}
69+
catch (Exception e)
70+
{
71+
// ignore
72+
}
2773
}
74+
75+
private static string GetConfigFile() => Path.Combine(Configuration.PluginsDirectory, "hicolor-config.xml");
2876
}
29-
}
77+
}

source/HIColorer/EntryPoint.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using System.Diagnostics;
22
using System.Globalization;
33
using System.Windows.Forms;
4+
using Nikse.SubtitleEdit.Core.Common;
5+
using Nikse.SubtitleEdit.Core.SubtitleFormats;
46

57
namespace Nikse.SubtitleEdit.PluginLogic
68
{
9+
/// <summary>
10+
/// Provides functionality for highlighting and modifying subtitles specifically for Hearing Impaired (HI) purposes.
11+
/// </summary>
712
public class HIColorer : IPlugin
813
{
914
string IPlugin.Name => "HI Colorer";
1015

1116
string IPlugin.Text => "HI Colorer";
1217

13-
decimal IPlugin.Version => 1.1M;
18+
decimal IPlugin.Version => 2M;
1419

1520
string IPlugin.Description => "Set color for Hearing Impaired annotations (by Ivandrofly)";
1621

@@ -34,7 +39,7 @@ string IPlugin.DoAction(Form parentForm, string subtitle, double frameRate, stri
3439
}
3540

3641
var subRip = new SubRip();
37-
var sub = new Subtitle(subRip);
42+
var sub = new Subtitle();
3843
subRip.LoadSubtitle(sub, subtitle.SplitToLines(), subtitleFileName);
3944
if (sub.Paragraphs.Count < 1)
4045
{
@@ -49,4 +54,18 @@ string IPlugin.DoAction(Form parentForm, string subtitle, double frameRate, stri
4954
return string.Empty;
5055
}
5156
}
52-
}
57+
58+
/// <summary>
59+
/// Represents a plugin interface for subtitle editing functionality.
60+
/// </summary>
61+
public interface IPlugin
62+
{
63+
string Name { get; }
64+
string Text { get; }
65+
decimal Version { get; }
66+
string Description { get; }
67+
string ActionType { get; }
68+
string Shortcut { get; }
69+
string DoAction(Form parentForm, string subtitle, double frameRate, string listViewLineSeparatorString, string subtitleFileName, string videoFileName, string rawText);
70+
}
71+
}

source/HIColorer/HIColorer.csproj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
3-
<OutputType>Library</OutputType>
4-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5-
<UseWindowsForms>true</UseWindowsForms>
6-
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
7-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8-
</PropertyGroup>
9-
<Import Project="..\Plugin-Shared\Plugin-Shared.projitems" Label="Shared" />
2+
<PropertyGroup>
3+
<OutputType>Library</OutputType>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<UseWindowsForms>true</UseWindowsForms>
6+
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="libse" IncludeAssets="compile"/>
11+
</ItemGroup>
1012
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Nikse.SubtitleEdit.PluginLogic;
4+
5+
public static class StringExtensions
6+
{
7+
public static bool HasColor(this string input)
8+
{
9+
return input?.IndexOf("<font color=", StringComparison.OrdinalIgnoreCase) >= 0;
10+
}
11+
}

0 commit comments

Comments
 (0)