Merge pull request #8821 from ivandrofly/doc

Improves/Add documentation
This commit is contained in:
Nikolaj Olsson 2024-09-15 15:28:56 +02:00 committed by GitHub
commit 9ae93c4d49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 307 additions and 5 deletions

View File

@ -13,7 +13,7 @@ using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.Common namespace Nikse.SubtitleEdit.Core.Common
{ {
/// <summary> /// <summary>
/// File related utilities. /// Provides utility methods for file operations and file type identification.
/// </summary> /// </summary>
public static class FileUtil public static class FileUtil
{ {
@ -21,7 +21,7 @@ namespace Nikse.SubtitleEdit.Core.Common
/// Opens a binary file in read/write shared mode, reads the contents of the file into a /// Opens a binary file in read/write shared mode, reads the contents of the file into a
/// byte array, and then closes the file. /// byte array, and then closes the file.
/// </summary> /// </summary>
/// <param name="path">The file to open for reading. </param> /// <param name="path">The file to open for reading.</param>
/// <returns>A byte array containing the contents of the file.</returns> /// <returns>A byte array containing the contents of the file.</returns>
public static byte[] ReadAllBytesShared(string path) public static byte[] ReadAllBytesShared(string path)
{ {
@ -51,6 +51,12 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Opens a binary file in read/write shared mode, reads the specified number of bytes from the file into a byte array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="bytesToRead">The number of bytes to read from the file.</param>
/// <returns>A byte array containing the specified number of bytes read from the file.</returns>
public static byte[] ReadBytesShared(string path, int bytesToRead) public static byte[] ReadBytesShared(string path, int bytesToRead)
{ {
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -80,6 +86,13 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Opens a text file in read/write shared mode, reads all lines of the file into a list of strings,
/// and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="encoding">The encoding used to decode the text from the file.</param>
/// <returns>A list of strings containing all lines of the file.</returns>
public static List<string> ReadAllLinesShared(string path, Encoding encoding) public static List<string> ReadAllLinesShared(string path, Encoding encoding)
{ {
var bytes = ReadAllBytesShared(path); var bytes = ReadAllBytesShared(path);
@ -96,6 +109,13 @@ namespace Nikse.SubtitleEdit.Core.Common
return encoding.GetString(bytes).SplitToLines(); return encoding.GetString(bytes).SplitToLines();
} }
/// <summary>
/// Opens a text file in read/write shared mode, reads the contents of the file into a
/// string, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>A string containing the contents of the file.</returns>
public static string ReadAllTextShared(string path, Encoding encoding) public static string ReadAllTextShared(string path, Encoding encoding)
{ {
var bytes = ReadAllBytesShared(path); var bytes = ReadAllBytesShared(path);
@ -112,6 +132,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return encoding.GetString(bytes); return encoding.GetString(bytes);
} }
/// <summary>
/// Determines whether the specified file is a ZIP archive based on its header bytes.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a ZIP archive; otherwise, false.</returns>
public static bool IsZip(string fileName) public static bool IsZip(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -130,6 +155,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the specified file is a 7-Zip file by reading its signature bytes.
/// </summary>
/// <param name="fileName">The path to the file to check.</param>
/// <returns>True if the file is a 7-Zip file; otherwise, false.</returns>
public static bool Is7Zip(string fileName) public static bool Is7Zip(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -150,6 +180,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the given file is an MP3 file by examining its file headers and extension.
/// </summary>
/// <param name="fileName">The path of the file to check.</param>
/// <returns>True if the file is an MP3 file; otherwise, false.</returns>
public static bool IsMp3(string fileName) public static bool IsMp3(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -167,6 +202,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the specified file is a WAV audio file by examining its header and file extension.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a WAV audio file; otherwise, false.</returns>
public static bool IsWav(string fileName) public static bool IsWav(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -192,6 +232,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines if the specified file is a RAR archive by checking its magic number.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a RAR archive; otherwise, false.</returns>
public static bool IsRar(string fileName) public static bool IsRar(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -210,6 +255,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the specified file is a PNG image by reading its header bytes.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a PNG image; otherwise, false.</returns>
public static bool IsPng(string fileName) public static bool IsPng(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -232,6 +282,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the specified file is an SRR (Sample Rate Reduction) file by reading its initial bytes.
/// </summary>
/// <param name="fileName">The path to the file to check.</param>
/// <returns>True if the file is an SRR file, otherwise false.</returns>
public static bool IsSrr(string fileName) public static bool IsSrr(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -249,6 +304,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if the file is a JPG by reading the header of the file.
/// </summary>
/// <param name="fileName">The path to the file to check.</param>
/// <returns>True if the file is a JPG, otherwise false.</returns>
public static bool IsJpg(string fileName) public static bool IsJpg(string fileName)
{ {
// jpeg header - always starts with FFD8 (Start Of Image marker) + FF + a unknown byte (most often E0 or E1 though) // jpeg header - always starts with FFD8 (Start Of Image marker) + FF + a unknown byte (most often E0 or E1 though)
@ -267,6 +327,12 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines if the specified file is a Torrent file by reading its initial bytes and
/// checking for the presence of a specific Torrent file signature.
/// </summary>
/// <param name="fileName">The name of the file to verify.</param>
/// <returns>True if the file is a Torrent file; otherwise, false.</returns>
public static bool IsTorrentFile(string fileName) public static bool IsTorrentFile(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -277,6 +343,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if a given file is a Blu-ray subtitle (.sup) file by reading its first two bytes.
/// </summary>
/// <param name="fileName">The file to check for Blu-ray subtitle format.</param>
/// <returns>True if the file is a Blu-ray subtitle file, otherwise false.</returns>
public static bool IsBluRaySup(string fileName) public static bool IsBluRaySup(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -288,6 +359,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines if a file is a transport stream by inspecting its contents.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is identified as a transport stream; otherwise, false.</returns>
public static bool IsTransportStream(string fileName) public static bool IsTransportStream(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -312,6 +388,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines whether the specified file is a M2 transport stream.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>true if the file is a M2 transport stream; otherwise, false.</returns>
public static bool IsM2TransportStream(string fileName) public static bool IsM2TransportStream(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -320,6 +401,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines whether the specified file is an MPEG-2 Private Stream 2 file.
/// </summary>
/// <param name="fileName">The file to check.</param>
/// <returns>True if the specified file is an MPEG-2 Private Stream 2 file, otherwise false.</returns>
public static bool IsMpeg2PrivateStream2(string fileName) public static bool IsMpeg2PrivateStream2(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -330,6 +416,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines whether the specified file is a VobSub subtitle file.
/// </summary>
/// <param name="fileName">The path of the file to check.</param>
/// <returns>True if the file is a VobSub subtitle file; otherwise, false.</returns>
public static bool IsVobSub(string fileName) public static bool IsVobSub(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -341,6 +432,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines whether the specified file is a Manzanita file by reading the first 17 bytes.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a Manzanita file; otherwise, false.</returns>
public static bool IsManzanita(string fileName) public static bool IsManzanita(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -352,6 +448,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines if the specified file is a SP (Subtitle Processor) DVD SUP file format.
/// </summary>
/// <param name="fileName">The path of the file to check.</param>
/// <returns>True if the file is a SP DVD SUP file; otherwise, false.</returns>
public static bool IsSpDvdSup(string fileName) public static bool IsSpDvdSup(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -423,6 +524,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return false; return false;
} }
/// <summary>
/// Determines whether a file is in Rich Text Format (RTF).
/// </summary>
/// <param name="fileName">The path to the file to check.</param>
/// <returns>true if the file is in RTF format; otherwise, false.</returns>
public static bool IsRtf(string fileName) public static bool IsRtf(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -440,6 +546,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if a file contains a UTF-8 byte order mark (BOM).
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file starts with a UTF-8 BOM; otherwise, false.</returns>
public static bool HasUtf8Bom(string fileName) public static bool HasUtf8Bom(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -450,6 +561,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines if a specified subtitle file consists entirely of binary zeroes.
/// </summary>
/// <param name="fileName">The path to the subtitle file to be checked.</param>
/// <returns>True if the file consists entirely of binary zeroes; otherwise, false.</returns>
public static bool IsSubtitleFileAllBinaryZeroes(string fileName) public static bool IsSubtitleFileAllBinaryZeroes(string fileName)
{ {
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@ -476,6 +592,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return true; return true;
} }
/// <summary>
/// Determines whether the specified path represents a file.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>True if the specified path is a file; otherwise, false.</returns>
public static bool IsFile(string path) public static bool IsFile(string path)
{ {
if (!Path.IsPathRooted(path)) if (!Path.IsPathRooted(path))
@ -486,6 +607,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return (File.GetAttributes(path) & FileAttributes.Directory) != FileAttributes.Directory; return (File.GetAttributes(path) & FileAttributes.Directory) != FileAttributes.Directory;
} }
/// <summary>
/// Determines if the specified path refers to an existing directory.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>true if the path refers to an existing directory; otherwise, false.</returns>
public static bool IsDirectory(string path) public static bool IsDirectory(string path)
{ {
if (!Path.IsPathRooted(path)) if (!Path.IsPathRooted(path))
@ -496,6 +622,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory; return (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
} }
/// <summary>
/// Determines whether the specified file is plain text based on its content and length.
/// </summary>
/// <param name="fileName">The path to the file to check.</param>
/// <returns>True if the file is determined to be plain text; otherwise, false.</returns>
public static bool IsPlainText(string fileName) public static bool IsPlainText(string fileName)
{ {
var fileInfo = new FileInfo(fileName); var fileInfo = new FileInfo(fileName);
@ -565,6 +696,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return numberCount < numberThreshold && letterCount > letterThreshold; return numberCount < numberThreshold && letterCount > letterThreshold;
} }
/// <summary>
/// Attempts to read video information from the Matroska header of a specified file.
/// </summary>
/// <param name="fileName">The path to the Matroska file.</param>
/// <returns>A VideoInfo object containing video properties if successful; otherwise, an object with Success set to false.</returns>
public static VideoInfo TryReadVideoInfoViaMatroskaHeader(string fileName) public static VideoInfo TryReadVideoInfoViaMatroskaHeader(string fileName)
{ {
var info = new VideoInfo { Success = false }; var info = new VideoInfo { Success = false };
@ -587,6 +723,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return info; return info;
} }
/// <summary>
/// Attempts to read video information from an AVI file header.
/// </summary>
/// <param name="fileName">The path to the AVI file to be read.</param>
/// <returns>A <see cref="VideoInfo"/> object containing details about the video. If reading the information fails, the <c>Success</c> property of the returned object will be <c>false</c>.</returns>
public static VideoInfo TryReadVideoInfoViaAviHeader(string fileName) public static VideoInfo TryReadVideoInfoViaAviHeader(string fileName)
{ {
var info = new VideoInfo { Success = false }; var info = new VideoInfo { Success = false };
@ -619,6 +760,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return info; return info;
} }
/// <summary>
/// Attempts to read video information from an MP4 file.
/// </summary>
/// <param name="fileName">The path to the MP4 file.</param>
/// <returns>A VideoInfo object containing the video information, with the Success property indicating whether reading was successful.</returns>
public static VideoInfo TryReadVideoInfoViaMp4(string fileName) public static VideoInfo TryReadVideoInfoViaMp4(string fileName)
{ {
var info = new VideoInfo { Success = false }; var info = new VideoInfo { Success = false };
@ -646,11 +792,23 @@ namespace Nikse.SubtitleEdit.Core.Common
return info; return info;
} }
/// <summary>
/// Generates a unique temporary file name with the specified extension.
/// </summary>
/// <param name="extension">The extension for the temporary file name.</param>
/// <returns>A string containing the full path of the temporary file.</returns>
public static string GetTempFileName(string extension) public static string GetTempFileName(string extension)
{ {
return Path.GetTempPath() + Guid.NewGuid() + extension; return Path.GetTempPath() + Guid.NewGuid() + extension;
} }
/// <summary>
/// Writes the specified string to a file, using the specified encoding. If the encoding
/// is UTF-8 without BOM, it writes the content without a BOM.
/// </summary>
/// <param name="fileName">The file to write to.</param>
/// <param name="contents">The string to write to the file.</param>
/// <param name="encoding">The encoding to use for writing the text.</param>
public static void WriteAllText(string fileName, string contents, TextEncoding encoding) public static void WriteAllText(string fileName, string contents, TextEncoding encoding)
{ {
if (encoding.DisplayName == TextEncoding.Utf8WithoutBom) if (encoding.DisplayName == TextEncoding.Utf8WithoutBom)
@ -667,6 +825,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Writes specified text to a file with UTF-8 encoding, checking the global setting for the preferred encoding method (with or without BOM).
/// </summary>
/// <param name="fileName">The path and name of the file to write to.</param>
/// <param name="contents">The text content to be written to the file.</param>
public static void WriteAllTextWithDefaultUtf8(string fileName, string contents) public static void WriteAllTextWithDefaultUtf8(string fileName, string contents)
{ {
if (Configuration.Settings.General.DefaultEncoding == TextEncoding.Utf8WithoutBom) if (Configuration.Settings.General.DefaultEncoding == TextEncoding.Utf8WithoutBom)
@ -683,6 +846,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Determines whether the specified file is a valid Matroska file.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is a valid Matroska file; otherwise, false.</returns>
public static bool IsMatroskaFile(string fileName) public static bool IsMatroskaFile(string fileName)
{ {
using (var validator = new MatroskaFile(fileName)) using (var validator = new MatroskaFile(fileName))
@ -691,6 +859,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Checks if a file is locked by attempting to open it with exclusive read access.
/// </summary>
/// <param name="fileName">The name of the file to check.</param>
/// <returns>True if the file is locked, otherwise false.</returns>
public static bool IsFileLocked(string fileName) public static bool IsFileLocked(string fileName)
{ {
try try
@ -709,6 +882,12 @@ namespace Nikse.SubtitleEdit.Core.Common
return false; return false;
} }
/// <summary>
/// Searches for a subtitle file that matches the given video file in specified directories.
/// </summary>
/// <param name="path">The base path to search for subtitle files.</param>
/// <param name="videoFileName">The video file name for which a matching subtitle is being sought.</param>
/// <returns>The full path of the found subtitle file, or an empty string if no matching subtitle is found.</returns>
public static string TryLocateSubtitleFile(string path, string videoFileName) public static string TryLocateSubtitleFile(string path, string videoFileName)
{ {
// search in these subdirectories: \Subs;\Sub;\Subtitles; // search in these subdirectories: \Subs;\Sub;\Subtitles;

View File

@ -14,10 +14,29 @@ namespace Nikse.SubtitleEdit.Core.Common
/// </summary> /// </summary>
public static class HtmlUtil public static class HtmlUtil
{ {
/// <summary>
/// Represents the HTML tag used for italic text formatting.
/// </summary>
public static string TagItalic => "i"; public static string TagItalic => "i";
/// <summary>
/// Represents the HTML tag used for bold text formatting.
/// </summary>
public static string TagBold => "b"; public static string TagBold => "b";
/// <summary>
/// Represents the HTML tag used for underlined text formatting.
/// </summary>
public static string TagUnderline => "u"; public static string TagUnderline => "u";
/// <summary>
/// Represents the HTML tag used for specifying font attributes.
/// </summary>
public static string TagFont => "font"; public static string TagFont => "font";
/// <summary>
/// Represents the HTML tag used for the Cyrillic character 'і'.
/// </summary>
public static string TagCyrillicI => "\u0456"; // Cyrillic Small Letter Byelorussian-Ukrainian i (http://graphemica.com/%D1%96) public static string TagCyrillicI => "\u0456"; // Cyrillic Small Letter Byelorussian-Ukrainian i (http://graphemica.com/%D1%96)
private static readonly Regex TagOpenRegex = new Regex(@"<\s*(?:/\s*)?(\w+)[^>]*>", RegexOptions.Compiled); private static readonly Regex TagOpenRegex = new Regex(@"<\s*(?:/\s*)?(\w+)[^>]*>", RegexOptions.Compiled);
@ -367,6 +386,12 @@ namespace Nikse.SubtitleEdit.Core.Common
return encoded.ToString(); return encoded.ToString();
} }
/// <summary>
/// Remove all HTML tags from the input string, optionally including SSA tags.
/// </summary>
/// <param name="input">The input string that may contain HTML tags.</param>
/// <param name="alsoSsaTags">A boolean value indicating whether SSA tags should also be removed.</param>
/// <returns>A new string with all HTML tags removed, and optionally SSA tags removed.</returns>
public static string RemoveHtmlTags(string input, bool alsoSsaTags = false) public static string RemoveHtmlTags(string input, bool alsoSsaTags = false)
{ {
if (input == null || input.Length < 3) if (input == null || input.Length < 3)
@ -523,6 +548,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return new string(array, 0, arrayIndex); return new string(array, 0, arrayIndex);
} }
/// <summary>
/// Determines whether the specified text is a URL.
/// </summary>
/// <param name="text">The text to evaluate.</param>
/// <returns>True if the text is considered a URL, otherwise false.</returns>
public static bool IsUrl(string text) public static bool IsUrl(string text)
{ {
if (string.IsNullOrWhiteSpace(text) || text.Length < 6 || text.IndexOf('.') < 0 || text.IndexOf(' ') >= 0) if (string.IsNullOrWhiteSpace(text) || text.Length < 6 || text.IndexOf('.') < 0 || text.IndexOf(' ') >= 0)
@ -546,6 +576,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return false; return false;
} }
/// <summary>
/// Determines if the provided text starts with a URL-like string.
/// </summary>
/// <param name="text">The text to examine.</param>
/// <returns>True if the text starts with a URL-like string; otherwise, false.</returns>
public static bool StartsWithUrl(string text) public static bool StartsWithUrl(string text)
{ {
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
@ -564,6 +599,11 @@ namespace Nikse.SubtitleEdit.Core.Common
private static readonly string[] UppercaseTags = { "<I>", "<U>", "<B>", "<FONT", "</I>", "</U>", "</B>", "</FONT>" }; private static readonly string[] UppercaseTags = { "<I>", "<U>", "<B>", "<FONT", "</I>", "</U>", "</B>", "</FONT>" };
/// <summary>
/// Converts all uppercase HTML tags in the input string to lowercase.
/// </summary>
/// <param name="input">The input string containing HTML tags to be converted.</param>
/// <returns>A new string with all uppercase HTML tags converted to lowercase.</returns>
public static string FixUpperTags(string input) public static string FixUpperTags(string input)
{ {
if (string.IsNullOrEmpty(input) || input.IndexOf('<') < 0) if (string.IsNullOrEmpty(input) || input.IndexOf('<') < 0)
@ -588,6 +628,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
/// <summary>
/// Determines if the provided text contains formattable content not enclosed within HTML-like tags.
/// </summary>
/// <param name="text">The input text to be checked.</param>
/// <returns>True if the text contains any formattable content (letters or digits) outside of HTML-like tags; otherwise, false.</returns>
public static bool IsTextFormattable(in string text) public static bool IsTextFormattable(in string text)
{ {
if (string.IsNullOrEmpty(text)) if (string.IsNullOrEmpty(text))
@ -642,6 +687,11 @@ namespace Nikse.SubtitleEdit.Core.Common
"< / i>", "</I>", "< / I >", "< /I>", "</ I>", "< /I >", "</I >", "</ I >", "< / I>", "</i<", "</I<", "</I>" "< / i>", "</I>", "< / I >", "< /I>", "</ I>", "< /I >", "</I >", "</ I >", "< / I>", "</i<", "</I<", "</I>"
}; };
/// <summary>
/// Fix invalid or improperly formatted italic tags in the input HTML string.
/// </summary>
/// <param name="input">The input HTML string to process.</param>
/// <returns>A string with corrected italic tags.</returns>
public static string FixInvalidItalicTags(string input) public static string FixInvalidItalicTags(string input)
{ {
var text = input; var text = input;
@ -1000,6 +1050,14 @@ namespace Nikse.SubtitleEdit.Core.Common
return preTags + text; return preTags + text;
} }
/// <summary>
/// Toggles the specified HTML or SSA/ASS tag on or off in the provided text.
/// </summary>
/// <param name="input">The input string to apply the tag toggle.</param>
/// <param name="tag">The HTML or SSA/ASS tag to be toggled.</param>
/// <param name="wholeLine">Specifies whether the whole line should be toggled or just part of it.</param>
/// <param name="assa">Indicates whether the text contains SSA/ASS tags.</param>
/// <returns>A new string with the specified tag toggled.</returns>
public static string ToggleTag(string input, string tag, bool wholeLine, bool assa) public static string ToggleTag(string input, string tag, bool wholeLine, bool assa)
{ {
var text = input; var text = input;
@ -1060,6 +1118,14 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
/// <summary>
/// Determines if the specified tag is present in the input HTML or SSA/ASS string.
/// </summary>
/// <param name="input">The input string to search for the specified tag.</param>
/// <param name="tag">The HTML or SSA/ASS tag to check for.</param>
/// <param name="wholeLine">Specifies whether the search should consider the whole line.</param>
/// <param name="assa">Indicates if the input string is in SSA/ASS format.</param>
/// <returns>True if the tag is present in the input; otherwise, false.</returns>
public static bool IsTagOn(string input, string tag, bool wholeLine, bool assa) public static bool IsTagOn(string input, string tag, bool wholeLine, bool assa)
{ {
var text = input; var text = input;
@ -1079,6 +1145,14 @@ namespace Nikse.SubtitleEdit.Core.Common
text.IndexOf("</" + tag + ">", StringComparison.OrdinalIgnoreCase) >= 0; text.IndexOf("</" + tag + ">", StringComparison.OrdinalIgnoreCase) >= 0;
} }
/// <summary>
/// Applies the specified HTML or ASSA tag to the input string.
/// </summary>
/// <param name="input">The input string to which the tag will be applied.</param>
/// <param name="tag">The tag to apply to the input string.</param>
/// <param name="wholeLine">If true, the tag is applied to the entire line; otherwise, the tag is applied to a portion of the line.</param>
/// <param name="assa">If true, the tag is treated as an ASSA tag; otherwise, it is treated as an HTML tag.</param>
/// <returns>A new string with the specified tag applied.</returns>
public static string TagOn(string input, string tag, bool wholeLine, bool assa) public static string TagOn(string input, string tag, bool wholeLine, bool assa)
{ {
var text = input; var text = input;
@ -1125,6 +1199,14 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
/// <summary>
/// Remove the specified HTML tag from the input string.
/// </summary>
/// <param name="input">The input string containing HTML tags.</param>
/// <param name="tag">The HTML tag to be removed.</param>
/// <param name="wholeLine">Indicates whether the operation applies to the whole line.</param>
/// <param name="assa">Indicates whether ASSA (Advanced SubStation Alpha) tags are used.</param>
/// <returns>A new string with the specified tag removed.</returns>
public static string TagOff(string input, string tag, bool wholeLine, bool assa) public static string TagOff(string input, string tag, bool wholeLine, bool assa)
{ {
var text = input; var text = input;
@ -1174,6 +1256,12 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
/// <summary>
/// Converts a string representation of a color to a Color object. The string can be in various formats such as
/// "rgb(r, g, b)", "rgba(r, g, b, a)", or a hex color string like "#RRGGBB" or "#RRGGBBAA".
/// </summary>
/// <param name="s">The string representation of the color.</param>
/// <returns>A Color object corresponding to the input string. If the string cannot be parsed, the default color is white.</returns
public static Color GetColorFromString(string s) public static Color GetColorFromString(string s)
{ {
try try
@ -1229,6 +1317,11 @@ namespace Nikse.SubtitleEdit.Core.Common
} }
} }
/// <summary>
/// Remove color tags from the input string, adjusting for potentially surrounding font tags.
/// </summary>
/// <param name="input">The string from which to remove color tags.</param>
/// <returns>A new string with color tags removed.</returns>
public static string RemoveColorTags(string input) public static string RemoveColorTags(string input)
{ {
var r = new Regex("[ ]*(COLOR|color|Color)=[\"']*[#\\dA-Za-z]*[\"']*[ ]*"); var r = new Regex("[ ]*(COLOR|color|Color)=[\"']*[#\\dA-Za-z]*[\"']*[ ]*");
@ -1325,6 +1418,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return s; return s;
} }
/// <summary>
/// Removes ASS and SSA alignment tags from the given string.
/// </summary>
/// <param name="s">The input string from which to remove the alignment tags.</param>
/// <returns>A new string without ASS and SSA alignment tags.</returns>
public static string RemoveAssAlignmentTags(string s) public static string RemoveAssAlignmentTags(string s)
{ {
return s.Replace("{\\an1}", string.Empty) // ASS tags alone return s.Replace("{\\an1}", string.Empty) // ASS tags alone
@ -1378,6 +1476,11 @@ namespace Nikse.SubtitleEdit.Core.Common
.Replace("{\\a9}", string.Empty); .Replace("{\\a9}", string.Empty);
} }
/// <summary>
/// Remove color tags specific to Advanced SubStation Alpha (ASSA) format from the input string.
/// </summary>
/// <param name="input">The input string potentially containing ASSA color tags.</param>
/// <returns>A new string with all ASSA color tags removed.</returns>
public static string RemoveAssaColor(string input) public static string RemoveAssaColor(string input)
{ {
var text = input; var text = input;
@ -1389,6 +1492,11 @@ namespace Nikse.SubtitleEdit.Core.Common
return text; return text;
} }
/// <summary>
/// Gets the closing HTML tag for the specified opening tag.
/// </summary>
/// <param name="tag">The opening HTML tag to find the closing pair for.</param>
/// <returns>The closing HTML tag corresponding to the specified opening tag.</returns>
public static string GetClosingPair(string tag) public static string GetClosingPair(string tag)
{ {
switch (tag) switch (tag)
@ -1400,10 +1508,25 @@ namespace Nikse.SubtitleEdit.Core.Common
return tag.StartsWith("<font ", StringComparison.Ordinal) ? "</font>" : string.Empty; return tag.StartsWith("<font ", StringComparison.Ordinal) ? "</font>" : string.Empty;
} }
/// <summary>
/// Get the corresponding closing character for a given opening character.
/// </summary>
/// <param name="ch">The opening character to find the closing pair for.</param>
/// <returns>The corresponding closing character.</returns>
public static char GetClosingPair(char ch) => ch == '<' ? '>' : '}'; public static char GetClosingPair(char ch) => ch == '<' ? '>' : '}';
/// <summary>
/// Determines if the provided HTML tag is an opening tag.
/// </summary>
/// <param name="tag">The HTML tag to check.</param>
/// <returns>True if the tag is an opening tag, otherwise false.</returns
public static bool IsOpenTag(string tag) => tag.Length > 1 && tag[1] != '/'; public static bool IsOpenTag(string tag) => tag.Length > 1 && tag[1] != '/';
/// <summary>
/// Determines whether the specified character is a start tag symbol for HTML or similar markup.
/// </summary>
/// <param name="ch">The character to check.</param>
/// <returns>True if the character is a start tag symbol; otherwise, false.</returns>
public static bool IsStartTagSymbol(char ch) => ch == '<' || ch == '{'; public static bool IsStartTagSymbol(char ch) => ch == '<' || ch == '{';
} }
} }