From 7c74a35d801928b011f29bcf658a486d0d8a4043 Mon Sep 17 00:00:00 2001 From: TakenPt Date: Sun, 28 Apr 2024 17:29:35 +0900 Subject: [PATCH] =?UTF-8?q?#36=20=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=81=BF?= =?UTF-8?q?=E3=82=B5=E3=83=BC=E3=83=93=E3=82=B9=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ScrapingClaudeStroyService.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs diff --git a/Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs b/Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs new file mode 100644 index 0000000..715d7d7 --- /dev/null +++ b/Epub/KoeBook.Epub/Services/ScrapingClaudeStroyService.cs @@ -0,0 +1,88 @@ +using System.Xml; +using System.Xml.Linq; +using System.Xml.Schema; +using KoeBook.Epub.Models; +using KoeBook.Core; +using System.Xml.Serialization; +using KoeBook.Core.Utilities; +using KoeBook.Epub.Contracts.Services; +using KoeBook.Core.Models; + +namespace KoeBook.Epub.Services +{ + public partial class ScrapingClaudeStroyService(ISplitBraceService splitBraceService) + { + + private readonly ISplitBraceService _splitBraceService = splitBraceService; + public async ValueTask ScrapingAsync(string xmlText, string coverFillePath, string imgDirectory, Guid id, CancellationToken ct) + { + if (!FitXmlSchema(xmlText)) + throw new EbookException(ExceptionType.XmlSchemaUnfulfilled); + + var book = LoadBook(xmlText); + + var paragraphbuilder = new SplittedLineBuilder(); + var scriptLineBuilder = new SplittedLineBuilder(); + + var document = new EpubDocument(book.Title, "", coverFillePath, id); + document.Chapters.Add(new Chapter()); + foreach (var section in book.Content.Sections) + { + var docSection = new Section(section.Title ?? ""); + foreach (var paragraph in section.Paragraphs) + { + foreach (var item in paragraph.Texts) + { + switch (item) + { + case Text text: + paragraphbuilder.Append(text.InnerText); + scriptLineBuilder.Append(text.InnerText); + break; + case Ruby ruby: + paragraphbuilder.Append($"{ruby.Rb}({ruby.Rt})"); + scriptLineBuilder.Append($"{ruby.Rt}"); + break; + default: + throw new EbookException(ExceptionType.XmlSchemaUnfulfilled); + } + } + + foreach ((var textSplit, var scriptSplit) in + _splitBraceService.SplitBrace(paragraphbuilder.ToLinesAndClear()).Zip( + _splitBraceService.SplitBrace(scriptLineBuilder.ToLinesAndClear()) + ) + ) + { + docSection.Elements.Add(new Paragraph() { Text = textSplit, ScriptLine = new ScriptLine(scriptSplit, "", "") }); + } + } + document.Chapters.Single().Sections.Add(docSection); + } + return document; + } + + internal bool FitXmlSchema(string xmlText) + { + // スキーマファイルのパスを入れる。 + using var xsdFs = new FileStream(@"", FileMode.Open); + XmlSchemaSet schema = new XmlSchemaSet(); + schema.Add("", XmlReader.Create(xsdFs)); + + XDocument xml = XDocument.Parse(xmlText); + + bool errorExist = false; + xml.Validate(schema, (o, e) => errorExist = true); + + return !errorExist; + } + + internal Book LoadBook(string xmlText) + { + using var xmlStringReader = new StringReader(xmlText); + var serializer = new XmlSerializer(typeof(Book)); + return (Book)serializer.Deserialize(xmlStringReader) + ?? throw new EbookException(ExceptionType.XmlSchemaUnfulfilled); + } + } +}