Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into feat/#31-2
Browse files Browse the repository at this point in the history
  • Loading branch information
herring101 committed Feb 24, 2024
2 parents a9cd34a + 8f66349 commit 04c904d
Show file tree
Hide file tree
Showing 18 changed files with 852 additions and 36 deletions.
13 changes: 13 additions & 0 deletions Epub/KoeBook.Epub/EpubDocumentException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KoeBook.Epub
{
public class EpubDocumentException : Exception
{
public EpubDocumentException(string? message) : base(message) { }
}
}
2 changes: 2 additions & 0 deletions Epub/KoeBook.Epub/KoeBook.Epub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="NAudio" Version="2.2.1" />
</ItemGroup>

Expand Down
662 changes: 662 additions & 0 deletions Epub/KoeBook.Epub/ScrapingAozora.cs

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions Epub/KoeBook.Epub/ScrapingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Net;
using System.Reflection.Metadata;

namespace KoeBook.Epub;

internal static class ScrapingHelper
{
internal static void checkChapter(EpubDocument document)
{
if (document.Chapters.Count == 0)
{
document.Chapters.Add(new Chapter() { Title = null });
}
return;
}

internal static void checkSection(EpubDocument document, int ChapterNum)
{

checkChapter(document);

if (document.Chapters[ChapterNum].Sections.Count == 0)
{
if (document.Chapters[ChapterNum].Title != null)
{
document.Chapters[ChapterNum].Sections.Add(new Section(document.Chapters[ChapterNum].Title!));
}
else
{
document.Chapters[ChapterNum].Sections.Add(new Section(document.Title));
}

}
return;
}

internal static void checkParagraph(EpubDocument document, int chapterNum, int sectionNum)
{
checkSection(document, chapterNum);
if (document.Chapters[chapterNum].Sections[sectionNum].Elements.Count == 0)
{
document.Chapters[chapterNum].Sections[sectionNum].Elements.Add(new Paragraph());
}
return;
}

public static List<string> SplitBrace(string text)
{
var result = new List<string>();
int bracket = 0;
var brackets = new List<int>();
foreach (char c in text)
{
if (c == '「') bracket++;
if (c == '」') bracket--;
brackets.Add(bracket);
}
var mn = Math.Min(0, brackets.Min());
int startIdx = 0;
for (int i = 0; i < brackets.Count; i++)
{
brackets[i] -= mn;
if (text[i] == '「' && brackets[i] == 1 && i != 0)
{
result.Add(text[startIdx..(i - startIdx)]);
startIdx = i;
}
if (text[i] == '」' && brackets[i] == 0 && i != 0)
{
result.Add(text[startIdx..(i - startIdx + 1)]);
startIdx = i + 1;
}
}
if (startIdx != text.Length - 1)
{
result.Add(text[startIdx..]);
}

return result;
}
}
2 changes: 1 addition & 1 deletion Epub/KoeBook.Epub/Service/IScrapingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IScrapingService
{
public Task<EpubDocument> ScrapingAsync(string url, string coverFil9lePath, Guid id, CancellationToken ct);
public Task<EpubDocument> ScrapingAsync(string url, string coverFillePath, string imageDirectory, Guid id, CancellationToken ct);
}
1 change: 0 additions & 1 deletion KoeBook.Test/KoeBook.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

