Skip to content

Commit

Permalink
StoringStorerの作成、
Browse files Browse the repository at this point in the history
AddText, GetTextから、StoringStorerへの変更
  • Loading branch information
TakenPt committed Mar 17, 2024
1 parent 40b64a0 commit 607adaa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
3 changes: 0 additions & 3 deletions Epub/KoeBook.Epub/Contracts/Services/ISplitBraceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ public interface ISplitBraceService
{
List<string> SplitBrace(string text);
List<string> SplitBrace(List<string> texts);
void AddText(string text);
void AddText(List<string> texts);
List<string> GetText();
}
13 changes: 8 additions & 5 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using AngleSharp.Html.Dom;
using AngleSharp.Io;
using KoeBook.Core;
using KoeBook.Core.Utility;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -137,6 +138,8 @@ private record SectionWithChapterTitle(string? title, Section section);

private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
{
var store = new StringStorer();

var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
var doc = await context.OpenAsync(url, ct).ConfigureAwait(false);
Expand Down Expand Up @@ -183,7 +186,7 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool
{
if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
_splitBraceService.AddText(item.InnerHtml);
store.Store(item.InnerHtml);
}
}
else if (item.ChildElementCount == 1)
Expand Down Expand Up @@ -217,12 +220,12 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool
{
if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
_splitBraceService.AddText(item.InnerHtml);
store.Store(item.InnerHtml);
}
}
else if (item.Children[0] is IHtmlBreakRowElement)
{
foreach (var split in _splitBraceService.SplitBrace(_splitBraceService.GetText()))
foreach (var split in _splitBraceService.SplitBrace(store.Release()))
{
section.Elements.Add(new Paragraph() { Text = split });
}
Expand All @@ -247,10 +250,10 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool

if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
_splitBraceService.AddText(item.InnerHtml);
store.Store(item.InnerHtml);
}
}
foreach (var split in _splitBraceService.SplitBrace(_splitBraceService.GetText()))
foreach (var split in _splitBraceService.SplitBrace(store.Release()))
{
section.Elements.Add(new Paragraph() { Text = split });
}
Expand Down
27 changes: 0 additions & 27 deletions Epub/KoeBook.Epub/Services/SplitBraceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace KoeBook.Epub.Services;

public class SplitBraceService : ISplitBraceService
{
private List<StringBuilder> stringBuilders = new();

public List<string> SplitBrace(string text)
{
// textが空白だった時 paragraph を挿入する処理をスキップ
Expand Down Expand Up @@ -59,29 +57,4 @@ public List<string> SplitBrace(List<string> texts)
}
return result;
}

public void AddText(string text)
{
stringBuilders[^1].Append(text);
}

public void AddText(List<string> texts)
{
stringBuilders[^1].Append(texts[0]);
for (int i = 1; i < texts.Count; i++)
{
stringBuilders.Add(new StringBuilder(texts[i]));
}
}

public List<string> GetText()
{
List<string> result = new List<string>();
foreach (StringBuilder stringBuilder in stringBuilders)
{
result.Add(stringBuilder.ToString());
}
stringBuilders.Clear();
return result;
}
}
25 changes: 25 additions & 0 deletions KoeBook.Core/Utility/StringStorer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KoeBook.Core.Utility
{
public class StringStorer
{
private StringBuilder _stringBuilder = new();

public void Store(string text)
{
_stringBuilder.Append(text);
}

public string Release()
{
var result = _stringBuilder.ToString();
_stringBuilder.Clear();
return result;
}
}
}

0 comments on commit 607adaa

Please sign in to comment.