Skip to content

Commit

Permalink
Wrap on spaces, Overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
xiety committed Apr 12, 2024
1 parent 19f1c49 commit 8a08979
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 92 deletions.
2 changes: 2 additions & 0 deletions AnkiPoetry.BlazorWasm/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<div class="parameters">
<div><div class="parameter-name">Chunk size: </div><input type="number" min="1" @bind="parameters.ChunkSize" /></div>
<div><div class="parameter-name">Word Wrap: </div><input type="number" min="-1" @bind="parameters.WordWrap" /></div>
<div><div class="parameter-name">On spaces: </div><input type="checkbox" @bind="parameters.WrapOnSpaces" /></div>
<div><div class="parameter-name">Colors: </div><input type="number" min="1" @bind="parameters.Colors" /></div>
<div><div class="parameter-name">Overlap: </div><input type="checkbox" @bind="parameters.OverlapChapters" /></div>
</div>
<div class="item">
<div class="item-name">Input:</div>
Expand Down
7 changes: 5 additions & 2 deletions AnkiPoetry.BlazorWasm/App.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ private void Generate()
{
LocalStorage.SetItem(ParametersKey, parameters);

var doc = LoaderText.LoadLines(parameters.Text.Replace("\r\n", "\n").Split("\n"), parameters.WordWrap);
var chunks = Chunker.Run(doc, parameters.ChunkSize).ToArray();
var doc = LoaderText.LoadLines(parameters.Text.Replace("\r\n", "\n").Split("\n"), parameters.WordWrap, parameters.WrapOnSpaces);
var chunks = Chunker.Run(doc, parameters.ChunkSize, parameters.OverlapChapters).ToArray();

samples = creator_sample.Run(chunks, parameters.Colors);

Expand Down Expand Up @@ -110,7 +110,10 @@ record Parameters
{
public int ChunkSize { get; set; }
public int WordWrap { get; set; }
public bool WrapOnSpaces { get; set; }
public int Colors { get; set; }
public bool OverlapChapters { get; set; } = true;

public string Text { get; set; } = "";
}
}
2 changes: 1 addition & 1 deletion AnkiPoetry.BlazorWasm/App.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
width: 100px;
}

