Skip to content

Commit

Permalink
Add horizontal line
Browse files Browse the repository at this point in the history
  • Loading branch information
xiety committed May 1, 2024
1 parent f22dfe4 commit 7eb9b2c
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 27 deletions.
6 changes: 6 additions & 0 deletions AnkiPoetry.BlazorWasm/App.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
font-size: 14px;
}

::deep.card hr {
height: 0px;
border: none;
border-top: 1px solid white;
}

::deep.card .line0 {
color: #af2acd;
}
Expand Down
19 changes: 12 additions & 7 deletions AnkiPoetry.Engine/BaseCreatorPerLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace AnkiPoetry.Engine;

public abstract class BaseCreatorPerLine<T> : BaseCreator<T>
{
protected abstract IEnumerable<T> CreateCard(string number, string beginning, MyLine to, int colors, bool line_numbers);
protected abstract T CreateCard(string number, string beginning, string ending, MyLine to, int colors, bool line_numbers);

protected virtual MyLine[] FilterLines(MyLine[] lines)
=> lines;

protected static string JoinLines(MyLine[] list, int colors, bool line_numbers)
{
Expand All @@ -21,17 +24,19 @@ protected static string JoinLines(MyLine[] list, int colors, bool line_numbers)

protected override IEnumerable<T> CardFromChunk(Chunk chunk, int colors, bool line_numbers)
{
for (var i = 0; i < chunk.Lines.Length - 1; ++i)
var filtered = FilterLines(chunk.Lines);

for (var i = 0; i < filtered.Length - 1; ++i)
{
var to = chunk.Lines[i + 1];
var to = filtered[i + 1];
var number = CreateNumber(chunk.MaxSongNumber, chunk.SectionNumber, chunk.SongNumber, to.LineNumber);

var beginning = CreateHeader(chunk.Header) + JoinLines(chunk.Lines[..(i + 1)], colors, line_numbers);
var beginning = CreateHeader(chunk.Header) + JoinLines(filtered[..(i + 1)], colors, line_numbers);
var ending = to.IsLast ? "<hr>" : "";

var cards = CreateCard(number, beginning, to, colors, line_numbers);
var card = CreateCard(number, beginning, ending, to, colors, line_numbers);

foreach (var card in cards)
yield return card;
yield return card;
}
}
}
25 changes: 16 additions & 9 deletions AnkiPoetry.Engine/Chunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,48 @@ private static IEnumerable<MySong> Augmented(MySection section, bool overlap_cha

var title = !String.IsNullOrEmpty(section.Songs[i].SongName) ? section.Songs[i].SongName : section.SectionName;
var text_begin = (i != 0 && overlap_chapters) ? section.Songs[i - 1].Lines.Last().Text : (title_to_begin ? title : "");
lines.Add(new(0, text_begin, LineType.Prev));
lines.Add(new(0, text_begin, LineType.Prev, false));

lines.AddRange(section.Songs[i].Lines);
lines.AddRange(section.Songs[i].Lines[..^1]);

var last = section.Songs[i].Lines[^1];
lines.Add(last with { IsLast = true });

var new_num = section.Songs[i].Lines.Max(a => a.LineNumber) + 1;

if (overlap_chapters && i != section.Songs.Length - 1)
{
var text_end = section.Songs[i + 1].Lines.First().Text;
lines.Add(new(new_num, text_end, LineType.Next));
lines.Add(new(new_num, text_end, LineType.Next, false));
}
else if (empty_end_element)
{
lines.Add(new(new_num, "", LineType.Next));
lines.Add(new(new_num, "", LineType.Next, false));
}

yield return section.Songs[i] with { Lines = [.. lines] };
}
}

private static IEnumerable<TItem[]> ChunksWithOverlap<TItem>(IEnumerable<TItem> lines, int chunk_size)
private static IEnumerable<MyLine[]> ChunksWithOverlap(IEnumerable<MyLine> lines, int chunk_size)
{
var result = new List<TItem>();
var result = new List<MyLine>();

foreach (var line in lines)
{
result.Add(line);

if (result.Count == chunk_size)
if (result.Count + 1 == chunk_size)
{
result.Add(line with { IsLast = (line.LineType == LineType.Norm) });

yield return result.ToArray();

result.Clear();
result.Add(line);
}
else
{
result.Add(line);
}
}

if (result.Count > 1)
Expand Down
2 changes: 1 addition & 1 deletion AnkiPoetry.Engine/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public record MyDocument(MySection[] Sections, int MaxSongNumber);
public record MySection(int SectionNumber, string SectionName, MySong[] Songs);
public record MySong(int SongNumber, string SongName, MyLine[] Lines);
public record MyLine(int LineNumber, string Text, LineType LineType);
public record MyLine(int LineNumber, string Text, LineType LineType, bool IsLast);
public record Card(string Number, string Text);

public enum LineType { Norm, Prev, Next };
4 changes: 2 additions & 2 deletions AnkiPoetry.Engine/LineCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public class LineCreator : BaseCreatorPerLine<Card>
{
protected override IEnumerable<Card> CreateCard(string number, string beginning, MyLine to, int colors, bool line_numbers)
protected override Card CreateCard(string number, string beginning, string ending, MyLine to, int colors, bool line_numbers)
{
var text = MakeCloze(AddPrefixPostfix(to.Text, to.LineType));
var cloze = AddLineNumber(to.LineNumber, text, colors, line_numbers);
yield return new(number, beginning + cloze);
return new(number, beginning + cloze + ending);
}

private static string MakeCloze(string text)
Expand Down
2 changes: 1 addition & 1 deletion AnkiPoetry.Engine/LoaderText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static MyDocument LoadLines(string[] lines, int word_wrap, bool wrap_on_s

foreach (var wrap in wrapped)
{
songLines.Add(new(lineNumber, wrap, LineType.Norm));
songLines.Add(new(lineNumber, wrap, LineType.Norm, false));
lineNumber++;
}
}
Expand Down
2 changes: 2 additions & 0 deletions AnkiPoetry.Engine/PageCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected override IEnumerable<Card> CardFromChunk(Chunk chunk, int colors, bool
even = !even;
}

sb.Append("<hr>");

yield return new(number, sb.ToString());
}

Expand Down
3 changes: 3 additions & 0 deletions AnkiPoetry.Engine/SampleCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ protected override IEnumerable<Card> CardFromChunk(Chunk chunk, int colors, bool
{
var text = GetLineText(line.Text, line, colors, line_numbers);
sb.Append(text);

if (line.IsLast)
sb.Append("<hr>");
}

yield return new(number, sb.ToString());
Expand Down
12 changes: 5 additions & 7 deletions AnkiPoetry.Engine/WordCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace AnkiPoetry.Engine;

public partial class WordCreator : BaseCreatorPerLine<Card>
{
protected override IEnumerable<Card> CreateCard(string number, string beginning, MyLine to, int colors, bool line_numbers)
{
if (to.LineType != LineType.Norm)
yield break;
protected override MyLine[] FilterLines(MyLine[] lines)
=> lines.Where(a => a.LineType != LineType.Next).ToArray();

protected override Card CreateCard(string number, string beginning, string ending, MyLine to, int colors, bool line_numbers)
{
var cloze = GetLineText(MakeCloze(to.Text), to, colors, line_numbers);
var text = beginning + cloze;

yield return new(number, text);
return new(number, beginning + cloze + ending);
}

private static string MakeCloze(string text)
Expand Down

0 comments on commit 7eb9b2c

Please sign in to comment.