Tranga-Website/Tranga/Chapter.cs

40 lines
1.6 KiB
C#
Raw Normal View History

2023-05-18 20:06:29 +02:00
using System.Globalization;
2023-05-25 16:55:58 +02:00
using System.Text.RegularExpressions;
2023-05-18 20:06:29 +02:00
namespace Tranga;
/// <summary>
/// Has to be Part of a publication
/// Includes the Chapter-Name, -VolumeNumber, -ChapterNumber, the location of the chapter on the internet and the saveName of the local file.
/// </summary>
public struct Chapter
{
public string? name { get; }
public string? volumeNumber { get; }
public string? chapterNumber { get; }
public string url { get; }
public string fileName { get; }
2023-05-20 02:39:23 +02:00
public string sortNumber { get; }
2023-05-25 16:55:58 +02:00
2023-05-25 17:34:24 +02:00
private static readonly Regex LegalCharacters = new Regex(@"([A-z]*[0-9]* *\.*-*,*\]*\[*'*\'*\)*\(*~*!*)*");
public Chapter(string? name, string? volumeNumber, string? chapterNumber, string url)
{
this.name = name;
2023-06-03 22:25:24 +02:00
this.volumeNumber = volumeNumber;
this.chapterNumber = chapterNumber;
this.url = url;
2023-05-20 02:10:10 +02:00
NumberFormatInfo nfi = new NumberFormatInfo()
{
NumberDecimalSeparator = "."
};
2023-06-05 19:45:50 +02:00
sortNumber = decimal.Round(Convert.ToDecimal(this.volumeNumber ?? "1") * Convert.ToDecimal(this.chapterNumber, nfi), 1)
2023-05-20 02:39:23 +02:00
.ToString(nfi);
2023-06-03 22:25:24 +02:00
string chapterName = string.Concat(LegalCharacters.Matches(name ?? ""));
string volStr = this.volumeNumber is not null ? $"Vol.{this.volumeNumber} " : "";
string chNumberStr = this.chapterNumber is not null ? $"Ch.{chapterNumber} " : "";
string chNameStr = chapterName.Length > 0 ? $"- {chapterName}" : "";
2023-06-03 23:44:58 +02:00
chNameStr = chNameStr.Replace("Volume", "").Replace("volume", "");
2023-06-03 22:25:24 +02:00
this.fileName = $"{volStr}{chNumberStr}{chNameStr}";
}
}