Skip to content

Commit

Permalink
Merge branch 'main' into herring/claudia
Browse files Browse the repository at this point in the history
  • Loading branch information
aiueo-1234 committed Apr 27, 2024
2 parents 47b0c5f + 5d64784 commit e0804df
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 428 deletions.
682 changes: 267 additions & 415 deletions Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
{
switch (child)
{
case { TagName: TagNames.Anchor, Children: [IHtmlImageElement img] } when img.Source is not null:
case { TagName: TagNames.A, Children: [IHtmlImageElement img] } when img.Source is not null:
{
// 画像のダウンロード
var filePath = Path.Combine(imageDirectory, new Uri(img.Source, Options.RawUri).Segments[^1].TrimEnd('/'));
Expand All @@ -143,7 +143,7 @@ public async ValueTask<EpubDocument> ScrapingAsync(string url, string coverFileP
if (!string.IsNullOrWhiteSpace(item.InnerHtml))
lineBuilder.Append(item.InnerHtml);
break;
case { TagName: TagNames.BreakRow }:
case { TagName: TagNames.Br }:
foreach (var split in _splitBraceService.SplitBrace(lineBuilder.ToLinesAndClear()))
{
section.Elements.Add(new Paragraph() { Text = split });
Expand Down
15 changes: 6 additions & 9 deletions Epub/KoeBook.Epub/TagNames.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KoeBook.Epub
namespace KoeBook.Epub
{
internal static class TagNames
{
public const string Anchor = "A";
public const string A = "A";
public const string Br = "BR";
public const string Div = "Div";
public const string Img = "IMG";
public const string Ruby = "RUBY";
public const string BreakRow = "BR";
public const string Span = "SPAN";
}
}
5 changes: 5 additions & 0 deletions KoeBook.Core/Utilities/EnumerableEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public static class EnumerableEx
yield return (current, false, !hasNext);
}
}

public static void RemoveAt<T>(this List<T> list, Index index)
{
list.RemoveAt(index.GetOffset(list.Count));
}
}
62 changes: 62 additions & 0 deletions KoeBook.Test/Epub/ScrapingAozoraServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Runtime.CompilerServices;
using AngleSharp;
using AngleSharp.Dom;
using KoeBook.Epub.Models;
using KoeBook.Epub.Services;

namespace KoeBook.Test.Epub;

public class ScrapingAozoraServiceTest
{
//[Theory]
//[InlineData("", "")]
public async Task TextProcess(string input, string expected)
{
using var context = BrowsingContext.New(Configuration.Default);
using var doc = await context.OpenAsync(req => req.Content(input));
Assert.NotNull(doc.ParentElement);

var result = ScrapingAozora.TextProcess(null, doc.ParentElement!);

Assert.Equal(expected, result);
}

//[Theory]
//[InlineData("", new[] { "" })]
public async Task AddParagraphs1(string input, string[] expected)
{
using var context = BrowsingContext.New(Configuration.Default);
using var doc = await context.OpenAsync(req => req.Content(input));
Assert.NotNull(doc.ParentElement);
var epubDocument = new EpubDocument("title", "author", "", default)
{
Chapters = [new() { Sections = [new("section title") { Elements = [new Paragraph() { Text = "test" }] }] }]
};

Assert.Equal(expected.Length, epubDocument.Chapters[0].Sections[0].Elements.Count);
Assert.All(epubDocument.Chapters[0].Sections[0].Elements.Zip(expected), v =>
{
var (element, expected) = v;
var paragraph = Assert.IsType<Paragraph>(element);
Assert.Equal(expected, paragraph.Text);
});
}
}

file static class ScrapingAozora
{
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
public static extern string TextProcess(ScrapingAozoraService? _, IElement element);

[UnsafeAccessor(UnsafeAccessorKind.Method)]
public static extern void AddParagraphs(ScrapingAozoraService service, List<KoeBook.Epub.Models.Element> focusElements, IElement element, bool lastEmpty);

[UnsafeAccessor(UnsafeAccessorKind.Method)]
public static extern void AddParagraphs(ScrapingAozoraService service, List<KoeBook.Epub.Models.Element> focusElements, string input, bool lastEmpty);

[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
public static extern string TextReplace(ScrapingAozoraService? _, string text);

[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
public static extern (List<int> contentsIds, bool hasChapter, bool hasSection) LoadToc(ScrapingAozoraService? _, IDocument doc, EpubDocument epubDocument);
}
1 change: 1 addition & 0 deletions KoeBook/Services/CoreMocks/AnalyzerServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class AnalyzerServiceMock(IDisplayStateChangeService stateService) : IAna

public async ValueTask<BookScripts> AnalyzeAsync(BookProperties bookProperties, string tempDirectory, CancellationToken cancellationToken)
{
Directory.CreateDirectory(tempDirectory);
DisplayStateChanging stateChanging;
if (bookProperties.SourceType == SourceType.Url)
{
Expand Down
8 changes: 6 additions & 2 deletions KoeBook/Services/GenerationTaskRunnerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private async ValueTask RunAsync(GenerationTask task)
return;

await RunAsyncCore(task, true);
await RunAsyncCore(task, false);
if (task.SkipEdit)
await RunAsyncCore(task, false);
}

public async void RunGenerateEpubAsync(GenerationTask task)
Expand Down Expand Up @@ -83,7 +84,10 @@ private async ValueTask RunAsyncCore(GenerationTask task, bool firstStep)
task.Progress = 1;
task.MaximumProgress = 1;
var fileName = Path.GetFileName(resultPath);
File.Move(resultPath, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "KoeBook", fileName), true);
var resultDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "KoeBook");
if (!Directory.Exists(resultDirectory))
Directory.CreateDirectory(resultDirectory);
File.Move(resultPath, Path.Combine(resultDirectory, fileName), true);
}
else
throw new InvalidOperationException();
Expand Down

0 comments on commit e0804df

Please sign in to comment.