Skip to content

Commit

Permalink
Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
xiety committed May 8, 2024
1 parent f96f5a6 commit 7c81214
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 132 deletions.
43 changes: 10 additions & 33 deletions AnkiPoetry.BlazorWasm/App.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public sealed partial class App : IAsyncDisposable

private readonly CreatorInfo[] infos =
[
new(new WordCreator(), "word", "Word", "1. word.csv"),
new(new LineCreator(), "line", "Line", "2. line.csv"),
new(new PageCreator(), "page", "Page", "3. page.csv"),
new(new WordCreator(), "word", "Word", "1. word"),
new(new LineCreator(), "line", "Line", "2. line"),
new(new PageCreator(), "page", "Page", "3. page"),
];

private readonly SampleCreator creator_sample = new();
Expand Down Expand Up @@ -77,26 +77,16 @@ private void Generate()
{
LocalStorage.SetItem(StateKey, state);

var doc = LoaderText.LoadText(
state.Text,
state.Parameters.WordWrap,
state.Parameters.WrapOnSpaces,
state.Parameters.AddDots
);
var doc = LoaderText.LoadText(state.Text, state.Parameters);

var chunks = Chunker.Run(doc,
state.Parameters.ChunkSize,
state.Parameters.OverlapChapters,
state.Parameters.EmptyEndElement,
state.Parameters.TitleToBegin
);
var chunks = Chunker.Run(doc, state.Parameters);

samples = creator_sample.Run(chunks, state.Parameters.Colors, state.Parameters.LineNumbers);
samples = creator_sample.Run(chunks, state.Parameters);

foreach (var info in infos)
{
var cards = info.Creator.Run(chunks, state.Parameters.Colors, state.Parameters.LineNumbers);
info.Csv = CsvSaver.CreateCsv(cards, [info.NoteType]);
var cards = info.Creator.Run(chunks, state.Parameters);
info.Csv = CsvSaver.CreateCsv(cards, [$"#notetype:poetry::{info.Id}", $"#deck:{state.Parameters.DeckName}::{info.DeckName}"]);
}
}

Expand Down Expand Up @@ -125,29 +115,16 @@ public async ValueTask DisposeAsync()
await module.DisposeAsync();
}

record CreatorInfo(BaseCreator<Card> Creator, string Id, string Title, string FileName)
record CreatorInfo(BaseCreator<Card> Creator, string Id, string Title, string DeckName)
{
public string Csv { get; set; } = "";
public string ElementId => $"text_{Id}";
public string NoteType => $"#notetype:poetry::{Id}";
public string FileName => DeckName + ".csv";
}

record State
{
public string Text { get; set; } = "";
public Parameters Parameters { get; set; } = new();
}

record Parameters
{
public int ChunkSize { get; set; } = 20;
public int WordWrap { get; set; } = -1;
public bool WrapOnSpaces { get; set; } = true;
public bool AddDots { get; set; } = false;
public int Colors { get; set; } = 6;
public bool OverlapChapters { get; set; } = true;
public bool EmptyEndElement { get; set; } = true;
public bool TitleToBegin { get; set; } = true;
public bool LineNumbers { get; set; } = true;
}
}
12 changes: 6 additions & 6 deletions AnkiPoetry.Engine.Tests/ChunkerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public void DoesNotCreatePageWithSingleLine()
4
""";

var doc = LoaderText.LoadText(text, -1, false, false);

var chunks = Chunker.Run(doc, 5, true, false, true);
var parameters = new Parameters();
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);

Assert.AreEqual(1, chunks.Length);
}
Expand All @@ -29,9 +29,9 @@ public void DoesNotCreatePageWithSingleLine_WithEmptyEndElement()
3
""";

var doc = LoaderText.LoadText(text, -1, false, false);

var chunks = Chunker.Run(doc, 5, true, empty_end_element: true, true);
var parameters = new Parameters();
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);