<ItemGroup>
<ProjectReference Include="..\Epub\KoeBook.Epub\KoeBook.Epub.csproj" />
<ProjectReference Include="..\KoeBook.Common\KoeBook.Common.csproj" />
<ProjectReference Include="..\KoeBook.Core\KoeBook.Core.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="FastEnum" Version="1.8.0" />
<PackageReference Include="ReactiveProperty" Version="9.4.1" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions KoeBook.Unsafe/NativeMethods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json",
"public": true
}
1 change: 1 addition & 0 deletions KoeBook.Unsafe/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MessageBox
38 changes: 19 additions & 19 deletions KoeBook.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook", "KoeBook\KoeBook.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Core", "KoeBook.Core\KoeBook.Core.csproj", "{50444E76-C6A7-40AF-879C-0AD88A287510}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Common", "KoeBook.Common\KoeBook.Common.csproj", "{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Epub", "Epub\KoeBook.Epub\KoeBook.Epub.csproj", "{1DE55F58-E4F3-4077-A241-AFFD736A2B01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KoeBook.Test", "KoeBook.Test\KoeBook.Test.csproj", "{67CEB31C-B026-499A-83B4-528914EABDBF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoeBook.Test", "KoeBook.Test\KoeBook.Test.csproj", "{67CEB31C-B026-499A-83B4-528914EABDBF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KoeBook.Unsafe", "KoeBook.Unsafe\KoeBook.Unsafe.csproj", "{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -65,22 +65,6 @@ Global
{50444E76-C6A7-40AF-879C-0AD88A287510}.Release|x64.Build.0 = Release|x64
{50444E76-C6A7-40AF-879C-0AD88A287510}.Release|x86.ActiveCfg = Release|x86
{50444E76-C6A7-40AF-879C-0AD88A287510}.Release|x86.Build.0 = Release|x86
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|arm64.ActiveCfg = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|arm64.Build.0 = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|x64.ActiveCfg = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|x64.Build.0 = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|x86.ActiveCfg = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Debug|x86.Build.0 = Debug|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|Any CPU.Build.0 = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|arm64.ActiveCfg = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|arm64.Build.0 = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|x64.ActiveCfg = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|x64.Build.0 = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|x86.ActiveCfg = Release|Any CPU
{1E5B40AE-1D42-40D0-85D1-E921B17BFA69}.Release|x86.Build.0 = Release|Any CPU
{1DE55F58-E4F3-4077-A241-AFFD736A2B01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DE55F58-E4F3-4077-A241-AFFD736A2B01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DE55F58-E4F3-4077-A241-AFFD736A2B01}.Debug|arm64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -113,6 +97,22 @@ Global
{67CEB31C-B026-499A-83B4-528914EABDBF}.Release|x64.Build.0 = Release|Any CPU
{67CEB31C-B026-499A-83B4-528914EABDBF}.Release|x86.ActiveCfg = Release|Any CPU
{67CEB31C-B026-499A-83B4-528914EABDBF}.Release|x86.Build.0 = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|arm64.ActiveCfg = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|arm64.Build.0 = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|x64.ActiveCfg = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|x64.Build.0 = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|x86.ActiveCfg = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Debug|x86.Build.0 = Debug|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|Any CPU.Build.0 = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|arm64.ActiveCfg = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|arm64.Build.0 = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|x64.ActiveCfg = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|x64.Build.0 = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|x86.ActiveCfg = Release|Any CPU
{30AE8B29-D2D1-4D46-9A5E-68A3F4339892}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 24 additions & 4 deletions KoeBook/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using KoeBook.Activation;
using FastEnumUtility;
using KoeBook.Activation;
using KoeBook.Components.Dialog;
using KoeBook.Contracts.Services;
using KoeBook.Core;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Services;
using KoeBook.Core.Services.Mocks;
Expand All @@ -14,7 +16,10 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using Microsoft.Extensions.Http;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using WinRT.Interop;

namespace KoeBook;

Expand Down Expand Up @@ -122,8 +127,23 @@ public App()

private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
var hwnd = WindowNative.GetWindowHandle(MainWindow);
if (e.Exception is EbookException ebookException)
{
PInvoke.MessageBox((HWND)hwnd,
$"ラーが発生しました。KoeBookを終了します。\n{ebookException.ExceptionType.GetEnumMemberValue()}",
"KoeBookからのお知らせ",
MESSAGEBOX_STYLE.MB_OK | MESSAGEBOX_STYLE.MB_ICONWARNING);
}
else
{
PInvoke.MessageBox((HWND)hwnd,
$"不明なエラーが発生しました。KoeBookを終了します。\n{e.Exception.Message}\n\n{e.Exception.StackTrace}",
"KoeBookからのお知らせ",
MESSAGEBOX_STYLE.MB_OK | MESSAGEBOX_STYLE.MB_ICONWARNING);
}
e.Handled = true;
Current.Exit();
}

protected override async void OnLaunched(LaunchActivatedEventArgs args)
Expand Down
11 changes: 11 additions & 0 deletions KoeBook/Components/Dialog/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ public Task<ContentDialogResult> ShowAsync(
{
return ShowAsync(title, new DialogContentControl(content), primaryText, closeText, defaultButton, cancellationToken);
}

public Task<ContentDialogResult> ShowInfoAsync(string title, string content, string primaryText, CancellationToken cancellationToken)
{
var dialog = new SharedContentDialog(title, primaryText, null, ContentDialogButton.Primary)
{
XamlRoot = App.MainWindow.Content.XamlRoot,
Content = new DialogContentControl(content),
RequestedTheme = _themeSelectorService.Theme
};
return dialog.ShowAsync().AsTask(cancellationToken);
}
}
2 changes: 1 addition & 1 deletion KoeBook/Components/Dialog/SharedContentDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace KoeBook.Components.Dialog;

public sealed partial class SharedContentDialog : ContentDialog
{
public SharedContentDialog(string title, string primaryText, string closeText, ContentDialogButton defaultButton)
public SharedContentDialog(string title, string primaryText, string? closeText, ContentDialogButton defaultButton)
{
Title = title;
PrimaryButtonText = primaryText;
Expand Down
6 changes: 6 additions & 0 deletions KoeBook/Contracts/Services/IDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Task<ContentDialogResult> ShowAsync(
string closeText,
ContentDialogButton defaultButton,
CancellationToken cancellationToken);

Task<ContentDialogResult> ShowInfoAsync(
string title,
string content,
string primaryText,
CancellationToken cancellationToken);
}

public static class DialogServiceEx
Expand Down
1 change: 1 addition & 0 deletions KoeBook/KoeBook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ItemGroup>
<ProjectReference Include="..\KoeBook.Core\KoeBook.Core.csproj" />
<ProjectReference Include="..\Epub\KoeBook.Epub\KoeBook.Epub.csproj" />
<ProjectReference Include="..\KoeBook.Unsafe\KoeBook.Unsafe.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
Expand Down
25 changes: 20 additions & 5 deletions KoeBook/Services/GenerationTaskRunnerService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using KoeBook.Contracts.Services;
using FastEnumUtility;
using KoeBook.Contracts.Services;
using KoeBook.Core;
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Models;
using KoeBook.Models;
Expand All @@ -9,19 +11,22 @@ namespace KoeBook.Services;
public class GenerationTaskRunnerService
{
private readonly IGenerationTaskService _taskService;

private readonly IAnalyzerService _analyzerService;

private readonly IEpubGenerateService _epubGenService;

private readonly IDialogService _dialogService;
private readonly string _tempFolder = ApplicationData.Current.TemporaryFolder.Path;

public GenerationTaskRunnerService(IGenerationTaskService taskService, IAnalyzerService analyzerService, IEpubGenerateService epubGenService)
public GenerationTaskRunnerService(
IGenerationTaskService taskService,
IAnalyzerService analyzerService,
IEpubGenerateService epubGenService,
IDialogService dialogService)
{
_taskService = taskService;
_taskService.OnTasksChanged += TasksChanged;
_analyzerService = analyzerService;
_epubGenService = epubGenService;
_dialogService = dialogService;
}

private async void TasksChanged(GenerationTask task, ChangedEvents changedEvents)
Expand Down Expand Up @@ -62,6 +67,11 @@ private async ValueTask RunAsync(GenerationTask task)
{
task.State = GenerationState.Failed;
}
catch (EbookException e)
{
task.State = GenerationState.Failed;
await _dialogService.ShowInfoAsync("生成失敗", e.ExceptionType.GetEnumMemberValue()!, "OK", default);
}
catch
{
task.State = GenerationState.Failed;
Expand All @@ -83,6 +93,11 @@ public async void RunGenerateEpubAsync(GenerationTask task)
{
task.State = GenerationState.Failed;
}
catch (EbookException e)
{
task.State = GenerationState.Failed;
await _dialogService.ShowInfoAsync("生成失敗", e.ExceptionType.GetEnumMemberValue()!, "OK", default);
}
catch
{
task.State = GenerationState.Failed;
Expand Down
2 changes: 1 addition & 1 deletion KoeBook/ViewModels/GenerationTaskViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private async Task StopTask(CancellationToken cancellationToken)
if (Task is null)
return;

var result = await _dialogService.ShowAsync("KoeBookからのお知らせ", "実行中のタスクをキャンセルし、削除します。この操作は戻せません。", "削除", cancellationToken);
var result = await _dialogService.ShowAsync("タスクを削除します", "実行中のタスクをキャンセルし、削除します。この操作は戻せません。", "削除", cancellationToken);
if (result != ContentDialogResult.Primary)
return;
_runner.StopTask(Task);
Expand Down
2 changes: 1 addition & 1 deletion KoeBook/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async void BeforeTextChanging(TextBox _, TextBoxBeforeTextChangingEventAr
if (EbookFilePath is null || string.IsNullOrEmpty(args.NewText))
return;

var result = await _dialogService.ShowAsync("外部URLから使用する場合、ローカルファイルは無視されます。", default);
var result = await _dialogService.ShowAsync("ローカルファイルを無視します", "外部URLから使用する場合、ローカルファイルは無視されます。", default);
if (result == ContentDialogResult.Primary)
{
EbookFilePath = null;
Expand Down

0 comments on commit 04c904d

Please sign in to comment.