Skip to content

Commit

Permalink
Update HIColorer plugin for improved functionality and clarity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ivandrofly committed Feb 17, 2025
1 parent 240a1af commit 4d13eaa
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 80 deletions.
3 changes: 2 additions & 1 deletion source/HIColorer/Artists/Artist.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using Nikse.SubtitleEdit.Core.Common;

namespace Nikse.SubtitleEdit.PluginLogic
{
Expand All @@ -11,7 +12,7 @@ public abstract class Artist

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

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

}
}
4 changes: 3 additions & 1 deletion source/HIColorer/Artists/MoodsArtist.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Nikse.SubtitleEdit.PluginLogic
using Nikse.SubtitleEdit.Core.Common;

namespace Nikse.SubtitleEdit.PluginLogic
{
public class MoodsArtist : Artist
{
Expand Down
1 change: 1 addition & 0 deletions source/HIColorer/Artists/MusicArtist.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Nikse.SubtitleEdit.Core.Common;

namespace Nikse.SubtitleEdit.PluginLogic
{
Expand Down
22 changes: 12 additions & 10 deletions source/HIColorer/Artists/NarratorArtist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Core.Common;

namespace Nikse.SubtitleEdit.PluginLogic
{
public class NarratorArtist : Artist
{
private static readonly Regex _regexColonNonWord = new Regex(":\\B", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
private static readonly Regex RegexColonNonWord = new(":\\B", RegexOptions.Compiled | RegexOptions.ExplicitCapture);

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

/// <summary>
/// Narrator ignore words
Expand All @@ -35,10 +36,11 @@ public override void Paint(Subtitle subtitle)
{
var p = paragraphs[i];
string text = p.Text;
if (_regexColonNonWord.IsMatch(text))
if (RegexColonNonWord.IsMatch(text))
{
text = ProcessText(text);
}

if (text.Length != p.Text.Length)
{
p.Text = text;
Expand All @@ -49,13 +51,13 @@ public override void Paint(Subtitle subtitle)
private string ProcessText(string text)
{
char[] trimChars = { '"', '\\' };
string[] lines = text.SplitToLines();
for (int i = 0; i < lines.Length; i++)
var lines = text.SplitToLines();
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i];

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

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

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

string firstChr = _regexFirtCharNotTag.Match(preText).Value;
string firstChr = RegexFirtCharNotTag.Match(preText).Value;
if (string.IsNullOrEmpty(firstChr))
{
continue;
Expand All @@ -87,8 +89,8 @@ private string ProcessText(string text)
line = line.Insert(0, preText);

lines[i] = line;

}

// re-construct text
return string.Join(Environment.NewLine, lines);
}
Expand All @@ -105,7 +107,7 @@ private bool ShouldApplyColor(string textNoTags, bool isFirstLine)

// (foobar) foobar: hello world!
string pre = textNoTags.Substring(0, colonIdx).TrimStart();
if (pre.ContainsAny(new[] { ')', ']' }))
if (pre.Contains(new[] { ')', ']' }))
{
return false;
}
Expand Down Expand Up @@ -137,4 +139,4 @@ private bool ShouldApplyColor(string textNoTags, bool isFirstLine)
return true;
}
}
}
}
64 changes: 56 additions & 8 deletions source/HIColorer/ColorConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Drawing;
using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using Nikse.SubtitleEdit.Core.Common;

namespace Nikse.SubtitleEdit.PluginLogic
{
public class ColorConfig : Configuration<ColorConfig>
public class ColorConfig // : Configuration<ColorConfig>
{
/// <summary>
/// Narrators color.
Expand All @@ -13,17 +18,60 @@ public class ColorConfig : Configuration<ColorConfig>
/// Moods color.
/// </summary>
public int Moods { get; set; }

/// <summary>
/// Music paragraph color
/// </summary>
public int Music { get; set; }

public ColorConfig(string configFile) : base(configFile)
private ColorConfig()
{
}

public static ColorConfig LoadOrCreateConfigurations()
{
// old config file
try
{
File.Delete("hicolor.xml");
}
catch
{
}

var configFile = GetConfigFile();
if (File.Exists(configFile))
{
var xdoc = XDocument.Load(configFile);
var colorConfig = new ColorConfig()
{
Narrator = Convert.ToInt32(xdoc.Root.Element("Narrator").Value),
Moods = Convert.ToInt32(xdoc.Root.Element("Moods").Value),
Music = Convert.ToInt32(xdoc.Root.Element("Music").Value)
};
return colorConfig;
}

return new ColorConfig();
}

public void Save()
{
Narrator = Color.Blue.ToArgb();
Moods = Color.Maroon.ToArgb();
Moods = Color.Maroon.ToArgb();
try
{
var configFile = GetConfigFile();
XDocument xdoc = File.Exists(configFile) ? XDocument.Load(configFile) : new XDocument(new XElement("HIColorer"));
xdoc.Root.Element("Narrator").Value = Narrator.ToString();
xdoc.Root.Element("Moods").Value = Moods.ToString();
xdoc.Root.Element("Music").Value = Music.ToString();
xdoc.Save(configFile);
}
catch (Exception e)
{
// ignore
}
}

private static string GetConfigFile() => Path.Combine(Configuration.PluginsDirectory, "hicolor-config.xml");
}
}
}
25 changes: 22 additions & 3 deletions source/HIColorer/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.SubtitleFormats;

namespace Nikse.SubtitleEdit.PluginLogic
{
/// <summary>
/// Provides functionality for highlighting and modifying subtitles specifically for Hearing Impaired (HI) purposes.
/// </summary>
public class HIColorer : IPlugin
{
string IPlugin.Name => "HI Colorer";

string IPlugin.Text => "HI Colorer";

decimal IPlugin.Version => 1.1M;
decimal IPlugin.Version => 2M;

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

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

var subRip = new SubRip();
var sub = new Subtitle(subRip);
var sub = new Subtitle();
subRip.LoadSubtitle(sub, subtitle.SplitToLines(), subtitleFileName);
if (sub.Paragraphs.Count < 1)
{
Expand All @@ -49,4 +54,18 @@ string IPlugin.DoAction(Form parentForm, string subtitle, double frameRate, stri
return string.Empty;
}
}
}

/// <summary>
/// Represents a plugin interface for subtitle editing functionality.
/// </summary>
public interface IPlugin
{
string Name { get; }
string Text { get; }
decimal Version { get; }
string Description { get; }
string ActionType { get; }
string Shortcut { get; }
string DoAction(Form parentForm, string subtitle, double frameRate, string listViewLineSeparatorString, string subtitleFileName, string videoFileName, string rawText);
}
}
18 changes: 10 additions & 8 deletions source/HIColorer/HIColorer.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="..\Plugin-Shared\Plugin-Shared.projitems" Label="Shared" />
<PropertyGroup>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="libse" IncludeAssets="compile"/>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions source/HIColorer/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Nikse.SubtitleEdit.PluginLogic;

public static class StringExtensions
{
public static bool HasColor(this string input)
{
return input?.IndexOf("<font color=", StringComparison.OrdinalIgnoreCase) >= 0;
}
}
Loading

0 comments on commit 4d13eaa

Please sign in to comment.