Assert.AreEqual(1, chunks.Length);
}
Expand Down
44 changes: 22 additions & 22 deletions AnkiPoetry.Engine.Tests/LineCreatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public void TestNormal()
E
""";

var doc = LoaderText.LoadText(text, -1, false, false);
var chunks = Chunker.Run(doc, 5, true, true, false);

var cards = lineCreator.Run(chunks, 6, true);
var parameters = new Parameters { OverlapChapters = false, TitleToBegin = false, ChunkSize = 5 };
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);
var cards = lineCreator.Run(chunks, parameters);

Assert.AreEqual(6, cards.Length);

Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div>""", cards[2].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><div class="line4"> 4. {{c1::D}}</div>""", cards[3].Text);
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. {{c1::E}}</div>""", cards[4].Text);
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. E</div><div class="line0"> 6. {{c1::(End)}}</div>""", cards[5].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text, message: "0");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text, message: "1");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div>""", cards[2].Text, message: "2");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><div class="line4"> 4. {{c1::D}}</div><hr>""", cards[3].Text, message: "3");
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. {{c1::E}}</div><hr>""", cards[4].Text, message: "4");
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. E</div><hr><div class="line0"> 6. {{c1::(End)}}</div>""", cards[5].Text, message: "5");
}

[TestMethod]
Expand All @@ -48,20 +48,20 @@ public void TestOverlap()
F
""";

var doc = LoaderText.LoadText(text, -1, false, false);
var chunks = Chunker.Run(doc, 5, true, true, false);

var cards = lineCreator.Run(chunks, 6, true);
var parameters = new Parameters { OverlapChapters = true, TitleToBegin = false, ChunkSize = 5, Continous = false };
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);
var cards = lineCreator.Run(chunks, parameters);

Assert.AreEqual(8, cards.Length);

Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text);
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div>""", cards[2].Text);
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><div class="line4"> 4. {{c1::(End) D}}</div>""", cards[3].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. {{c1::D}}</div>""", cards[4].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. {{c1::E}}</div>""", cards[5].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. E</div><div class="line3"> 3. {{c1::F}}</div>""", cards[6].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. E</div><div class="line3"> 3. F</div><div class="line4"> 4. {{c1::(End)}}</div>""", cards[7].Text);
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text, message: "0");
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text, message: "1");
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div><hr>""", cards[2].Text, message: "2");
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><hr><div class="line4"> 4. {{c1::(End) D}}</div>""", cards[3].Text, message: "3");
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. {{c1::D}}</div>""", cards[4].Text, message: "4");
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. {{c1::E}}</div>""", cards[5].Text, message: "5");
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. E</div><div class="line3"> 3. {{c1::F}}</div><hr>""", cards[6].Text, message: "6");
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. C (Begin)</div><div class="line1"> 1. D</div><div class="line2"> 2. E</div><div class="line3"> 3. F</div><hr><div class="line4"> 4. {{c1::(End)}}</div>""", cards[7].Text, message: "7");
}
}
24 changes: 12 additions & 12 deletions AnkiPoetry.Engine.Tests/PageCreatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public void TestNormal()
E
""";

var doc = LoaderText.LoadText(text, -1, false, false);
var chunks = Chunker.Run(doc, 5, true, true, false);

var cards = pageCreator.Run(chunks, 6, true);
var parameters = new Parameters { ChunkSize = 5, TitleToBegin = false };
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);
var cards = pageCreator.Run(chunks, parameters);

Assert.AreEqual(2, cards.Length);

Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. {{c1::(Begin)}}</div><div class="line1"> 1. {{c2::A}}</div><div class="line2"> 2. {{c1::B}}</div><div class="line3"> 3. {{c2::C}}</div><div class="line4"> 4. {{c1::D}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. {{c1::D}}</div><div class="line5"> 5. {{c2::E}}</div><div class="line0"> 6. {{c1::(End)}}</div>""", cards[1].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. {{c1::(Begin)}}</div><div class="line1"> 1. {{c2::A}}</div><div class="line2"> 2. {{c1::B}}</div><div class="line3"> 3. {{c2::C}}</div><div class="line4"> 4. {{c1::D}}</div><hr>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. {{c1::D}}</div><div class="line5"> 5. {{c2::E}}</div><hr><div class="line0"> 6. {{c1::(End)}}</div>""", cards[1].Text);
}

[TestMethod]
Expand All @@ -44,14 +44,14 @@ public void TestOverlap()
F
""";

var doc = LoaderText.LoadText(text, -1, false, false);
var chunks = Chunker.Run(doc, 5, true, true, false);

var cards = pageCreator.Run(chunks, 6, true);
var parameters = new Parameters { ChunkSize = 5, TitleToBegin = false, Continous = false };
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);
var cards = pageCreator.Run(chunks, parameters);

