Skip to content

Commit

Permalink
feat: Add ToString() and ToString() Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sun128764 committed Sep 12, 2023
1 parent 49ef54f commit 762f249
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 79 deletions.
189 changes: 187 additions & 2 deletions AssParser.Lib/AssParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static async Task<AssSubtitleModel> ParseAssFile(StreamReader assStream)
switch (tag)
{
case "[Script Info]":
int commentCount = 0;
while (assStream.Peek() is not '[' and > -1)
{
if (assStream.Peek() is '\r' or '\n')
Expand All @@ -58,8 +59,8 @@ public static async Task<AssSubtitleModel> ParseAssFile(StreamReader assStream)
}
if (assStream.Peek() == ';')
{
assStream.Read();
assSubtitleModel.ScriptInfo.SciptInfoItems.Add(";", await assStream.ReadLineAsync());
assSubtitleModel.ScriptInfo.SciptInfoItems.Add($";{commentCount++}", await assStream.ReadLineAsync());
continue;
}
(header, body) = await ParseLine(assStream);
assSubtitleModel.ScriptInfo.SciptInfoItems.Add(header, body);
Expand Down Expand Up @@ -278,5 +279,189 @@ public static async Task<AssSubtitleModel> ParseAssFile(string assFile)
using StreamReader assStream = new(File.Open(assFile, FileMode.Open));
return await ParseAssFile(assStream);
}
/// <summary>
/// Build the ass file and write it into StreamWriter.
/// </summary>
/// <param name="assSubtitleModel">Ass model.</param>
/// <param name="stream">Destination stream writer.</param>
/// <returns>A Task.</returns>
/// <exception cref="Exception">If there is any invalid element in the ass model.</exception>
public static async Task WriteToStreamAsync(AssSubtitleModel assSubtitleModel, StreamWriter stream)
{
await stream.WriteLineAsync("[Script Info]");
foreach (var item in assSubtitleModel.ScriptInfo.SciptInfoItems)
{
if (item.Key.StartsWith(";"))
{
await stream.WriteLineAsync($"{item.Value}");
}
else
{
await stream.WriteLineAsync($"{item.Key}: {item.Value}");
}
}
await stream.WriteLineAsync();
if (assSubtitleModel.UnknownSections.ContainsKey("[Aegisub Project Garbage]"))
{
await stream.WriteLineAsync("[Aegisub Project Garbage]");
await stream.WriteLineAsync(assSubtitleModel.UnknownSections["[Aegisub Project Garbage]"]);
}
await stream.WriteLineAsync("[V4+ Styles]");
await stream.WriteLineAsync($"Format: {string.Join(", ", assSubtitleModel.Styles.Format)}");
foreach (var style in assSubtitleModel.Styles.styles)
{
await stream.WriteAsync("Style: ");
for (int i = 0; i < assSubtitleModel.Styles.Format.Length; i++)
{
switch (assSubtitleModel.Styles.Format[i])
{
case "Name":
await stream.WriteAsync(style.Name);
break;
case "Fontname":
await stream.WriteAsync(style.Fontname);
break;
case "Fontsize":
await stream.WriteAsync(style.Fontsize);
break;
case "PrimaryColour":
await stream.WriteAsync(style.PrimaryColour);
break;
case "SecondaryColour":
await stream.WriteAsync(style.SecondaryColour);
break;
case "OutlineColour":
await stream.WriteAsync(style.OutlineColour);
break;
case "BackColour":
await stream.WriteAsync(style.BackColour);
break;
case "Bold":
await stream.WriteAsync(style.Bold);
break;
case "Italic":
await stream.WriteAsync(style.Italic);
break;
case "Underline":
await stream.WriteAsync(style.Underline);
break;
case "StrikeOut":
await stream.WriteAsync(style.StrikeOut);
break;
case "ScaleX":
await stream.WriteAsync(style.ScaleX);
break;
case "ScaleY":
await stream.WriteAsync(style.ScaleY);
break;
case "Spacing":
await stream.WriteAsync(style.Spacing);
break;
case "Angle":
await stream.WriteAsync(style.Angle);
break;
case "BorderStyle":
await stream.WriteAsync(style.BorderStyle);
break;
case "Outline":
await stream.WriteAsync(style.Outline);
break;
case "Shadow":
await stream.WriteAsync(style.Shadow);
break;
case "Alignment":
await stream.WriteAsync(style.Alignment);
break;
case "MarginL":
await stream.WriteAsync(style.MarginL);
break;
case "MarginR":
await stream.WriteAsync(style.MarginR);
break;
case "MarginV":
await stream.WriteAsync(style.MarginV);
break;
case "Encoding":
await stream.WriteAsync(style.Encoding);
break;
default:
throw new Exception("Invalid style");
}
if (i != assSubtitleModel.Styles.Format.Length - 1)
{
await stream.WriteAsync(',');
}
}
await stream.WriteAsync(Environment.NewLine);
}
await stream.WriteLineAsync();
if (assSubtitleModel.UnknownSections.ContainsKey("[Fonts]"))
{
await stream.WriteLineAsync("[Fonts]");
await stream.WriteLineAsync(assSubtitleModel.UnknownSections["[Fonts]"]);
}
if (assSubtitleModel.UnknownSections.ContainsKey("[Graphics]"))
{
await stream.WriteLineAsync("[Graphics]");
await stream.WriteLineAsync(assSubtitleModel.UnknownSections["[Graphics]"]);
}
await stream.WriteLineAsync("[Events]");
await stream.WriteAsync($"Format: {string.Join(", ", assSubtitleModel.Events.Format)}");
foreach (var item in assSubtitleModel.Events.events)
{
await stream.WriteAsync(Environment.NewLine);
await stream.WriteAsync($"{(item.Type == EventType.Dialogue ? "Dialogue" : "Comment")}: ");
for (int i = 0; i < assSubtitleModel.Events.Format.Length; i++)
{
switch (assSubtitleModel.Events.Format[i])
{
case "Layer":
await stream.WriteAsync(item.Layer);
break;
case "Start":
await stream.WriteAsync(item.Start);
break;
case "End":
await stream.WriteAsync(item.End);
break;
case "Style":
await stream.WriteAsync(item.Style);
break;
case "Name":
await stream.WriteAsync(item.Name);
break;
case "MarginL":
await stream.WriteAsync(item.MarginL);
break;
case "MarginR":
await stream.WriteAsync(item.MarginR);
break;
case "MarginV":
await stream.WriteAsync(item.MarginV);
break;
case "Effect":
await stream.WriteAsync(item.Effect);
break;
case "Text":
await stream.WriteAsync(item.Text);
break;
default:
throw new Exception("Invalid event");
}
if (i != assSubtitleModel.Events.Format.Length - 1)
{
await stream.WriteAsync(',');
}
}
}
if (assSubtitleModel.UnknownSections.ContainsKey("[Aegisub Extradata]"))
{
await stream.WriteLineAsync();
await stream.WriteLineAsync();
await stream.WriteLineAsync("[Aegisub Extradata]");
await stream.WriteAsync(assSubtitleModel.UnknownSections["[Aegisub Extradata]"]);
}
await stream.FlushAsync();
}
}
}
11 changes: 10 additions & 1 deletion AssParser.Lib/AssSubtitleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public class AssSubtitleModel
public Events Events { get; set; } = new();
public List<string> Ord { get; set; } = new();
public Dictionary<string, string> UnknownSections { get; set; } = new();
public override string ToString()
{
using MemoryStream stream = new();
using StreamWriter writer = new(stream, leaveOpen: true);
AssParser.WriteToStreamAsync(this, writer).Wait();
stream.Position = 0;
using StreamReader reader = new(stream, leaveOpen: true);
return reader.ReadToEnd();
}
}

public class ScriptInfo
Expand All @@ -18,7 +27,7 @@ public ScriptInfo()
{
SciptInfoItems = new();
}
public SortedList<string, string?> SciptInfoItems;
public Dictionary<string, string?> SciptInfoItems;
public string? Title
{
set
Expand Down
Loading

0 comments on commit 762f249

Please sign in to comment.