diff --git a/Epub/KoeBook.Epub/ScrapingAozora.cs b/Epub/KoeBook.Epub/ScrapingAozora.cs index a991c7e..765afda 100644 --- a/Epub/KoeBook.Epub/ScrapingAozora.cs +++ b/Epub/KoeBook.Epub/ScrapingAozora.cs @@ -106,7 +106,6 @@ public async Task ScrapingAsync(string url, string coverFilePath, checkSection(document, chapterNum); document.Chapters[chapterNum].Sections[sectionNum].Elements.Add(new Paragraph()); } - } else if (element.TagName == "DIV") { @@ -180,7 +179,6 @@ public async Task ScrapingAsync(string url, string coverFilePath, } document.Chapters[chapterNum].Sections[sectionNum].Elements.Add(new Paragraph()); } - } } } @@ -251,7 +249,6 @@ public async Task ScrapingAsync(string url, string coverFilePath, } } } - } else if (element.TagName == "IMG") { @@ -333,7 +330,6 @@ public async Task ScrapingAsync(string url, string coverFilePath, paragraph.Text = TextProcess(element) + "の画像"; } } - } else if (element.ClassName == "notes") { @@ -451,7 +447,7 @@ public async Task ScrapingAsync(string url, string coverFilePath, { if (nextNode.NodeType == NodeType.Text) { - if (nextNode.Text() != "\n") + if (!string.IsNullOrWhiteSpace(nextNode.Text())) { previous = true; @@ -490,7 +486,6 @@ public async Task ScrapingAsync(string url, string coverFilePath, paragraph2.Text += split[^1]; } } - } else { @@ -504,48 +499,12 @@ public async Task ScrapingAsync(string url, string coverFilePath, } } + // 末尾の空のparagraphを削除 document.Chapters[^1].Sections[^1].Elements.RemoveAt(document.Chapters[^1].Sections[^1].Elements.Count - 1); - if (checkEpubDocument(document)) - { - Console.WriteLine("Success"); - } - else - { - Console.WriteLine("False"); - } return document; } - private bool checkEpubDocument(EpubDocument document) - { - foreach (var chapter in document.Chapters) - { - foreach (var section in chapter.Sections) - { - foreach (var element in section.Elements) - { - if (element is Paragraph paragraph) - { - if (paragraph.Text == null) - { - Console.WriteLine($"{document.Chapters.IndexOf(chapter)}, {chapter.Sections.IndexOf(section)}, {section.Elements.IndexOf(element)}"); - return false; - } - } - else if (element is Picture picture) - { - if (picture.PictureFilePath == null) - { - Console.WriteLine($"{document.Chapters.IndexOf(chapter)}, {chapter.Sections.IndexOf(section)}, {section.Elements.IndexOf(element)}"); - return false; - } - } - } - } - } - return true; - } private static string TextProcess(IElement element) { @@ -563,7 +522,7 @@ private static string TextProcess(IElement element) { if (node.NodeType == NodeType.Text) { - if (node.Text() != "\n") + if (!string.IsNullOrWhiteSpace(node.Text())) { text += TextReplace(node.Text()); } @@ -587,14 +546,14 @@ private static string TextProcess(IElement element) } else { - if ((item.TextContent != "\n") && (!string.IsNullOrEmpty(item.TextContent))) + if (!string.IsNullOrWhiteSpace(item.TextContent) && (!string.IsNullOrEmpty(item.TextContent))) { text += TextReplace(item.TextContent); } } if (item.NextSibling != null) { - if ((item.NextSibling.TextContent != "\n") && (!string.IsNullOrEmpty(item.NextSibling.TextContent))) + if (!string.IsNullOrWhiteSpace(item.NextSibling.TextContent) && (!string.IsNullOrEmpty(item.NextSibling.TextContent))) { text += TextReplace(item.NextSibling.Text()); } @@ -655,8 +614,5 @@ private static string GetCardUrl(string url) [System.Text.RegularExpressions.GeneratedRegex(@"http.{1,}/([^/]{0,}\.[^/]{1,})")] private static partial System.Text.RegularExpressions.Regex FileUrlToFileName(); - - [System.Text.RegularExpressions.GeneratedRegex(@"(.{1,})(.{1,})")] - private static partial System.Text.RegularExpressions.Regex RubyToText(); } } diff --git a/Epub/KoeBook.Epub/ScrapingHelper.cs b/Epub/KoeBook.Epub/ScrapingHelper.cs index ec342f6..3bb8c59 100644 --- a/Epub/KoeBook.Epub/ScrapingHelper.cs +++ b/Epub/KoeBook.Epub/ScrapingHelper.cs @@ -3,7 +3,7 @@ namespace KoeBook.Epub; -internal static class ScrapingHelper +public static class ScrapingHelper { internal static void checkChapter(EpubDocument document) { @@ -46,6 +46,10 @@ internal static void checkParagraph(EpubDocument document, int chapterNum, int s public static List SplitBrace(string text) { + if (text.Length == 1 && text != "「" && text != "」") + { + return new List() { text }; + } var result = new List(); int bracket = 0; var brackets = new List(); @@ -62,12 +66,12 @@ public static List SplitBrace(string text) brackets[i] -= mn; if (text[i] == '「' && brackets[i] == 1 && i != 0) { - result.Add(text[startIdx..(i - startIdx)]); + result.Add(text[startIdx..i]); startIdx = i; } if (text[i] == '」' && brackets[i] == 0 && i != 0) { - result.Add(text[startIdx..(i - startIdx + 1)]); + result.Add(text[startIdx..(i + 1)]); startIdx = i + 1; } } @@ -75,6 +79,10 @@ public static List SplitBrace(string text) { result.Add(text[startIdx..]); } + if (result[^1] == "") + { + result.RemoveAt(result.Count - 1); + } return result; } diff --git a/KoeBook/ViewModels/MainViewModel.cs b/KoeBook/ViewModels/MainViewModel.cs index 14f9ac4..bda5ed5 100644 --- a/KoeBook/ViewModels/MainViewModel.cs +++ b/KoeBook/ViewModels/MainViewModel.cs @@ -4,6 +4,7 @@ using KoeBook.Core.Contracts.Services; using KoeBook.Core.Models; using KoeBook.Services; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Windows.Storage.Pickers; using WinRT.Interop; @@ -27,7 +28,14 @@ public sealed partial class MainViewModel : ObservableRecipient [NotifyCanExecuteChangedFor(nameof(StartProcessCommand))] private string? _ebookUrl; - private bool CanExecuteStartProcess => EbookFilePath is not null || !string.IsNullOrEmpty(EbookUrl); + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(ErrorTextVisibility))] + [NotifyPropertyChangedFor(nameof(CanExecuteStartProcess))] + private bool _ebookIsValid = true; + + public Visibility ErrorTextVisibility => EbookIsValid ? Visibility.Collapsed : Visibility.Visible; + + private bool CanExecuteStartProcess => EbookFilePath is not null || !string.IsNullOrEmpty(EbookUrl) && EbookIsValid; [ObservableProperty] private bool _skipEdit = true; @@ -135,4 +143,28 @@ static async void CoreAsync(ILocalSettingsService settingsService, bool skipEdit await settingsService.SaveSettingAsync(SkipEditSettingsKey, skipEdit); } } + + partial void OnEbookUrlChanged(string? value) + { + EbookIsValid = IsValid(value); + + static bool IsValid(string? value) + { + if (string.IsNullOrEmpty(value)) + return true; + ReadOnlySpan allowedOrigins = [ + "https://www.aozora.gr.jp" + ]; + + try + { + var uri = new Uri(value); + return allowedOrigins.Contains(uri.GetLeftPart(UriPartial.Authority)); + } + catch (UriFormatException) + { + return false; + } + } + } } diff --git a/KoeBook/Views/MainPage.xaml b/KoeBook/Views/MainPage.xaml index d282124..15af6f8 100644 --- a/KoeBook/Views/MainPage.xaml +++ b/KoeBook/Views/MainPage.xaml @@ -54,11 +54,17 @@ --> - + + Margin="{StaticResource XSmallTopMargin}" + PlaceholderText="https://www.aozora.gr.jp/cards/000035/files/1567_14913.html"/> +