.parameters input {
.parameters input[type="number"] {
width: 100px;
}

Expand Down
10 changes: 5 additions & 5 deletions AnkiPoetry.Engine/Chunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public static class Chunker
{
public static IEnumerable<Chunk> Run(MyDocument doc, int chunk_size)
public static IEnumerable<Chunk> Run(MyDocument doc, int chunk_size, bool overlap_chapters)
{
foreach (var section in doc.Sections)
{
foreach (var song in Augmented(section.Songs))
foreach (var song in Augmented(section.Songs, overlap_chapters))
{
var screen_number = 1;

Expand All @@ -20,20 +20,20 @@ public static IEnumerable<Chunk> Run(MyDocument doc, int chunk_size)
}
}

private static IEnumerable<MySong> Augmented(MySong[] songs)
private static IEnumerable<MySong> Augmented(MySong[] songs, bool overlap_chapters)
{
for (var i = 0; i < songs.Length; ++i)
{
var lines = new List<MyLine>();

var textBegin = ((i != 0) ? songs[i - 1].Lines.Last().Text : "");
var textBegin = ((overlap_chapters && i != 0) ? songs[i - 1].Lines.Last().Text : "");
lines.Add(new(0, textBegin, LineType.Prev));

lines.AddRange(songs[i].Lines);

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

var textEnd = ((i != songs.Length - 1) ? songs[i + 1].Lines.First().Text : "");
var textEnd = ((overlap_chapters && i != songs.Length - 1) ? songs[i + 1].Lines.First().Text : "");
lines.Add(new(new_num, textEnd, LineType.Next));

yield return songs[i] with { Lines = [.. lines] };
Expand Down
88 changes: 4 additions & 84 deletions AnkiPoetry.Engine/LoaderText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public static partial class LoaderText
{
public static MyDocument LoadFile(string file, int word_wrap = -1)
=> LoadLines(File.ReadAllLines(file), word_wrap);
public static MyDocument LoadFile(string file, int word_wrap, bool wrap_on_spaces)
=> LoadLines(File.ReadAllLines(file), word_wrap, wrap_on_spaces);

public static MyDocument LoadLines(string[] lines, int word_wrap = -1)
public static MyDocument LoadLines(string[] lines, int word_wrap, bool wrap_on_spaces)
{
var songs = new List<MySong>();
var songLines = new List<MyLine>();
Expand Down Expand Up @@ -55,7 +55,7 @@ public static MyDocument LoadLines(string[] lines, int word_wrap = -1)
}
else
{
var wrapped = TextWrapper.Wrap(text, word_wrap);
var wrapped = TextWrapper.Wrap(text, word_wrap, wrap_on_spaces);

foreach (var wrap in wrapped)
{
Expand All @@ -74,83 +74,3 @@ public static MyDocument LoadLines(string[] lines, int word_wrap = -1)
return new([.. sections], 100);
}
}

public static class TextWrapper
{
public static string[] Wrap(string text, int word_wrap)
{
if (word_wrap == -1 || text.Length <= word_wrap)
return [text];

var lines = WrapParts(text, word_wrap);

return [..AddDots([.. lines])] ;
}

private static IEnumerable<string> WrapParts(string text, int word_wrap)
{
var items = Split(text);

var c = "";

foreach (var item in items)
{
var n = c + item;

if (n.Length > word_wrap)
{
yield return c.Trim();
c = item;
}
else
{
c = n;
}
}

if (c != "")
yield return c.Trim();
}

private static IEnumerable<string> AddDots(string[] lines)
{
if (lines.Length == 1)
{
yield return lines[0];
}
else
{
foreach (var (line, index) in lines.Indexed())
{
if (index == 0)
yield return $"{line}..";
else if (index == lines.Length - 1)
yield return $"..{line}";
else
yield return $"..{line}..";
}
}
}

private static IEnumerable<string> Split(string text)
{
var currentPart = "";

foreach (var c in text)
{
currentPart += c;

if (!char.IsLetterOrDigit(c) && c != ' ')
{
if (currentPart != "")
{
yield return currentPart;
currentPart = "";
}
}
}

if (currentPart != "")
yield return currentPart;
}
}
112 changes: 112 additions & 0 deletions AnkiPoetry.Engine/TextWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace AnkiPoetry.Engine;

public static class TextWrapper
{
public static string[] Wrap(string text, int word_wrap, bool wrap_on_spaces)
{
if (word_wrap == -1 || text.Length <= word_wrap)
return [text];

var lines = wrap_on_spaces
? WrapOnSpaces(text, word_wrap)
: WrapOnPunctuation(text, word_wrap);

return [.. AddDots([.. lines])];
}

private static IEnumerable<string> WrapOnPunctuation(string text, int word_wrap)
{
var items = Split(text);

var c = "";

foreach (var item in items)
{
var n = c + item;

if (n.Length > word_wrap)
{
if (c != "")
yield return c.Trim();

c = item;
}
else
{
c = n;
}
}

if (c != "")
yield return c.Trim();
}

private static IEnumerable<string> WrapOnSpaces(string text, int word_wrap)
{
var items = text.Split(' ');

var c = "";

foreach (var item in items)
{
var n = (c == "") ? item : c + " " + item;

if (n.Length > word_wrap)
{
if (c != "")
yield return c.Trim();

c = item;
}
else
{
c = n;
}
}

if (c != "")
yield return c.Trim();
}

private static IEnumerable<string> AddDots(string[] lines)
{
if (lines.Length == 1)
{
yield return lines[0];
}
else
{
foreach (var (line, index) in lines.Indexed())
{
if (index == 0)
yield return $"{line}..";
else if (index == lines.Length - 1)
yield return $"..{line}";
else
yield return $"..{line}..";
}
}
}

private static IEnumerable<string> Split(string text)
{
var currentPart = "";

foreach (var c in text)
{
currentPart += c;

if (!char.IsLetterOrDigit(c) && c != ' ')
{
if (currentPart != "")
{
yield return currentPart;
currentPart = "";
}
}
}

if (currentPart != "")
yield return currentPart;
}
}

0 comments on commit 8a08979

Please sign in to comment.