Assert.AreEqual(2, cards.Length);

Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. {{c1::(Begin)}}</div><div class="line1"> 1. {{c2::A}}</div><div class="line2"> 2. {{c1::B}}</div><div class="line3"> 3. {{c2::C}}</div><div class="line4"> 4. {{c1::(End) D}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. {{c1::C (Begin)}}</div><div class="line1"> 1. {{c2::D}}</div><div class="line2"> 2. {{c1::E}}</div><div class="line3"> 3. {{c2::F}}</div><div class="line4"> 4. {{c1::(End)}}</div>""", cards[1].Text);
Assert.AreEqual("""<div class="header">Test 1 (1)</div><br><div class="line0"> 0. {{c1::(Begin)}}</div><div class="line1"> 1. {{c2::A}}</div><div class="line2"> 2. {{c1::B}}</div><div class="line3"> 3. {{c2::C}}</div><hr><div class="line4"> 4. {{c1::(End) D}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test 2 (1)</div><br><div class="line0"> 0. {{c1::C (Begin)}}</div><div class="line1"> 1. {{c2::D}}</div><div class="line2"> 2. {{c1::E}}</div><div class="line3"> 3. {{c2::F}}</div><hr><div class="line4"> 4. {{c1::(End)}}</div>""", cards[1].Text);
}
}
18 changes: 9 additions & 9 deletions AnkiPoetry.Engine.Tests/WordCreatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public void TestNormal()
E
""";

var doc = LoaderText.LoadText(text, -1, false, false);
var chunks = Chunker.Run(doc, 5, true, true, false);

var cards = wordCreator.Run(chunks, 6, true);
var parameters = new Parameters { ChunkSize = 5, TitleToBegin = false, Continous = false };
var doc = LoaderText.LoadText(text, parameters);
var chunks = Chunker.Run(doc, parameters);
var cards = wordCreator.Run(chunks, parameters);

Assert.AreEqual(5, cards.Length);

Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div>""", cards[2].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><div class="line4"> 4. {{c1::D}}</div>""", cards[3].Text);
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. {{c1::E}}</div>""", cards[4].Text);
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. {{c1::A}}</div>""", cards[0].Text, message: "0");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. {{c1::B}}</div>""", cards[1].Text, message: "1");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. {{c1::C}}</div>""", cards[2].Text, message: "2");
Assert.AreEqual("""<div class="header">Test (1)</div><br><div class="line0"> 0. (Begin)</div><div class="line1"> 1. A</div><div class="line2"> 2. B</div><div class="line3"> 3. C</div><div class="line4"> 4. {{c1::D}}</div><hr>""", cards[3].Text, message: "3");
Assert.AreEqual("""<div class="header">Test (2)</div><br><div class="line4"> 4. D</div><div class="line5"> 5. {{c1::E}}</div><hr>""", cards[4].Text, message: "4");
}
}
18 changes: 9 additions & 9 deletions AnkiPoetry.Engine/BaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public abstract class BaseCreator<T>
private static string ColorLine(string text, int n, int colors)
=> $"<div class=\"line{n % colors}\">" + text + "</div>";

protected static string GetLineText(string text, MyLine line, int colors, bool line_numbers)
=> AddLineNumber(line.LineNumber, AddPrefixPostfix(text, line.LineType), colors, line_numbers);
protected string GetLineText(string text, MyLine line, Parameters parameters)
=> AddLineNumber(line, AddPrefixPostfix(text, line.LineType), parameters);

protected static string AddLineNumber(int lineNumber, string text, int colors, bool line_numbers)
=> ColorLine((line_numbers ? $"{lineNumber,3}. " : "") + text, lineNumber, colors);
protected string AddLineNumber(MyLine line, string text, Parameters parameters)
=> ColorLine((parameters.LineNumbers ? $"{(parameters.Continous ? line.ContinousNumber : line.LineNumber),3}. " : "") + text, line.LineNumber, parameters.Colors);

protected static string AddPrefixPostfix(string text, LineType lineType)
=> lineType switch
Expand All @@ -20,14 +20,14 @@ protected static string AddPrefixPostfix(string text, LineType lineType)
_ => throw new Exception(),
};

protected abstract IEnumerable<T> CardFromChunk(Chunk chunk, int colors, bool line_numbers);
protected abstract IEnumerable<T> CardFromChunk(Chunk chunk, Parameters parameters);

public T[] Run(Chunk[] chunks, int colors, bool line_numbers)
public T[] Run(Chunk[] chunks, Parameters parameters)
{
if (colors <= 0)
colors = 1;
if (parameters.Colors <= 0)
parameters.Colors = 1;

return chunks.SelectMany(a => CardFromChunk(a, colors, line_numbers)).ToArray();
return chunks.SelectMany(a => CardFromChunk(a, parameters)).ToArray();
}

protected virtual string CreateNumber(int maxSongNumber, int sectionNumber, int songNumber, int lineNumber)
Expand Down
Loading

0 comments on commit 7c81214

Please sign in to comment.