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/#42
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPt committed Feb 24, 2024
2 parents 3416322 + 6396cb3 commit 212ece4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
54 changes: 5 additions & 49 deletions Epub/KoeBook.Epub/ScrapingAozora.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
checkSection(document, chapterNum);
document.Chapters[chapterNum].Sections[sectionNum].Elements.Add(new Paragraph());
}

}
else if (element.TagName == "DIV")
{
Expand Down Expand Up @@ -180,7 +179,6 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
}
document.Chapters[chapterNum].Sections[sectionNum].Elements.Add(new Paragraph());
}

}
}
}
Expand Down Expand Up @@ -251,7 +249,6 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
}
}
}

}
else if (element.TagName == "IMG")
{
Expand Down Expand Up @@ -333,7 +330,6 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
paragraph.Text = TextProcess(element) + "の画像";
}
}

}
else if (element.ClassName == "notes")
{
Expand Down Expand Up @@ -451,7 +447,7 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
{
if (nextNode.NodeType == NodeType.Text)
{
if (nextNode.Text() != "\n")
if (!string.IsNullOrWhiteSpace(nextNode.Text()))
{
previous = true;

Expand Down Expand Up @@ -490,7 +486,6 @@ public async Task<EpubDocument> ScrapingAsync(string url, string coverFilePath,
paragraph2.Text += split[^1];
}
}

}
else
{
Expand All @@ -504,48 +499,12 @@ public async Task<EpubDocument> 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)
{
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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(@"<ruby><rb>(.{1,})</rb><rp>(</rp><rt>(.{1,})</rt><rp>)</rp></ruby>")]
private static partial System.Text.RegularExpressions.Regex RubyToText();
}
}
14 changes: 11 additions & 3 deletions Epub/KoeBook.Epub/ScrapingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace KoeBook.Epub;

internal static class ScrapingHelper
public static class ScrapingHelper
{
internal static void checkChapter(EpubDocument document)
{
Expand Down Expand Up @@ -46,6 +46,10 @@ internal static void checkParagraph(EpubDocument document, int chapterNum, int s

public static List<string> SplitBrace(string text)
{
if (text.Length == 1 && text != "「" && text != "」")
{
return new List<string>() { text };
}
var result = new List<string>();
int bracket = 0;
var brackets = new List<int>();
Expand All @@ -62,19 +66,23 @@ public static List<string> 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;
}
}
if (startIdx != text.Length - 1)
{
result.Add(text[startIdx..]);
}
if (result[^1] == "")
{
result.RemoveAt(result.Count - 1);
}

return result;
}
Expand Down
34 changes: 33 additions & 1 deletion KoeBook/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<string> allowedOrigins = [
"https://www.aozora.gr.jp"
];

try
{
var uri = new Uri(value);
return allowedOrigins.Contains(uri.GetLeftPart(UriPartial.Authority));
}
catch (UriFormatException)
{
return false;
}
}
}
}
10 changes: 8 additions & 2 deletions KoeBook/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@
</StackPanel>-->

<StackPanel>
<TextBlock Text="青空文庫を読み上げる"/>
<TextBlock Text="Webページを読み上げる (青空文庫のみ対応)"/>
<TextBox
Text="{x:Bind ViewModel.EbookUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BeforeTextChanging="{x:Bind ViewModel.BeforeTextChanging}"
Margin="{StaticResource XSmallTopMargin}" />
Margin="{StaticResource XSmallTopMargin}"
PlaceholderText="https://www.aozora.gr.jp/cards/000035/files/1567_14913.html"/>
<TextBlock
Visibility="{x:Bind ViewModel.ErrorTextVisibility, Mode=OneWay}"
Text="青空文庫のURLを入力してください"
Style="{ThemeResource CaptionTextBlockStyle}"
Foreground="{ThemeResource SystemErrorTextColor}"/>
</StackPanel>

<Button
Expand Down

0 comments on commit 212ece4

Please sign in to comment.