+{
+ private static readonly string[] OptionalEngines = ["pandoc", "libreoffice", "calibre"];
+ private const string Title = "The Lighthouse Keeper";
+ private const string Author = "Ada Lovelace";
+ private const string Line1 = "Ships passed safely through the narrow strait.";
+ private const string Accented = "Café, naïve — “quoted” €5";
+ private const string Korean = "등대지기의 밤";
+
+ private static CancellationToken Token => TestContext.Current.CancellationToken;
+
+ ///
+ /// A book of two chapters with a sub-heading, bold and italic text, a picture, a link to the second chapter, a
+ /// list, a table, a footnote and characters from beyond ASCII (Korean too unless ).
+ ///
+ private Book SampleBook(bool latinOnly = false)
+ {
+ var folder = fx.NewFolder();
+ using (var jpeg = new MagickImage(MagickColors.Teal, 40, 30))
+ jpeg.Write(Path.Combine(folder, "pic.jpg"), MagickFormat.Jpeg);
+ var html = $"""
+ {Title}
+ Chapter One
+ The lamp was lit at dusk every evening. {Accented}{(latinOnly ? "" : " " + Korean)}
+ 
+ Go to the second chapter. Back\slash and <footnote> text.
+
+
+ A Section
+ Water is H2O.
+ Chapter Two
+ {Line1}
+
+ """;
+ var source = Path.Combine(folder, "book.html");
+ File.WriteAllText(source, html, new UTF8Encoding(false));
+ var document = HtmlReader.Read(source, Path.Combine(folder, "media"));
+ var note = new HNote(endnote: false);
+ var noteParagraph = new HParagraph();
+ noteParagraph.Inlines.Add(new HText("A note at the end.", default));
+ note.Blocks.Add(noteParagraph);
+ document.Sections[^1].Blocks.OfType().Last().Inlines.Add(note);
+ var metadata = new BookMetadata { Title = Title, Language = "en", Publisher = "Harbour Press", Description = "A short story." };
+ metadata.Authors.Add(Author);
+ return new Book(document, metadata);
+ }
+
+ private static string Text(HDocument document) => PlainTextWriter.Write(document);
+
+ private static IEnumerable Inlines(HDocument document) => document.Sections.SelectMany(s => HDocumentWalker.Inlines(s.Blocks));
+
+ private static List Headings(HDocument document, int level) =>
+ [.. document.Sections.SelectMany(s => s.Blocks).OfType().Where(p => p.HeadingLevel == level).Select(p => HDocumentWalker.Text(p.Inlines))];
+
+ private string Output(string name) => Path.Combine(fx.NewFolder(), name);
+
+ /// Hands out paths in one folder, as the job queue does for the final output.
+ private sealed class FolderAllocator(string folder, string name) : IOutputAllocator
+ {
+ public string? Allocate(string extension, string? suffix = null) =>
+ Path.Combine(folder, name + suffix + (extension.Length > 0 ? "." + extension : ""));
+ }
+
+ // ───────────────────────── Routes ─────────────────────────
+
+ [Fact]
+ public void Legacy_formats_are_written_without_calibre_and_calibre_ranked_first_takes_epub()
+ {
+ var builtIn = new ConverterCatalog(fx.Converters.Where(c => !OptionalEngines.Contains(c.Id))) { Priority = fx.Catalog.Priority }.CreatePlanner(["rhwp"]);
+ foreach (var to in EbookConverter.LegacyWriters)
+ foreach (var from in new[] { "epub", "mobi", "azw3", "fb2", "htmlz", "chm", "pdb", "pml", "tcr", "rb", "snb", "oeb", "md", "html", "txt", "docx" })
+ if (from != to)
+ Assert.Equal("ebook", Assert.Single(builtIn.Plan(from, to)!.Steps).Converter.Id);
+
+ var withCalibre = fx.Catalog.CreatePlanner(["calibre", "rhwp"]);
+ foreach (var to in EbookConverter.LegacyWriters)
+ Assert.Equal("ebook", Assert.Single(withCalibre.Plan("epub", to)!.Steps).Converter.Id);
+
+ var order = fx.Catalog.Priority.Where(id => id != "calibre").Prepend("calibre").ToList();
+ var calibreFirst = new ConverterCatalog(fx.Converters) { Priority = order }.CreatePlanner(["calibre"]);
+ foreach (var to in EbookConverter.LegacyWriters)
+ Assert.Equal("calibre", Assert.Single(calibreFirst.Plan("epub", to)!.Steps).Converter.Id);
+ }
+
+ [Theory]
+ [InlineData("pdb")]
+ [InlineData("pml")]
+ [InlineData("tcr")]
+ [InlineData("rb")]
+ [InlineData("snb")]
+ public async Task Epub_converts_to_each_format_and_reads_back(string format)
+ {
+ var epub = EbookBuilders.Epub(fx.NewFolder());
+ await using var queue = new JobQueue(new ConverterCatalog(fx.Converters.Where(c => !OptionalEngines.Contains(c.Id))) { Priority = fx.Catalog.Priority });
+ var job = new ConversionJob([epub], new Preset { TargetFormat = format }, format);
+
+ await queue.RunAsync(job);
+
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ var output = Assert.Single(job.Outputs);
+ Assert.EndsWith("." + format, output);
+ var book = format switch
+ {
+ "pdb" => PdbReader.ReadBook(output, fx.NewFolder(), Token),
+ "pml" => PmlReader.ReadBook(output, fx.NewFolder(), Token),
+ "tcr" => TcrReader.ReadBook(output, fx.NewFolder(), Token),
+ "rb" => RocketBookReader.ReadBook(output, fx.NewFolder(), Token),
+ _ => SnbReader.ReadBook(output, fx.NewFolder(), Token),
+ };
+ var source = EpubReader.ReadBook(epub, fx.NewFolder(), Token).Document;
+ var expected = Headings(source, 1).Concat(Headings(source, 2)).Take(3);
+ var text = Text(book.Document);
+ foreach (var heading in expected)
+ Assert.Contains(heading, text);
+ if (format is "pml" or "rb" or "snb")
+ Assert.NotEmpty(Inlines(book.Document).OfType());
+ if (format is "rb" or "snb")
+ Assert.Equal("별빛 이야기", book.Metadata.Title);
+ if (format == "pml")
+ Assert.True(Directory.Exists(Path.ChangeExtension(output, null) + PmlPictures.FolderSuffix), "the pictures sit beside the book");
+ }
+
+ // ───────────────────────── PDB ─────────────────────────
+
+ [Fact]
+ public void Pdb_reads_back_with_its_text_title_and_bookmarks()
+ {
+ var book = SampleBook();
+ var path = Output("book.pdb");
+ PdbWriter.Write(book, path, path, Token);
+
+ var read = PdbReader.ReadBook(path, fx.NewFolder(), Token);
+ Assert.Equal(Title, read.Metadata.Title);
+ var expected = LegacyWriting.PlainText(book.Document, "\n");
+ var database = PalmDatabase.Open(File.ReadAllBytes(path));
+ Assert.Equal(expected, Encoding.UTF8.GetString(MobiReader.PalmDocText(database)));
+ var text = Text(read.Document);
+ foreach (var part in new[] { "Chapter One", Accented, Korean, "First item", Line1, "A note at the end." })
+ Assert.Contains(part, text);
+
+ Assert.Equal("TEXtREAd", database.TypeCreator);
+ var header = database.Record(0);
+ Assert.Equal(16, header.Length);
+ Assert.Equal(2, PalmDatabase.U16(header, 0)); // PalmDOC compression
+ var length = Encoding.UTF8.GetByteCount(expected);
+ Assert.Equal(length, PalmDatabase.U32(header, 4));
+ var textRecords = (length + 4095) / 4096;
+ Assert.Equal(textRecords, PalmDatabase.U16(header, 8));
+ Assert.Equal(4096, PalmDatabase.U16(header, 10));
+ // Bookmarks after the text: Chapter One, A Section, Chapter Two, each at its heading's byte offset.
+ Assert.Equal(1 + textRecords + 3, database.Count);
+ var bytes = Encoding.UTF8.GetBytes(expected);
+ var names = new List();
+ for (var i = 1 + textRecords; i < database.Count; i++)
+ {
+ var record = database.Record(i);
+ Assert.Equal(20, record.Length);
+ var name = Encoding.Latin1.GetString(record[..16]).TrimEnd('\0');
+ names.Add(name);
+ var position = (int)PalmDatabase.U32(record, 16);
+ Assert.StartsWith(name, Encoding.UTF8.GetString(bytes, position, bytes.Length - position));
+ }
+ Assert.Equal(["Chapter One", "A Section", "Chapter Two"], names);
+ }
+
+ [Fact]
+ public void Pdb_and_tcr_text_is_windows_1252_when_it_fits_and_utf8_otherwise()
+ {
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+ var latin = new Book(PlainText.ToDocument($"{Accented}\n\n{Line1}\n"), new BookMetadata { Title = Title });
+ var pdb = Output("latin.pdb");
+ PdbWriter.Write(latin, pdb, pdb, Token);
+ Assert.Equal(Encoding.GetEncoding(1252).GetBytes($"{Accented}\n\n{Line1}\n"), MobiReader.PalmDocText(PalmDatabase.Open(File.ReadAllBytes(pdb))));
+ Assert.Contains(Accented, Text(PdbReader.ReadBook(pdb, fx.NewFolder(), Token).Document));
+
+ var tcr = Output("latin.tcr");
+ TcrWriter.Write(latin, tcr, Token);
+ Assert.Equal(Encoding.GetEncoding(1252).GetBytes($"{Accented}\r\n\r\n{Line1}\r\n"), TcrReader.Decompress(File.ReadAllBytes(tcr), Token));
+
+ var korean = SampleBook();
+ TcrWriter.Write(korean, tcr, Token);
+ Assert.Equal(Encoding.UTF8.GetBytes(LegacyWriting.PlainText(korean.Document, "\r\n")), TcrReader.Decompress(File.ReadAllBytes(tcr), Token));
+ Assert.Contains(Korean, Text(TcrReader.ReadBook(tcr, fx.NewFolder(), Token).Document));
+ }
+
+ // ───────────────────────── TCR ─────────────────────────
+
+ [Fact]
+ public void Tcr_reads_back_exactly_with_a_dictionary_that_compresses()
+ {
+ var book = SampleBook();
+ var path = Output("book.tcr");
+ TcrWriter.Write(book, path, Token);
+
+ var read = TcrReader.ReadBook(path, fx.NewFolder(), Token);
+ foreach (var part in new[] { "Chapter Two", Accented, Korean, Line1 })
+ Assert.Contains(part, Text(read.Document));
+
+ // A long text: every byte it uses has an entry, all 256 codes are used, and it shrinks well.
+ var story = Encoding.UTF8.GetBytes(string.Concat(Enumerable.Range(1, 3000).Select(i => $"Line {i}: the keeper climbed the stairs and trimmed the wick. 등대 {i % 7}\r\n")));
+ var tcr = TcrWriter.Compress(story, Token);
+ Assert.Equal(story, TcrReader.Decompress(tcr, Token));
+ Assert.True(tcr.AsSpan().StartsWith("!!8-Bit!!"u8));
+ var entries = TcrWriter.Dictionary(story, Token);
+ Assert.Equal(256, entries.Count);
+ Assert.All(story.Distinct(), b => Assert.Contains(entries, e => e is [var only] && only == b));
+ Assert.All(entries, e => Assert.InRange(e.Length, 1, 255));
+ Assert.True(tcr.Length < story.Length / 2, $"{tcr.Length} of {story.Length} bytes");
+
+ // Every byte value: no code is left for pairs, and the text still comes back.
+ var all = Enumerable.Range(0, 512).Select(i => (byte)i).ToArray();
+ Assert.Equal(all, TcrReader.Decompress(TcrWriter.Compress(all, Token), Token));
+ Assert.Equal("!!8-Bit!!"u8.ToArray().Concat(Enumerable.Repeat((byte)0, 256)), TcrWriter.Compress([], Token));
+ }
+
+ // ───────────────────────── PML ─────────────────────────
+
+ [Fact]
+ public void Pml_keeps_chapters_styles_links_notes_pictures_and_characters()
+ {
+ var book = SampleBook();
+ var folder = fx.NewFolder();
+ var path = Path.Combine(folder, "book.pml");
+ PmlWriter.Write(book, path, new PmlPictures(path, new FolderAllocator(folder, "book"), fx.NewFolder()), Token);
+
+ var bytes = File.ReadAllBytes(path);
+ Assert.All(bytes, b => Assert.InRange(b, (byte)0x0A, (byte)0x7E)); // ASCII only
+ var pml = Encoding.ASCII.GetString(bytes);
+ Assert.Contains("\\xChapter One\\x\r\n", pml);
+ Assert.Contains("\\X1A Section\\X1\r\n", pml);
+ Assert.Contains("\\Blit\\B", pml);
+ Assert.Contains("\\idusk\\i", pml);
+ Assert.Contains("Caf\\a233, na\\a239ve \\a151 \\a147quoted\\a148 \\a1285", pml);
+ Assert.Contains("\\UB4F1", pml); // 등
+ Assert.Contains("Back\\\\slash and \\a060footnote>", pml);
+ Assert.Contains("\\q=\"#two\"the second chapter\\q", pml);
+ Assert.Contains("\\Q=\"two\"", pml);
+ Assert.Contains("\\m=\"img0001.png\"", pml);
+ Assert.Contains("H\\Sb2\\SbO", pml);
+ Assert.Contains("\\Fn=\"fn1\"1\\Fn", pml);
+ Assert.Contains("\r\nA note at the end.", pml);
+ var picture = Path.Combine(folder, "book" + PmlPictures.FolderSuffix, "img0001.png");
+ using (var image = new MagickImage(picture))
+ Assert.Equal(MagickFormat.Png, image.Format);
+
+ var read = PmlReader.ReadBook(path, fx.NewFolder(), Token).Document;
+ Assert.Equal(["Chapter One", "Chapter Two"], Headings(read, 1));
+ Assert.Equal(["A Section"], Headings(read, 2));
+ var text = Text(read);
+ foreach (var part in new[] { Accented, Korean, "Back\\slash and text.", "● First item", "Name | Value", Line1, "A note at the end." })
+ Assert.Contains(part, text);
+ var inlines = Inlines(read).ToList();
+ Assert.Contains(inlines, i => i is HText { Text: "lit", Format.Bold: true });
+ Assert.Contains(inlines, i => i is HText { Text: "dusk", Format.Italic: true });
+ Assert.Contains(inlines, i => i is HText { Text: "2", Format.Subscript: true });
+ Assert.Single(inlines.OfType());
+ var link = Assert.Single(inlines.OfType(), l => HDocumentWalker.Text(l.Content) == "the second chapter");
+ Assert.Contains(inlines, i => i is HBookmark b && "#" + b.Name == link.Target);
+ }
+
+ [Fact]
+ public void Pml_without_pictures_writes_no_folder()
+ {
+ var folder = fx.NewFolder();
+ var path = Path.Combine(folder, "book.pml");
+ PmlWriter.Write(SampleBook(), path, pictures: null, Token);
+
+ Assert.Equal([path], Directory.GetFileSystemEntries(folder));
+ Assert.DoesNotContain("\\m=", File.ReadAllText(path));
+ }
+
+ // ───────────────────────── RB ─────────────────────────
+
+ [Fact]
+ public void Rb_keeps_metadata_headings_styles_links_and_pictures()
+ {
+ var book = SampleBook();
+ var path = Output("book.rb");
+ RocketBookWriter.Write(book, path, path, fx.NewFolder(), Token);
+
+ var data = File.ReadAllBytes(path);
+ Assert.True(data.AsSpan().StartsWith((ReadOnlySpan)[0xB0, 0x0C, 0xB0, 0x0C, 0x02, 0x00, (byte)'N', (byte)'U', (byte)'V', (byte)'O']));
+ Assert.Equal(0x128u, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(0x18)));
+ Assert.Equal((uint)data.Length, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(0x1C)));
+ Assert.Equal(3u, BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(0x128)));
+ var entries = Enumerable.Range(0, 3).Select(i =>
+ {
+ var entry = data.AsSpan(0x128 + 4 + i * 44, 44);
+ return (Name: Encoding.ASCII.GetString(entry[..32]).TrimEnd('\0'), Length: BinaryPrimitives.ReadInt32LittleEndian(entry[32..]),
+ Offset: BinaryPrimitives.ReadInt32LittleEndian(entry[36..]), Flags: BinaryPrimitives.ReadInt32LittleEndian(entry[40..]));
+ }).ToList();
+ Assert.Equal([("book.info", 8), ("book.html", 1), ("img0001.png", 0)], entries.Select(e => (e.Name, e.Flags)));
+ Assert.Equal(0x128 + 4 + 3 * 44, entries[0].Offset);
+ Assert.Equal(data.Length, entries[^1].Offset + entries[^1].Length);
+ var info = Encoding.Latin1.GetString(data, entries[0].Offset, entries[0].Length);
+ Assert.Equal($"TITLE={Title}\r\nAUTHOR={Author}\r\nBODY=book.html\r\n", info);
+ // The page: its chunk count fits its size (4 KB each), and every chunk is a zlib stream.
+ var page = data.AsSpan(entries[1].Offset, entries[1].Length);
+ var chunks = BinaryPrimitives.ReadInt32LittleEndian(page);
+ var size = BinaryPrimitives.ReadInt32LittleEndian(page[4..]);
+ Assert.Equal((size + 4095) / 4096, chunks);
+ Assert.Equal(0x78, page[8 + chunks * 4]);
+
+ var read = RocketBookReader.ReadBook(path, fx.NewFolder(), Token);
+ Assert.Equal(Title, read.Metadata.Title);
+ Assert.Equal([Author], read.Metadata.Authors);
+ Assert.Equal(["Chapter One", "Chapter Two"], Headings(read.Document, 1));
+ Assert.Equal(["A Section"], Headings(read.Document, 2));
+ var text = Text(read.Document);
+ foreach (var part in new[] { Accented, Korean, "Back\\slash and text.", "First item", Line1, "A note at the end." })
+ Assert.Contains(part, text);
+ var inlines = Inlines(read.Document).ToList();
+ Assert.Contains(inlines, i => i is HText { Text: "lit", Format.Bold: true });
+ Assert.Contains(inlines, i => i is HText { Text: "dusk", Format.Italic: true });
+ Assert.Single(inlines.OfType());
+ var link = Assert.Single(inlines.OfType(), l => HDocumentWalker.Text(l.Content) == "the second chapter");
+ Assert.Contains(inlines, i => i is HBookmark b && "#" + b.Name == link.Target);
+ Assert.Contains(read.Document.Sections.SelectMany(s => s.Blocks), b => b is HTable);
+ }
+
+ // ───────────────────────── SNB ─────────────────────────
+
+ [Fact]
+ public void Snb_keeps_metadata_cover_chapters_and_pictures()
+ {
+ var book = SampleBook();
+ var cover = Path.Combine(fx.NewFolder(), "cover.png");
+ File.WriteAllBytes(cover, EbookBuilders.Png(30, 45, MagickColors.Navy));
+ book.Metadata.CoverImage = cover;
+ var path = Output("book.snb");
+ SnbWriter.Write(book, path, path, fx.NewFolder(), Token);
+
+ var data = File.ReadAllBytes(path);
+ Assert.True(data.AsSpan().StartsWith("SNBP000B"u8));
+ Assert.True(data.AsSpan(data.Length - 8).SequenceEqual("SNBP000B"u8));
+ var files = SnbReader.Unpack(data, DocumentLimits.Default, Token);
+ Assert.Equal(BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(20)), files.Count);
+ Assert.Equal(["snbc/ch0001.snbc", "snbc/ch0002.snbc", "snbc/images/img0001.png", "snbc/images/img0002.jpg", "snbf/book.snbf", "snbf/toc.snbf"],
+ files.Select(f => f.Name));
+ var toc = Encoding.UTF8.GetString(files.Single(f => f.Name == "snbf/toc.snbf").Bytes);
+ Assert.Contains("2", toc);
+ Assert.Contains("Chapter Two", toc);
+ var chapter = Encoding.UTF8.GetString(files.Single(f => f.Name == "snbc/ch0001.snbc").Bytes);
+ Assert.Contains("
img0002.jpg", chapter);
+ Assert.Contains("", chapter);
+ // The text stream's one block is bzip2.
+ var textSize = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(36));
+ Assert.Equal(files.Where(f => f.Name.Contains(".snb")).Sum(f => f.Bytes.Length), textSize);
+ var binarySize = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(32));
+ var textBlock = 44 + BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(28)) + binarySize;
+ Assert.True(data.AsSpan(textBlock).StartsWith("BZh9"u8));
+
+ var read = SnbReader.ReadBook(path, fx.NewFolder(), Token);
+ Assert.Equal(Title, read.Metadata.Title);
+ Assert.Equal([Author], read.Metadata.Authors);
+ Assert.Equal("en", read.Metadata.Language);
+ Assert.Equal("Harbour Press", read.Metadata.Publisher);
+ Assert.Equal("A short story.", read.Metadata.Description);
+ Assert.NotNull(read.Metadata.CoverImage);
+ Assert.Equal(["Chapter One", "Chapter Two"], Headings(read.Document, 1));
+ var text = Text(read.Document);
+ foreach (var part in new[] { Accented, Korean, "● First item", "Name | Value", Line1, "[1] A note at the end." })
+ Assert.Contains(part, text);
+ Assert.Single(Inlines(read.Document).OfType());
+ }
+
+ [Fact]
+ public void Snb_text_of_several_blocks_reads_back_and_incompressible_blocks_are_stored()
+ {
+ // Text of several blocks: each one bzip2.
+ var story = Encoding.UTF8.GetBytes(string.Concat(Enumerable.Range(1, 4000).Select(i => $"Line {i} of the story.
")));
+ var data = SnbWriter.Container([new("snbc/a.snbc", story, Text: true)], Token);
+ Assert.Equal(story, Assert.Single(SnbReader.Unpack(data, DocumentLimits.Default, Token)).Bytes);
+ Assert.True(story.Length > 3 * 0x8000);
+
+ // Random bytes: the full block is stored, the short last one that bzip2 can't shrink below a block is padded
+ // to a stored block of its own.
+ var noise = new byte[2 * 0x8000 - 10];
+ new Random(7).NextBytes(noise);
+ data = SnbWriter.Container([new("snbc/b.snbc", noise, Text: true)], Token);
+ Assert.Equal(noise, Assert.Single(SnbReader.Unpack(data, DocumentLimits.Default, Token)).Bytes);
+ Assert.Equal(2 * 0x8000, BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(36)));
+ var firstBlock = 44 + BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(28));
+ Assert.True(data.AsSpan(firstBlock, 0x8000).SequenceEqual(noise.AsSpan(0, 0x8000)));
+ }
+
+ // ───────────────────────── Limits and failures ─────────────────────────
+
+ [Fact]
+ public void Books_beyond_the_readers_limits_are_refused()
+ {
+ var text = Assert.Throws(() => LegacyWriting.CheckText(DocumentLimits.Default.MaxPartBytes + 1, "PDB"));
+ Assert.Equal(DocumentLimits.ErrorKey, text.ErrorKey);
+ var records = Assert.Throws(() => PalmDatabase.Build("x", "TEXtREAd", Enumerable.Repeat(Array.Empty(), 70_000).ToList()));
+ Assert.Equal(DocumentLimits.ErrorKey, records.ErrorKey);
+ var many = Enumerable.Range(0, DocumentLimits.Default.MaxPackageEntries + 1).Select(i => new SnbWriter.SnbFile($"f{i}", [], Text: true)).ToList();
+ Assert.Equal(DocumentLimits.ErrorKey, Assert.Throws(() => SnbWriter.Container(many, Token)).ErrorKey);
+ }
+
+ [Theory]
+ [InlineData("pdb")]
+ [InlineData("pml")]
+ [InlineData("tcr")]
+ [InlineData("rb")]
+ [InlineData("snb")]
+ public void A_cancelled_write_leaves_no_file(string format)
+ {
+ var book = SampleBook();
+ var folder = fx.NewFolder();
+ var path = Path.Combine(folder, "book." + format);
+ using var cancelled = new CancellationTokenSource();
+ cancelled.Cancel();
+
+ Assert.ThrowsAny(() =>
+ {
+ switch (format)
+ {
+ case "pdb":
+ PdbWriter.Write(book, path, path, cancelled.Token);
+ break;
+ case "pml":
+ PmlWriter.Write(book, path, new PmlPictures(path, new FolderAllocator(folder, "book"), fx.NewFolder()), cancelled.Token);
+ break;
+ case "tcr":
+ TcrWriter.Write(book, path, cancelled.Token);
+ break;
+ case "rb":
+ RocketBookWriter.Write(book, path, path, fx.NewFolder(), cancelled.Token);
+ break;
+ default:
+ SnbWriter.Write(book, path, path, fx.NewFolder(), cancelled.Token);
+ break;
+ }
+ });
+ Assert.Empty(Directory.GetFiles(folder, "*", SearchOption.AllDirectories));
+ }
+}
diff --git a/tests/Filee.Engines.Tests/MacroMarkupWriterTests.cs b/tests/Filee.Engines.Tests/MacroMarkupWriterTests.cs
new file mode 100644
index 000000000..80e6fc366
--- /dev/null
+++ b/tests/Filee.Engines.Tests/MacroMarkupWriterTests.cs
@@ -0,0 +1,503 @@
+// The built-in writers of LaTeX, AsciiDoc, Typst and man pages (Hwp/Hwpx/Markup), which Pandoc wrote before: one
+// model built in code with everything they write (headings, emphasis, nested and numbered lists, quotes, code, a table
+// with merged cells, links, footnotes, pictures, equations and every special character), the text each writes for it,
+// the structure that keeps the file readable (balanced braces and environments, requests, blocks), their routes, which
+// now win over Pandoc's, and cancellation and limits.
+
+using System.Text;
+using System.Text.RegularExpressions;
+using Filee.Core.Conversion;
+using Filee.Core.Presets;
+using Filee.Engines.Documents;
+using Filee.Engines.Hwp.Hwpx;
+using Filee.Engines.Hwp.Hwpx.Markup;
+using Filee.Engines.Text;
+
+namespace Filee.Engines.Tests;
+
+public sealed partial class MacroMarkupWriterTests(EngineFixture fx) : IClassFixture
+{
+ /// Text with every character one of the four languages treats specially.
+ private const string Specials = @"a\b {c} $d & #e _f %g ~h ^i |k| ""l"" [m] n--o '' `p` q@r *s* t+u :: v;; w... x://y (C) -? //z";
+
+ private static HText Text(string text, HCharFormat format = default) => new(text, format);
+
+ private static HParagraph Paragraph(params HInline[] inlines)
+ {
+ var paragraph = new HParagraph();
+ paragraph.Inlines.AddRange(inlines);
+ return paragraph;
+ }
+
+ private static HCell Cell(string text, int rowSpan = 1, int colSpan = 1)
+ {
+ var cell = new HCell { RowSpan = rowSpan, ColSpan = colSpan };
+ cell.Blocks.Add(Paragraph(Text(text)));
+ return cell;
+ }
+
+ private static HParagraph Item(HNumbering numbering, int level, string text, bool numbered = true)
+ {
+ var paragraph = Paragraph(Text(text));
+ paragraph.List = new HListRef(numbering, level, numbered);
+ return paragraph;
+ }
+
+ private static HDocument Document(params HBlock[] blocks)
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ section.Blocks.AddRange(blocks);
+ document.Sections.Add(section);
+ return document;
+ }
+
+ /// A document with everything the writers write.
+ private static HDocument Sample()
+ {
+ var numbering = new HNumbering();
+ numbering.Levels.Add(new HNumberingLevel("DIGIT", "•", 1, Bullet: true));
+ numbering.Levels.Add(new HNumberingLevel("DIGIT", "^2.", 3, Bullet: false));
+ numbering.Levels.Add(new HNumberingLevel("DIGIT", "◦", 1, Bullet: true));
+
+ var note = new HNote(endnote: false);
+ note.Blocks.Add(Paragraph(Text("The note text.")));
+ var link = new HLink("https://example.com/a b#c");
+ link.Content.Add(Text("the site"));
+ var fraction = new MathFraction(new MathText("a"), new MathText("b"));
+ var display = new HParagraph { Format = new HParaFormat(Align: HAlign.Center) };
+ display.Inlines.Add(new HEquation(EquationParser.Parse("SUM _{i=1} ^{n} i"), default));
+
+ var table = new HTable { ColumnCount = 3 };
+ var header = new HRow { Header = true };
+ header.Cells.AddRange([Cell("H1"), Cell("H2"), Cell("H3")]);
+ var first = new HRow();
+ first.Cells.AddRange([Cell("wide", colSpan: 2), Cell("tall", rowSpan: 2)]);
+ var second = new HRow();
+ second.Cells.AddRange([Cell("x"), Cell("y")]);
+ table.Rows.AddRange([header, first, second]);
+
+ var document = Document(
+ new HParagraph { HeadingLevel = 1, Inlines = { Text("Intro_1") } },
+ Paragraph(Text("Plain "), Text("bold", new(Bold: true)), Text(" "), Text("italic", new(Italic: true)), Text(" "),
+ Text("under", new(Underline: true)), Text(" "), Text("gone", new(Strike: true)), Text(" "),
+ Text("code()", new(Shade: HtmlReader.CodeShade)), Text(" E=mc"), Text("2", new(Superscript: true)), Text(" H"),
+ Text("2", new(Subscript: true)), Text("O"), new HLineBreak(default), Text("next line")),
+ Paragraph(Text(Specials)),
+ Paragraph(Text("See "), link, Text(" and a note"), note, Text(".")),
+ Paragraph(Text("Half is "), new HEquation(fraction, default), Text(".")),
+ display,
+ Item(numbering, 0, "first bullet"),
+ Item(numbering, 1, "third numbered"),
+ Item(numbering, 2, "deep bullet"),
+ Item(numbering, 1, "fourth numbered"),
+ Item(numbering, 1, "its second paragraph", numbered: false),
+ Item(numbering, 0, "last bullet"),
+ new HParagraph { Format = new HParaFormat(Left: MarkupBlocks.QuoteIndent), Inlines = { Text("A quoted line.") } },
+ Paragraph(Text("int main() {", MarkupBlocks.Code)),
+ Paragraph(Text(" return 0; // \\end{verbatim}? no", MarkupBlocks.Code)),
+ Paragraph(Text("}", MarkupBlocks.Code)),
+ table,
+ new HParagraph { HeadingLevel = 2, Inlines = { Text("Pictures") } },
+ Paragraph(new HImage("C:/pictures/photo one.png")),
+ Paragraph(new HShape(HShapeKind.Line)),
+ Paragraph(Text("한글 텍스트 and café 😀")));
+ document.Title = "Report & Notes";
+ document.Authors.Add("Kim");
+ return document;
+ }
+
+ private static string? Pictures(string path) => "report_files/" + Uri.EscapeDataString(Path.GetFileName(path));
+
+ // ───────────────────────── LaTeX ─────────────────────────
+
+ [Fact]
+ public void Latex_is_a_whole_article_with_everything_escaped()
+ {
+ var tex = LatexWriter.Write(Sample(), Pictures);
+ Assert.StartsWith("\\documentclass{article}\n", tex, StringComparison.Ordinal);
+ Assert.EndsWith("\\end{document}\n", tex, StringComparison.Ordinal);
+ foreach (var expected in new[]
+ {
+ // The preamble loads what the body uses.
+ "\\usepackage{iftex}", "\\usepackage{fontspec}", "\\usepackage{xeCJK}", "\\usepackage{amsmath,amssymb}", "\\usepackage{graphicx}",
+ "\\usepackage{longtable,array,multirow}", "\\usepackage[normalem]{ulem}", "\\usepackage{hyperref}",
+ "\\title{Report \\& Notes}", "\\author{Kim}", "\\maketitle",
+ "\\section{Intro\\_1}", "\\subsection{Pictures}",
+ "\\textbf{bold}", "\\emph{italic}", "\\protect\\uline{under}", "\\protect\\sout{gone}", "\\texttt{code()}",
+ "E=mc\\textsuperscript{2}", "H\\textsubscript{2}O", "Plain", "\\\\\nnext line",
+ // Specials
+ "a\\textbackslash{}b \\{c\\} \\$d \\& \\#e \\_f \\%g \\textasciitilde{}h \\textasciicircum{}i \\textless{}j\\textgreater{}",
+ "\\textbar{}k\\textbar{} \\textquotedbl{}l\\textquotedbl{} {[}m{]} n-{}-o '{}' \\textasciigrave{}p\\textasciigrave{}",
+ "\\href{https://example.com/a%20b\\#c}{the site}", "\\protect\\footnote{The note text.}",
+ "Half is $\\frac{a}{b}$.", "\\[\n\\sum_{i=1}^{n}i\n\\]",
+ "\\begin{itemize}\n\\item first bullet\n\\begin{enumerate}\n\\item[3.] third numbered\n\\begin{itemize}\n\\item deep bullet\n\\end{itemize}\n\\item[4.] fourth numbered\n\nits second paragraph\n\\end{enumerate}\n\\item last bullet\n\\end{itemize}",
+ "\\begin{quote}\nA quoted line.\n\n\\end{quote}",
+ "\\begin{longtable}",
+ "\\multicolumn{2}{|p{\\dimexpr 0.6667\\linewidth-2\\tabcolsep-\\arrayrulewidth\\relax}|}{wide}", "\\multirow{2}{=}{tall}",
+ "\\cline{1-2}", "\\endhead", "\\includegraphics{report_files/photo one.png}", "\\noindent\\rule{\\linewidth}{0.4pt}",
+ "한글 텍스트 and café 😀",
+ })
+ Assert.True(tex.Contains(expected, StringComparison.Ordinal), $"lacks {expected}:\n{tex}");
+ // Code holding \end{verbatim} can't be verbatim: its lines are typewriter text.
+ Assert.DoesNotContain("\\begin{verbatim}", tex, StringComparison.Ordinal);
+ Assert.Contains("{\\ttfamily int\\ main()\\ \\{\\\\", tex, StringComparison.Ordinal);
+ AssertBalancedLatex(tex);
+ }
+
+ [Fact]
+ public void Latex_writes_code_verbatim_and_tex_math_as_its_source()
+ {
+ var read = MarkdownReader.Read("Text $\\alpha^2$ here.\n\n$$\\begin{align} a &= b \\end{align}$$\n\n```\nif (a < b) { c(); }\n```\n", fx.NewFolder());
+ MarkupBlocks.OpenCodeBoxes(read); // as MarkupConverter does
+ var tex = LatexWriter.Write(read, _ => null);
+ Assert.Contains("Text $\\alpha^2$ here.", tex, StringComparison.Ordinal);
+ // A display environment of its own is written as it is, not inside \[ \].
+ Assert.Contains("\\begin{align} a &= b \\end{align}", tex, StringComparison.Ordinal);
+ Assert.DoesNotContain("\\[\n\\begin{align}", tex, StringComparison.Ordinal);
+ Assert.Contains("\\begin{verbatim}\nif (a < b) { c(); }\n\\end{verbatim}", tex, StringComparison.Ordinal);
+ Assert.DoesNotContain("xeCJK", tex, StringComparison.Ordinal);
+ Assert.DoesNotContain("graphicx", tex, StringComparison.Ordinal);
+ AssertBalancedLatex(tex);
+ }
+
+ [Fact]
+ public void Latex_lists_deeper_than_four_levels_stay_at_the_fourth()
+ {
+ var numbering = new HNumbering();
+ numbering.Levels.Add(new HNumberingLevel("DIGIT", "•", 1, Bullet: true));
+ var blocks = Enumerable.Range(0, 9).Select(level => (HBlock)Item(numbering, level, "level " + level)).ToArray();
+ var tex = LatexWriter.Write(Document(blocks), _ => null);
+ Assert.Equal(4, Regex.Matches(tex, @"\\begin\{itemize\}").Count);
+ Assert.Contains("\\item level 8", tex, StringComparison.Ordinal);
+ AssertBalancedLatex(tex);
+ }
+
+ ///
+ /// Braces balance (escaped ones and verbatim aside), environments close in order, and no [ follows \\ or \item
+ /// unbraced.
+ ///
+ private static void AssertBalancedLatex(string tex)
+ {
+ var environments = new Stack();
+ var depth = 0;
+ for (var i = 0; i < tex.Length; i++)
+ {
+ if (string.CompareOrdinal(tex, i, "\\begin{verbatim}", 0, 16) == 0)
+ {
+ var end = tex.IndexOf("\\end{verbatim}", i + 16, StringComparison.Ordinal);
+ Assert.True(end > 0, "verbatim never ends");
+ i = end + "\\end{verbatim}".Length - 1;
+ continue;
+ }
+ var m = Environment().Match(tex, i);
+ if (m.Success && m.Index == i)
+ {
+ if (m.Groups[1].Value == "begin")
+ environments.Push(m.Groups[2].Value);
+ else
+ Assert.Equal(environments.Pop(), m.Groups[2].Value);
+ }
+ switch (tex[i])
+ {
+ case '\\':
+ i++; // an escaped character or the first letter of a control word
+ break;
+ case '{':
+ depth++;
+ break;
+ case '}':
+ depth--;
+ Assert.True(depth >= 0, $"unbalanced }} at {i}: {tex[Math.Max(0, i - 40)..Math.Min(tex.Length, i + 40)]}");
+ break;
+ }
+ }
+ Assert.Equal(0, depth);
+ Assert.Empty(environments);
+ Assert.DoesNotMatch(@"(\\\\|\\item)\s*\[(?!\d+\.\]|\])", tex);
+ }
+
+ [GeneratedRegex(@"\\(begin|end)\{([^}]*)\}")]
+ private static partial Regex Environment();
+
+ // ───────────────────────── AsciiDoc ─────────────────────────
+
+ [Fact]
+ public void Asciidoc_writes_everything_and_keeps_text_from_reading_as_markup()
+ {
+ var adoc = AsciiDocWriter.Write(Sample(), Pictures);
+ Assert.StartsWith("= Report ++&++ Notes\n:stem: latexmath\n\n", adoc, StringComparison.Ordinal);
+ foreach (var expected in new[]
+ {
+ "== Intro++_++1", "=== Pictures", "**bold**", "__italic__", "[.underline]##under##", "[.line-through]##gone##",
+ "``code()``", "E=mc^2^", "H~2~O", " +\nnext line",
+ // Specials in passthroughs, + and | as attributes.
+ "a++\\++b ++{++c++}++ $d ++&++ ++#++e ++_++f %g ++~++h ++^++i ++<++j++>++ {vbar}k{vbar} \"l\" ++[++m++]++ n++-++-o",
+ "q++@++r ++*++s++*++ t{plus}u ++:++: v++;++; w++.++.. x++:++//y ++(++C) -? //z",
+ "link:++https://example.com/a%20b#c++[the site]", "footnote:[The note text.]",
+ "Half is latexmath:[\\frac{a}{b}].", "[latexmath]\n++++\n\\sum_{i=1}^{n}i\n++++",
+ "* first bullet\n.. third numbered\n*** deep bullet\n.. fourth numbered\n+\nits second paragraph\n* last bullet",
+ "____\nA quoted line.\n\n____",
+ "----\nint main() {\n return 0; // \\end{verbatim}? no\n}\n----",
+ "[cols=\"3*\",options=\"header\"]\n|===\n| H1\n| H2\n| H3\n\n2+| wide\n.2+| tall\n\n| x\n| y\n\n|===",
+ "image:report_files/photo%20one.png[]", "'''",
+ })
+ Assert.True(adoc.Contains(expected, StringComparison.Ordinal), $"lacks {expected}:\n{adoc}");
+ AssertNoBlockStarts(adoc);
+ }
+
+ [Fact]
+ public void Asciidoc_guards_line_starts_directives_and_callouts()
+ {
+ var document = Document(
+ Paragraph(Text("* not a list"), new HLineBreak(default), Text("include::secret.txt[]"), new HLineBreak(default), Text(". not a title"),
+ new HLineBreak(default), Text("1. not an item"), new HLineBreak(default), Text("Term:: not a definition")),
+ Paragraph(Text("include::../../etc/passwd[]", MarkupBlocks.Code)),
+ Paragraph(Text("x = 1; <1>", MarkupBlocks.Code)));
+ var adoc = AsciiDocWriter.Write(document, _ => null);
+ Assert.Contains("{empty}++*++ not a list +\ninclude++:++:secret.txt++[]++ +\n{empty}. not a title +\n{empty}1. not an item +\nTerm++:++: not a definition", adoc, StringComparison.Ordinal);
+ Assert.Contains("[subs=\"specialchars\"]\n----\n\\include::../../etc/passwd[]\nx = 1; <1>\n----", adoc, StringComparison.Ordinal);
+ Assert.DoesNotMatch(new Regex("^include::", RegexOptions.Multiline), adoc);
+ }
+
+ [Fact]
+ public void Asciidoc_numbers_a_list_from_its_first_number_and_nests_tables_once()
+ {
+ var numbering = new HNumbering();
+ var level = new HNumberingLevel("DIGIT", "^1.", 5, Bullet: false);
+ numbering.Levels.Add(level);
+ var inner = new HTable { ColumnCount = 1 };
+ var row = new HRow();
+ row.Cells.Add(Cell("inner!cell"));
+ inner.Rows.Add(row);
+ var outer = new HTable { ColumnCount = 1 };
+ var outerRow = new HRow();
+ var cell = new HCell();
+ cell.Blocks.Add(inner);
+ outerRow.Cells.Add(cell);
+ outer.Rows.Add(outerRow);
+ var adoc = AsciiDocWriter.Write(Document(Item(numbering, 0, "five"), Item(numbering, 0, "six"), outer), _ => null);
+ Assert.Contains("[start=5]\n. five\n. six", adoc, StringComparison.Ordinal);
+ Assert.Contains("a| [cols=\"1*\"]\n!===\n! inner\\!cell\n\n!===", adoc, StringComparison.Ordinal);
+ }
+
+ /// Every line in a paragraph that isn't markup Filee wrote starts with a letter, digit or {empty}.
+ private static void AssertNoBlockStarts(string adoc)
+ {
+ foreach (var line in adoc.Split('\n'))
+ Assert.False(Regex.IsMatch(line, @"^(include|ifdef|ifndef|ifeval|endif)::"), line);
+ }
+
+ // ───────────────────────── Typst ─────────────────────────
+
+ [Fact]
+ public void Typst_writes_everything_and_escapes_its_markup()
+ {
+ var typ = TypstWriter.Write(Sample(), Pictures);
+ foreach (var expected in new[]
+ {
+ "#set document(title: \"Report & Notes\", author: (\"Kim\",))", "#align(center, text(size: 1.7em, weight: \"bold\")[Report & Notes])",
+ "= Intro\\_1", "== Pictures", "#strong[bold]", "#emph[italic]", "#underline[under]", "#strike[gone]", "`code()`",
+ "E=mc#super[2]", "H#sub[2]O", " \\\nnext line",
+ "a\\\\b {c} \\$d & \\#e \\_f %g \\~h ^i \\ |k| \\\"l\\\" \\[m\\] n\\--o \\'\\' \\`p\\` q\\@r \\*s\\* t+u :: v;; w\\... x\\:\\//y (C) \\-? \\//z",
+ "#link(\"https://example.com/a b#c\")[the site]", "#footnote[The note text.]",
+ "Half is $frac(a, b)$\\.", "$ ∑_(i = 1)^(n) i $",
+ "- first bullet\n 3. third numbered\n - deep bullet\n 4. fourth numbered\n\n its second paragraph\n- last bullet",
+ "#quote(block: true)[\nA quoted line.\n\n]",
+ "```\nint main() {\n return 0; // \\end{verbatim}? no\n}\n```",
+ "#table(\n columns: 3,\n table.header(\n [H1],\n [H2],\n [H3],\n ),\n table.cell(colspan: 2)[wide],\n table.cell(rowspan: 2)[tall],\n [x],\n [y],\n)",
+ "#image(\"report_files/photo one.png\")", "#line(length: 100%)",
+ })
+ Assert.True(typ.Contains(expected, StringComparison.Ordinal), $"lacks {expected}:\n{typ}");
+ AssertBalancedTypst(typ);
+ }
+
+ [Fact]
+ public void Typst_guards_line_starts()
+ {
+ var typ = TypstWriter.Write(Document(Paragraph(Text("= not a heading"), new HLineBreak(default), Text("- not a list"), new HLineBreak(default),
+ Text("12. not an item"), new HLineBreak(default), Text("+ no"), new HLineBreak(default), Text("/ no: term"))), _ => null);
+ Assert.Contains("\\= not a heading \\\n\\- not a list \\\n12\\. not an item \\\n\\+ no \\\n\\/ no: term", typ, StringComparison.Ordinal);
+ }
+
+ /// Square brackets and dollar signs pair up outside strings, raw text and escapes.
+ private static void AssertBalancedTypst(string typ)
+ {
+ var depth = 0;
+ var dollars = 0;
+ for (var i = 0; i < typ.Length; i++)
+ {
+ switch (typ[i])
+ {
+ case '\\':
+ i++;
+ break;
+ case '"':
+ for (i++; i < typ.Length && typ[i] != '"'; i++)
+ if (typ[i] == '\\')
+ i++;
+ break;
+ case '`':
+ {
+ var fence = typ.Length - i >= 3 && typ.Substring(i, 3) == "```" ? "```" : "`";
+ var end = typ.IndexOf(fence, i + fence.Length, StringComparison.Ordinal);
+ Assert.True(end > 0, "raw text never ends");
+ i = end + fence.Length - 1;
+ break;
+ }
+ case '[':
+ depth++;
+ break;
+ case ']':
+ depth--;
+ Assert.True(depth >= 0, $"unbalanced ] at {i}");
+ break;
+ case '$':
+ dollars++;
+ break;
+ }
+ }
+ Assert.Equal(0, depth);
+ Assert.Equal(0, dollars % 2);
+ }
+
+ // ───────────────────────── man ─────────────────────────
+
+ [Fact]
+ public void Man_page_writes_everything_in_ascii_requests()
+ {
+ var man = new ManWriter().Write(Sample());
+ Assert.StartsWith(".TH \"Report & Notes\" \"1\" \"\" \"\" \"\"\n", man, StringComparison.Ordinal);
+ foreach (var expected in new[]
+ {
+ ".SH \"Intro_1\"", ".SS \"Pictures\"", "\\fBbold\\fP", "\\fIitalic\\fP", "\\fIunder\\fP", "\\f(CRcode()\\fP", "\n.br\nnext line",
+ "a\\eb {c} $d & #e _f %g \\(tih \\(hai |k| \\(dql\\(dq [m] n\\-\\-o \\(aq\\(aq \\(gap\\(ga q@r *s* t+u :: v;; w... x://y (C) \\-? //z",
+ "the site ", "and a note[1].", ".SH NOTES\n.IP \"[1]\" 5\nThe note text.",
+ "Half is a/b.",
+ ".IP \"\\(bu\" 4\nfirst bullet\n.RS 4\n.IP \"3.\" 4\nthird numbered\n.RS 4\n.IP \"\\(bu\" 4\ndeep bullet\n.RE\n.IP \"4.\" 4\nfourth numbered\n.IP \"\" 4\nits second paragraph\n.RE\n.IP \"\\(bu\" 4\nlast bullet",
+ ".RS 4\n.PP\nA quoted line.\n.RE",
+ ".EX\nint main() {\n\\& return 0; // \\eend{verbatim}? no\n}\n.EE",
+ ".TS\nallbox tab(@);\nlb lb lb\nl s l\nl l ^.\nT{\nH1\nT}@T{\nH2\nT}@T{\nH3\nT}\nT{\nwide\nT}@@T{\ntall\nT}\nT{\nx\nT}@T{\ny\nT}\n.TE",
+ "\\[uD55C]\\[uAE00] \\[uD14D]\\[uC2A4]\\[uD2B8] and caf\\[u00E9] \\[u1F600]",
+ })
+ Assert.True(man.Contains(expected, StringComparison.Ordinal), $"lacks {expected}:\n{man}");
+ Assert.DoesNotContain("photo", man, StringComparison.Ordinal); // a man page has no pictures
+ Assert.All(man, c => Assert.True(c < 0x80, $"U+{(int)c:X4}"));
+ // Every line starting with "." or "'" is a request Filee wrote.
+ foreach (var line in man.Split('\n').Where(l => l.StartsWith('.') || l.StartsWith('\'')))
+ Assert.Matches(@"^\.(TH|SH|SS|PP|br|IP|RS|RE|EX|EE|TS|TE|ce)( |$)", line);
+ Assert.Equal(Regex.Matches(man, "^\\.RS", RegexOptions.Multiline).Count, Regex.Matches(man, "^\\.RE", RegexOptions.Multiline).Count);
+ }
+
+ [Fact]
+ public void Man_page_text_never_reads_as_a_request()
+ {
+ var man = new ManWriter().Write(Document(Paragraph(Text(".hidden request"), new HLineBreak(default), Text("'also hidden"), new HLineBreak(default),
+ Text(" indented"), new HLineBreak(default), Text("T} ends a cell"))));
+ Assert.Contains(".PP\n\\&.hidden request\n.br\n\\(aqalso hidden\n.br\n\\& indented\n.br\n\\&T} ends a cell", man, StringComparison.Ordinal);
+ }
+
+ // ───────────────────────── Shared ─────────────────────────
+
+ private static IEnumerable<(string Name, Func Write)> Writers(DocumentLimits l, CancellationToken cancellation)
+ {
+ yield return ("tex", d => new LatexWriter(_ => null) { Limits = l, Cancellation = cancellation }.Write(d));
+ yield return ("adoc", d => new AsciiDocWriter(_ => null) { Limits = l, Cancellation = cancellation }.Write(d));
+ yield return ("typ", d => new TypstWriter(_ => null) { Limits = l, Cancellation = cancellation }.Write(d));
+ yield return ("man", d => new ManWriter { Limits = l, Cancellation = cancellation }.Write(d));
+ }
+
+ [Fact]
+ public void A_cancelled_conversion_stops_the_writers()
+ {
+ using var cancelled = new CancellationTokenSource();
+ cancelled.Cancel();
+ foreach (var (_, write) in Writers(DocumentLimits.Default, cancelled.Token))
+ Assert.ThrowsAny(() => write(Sample()));
+ }
+
+ [Fact]
+ public void A_footnote_shared_by_many_references_is_refused_by_the_limits()
+ {
+ var note = new HNote(endnote: false);
+ note.Blocks.Add(Paragraph(Text(new string('n', 100_000))));
+ var paragraph = new HParagraph();
+ for (var i = 0; i < 100; i++)
+ paragraph.Inlines.AddRange([Text("ref" + i + " "), note]);
+ var small = DocumentLimits.Default with { MaxPartBytes = 1024 * 1024 };
+ foreach (var (name, write) in Writers(small, TestContext.Current.CancellationToken))
+ {
+ var refusal = Assert.Throws(() => write(Document(paragraph)));
+ Assert.True(refusal.ErrorKey == DocumentLimits.ErrorKey, name);
+ }
+ }
+
+ [Fact]
+ public void Hostile_lists_quotes_and_equations_are_written_in_time()
+ {
+ // List levels and quote indents far past what any language nests, and an equation 20,000 levels deep.
+ var numbering = new HNumbering();
+ numbering.Levels.Add(new HNumberingLevel("DIGIT", "^1.", 1, Bullet: false));
+ MathNode deep = new MathText("x");
+ for (var i = 0; i < 20_000; i++)
+ deep = new MathRoot(null, deep);
+ var blocks = new List
+ {
+ Item(numbering, 10_000, "deep item"),
+ new HParagraph { Format = new HParaFormat(Left: int.MaxValue), Inlines = { Text("deep quote") } },
+ Paragraph(new HEquation(deep, default, "deep equation")),
+ };
+ var watch = System.Diagnostics.Stopwatch.StartNew();
+ foreach (var (name, write) in Writers(DocumentLimits.Default, TestContext.Current.CancellationToken))
+ {
+ var text = write(Document([.. blocks]));
+ foreach (var expected in new[] { "deep item", "deep quote", "deep equation" })
+ Assert.True(text.Contains(expected, StringComparison.Ordinal), $"{name} lacks {expected}");
+ }
+ Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"{watch.Elapsed}");
+ }
+
+ // ───────────────────────── Routes ─────────────────────────
+
+ public static TheoryData Targets() => [.. MarkupConverter.WriteOnly.Keys];
+
+ [Theory]
+ [MemberData(nameof(Targets))]
+ public void Built_in_writers_win_over_pandoc_which_keeps_reading_them(string target)
+ {
+ foreach (var planner in new[] { fx.Catalog.CreatePlanner(["pandoc"]), MarkupTestKit.WithoutPandoc(fx).CreatePlanner() })
+ {
+ foreach (var source in new[] { "md", "html", "docx", "odt", "hwpx", "hwp", "rtf", "rst", "org", "txt" })
+ Assert.Equal("markup", Assert.Single(planner.Plan(source, target)!.Steps).Converter.Id);
+ }
+ var withPandoc = fx.Catalog.CreatePlanner(["pandoc"]);
+ Assert.Equal("pandoc", Assert.Single(withPandoc.Plan(target, "md")!.Steps).Converter.Id);
+ // The built-in markup engine only writes these. Where Pandoc is installed, other engines still read them through
+ // Pandoc's AST, so a route may exist without the Pandoc converter, but it never starts with the markup engine.
+ var withoutPandoc = MarkupTestKit.WithoutPandoc(fx).CreatePlanner().Plan(target, "md");
+ Assert.True(withoutPandoc is null || withoutPandoc.Steps[0].Converter.Id != "markup", $"{target} → md starts with the markup engine");
+ }
+
+ [Theory]
+ [MemberData(nameof(Targets))]
+ public async Task Markdown_converts_to_each_without_pandoc(string target)
+ {
+ var folder = fx.NewFolder();
+ var source = Path.Combine(folder, "notes.md");
+ await File.WriteAllTextAsync(source, "# Title_1\n\nSome *text* with $x^2$ and a [link](https://example.com).\n\n- one\n- two\n",
+ new UTF8Encoding(false), TestContext.Current.CancellationToken);
+ var output = await MarkupTestKit.ConvertAsync(MarkupTestKit.WithoutPandoc(fx), source, target);
+ var text = await File.ReadAllTextAsync(output, TestContext.Current.CancellationToken);
+ Assert.Contains("Title", text, StringComparison.Ordinal);
+ Assert.Contains("one", text, StringComparison.Ordinal);
+ Assert.Contains(target switch
+ {
+ "tex" => "$x^2$",
+ "adoc" => "latexmath:[x^2]",
+ "typ" => "$x^(2)$",
+ _ => "x\\(ha2",
+ }, text, StringComparison.Ordinal);
+ // Written through a temporary file, which is gone.
+ Assert.Empty(Directory.GetFiles(Path.GetDirectoryName(output)!, "*.tmp"));
+ }
+}
diff --git a/tests/Filee.Engines.Tests/MarkupRouteTests.cs b/tests/Filee.Engines.Tests/MarkupRouteTests.cs
index 08999cc7c..dc67d83ce 100644
--- a/tests/Filee.Engines.Tests/MarkupRouteTests.cs
+++ b/tests/Filee.Engines.Tests/MarkupRouteTests.cs
@@ -28,10 +28,12 @@ public void Built_in_routes_win_over_pandoc_and_pandoc_keeps_the_rest(string mar
Assert.Equal(engine, Assert.Single(planner.Plan(from, to)!.Steps).Converter.Id);
}
- // Slides come from the PPTX writer, which takes every built-in reader; what only Pandoc writes stays Pandoc's.
+ // Slides come from the PPTX writer, which takes every built-in reader; LaTeX is written here too (WriteOnly),
+ // and what only Pandoc reads stays Pandoc's.
var withPandoc = fx.Catalog.CreatePlanner(["pandoc"]);
Assert.Equal("pptx-writer", Assert.Single(withPandoc.Plan(markup, "pptx")!.Steps).Converter.Id);
- Assert.Equal("pandoc", Assert.Single(withPandoc.Plan(markup, "tex")!.Steps).Converter.Id);
+ Assert.Equal("markup", Assert.Single(withPandoc.Plan(markup, "tex")!.Steps).Converter.Id);
+ Assert.Equal("pandoc", Assert.Single(withPandoc.Plan("tex", markup)!.Steps).Converter.Id);
}
[Fact]
diff --git a/tests/Filee.Engines.Tests/MarkupWriterLimitTests.cs b/tests/Filee.Engines.Tests/MarkupWriterLimitTests.cs
index 3e72c2671..52b9b29da 100644
--- a/tests/Filee.Engines.Tests/MarkupWriterLimitTests.cs
+++ b/tests/Filee.Engines.Tests/MarkupWriterLimitTests.cs
@@ -27,6 +27,10 @@ public class MarkupWriterLimitTests(EngineFixture fx) : IClassFixture MarkdownWriter.Write(d, _ => null)),
("ipynb", d => NotebookWriter.Write(d, fx.NewFolder(), pictures: false)),
("opml", d => OpmlWriter.Write(d, _ => null)),
+ ("tex", d => LatexWriter.Write(d, _ => null)),
+ ("adoc", d => AsciiDocWriter.Write(d, _ => null)),
+ ("typ", d => TypstWriter.Write(d, _ => null)),
+ ("man", d => new ManWriter().Write(d)),
];
private static T InTime(Func write)
diff --git a/tests/Filee.Engines.Tests/PandocMarkupTests.cs b/tests/Filee.Engines.Tests/PandocMarkupTests.cs
index 56162fdb6..dd3de807e 100644
--- a/tests/Filee.Engines.Tests/PandocMarkupTests.cs
+++ b/tests/Filee.Engines.Tests/PandocMarkupTests.cs
@@ -56,8 +56,11 @@ private async Task ConvertAsync(string input, string target)
public void Markup_routes()
{
var planner = fx.Catalog.CreatePlanner(["pandoc"]);
- foreach (var (from, to) in new[] { ("tex", "html"), ("tex", "epub"), ("docx", "tex"), ("md", "adoc") })
+ foreach (var (from, to) in new[] { ("tex", "html"), ("tex", "epub"), ("tex", "adoc") })
Assert.Equal("pandoc", Assert.Single(planner.Plan(from, to)!.Steps).Converter.Id);
+ // LaTeX, AsciiDoc, Typst and man pages are written by Filee itself (read by Pandoc).
+ foreach (var (from, to) in new[] { ("docx", "tex"), ("md", "adoc"), ("html", "typ"), ("md", "man") })
+ Assert.Equal("markup", Assert.Single(planner.Plan(from, to)!.Steps).Converter.Id);
// reStructuredText is read and written by Filee itself.
Assert.Equal("docx-writer", Assert.Single(planner.Plan("rst", "docx")!.Steps).Converter.Id);
Assert.Equal("markup", Assert.Single(planner.Plan("rst", "md")!.Steps).Converter.Id);
@@ -80,13 +83,14 @@ public void More_markup_languages_and_slides()
foreach (var other in new[] { "md", "html", "docx", "odt" })
{
// The languages Filee writes itself take the built-in writer.
- Assert.Equal(Filee.Engines.Text.MarkupConverter.Writers.ContainsKey(markup) ? "markup" : "pandoc",
+ Assert.Equal(Filee.Engines.Text.MarkupConverter.Writers.ContainsKey(markup) || Filee.Engines.Text.MarkupConverter.WriteOnly.ContainsKey(markup) ? "markup" : "pandoc",
Assert.Single(planner.Plan(other, markup)!.Steps).Converter.Id);
Assert.NotNull(planner.Plan(markup, other));
}
Assert.NotNull(planner.Plan(markup, "epub"));
}
- Assert.Equal("pandoc", Assert.Single(planner.Plan("org", "adoc")!.Steps).Converter.Id);
+ Assert.Equal("markup", Assert.Single(planner.Plan("org", "adoc")!.Steps).Converter.Id);
+ Assert.Equal("pandoc", Assert.Single(planner.Plan("typ", "adoc")!.Steps).Converter.Id);
// Markdown and the markup languages Filee reads itself (Org) get slides from the built-in PPTX writer. Those it
// reads only through Pandoc go through Pandoc's AST into the built-in writer when Pandoc is really there, else
// (only assumed) through Pandoc's own PowerPoint writer.
diff --git a/tests/Filee.Engines.Tests/TexMathWriterTests.cs b/tests/Filee.Engines.Tests/TexMathWriterTests.cs
new file mode 100644
index 000000000..5b822e298
--- /dev/null
+++ b/tests/Filee.Engines.Tests/TexMathWriterTests.cs
@@ -0,0 +1,194 @@
+// The equation tree written as TeX (TexMathWriter: LaTeX, Markdown, AsciiDoc) and as Typst math (TypstMathWriter):
+// TeX read into the tree, written back and read again gives the same tree; Word math (OMML) and 한글 equation scripts,
+// which come as a tree, become TeX that Markdown reads back as the same equations; and a tree nested past the limit is
+// left to the equation's text, quickly.
+
+using System.Diagnostics;
+using System.Xml.Linq;
+using Filee.Engines.Hwp.Hwpx;
+using Filee.Engines.Hwp.Hwpx.Docx;
+
+namespace Filee.Engines.Tests;
+
+public class TexMathWriterTests(EngineFixture fx) : IClassFixture
+{
+ private static readonly XNamespace M = "http://schemas.openxmlformats.org/officeDocument/2006/math";
+
+ [Theory]
+ [InlineData(@"\frac{a+b}{2c}")]
+ [InlineData(@"x_i^2 + y^{n+1}")]
+ [InlineData(@"\sqrt[3]{x} + \sqrt{x^2+1}")]
+ [InlineData(@"\sum_{i=1}^{n} i^2")]
+ [InlineData(@"\int_0^\infty e^{-x}\,dx")]
+ [InlineData(@"\lim_{x \to 0} \frac{\sin x}{x}")]
+ [InlineData(@"\begin{pmatrix} a & b \\ c & d \end{pmatrix}")]
+ [InlineData(@"\begin{vmatrix} a & b \\ c & d \end{vmatrix} + \begin{bmatrix} 1 \\ 2 \end{bmatrix}")]
+ [InlineData(@"|x| = \begin{cases} x & x \ge 0 \\ -x & \text{otherwise} \end{cases}")]
+ [InlineData(@"\left( \frac{a}{b} \right) + \left\{ x \right. + \left\langle y \right\rangle")]
+ [InlineData(@"\hat{x} + \overline{AB} + \vec{v} + \tilde{y} + \underline{z}")]
+ [InlineData(@"\mathbf{v} \cdot \mathrm{d}x + \boldsymbol{\alpha}")]
+ [InlineData(@"\alpha \leq \beta \neq \gamma \to \infty")]
+ [InlineData(@"\binom{n}{k}")]
+ [InlineData(@"\begin{aligned} a &= b \\ c &= d \end{aligned}")]
+ [InlineData(@"f'(x) + f''(y)")]
+ [InlineData(@"\{x\} \cup \emptyset")]
+ [InlineData(@"x \in \mathbb{R} \subseteq \mathcal{L}")]
+ [InlineData(@"\operatorname{lcm}(a, b) + \max_{i} x_i")]
+ [InlineData(@"a \quad b \qquad c \, d \; e")]
+ [InlineData(@"\not\in \not= \pm \times \div")]
+ [InlineData(@"\Gamma \Delta \Omega \omega")]
+ public void Tex_written_from_the_tree_reads_back_as_the_same_tree(string tex)
+ {
+ var tree = LatexMath.Parse(tex)!;
+ var written = TexMathWriter.Write(tree);
+ Assert.NotNull(written);
+ var again = LatexMath.Parse(written);
+ Assert.NotNull(again);
+ Assert.Equal(EquationScript.Script(tree), EquationScript.Script(again));
+ }
+
+ [Fact]
+ public void Characters_tex_treats_specially_are_escaped()
+ {
+ var tree = new MathRow([
+ new MathQuoted("50% & #1 {a} $x_y ~^\\"),
+ new MathText("한"),
+ new MathCommand("Alpha", "Α"),
+ new MathSymbol("{"),
+ new MathSymbol("#"),
+ ]);
+ var tex = TexMathWriter.Write(tree)!;
+ Assert.Contains(@"\text{50\% \& \#1 \{a\} \$x\_y \textasciitilde{}\textasciicircum{}\textbackslash{}}", tex, StringComparison.Ordinal);
+ Assert.Contains(@"\text{한}", tex, StringComparison.Ordinal);
+ Assert.Contains("A", tex, StringComparison.Ordinal);
+ Assert.DoesNotContain("Α", tex, StringComparison.Ordinal); // Greek Alpha has no command: the Latin letter
+ Assert.Contains(@"\{\#", tex, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void A_control_word_is_kept_apart_from_a_letter_after_it()
+ {
+ var tex = TexMathWriter.Write(new MathRow([new MathCommand("alpha", "α"), new MathText("x"), new MathFunction("sin"), new MathText("y")]))!;
+ Assert.Equal(@"\alpha x\sin y", tex);
+ }
+
+ [Fact]
+ public void Word_math_becomes_tex()
+ {
+ // (a + b) / 2 under a square root, with x² beside it.
+ var oMath = new XElement(M + "oMath",
+ new XElement(M + "rad", new XElement(M + "radPr", new XElement(M + "degHide", new XAttribute(M + "val", "1"))), new XElement(M + "deg"),
+ new XElement(M + "e",
+ new XElement(M + "f",
+ new XElement(M + "num", Run("a+b")),
+ new XElement(M + "den", Run("2"))))),
+ new XElement(M + "sSup", new XElement(M + "e", Run("x")), new XElement(M + "sup", Run("2"))));
+ var tex = TexMathWriter.Write(OmmlReader.Read(oMath))!;
+ Assert.Equal(@"\sqrt{\frac{a+b}{2}}x^{2}", tex);
+
+ static XElement Run(string text) => new(M + "r", new XElement(M + "t", text));
+ }
+
+ [Fact]
+ public void A_hangul_equation_becomes_tex()
+ {
+ var tex = TexMathWriter.Write(EquationParser.Parse("{a} over {b} + sqrt {x} + SUM _{i=1} ^{n} i + alpha LEQ beta"))!;
+ Assert.Equal(@"\frac{a}{b}+\sqrt{x}+\sum_{i=1}^{n}i+\alpha\leq\beta", tex);
+ }
+
+ [Theory]
+ [InlineData(@"\frac{a}{b}", "frac(a, b)")]
+ [InlineData(@"x_i^2", "x_(i)^(2)")]
+ [InlineData(@"\sqrt[3]{x} + \sqrt{y}", "root(3, x) + sqrt(y)")]
+ [InlineData(@"\sum_{i=1}^{n} i", "∑_(i = 1)^(n) i")]
+ [InlineData(@"\lim_{x \to 0} \sin x", "lim_(x → 0) op(\"sin\") x")]
+ [InlineData(@"\begin{pmatrix} a & b \\ c & d \end{pmatrix}", "mat(delim: \"(\", a, b; c, d)")]
+ [InlineData(@"\begin{cases} 1 & x > 0 \\ 0 & \text{else} \end{cases}", "cases(1 & x > 0, 0 & \"else\")")]
+ [InlineData(@"\left| x \right| + \left( a \right)", "abs(x) + (a)")]
+ [InlineData(@"\hat{x} + \vec{v} + \overline{AB}", "hat(x) + arrow(v) + overline(A B)")]
+ [InlineData(@"\mathbf{v} + \mathrm{d}", "bold(upright(v)) + upright(d)")]
+ [InlineData(@"\binom{n}{k}", "binom(n, k)")]
+ [InlineData(@"f'(x)", "f' \\( x \\)")]
+ [InlineData(@"ab", "a b")]
+ [InlineData(@"\frac{f(a,b)}{2}", "frac(f \\( a \\, b \\), 2)")]
+ [InlineData(@"a \quad b \, c", "a quad b thin c")]
+ public void Tex_becomes_typst_math(string tex, string typst) =>
+ Assert.Equal(typst, TypstMathWriter.Write(LatexMath.Parse(tex)!));
+
+ [Fact]
+ public void Aligned_lines_are_typst_lines_with_their_marks()
+ {
+ var typst = TypstMathWriter.Write(LatexMath.Parse(@"\begin{aligned} a &= b \\ c &= d \end{aligned}")!);
+ Assert.Equal("a & = b \\\nc & = d", typst);
+ // Inside something else, lines are a matrix.
+ Assert.StartsWith("(mat(delim: #none", TypstMathWriter.Write(LatexMath.Parse(@"\left( \begin{gathered} a \\ b \end{gathered} \right)")!), StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void Typst_escapes_its_syntax_and_quotes_text()
+ {
+ var typst = TypstMathWriter.Write(new MathRow([new MathQuoted("say \"hi\" \\ now"), new MathText("한글"), new MathSymbol("#"), new MathSymbol("$"), new MathSymbol("/")]))!;
+ Assert.Equal("\"say \\\"hi\\\" \\\\ now\" \"한글\" \\# \\$ \\/", typst);
+ }
+
+ // ───────────────────────── Markdown ─────────────────────────
+
+ private static HDocument Document(params HParagraph[] paragraphs)
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ section.Blocks.AddRange(paragraphs);
+ document.Sections.Add(section);
+ return document;
+ }
+
+ [Fact]
+ public void Markdown_writes_word_and_hangul_equations_as_tex_that_it_reads_back()
+ {
+ var inline = new HParagraph();
+ var fraction = new MathFraction(new MathRow([new MathText("a"), new MathSymbol("+"), new MathText("b")]), new MathNumber("2"));
+ inline.Inlines.AddRange([new HText("Half: ", default), new HEquation(fraction, default), new HText(" done.", default)]);
+ // Word's display equation: alone in a centred paragraph.
+ var display = new HParagraph { Format = new HParaFormat(Align: HAlign.Center) };
+ var sum = EquationParser.Parse("SUM _{i=1} ^{n} i ^{2}");
+ display.Inlines.Add(new HEquation(sum, default));
+
+ var markdown = MarkdownWriter.Write(Document(inline, display), _ => null);
+ Assert.Contains(@"Half: $\frac{a+b}{2}$ done.", markdown, StringComparison.Ordinal);
+ Assert.Contains(@"$$\sum_{i=1}^{n}i^{2}$$", markdown, StringComparison.Ordinal);
+ Assert.DoesNotContain("(a+b)/2", markdown, StringComparison.Ordinal);
+
+ var read = MarkdownReader.Read(markdown, fx.NewFolder());
+ var equations = HDocumentWalker.Inlines(read.Sections.SelectMany(s => s.Blocks)).OfType().ToList();
+ Assert.Equal(2, equations.Count);
+ Assert.Equal(EquationScript.Script(fraction), EquationScript.Script(equations[0].Math));
+ Assert.Equal(EquationScript.Script(sum), EquationScript.Script(equations[1].Math));
+ var paragraphs = read.Sections.SelectMany(s => s.Blocks).OfType().ToList();
+ Assert.Equal(HAlign.Center, paragraphs.Single(p => p.Inlines.Contains(equations[1])).Format.Align);
+ }
+
+ [Fact]
+ public void Markdown_keeps_tex_it_read_as_written()
+ {
+ var read = MarkdownReader.Read("Area $\\pi r^2$ here.\n", fx.NewFolder());
+ Assert.Contains("$\\pi r^2$", MarkdownWriter.Write(read, _ => null), StringComparison.Ordinal);
+ }
+
+ // ───────────────────────── Depth ─────────────────────────
+
+ [Fact]
+ public void A_tree_nested_thousands_deep_is_left_to_its_text_in_time()
+ {
+ MathNode node = new MathText("x");
+ for (var i = 0; i < 20_000; i++)
+ node = new MathFraction(node, new MathNumber("2"));
+ var watch = Stopwatch.StartNew();
+ Assert.Null(TexMathWriter.Write(node));
+ Assert.Null(TypstMathWriter.Write(node));
+ // The writers then write the equation's text (here its given source).
+ var paragraph = new HParagraph();
+ paragraph.Inlines.Add(new HEquation(node, default, "deep equation"));
+ Assert.Contains("deep equation", MarkdownWriter.Write(Document(paragraph), _ => null), StringComparison.Ordinal);
+ Assert.True(watch.Elapsed < TimeSpan.FromSeconds(20), $"{watch.Elapsed}");
+ }
+}
diff --git a/tests/Filee.Engines.Tests/UyapTests.cs b/tests/Filee.Engines.Tests/UyapTests.cs
index dc65f7ef6..ca5db191f 100644
--- a/tests/Filee.Engines.Tests/UyapTests.cs
+++ b/tests/Filee.Engines.Tests/UyapTests.cs
@@ -181,6 +181,39 @@ public void Tables_pictures_page_breaks_and_landscape_pages_are_read()
Assert.False(Assert.IsType(section.Blocks[3]).PageBreakBefore);
}
+ [Fact]
+ public void Lists_header_rows_and_cell_borders_fills_and_alignment_are_read()
+ {
+ var folder = fx.NewFolder();
+ var path = MakeUdf(folder, "lists.udf",
+ [
+ new("Numbered=\"true\" NumberType=\"NUMBER_TYPE_ROMAN_BIG_PARENTHESIS\" ListId=\"1\" ListLevel=\"0\"", ("One", "")),
+ new("Bulleted=\"true\" BulletType=\"BULLET_TYPE_DIAMOND\" ListId=\"1\" ListLevel=\"1\"", ("Under", "")),
+ new("Numbered=\"true\" NumberType=\"NUMBER_TYPE_ROMAN_BIG_PARENTHESIS\" ListId=\"1\" ListLevel=\"0\"", ("Two", "")),
+ ], extraWithText: text =>
+ {
+ string Cell(string attributes, string value) => $"" + Paragraph(text, new Para("", (value, ""))) + " | ";
+ return $"""{Cell("align=\"vcenter\" fillColor=\"-256\"", "A")}{Cell("border=\"borderNone\"", "B")}{Cell("borderSpec=\"5\" borderStyle=\"borderStyle-dashed\" borderWidth=\"1.0\" borderColor=\"-65536\" fillColor=\"-1\"", "C")}
""";
+ });
+
+ var blocks = Assert.Single(UyapReader.Read(path, folder).Sections).Blocks;
+
+ var items = blocks.Take(3).Cast().Select(p => p.List!).ToList();
+ Assert.Equal([0, 1, 0], items.Select(i => i.Level));
+ Assert.All(items, i => Assert.Same(items[0].Numbering, i.Numbering));
+ Assert.Equal(new HNumberingLevel("ROMAN_CAPITAL", "^1)", 1, false), items[0].Numbering.Levels[0]);
+ Assert.Equal(new HNumberingLevel("DIGIT", "◆", 1, true), items[0].Numbering.Levels[1]);
+ var row = Assert.Single(Assert.IsType(blocks[3]).Rows);
+ Assert.True(row.Header);
+ Assert.Equal(3000, row.Height);
+ Assert.Equal((HVerticalAlign.Center, "#FFFF00"), (row.Cells[0].VerticalAlign, row.Cells[0].Fill));
+ Assert.Equal(HBorders.Empty, row.Cells[1].Borders);
+ var dashed = row.Cells[2].Borders!.Value;
+ Assert.Equal((HBorderStyle.Dash, HBorderStyle.Dash, HBorderStyle.None, HBorderStyle.None), (dashed.Top.Style, dashed.Bottom.Style, dashed.Left.Style, dashed.Right.Style));
+ Assert.Equal(("#FF0000", 0.35), (dashed.Top.Color, dashed.Top.WidthMm));
+ Assert.Null(row.Cells[2].Fill); // white is the editor's no fill
+ }
+
[Fact]
public void Runs_pointing_outside_the_text_and_a_file_without_elements_still_read()
{
@@ -252,12 +285,12 @@ private static string MakeDisc(string folder)
}
[Fact]
- public void Udf_is_a_read_only_document_and_extract_takes_it_too()
+ public void Udf_is_a_document_and_extract_takes_it_too()
{
var format = FormatRegistry.Detect("petition.udf");
Assert.Equal(UyapReader.Format, format?.Id);
Assert.Equal(FormatCategory.Document, format!.Category);
- Assert.False(format.Writable);
+ Assert.True(format.Writable); // the UYAP writer (#1546) writes it
Assert.Contains(UyapReader.Format, DocumentReaders.BuiltInFormats);
var planner = fx.Catalog.CreatePlanner();
@@ -300,6 +333,9 @@ public async Task A_petition_becomes_a_pdf_with_the_filee_renderer()
fx.Catalog.Refresh();
var folder = fx.NewFolder();
Assert.Equal(["layout"], fx.Catalog.CreatePlanner().Plan("udf", "pdf")!.Steps.Select(s => s.Converter.Id));
+ // Pictures of the pages (#1633) are drawn from that PDF, as a Word document's are.
+ foreach (var target in new[] { "png", "jpg" })
+ Assert.Equal(["layout", "pdfium"], fx.Catalog.CreatePlanner().Plan("udf", target)!.Steps.Select(s => s.Converter.Id));
var job = await fx.ConvertAsync([MakePetition(folder)], new Preset { TargetFormat = "pdf" });
@@ -317,6 +353,11 @@ public async Task A_petition_becomes_a_pdf_with_the_filee_renderer()
Assert.InRange(middle, 595.28 / 2 - 6, 595.28 / 2 + 6);
// The signature is right-aligned at the right margin.
Assert.InRange(words.Single(w => w.Text == "Applicant").BoundingBox.Right, 595.28 - 42.5 - 4, 595.28 - 42.5 + 1);
+
+ var png = await fx.ConvertAsync([MakePetition(fx.NewFolder())], new Preset { TargetFormat = "png" });
+ Assert.True(png.State == JobState.Completed, string.Join("; ", png.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ using var picture = new MagickImage(Assert.Single(png.Outputs));
+ Assert.True(picture.Height > picture.Width); // an A4 page
}
[Fact]
diff --git a/tests/Filee.Engines.Tests/UyapWriterTests.cs b/tests/Filee.Engines.Tests/UyapWriterTests.cs
new file mode 100644
index 000000000..f024d321a
--- /dev/null
+++ b/tests/Filee.Engines.Tests/UyapWriterTests.cs
@@ -0,0 +1,503 @@
+// The built-in UYAP writer (Uyap/UyapWriter, #1546, #1633): a model built in code with what a UYAP document holds
+// (page setup, header and footer, paragraph and character formatting, tab stops, lists, tables with merged and shaded
+// cells, pictures, page breaks) is written as .udf and read back by Filee's UYAP reader; content.xml is well-formed XML
+// laid out as the editor writes it (one CDATA text, every element's offsets covering it in order); text XML can't hold
+// is made safe without shifting offsets; documents go to UDF through the writer, and UDF to XLSX (pictures: UyapTests); and
+// models past the document limits are refused without leaving a file.
+
+using System.IO.Compression;
+using System.Xml;
+using System.Xml.Linq;
+using Filee.Core.Conversion;
+using Filee.Core.Presets;
+using Filee.Engines.Documents;
+using Filee.Engines.Hwp.Hwpx;
+using Filee.Engines.Uyap;
+using ImageMagick;
+
+namespace Filee.Engines.Tests;
+
+public class UyapWriterTests(EngineFixture fx) : IClassFixture
+{
+ /// Writes , checks its content.xml and reads it back with Filee's UYAP reader.
+ private (HDocument Document, XDocument Xml, string Text) RoundTrip(HDocument document, DocumentLimits? limits = null)
+ {
+ var folder = fx.NewFolder();
+ var path = Path.Combine(folder, "out.udf");
+ UyapWriter.Write(document, path, limits ?? DocumentLimits.Default, TestContext.Current.CancellationToken);
+ Assert.Single(Directory.GetFiles(folder)); // no temporary file left beside it
+ var (xml, text) = AssertValidUdf(path);
+ return (UyapReader.Read(path, Path.Combine(folder, "media")), xml, text);
+ }
+
+ ///
+ /// A ZIP with one content.xml that is well-formed XML 1.0 (characters checked) with a template root, one content
+ /// text, the page format and elements whose runs, pictures and page breaks cover the text from its start to its end,
+ /// one after the other, as the editor writes them.
+ ///
+ internal static (XDocument Xml, string Text) AssertValidUdf(string path)
+ {
+ Assert.True(UyapReader.IsUyap(path));
+ using var zip = ZipFile.OpenRead(path);
+ var entry = Assert.Single(zip.Entries);
+ Assert.Equal("content.xml", entry.FullName);
+ XDocument xml;
+ using (var stream = entry.Open())
+ using (var reader = XmlReader.Create(stream, new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, CheckCharacters = true }))
+ xml = XDocument.Load(reader, LoadOptions.PreserveWhitespace);
+ var root = xml.Root!;
+ Assert.Equal("template", root.Name.LocalName);
+ Assert.Equal("1.8", root.Attribute("format_id")?.Value);
+ var text = root.Element("content")!.Value;
+ Assert.NotNull(root.Element("properties")?.Element("pageFormat"));
+ Assert.Equal("hvl-default", root.Element("elements")?.Attribute("resolver")?.Value);
+ Assert.Contains(root.Element("styles")!.Elements("style"), s => s.Attribute("name")?.Value == "hvl-default");
+
+ var next = 0;
+ foreach (var run in root.Element("elements")!.Descendants().Where(e => e.Attribute("startOffset") is not null))
+ {
+ var start = int.Parse(run.Attribute("startOffset")!.Value, System.Globalization.CultureInfo.InvariantCulture);
+ var length = int.Parse(run.Attribute("length")!.Value, System.Globalization.CultureInfo.InvariantCulture);
+ Assert.True(start == next, $"A run starts at {start}, the text before it ends at {next}.");
+ Assert.True(length > 0, "An empty run.");
+ next = start + length;
+ if (run.Name.LocalName == "image")
+ Assert.Equal("\uFFFC", text.Substring(start, length));
+ }
+ // The editor's text ends with one "\n" more than its elements cover.
+ Assert.Equal(text.Length - 1, next);
+ Assert.EndsWith("\n", text, StringComparison.Ordinal);
+ // Every paragraph ends with its "\n".
+ foreach (var paragraph in root.Descendants("paragraph"))
+ {
+ var last = paragraph.Elements().Last(e => e.Attribute("startOffset") is not null);
+ var end = int.Parse(last.Attribute("startOffset")!.Value, System.Globalization.CultureInfo.InvariantCulture)
+ + int.Parse(last.Attribute("length")!.Value, System.Globalization.CultureInfo.InvariantCulture);
+ Assert.Equal('\n', text[end - 1]);
+ }
+ return (xml, text);
+ }
+
+ private string Picture(MagickColor color, uint width = 40, uint height = 30, string extension = "png")
+ {
+ var path = Path.Combine(fx.NewFolder(), $"picture.{extension}");
+ if (extension == "png")
+ {
+ File.WriteAllBytes(path, EbookBuilders.Png(width, height, color));
+ }
+ else
+ {
+ using var image = new MagickImage(color, width, height);
+ image.Write(path, extension == "gif" ? MagickFormat.Gif : MagickFormat.Bmp);
+ }
+ return path;
+ }
+
+ private static HParagraph Para(params HInline[] inlines)
+ {
+ var paragraph = new HParagraph();
+ paragraph.Inlines.AddRange(inlines);
+ return paragraph;
+ }
+
+ private static HParagraph Para(string text, HCharFormat format = default) => Para(new HText(text, format));
+
+ private static HCell Cell(string text, Action? setup = null)
+ {
+ var cell = new HCell();
+ cell.Blocks.Add(Para(text));
+ setup?.Invoke(cell);
+ return cell;
+ }
+
+ private static string Text(HParagraph paragraph) => string.Concat(paragraph.Inlines.Select(i => i switch
+ {
+ HText t => t.Text,
+ HTab => "\t",
+ HLineBreak => "\n",
+ _ => "",
+ }));
+
+ private static List Paragraphs(HDocument document) => document.Sections.Single().Blocks.OfType().ToList();
+
+ // ───────────────────────── Round trip ─────────────────────────
+
+ [Fact]
+ public void Paragraphs_runs_page_header_and_footer_come_back()
+ {
+ var document = new HDocument();
+ var section = new HSection { Page = new HPage(59528, 84189, 7000, 5000, 2000, 1800, 2500, 2200, 0) };
+ document.Sections.Add(section);
+ var header = new HHeaderFooter(HPageType.Both);
+ header.Blocks.Add(new HParagraph { Format = new HParaFormat(Align: HAlign.Right), Inlines = { new HText("Header text", new HCharFormat(Italic: true)) } });
+ section.Headers.Add(header);
+ var footer = new HHeaderFooter(HPageType.Both);
+ footer.Blocks.Add(new HParagraph { Format = new HParaFormat(Align: HAlign.Center), Inlines = { new HText("Footer text", default) } });
+ section.Footers.Add(footer);
+
+ var blocks = section.Blocks;
+ blocks.Add(new HParagraph
+ {
+ Format = new HParaFormat(Align: HAlign.Center),
+ Inlines = { new HText("Sample Court Office", new HCharFormat(Bold: true, Size: 1400, Font: "Calibri")) },
+ });
+ blocks.Add(Para(
+ new HText("Plain ", default),
+ new HText("bold", new HCharFormat(Bold: true)),
+ new HText(" italic", new HCharFormat(Italic: true, Size: 1050)),
+ new HText(" under", new HCharFormat(Underline: true, Color: "#C00000")),
+ new HText(" struck", new HCharFormat(Strike: true, Shade: "#FFFF00")),
+ new HText("up", new HCharFormat(Superscript: true)),
+ new HLineBreak(default),
+ new HText("after a break", default),
+ new HTab(default),
+ new HText("after a tab", default)));
+ blocks.Add(new HParagraph
+ {
+ Format = new HParaFormat(Align: HAlign.Justify, Left: 4700, Right: 1000, FirstLine: -2200, Before: 600, After: 300),
+ Inlines = { new HText("1.", default), new HTab(default), new HText("A hanging item.", default) },
+ });
+ blocks.Add(new HParagraph
+ {
+ Format = new HParaFormat(Tabs: [new HTabStop(5000, HTabKind.Center, "NONE"), new HTabStop(10000, HTabKind.Right, "DOT"), new HTabStop(15000, HTabKind.Decimal, "NONE")]),
+ Inlines = { new HText("Tabs", default), new HTab(default), new HText("centre", default) },
+ });
+ blocks.Add(new HParagraph
+ {
+ Format = new HParaFormat(LineSpacing: new HLineSpacing(HLineSpacingKind.Percent, 195)),
+ Inlines = { new HText("One and a half lines", new HCharFormat(Font: "Calibri")) },
+ });
+ blocks.Add(new HParagraph { MarkFormat = new HCharFormat(Size: 2000) }); // an empty paragraph keeps its mark's size
+ blocks.Add(new HParagraph { PageBreakBefore = true, Inlines = { new HText("On a new page", default) } });
+
+ var (read, xml, text) = RoundTrip(document);
+
+ var readSection = Assert.Single(read.Sections);
+ Assert.Equal(section.Page, readSection.Page);
+ Assert.Equal("Header text", Text(Assert.IsType(Assert.Single(Assert.Single(readSection.Headers).Blocks))));
+ Assert.Equal(HAlign.Right, Assert.IsType(readSection.Headers[0].Blocks[0]).Format.Align);
+ Assert.Equal("Footer text", Text(Assert.IsType(Assert.Single(Assert.Single(readSection.Footers).Blocks))));
+ // Header, body, footer: the text in that order.
+ Assert.StartsWith("Header text\n", text, StringComparison.Ordinal);
+ Assert.EndsWith("Footer text\n\n", text, StringComparison.Ordinal);
+
+ var paragraphs = Paragraphs(read);
+ Assert.Equal(7, paragraphs.Count);
+ var title = paragraphs[0];
+ Assert.Equal(HAlign.Center, title.Format.Align);
+ var titleRun = Assert.IsType(Assert.Single(title.Inlines));
+ Assert.Equal("Sample Court Office", titleRun.Text);
+ Assert.Equal((true, 1400, "Calibri"), (titleRun.Format.Bold, titleRun.Format.Size, titleRun.Format.Font));
+
+ var runs = paragraphs[1].Inlines;
+ Assert.Equal("Plain bold italic under struckup\nafter a break\tafter a tab", Text(paragraphs[1]));
+ Assert.Equal("Times New Roman", ((HText)runs[0]).Format.Font); // unset: the editor's body style
+ Assert.True(((HText)runs[1]).Format.Bold);
+ Assert.Equal((true, 1050), (((HText)runs[2]).Format.Italic, ((HText)runs[2]).Format.Size));
+ Assert.Equal((true, "#C00000"), (((HText)runs[3]).Format.Underline, ((HText)runs[3]).Format.Color));
+ Assert.Equal((true, "#FFFF00"), (((HText)runs[4]).Format.Strike, ((HText)runs[4]).Format.Shade));
+ Assert.True(((HText)runs[5]).Format.Superscript);
+ Assert.IsType(runs[6]);
+
+ var item = paragraphs[2].Format;
+ Assert.Equal((HAlign.Justify, 4700, 1000, -2200, 600, 300), (item.Align!.Value, item.Left!.Value, item.Right!.Value, item.FirstLine!.Value, item.Before!.Value, item.After!.Value));
+ // A hanging paragraph has the editor's stop at the indent, as its own files have.
+ Assert.Equal("0.0:0:0", xml.Root!.Element("elements")!.Elements("paragraph").ElementAt(2).Attribute("TabSet")?.Value);
+
+ Assert.Equal([new HTabStop(5000, HTabKind.Center, "NONE"), new HTabStop(10000, HTabKind.Right, "DOT"), new HTabStop(15000, HTabKind.Decimal, "NONE")],
+ paragraphs[3].Format.Tabs!);
+ // 195 % in 한글's terms is a line and a half: LineSpacing 0.5, read back as Java's 1.5 × 15 pt Calibri line.
+ Assert.Equal(new HLineSpacing(HLineSpacingKind.AtLeast, 2250), paragraphs[4].Format.LineSpacing);
+ Assert.Empty(paragraphs[5].Inlines);
+ Assert.Equal(2000, paragraphs[5].MarkFormat.Size);
+ Assert.True(paragraphs[6].PageBreakBefore);
+ Assert.NotNull(xml.Root!.Element("elements")!.Element("page-break")?.Element("paragraph"));
+ }
+
+ [Fact]
+ public void Lists_tables_and_pictures_come_back()
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ document.Sections.Add(section);
+ var numbers = new HNumbering();
+ numbers.Levels.Add(new HNumberingLevel("DIGIT", "^1.", 1, Bullet: false));
+ numbers.Levels.Add(new HNumberingLevel("LATIN_SMALL", "^2)", 1, Bullet: false));
+ var bullets = new HNumbering();
+ bullets.Levels.Add(new HNumberingLevel("DIGIT", "▪", 1, Bullet: true));
+ section.Blocks.Add(new HParagraph { List = new HListRef(numbers, 0, true), Inlines = { new HText("First", default) } });
+ section.Blocks.Add(new HParagraph { List = new HListRef(numbers, 1, true), Inlines = { new HText("Nested", default) } });
+ section.Blocks.Add(new HParagraph { List = new HListRef(numbers, 0, true), Inlines = { new HText("Second", default) } });
+ section.Blocks.Add(new HParagraph { List = new HListRef(bullets, 0, true), Inlines = { new HText("A bullet", default) } });
+
+ var table = new HTable { ColumnCount = 3, ColumnWidths = [10000, 15000, 20000] };
+ var head = new HRow { Header = true, Height = 1500 };
+ head.Cells.Add(Cell("Wide head", c =>
+ {
+ c.ColSpan = 2;
+ c.Fill = "#DDEEFF";
+ }));
+ head.Cells.Add(Cell("Tall", c =>
+ {
+ c.RowSpan = 2;
+ c.VerticalAlign = HVerticalAlign.Center;
+ }));
+ table.Rows.Add(head);
+ var middle = new HRow();
+ middle.Cells.Add(Cell("A2", c => c.Borders = HBorders.Empty));
+ middle.Cells.Add(Cell("B2", c => c.VerticalAlign = HVerticalAlign.Bottom));
+ table.Rows.Add(middle);
+ section.Blocks.Add(table);
+ section.Blocks.Add(Para(new HText("Logo: ", default), new HImage(Picture(MagickColors.Teal)) { Width = 4000, Height = 3000 },
+ new HImage(Picture(MagickColors.Red, extension: "gif")), new HText(" end", default)));
+
+ var (read, xml, _) = RoundTrip(document);
+
+ var blocks = read.Sections.Single().Blocks;
+ var lists = blocks.Take(4).Cast().Select(p => p.List!).ToList();
+ Assert.Same(lists[0].Numbering, lists[1].Numbering);
+ Assert.Same(lists[0].Numbering, lists[2].Numbering);
+ Assert.NotSame(lists[0].Numbering, lists[3].Numbering);
+ Assert.Equal([0, 1, 0, 0], lists.Select(l => l.Level));
+ Assert.Equal(new HNumberingLevel("DIGIT", "^1.", 1, false), lists[0].Numbering.Levels[0]);
+ Assert.Equal(new HNumberingLevel("LATIN_SMALL", "^2)", 1, false), lists[0].Numbering.Levels[1]);
+ Assert.Equal(new HNumberingLevel("DIGIT", "▪", 1, true), lists[3].Numbering.Levels[0]);
+ var listXml = xml.Root!.Element("elements")!.Elements("paragraph").ToList();
+ Assert.Equal("NUMBER_TYPE_CHAR_SMALL_PARENTHESIS", listXml[1].Attribute("NumberType")?.Value);
+ Assert.Equal("BULLET_TYPE_RECTANGLE", listXml[3].Attribute("BulletType")?.Value);
+
+ var readTable = Assert.IsType(blocks[4]);
+ Assert.Equal(3, readTable.ColumnCount);
+ Assert.Equal([10000 / 45000.0, 15000 / 45000.0, 20000 / 45000.0], readTable.RelativeWidths!.Select(w => Math.Round(w, 4)), (a, b) => Math.Abs(a - b) < 0.001);
+ Assert.True(readTable.Rows[0].Header);
+ Assert.Equal(1500, readTable.Rows[0].Height);
+ var wide = readTable.Rows[0].Cells[0];
+ Assert.Equal((2, "#DDEEFF"), (wide.ColSpan, wide.Fill));
+ var tall = readTable.Rows[0].Cells[1];
+ Assert.Equal(HVerticalAlign.Center, tall.VerticalAlign);
+ // The cell over two rows: no bottom border, and under it a cell of its own without the top one.
+ Assert.Equal(HBorderStyle.None, tall.Borders!.Value.Bottom.Style);
+ Assert.Equal(HBorderStyle.Solid, tall.Borders!.Value.Top.Style);
+ Assert.Equal(["A2", "B2", ""], readTable.Rows[1].Cells.Select(c => Text((HParagraph)c.Blocks[0])));
+ Assert.Equal(HBorders.Empty, readTable.Rows[1].Cells[0].Borders);
+ Assert.Equal(HVerticalAlign.Bottom, readTable.Rows[1].Cells[1].VerticalAlign);
+ var under = readTable.Rows[1].Cells[2].Borders!.Value;
+ Assert.Equal((HBorderStyle.None, HBorderStyle.Solid), (under.Top.Style, under.Bottom.Style));
+ Assert.Equal("rowName", xml.Root!.Descendants("row").First().Attributes().First().Name.LocalName);
+
+ var pictured = Assert.IsType(blocks[5]);
+ var images = pictured.Inlines.OfType().ToList();
+ Assert.Equal(2, images.Count);
+ Assert.Equal((4000, 3000), (images[0].Width, images[0].Height));
+ Assert.True(File.Exists(images[1].Path));
+ Assert.Equal(".png", Path.GetExtension(images[1].Path)); // the GIF became a PNG
+ Assert.Equal("Logo: end", Text(pictured));
+ }
+
+ [Fact]
+ public void Sections_notes_text_boxes_and_links_flatten_into_the_one_story()
+ {
+ var document = new HDocument();
+ var first = new HSection { Page = new HPage(84189, 59528, 4000, 4000, 2000, 2000, 2000, 2000, 0) };
+ var note = new HNote(false);
+ note.Blocks.Add(Para("The note's text."));
+ var link = new HLink("https://example.com");
+ link.Content.Add(new HText("a link", new HCharFormat(Underline: true)));
+ var box = new HTextBox { Width = 5000, Height = 2000 };
+ box.Blocks.Add(Para("In a box"));
+ first.Blocks.Add(Para(new HBookmark("b"), new HText("Text", default), note, new HText(" and ", default), link, box));
+ var second = new HSection();
+ second.Blocks.Add(Para("Second section"));
+ document.Sections.Add(first);
+ document.Sections.Add(second);
+
+ var (read, _, _) = RoundTrip(document);
+
+ Assert.True(read.Sections.Single().Page!.Landscape);
+ var paragraphs = Paragraphs(read);
+ Assert.Equal(["Text1 and a link", "In a box", "1 The note's text.", "Second section"], paragraphs.Select(Text));
+ Assert.True(((HText)paragraphs[0].Inlines[1]).Format.Superscript);
+ Assert.True(paragraphs[3].PageBreakBefore);
+ }
+
+ [Fact]
+ public void Text_xml_cannot_hold_is_made_safe_without_moving_the_runs()
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ document.Sections.Add(section);
+ section.Blocks.Add(Para(new HText("a]]>b\r\0c\uD800d", default), new HText("😀 bold ]]", new HCharFormat(Bold: true)),
+ new HText(">tail & ", default)));
+ section.Blocks.Add(Para("Türkçe metin: ğüşıöç İ", new HCharFormat(Font: "Times New \"Roman\" <&>")));
+
+ var (read, _, text) = RoundTrip(document);
+
+ Assert.Contains("a]]>b", text, StringComparison.Ordinal);
+ var paragraphs = Paragraphs(read);
+ Assert.Equal("a]]>bc\uFFFDd😀 bold ]]>tail & ", Text(paragraphs[0]));
+ Assert.True(((HText)paragraphs[0].Inlines[1]).Format.Bold);
+ Assert.Equal("😀 bold ]]", ((HText)paragraphs[0].Inlines[1]).Text);
+ Assert.Equal("Türkçe metin: ğüşıöç İ", Text(paragraphs[1]));
+ Assert.Equal("Times New \"Roman\" <&>", ((HText)paragraphs[1].Inlines[0]).Format.Font);
+ }
+
+ [Fact]
+ public void An_empty_model_still_makes_a_document_with_a_paragraph()
+ {
+ var (read, _, text) = RoundTrip(new HDocument());
+ Assert.Equal("\n\n", text);
+ Assert.Empty(Assert.IsType(Assert.Single(read.Sections.Single().Blocks)).Inlines);
+ Assert.Equal(new HPage(59528, 84189, 4253, 4253, 2000, 2000, 2253, 2253, 0), read.Sections.Single().Page);
+ }
+
+ // ───────────────────────── Routes ─────────────────────────
+
+ [Fact]
+ public void Documents_go_to_udf_through_the_writer_and_udf_reaches_sheets()
+ {
+ var planner = fx.Catalog.CreatePlanner();
+ foreach (var from in new[] { "docx", "odt", "rtf", "hwp", "hwpx", "md", "html", "txt", "doc", "pdf" })
+ Assert.Equal("udf-writer", Assert.Single(planner.Plan(from, "udf")!.Steps).Converter.Id);
+ Assert.Null(planner.Plan("udf", "udf"));
+ foreach (var target in new[] { "xlsx", "ods", "csv" })
+ Assert.Equal("document-tables", Assert.Single(planner.Plan("udf", target)!.Steps).Converter.Id);
+ // A document written as UDF on the way never unpacks: Extract takes only a .udf itself.
+ foreach (var from in new[] { "md", "docx", "pdf" })
+ Assert.DoesNotContain(planner.Plan(from, Filee.Core.Formats.FormatRegistry.Folder)?.Steps ?? [], s => s.Converter.Id == "udf-writer");
+ Assert.True(Filee.Core.Formats.FormatRegistry.FindById("udf")!.Writable);
+ }
+
+ [Fact]
+ public async Task Markdown_becomes_a_udf_and_a_udf_with_a_table_becomes_a_workbook()
+ {
+ var folder = fx.NewFolder();
+ var markdown = Path.Combine(folder, "petition.md");
+ await File.WriteAllTextAsync(markdown, "# Dilekçe\n\nMetin **kalın** ve *italik*.\n\n| Ad | Tutar |\n|---|---|\n| Vekalet | 81.388,81 |\n| Masraf | 120 |\n",
+ TestContext.Current.CancellationToken);
+
+ var job = await fx.ConvertAsync([markdown], new Preset { TargetFormat = "udf" });
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ var udf = Assert.Single(job.Outputs);
+ AssertValidUdf(udf);
+ var read = UyapReader.Read(udf, Path.Combine(folder, "media"));
+ var heading = read.Sections[0].Blocks.OfType().First();
+ Assert.Equal("Dilekçe", Text(heading));
+ Assert.True(heading.Inlines.OfType().All(t => t.Format.Bold == true));
+
+ var sheets = await fx.ConvertAsync([udf], new Preset { TargetFormat = "xlsx" });
+ Assert.True(sheets.State == JobState.Completed, string.Join("; ", sheets.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ using var workbook = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(Assert.Single(sheets.Outputs), false);
+ Assert.Single(workbook.WorkbookPart!.Workbook!.Sheets!.ChildElements);
+ }
+
+ // ───────────────────────── Limits ─────────────────────────
+
+ [Fact]
+ public void A_part_beyond_the_size_limit_is_refused_without_leaving_anything()
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ for (var i = 0; i < 2000; i++)
+ section.Blocks.Add(Para($"Paragraph {i} with some text to fill the file."));
+ document.Sections.Add(section);
+ var folder = fx.NewFolder();
+
+ var refused = Assert.Throws(() =>
+ UyapWriter.Write(document, Path.Combine(folder, "big.udf"), new DocumentLimits { MaxPartBytes = 20_000 }, TestContext.Current.CancellationToken));
+ Assert.Equal(DocumentLimits.ErrorKey, refused.ErrorKey);
+ Assert.Throws(() =>
+ UyapWriter.Write(document, Path.Combine(folder, "many.udf"), new DocumentLimits { MaxXmlElements = 1000 }, TestContext.Current.CancellationToken));
+ Assert.Throws(() =>
+ UyapWriter.Write(document, Path.Combine(folder, "long.udf"), new DocumentLimits { MaxParagraphs = 100 }, TestContext.Current.CancellationToken));
+ Assert.Empty(Directory.GetFiles(folder));
+ }
+
+ [Fact]
+ public void A_part_that_would_pass_the_inflate_ratio_is_stored()
+ {
+ var document = new HDocument();
+ var section = new HSection();
+ for (var i = 0; i < 3000; i++)
+ section.Blocks.Add(Para("Same text, again and again."));
+ document.Sections.Add(section);
+ var path = Path.Combine(fx.NewFolder(), "same.udf");
+
+ UyapWriter.Write(document, path, new DocumentLimits { InflateRatioFloor = 10_000, MaxInflateRatio = 3 }, TestContext.Current.CancellationToken);
+
+ using var zip = ZipFile.OpenRead(path);
+ var entry = Assert.Single(zip.Entries);
+ Assert.Equal(entry.Length, entry.CompressedLength);
+ Assert.Equal(3000, UyapReader.Read(path, fx.NewFolder()).Sections[0].Blocks.Count);
+ }
+
+ [Fact]
+ public void Hostile_models_are_refused_or_written_quickly()
+ {
+ // Tables 5,000 deep are opened up into paragraphs past the nesting limit.
+ var deep = new HDocument();
+ var section = new HSection();
+ deep.Sections.Add(section);
+ var blocks = section.Blocks;
+ for (var i = 0; i < 5000; i++)
+ {
+ var table = new HTable { ColumnCount = 1 };
+ var row = new HRow();
+ var cell = new HCell();
+ cell.Blocks.Add(Para($"level {i}"));
+ row.Cells.Add(cell);
+ table.Rows.Add(row);
+ blocks.Add(table);
+ blocks = cell.Blocks;
+ }
+ var started = System.Diagnostics.Stopwatch.StartNew();
+ var (read, _, text) = RoundTrip(deep);
+ Assert.True(started.Elapsed < TimeSpan.FromSeconds(30), $"Took {started.Elapsed}.");
+ Assert.NotEmpty(read.Sections[0].Blocks);
+ Assert.Contains("level 4999\n", text, StringComparison.Ordinal);
+
+ // A cell spanning 16,384 columns over 1,000 rows passes the table cell limit.
+ var wide = new HDocument();
+ var wideSection = new HSection();
+ var huge = new HTable { ColumnCount = 16_384 };
+ for (var r = 0; r < 1000; r++)
+ {
+ var row = new HRow();
+ row.Cells.Add(new HCell { ColSpan = 16_384 });
+ huge.Rows.Add(row);
+ }
+ wideSection.Blocks.Add(huge);
+ wide.Sections.Add(wideSection);
+ var folder = fx.NewFolder();
+ Assert.Throws(() => UyapWriter.Write(wide, Path.Combine(folder, "wide.udf"), TestContext.Current.CancellationToken));
+ Assert.Empty(Directory.GetFiles(folder));
+
+ // A picture whose header claims ten gigapixels is left out.
+ var claiming = DocumentFuzzTests.TinyPng(4, 3);
+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(claiming.AsSpan(16), 100_000);
+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(claiming.AsSpan(20), 100_000);
+ var picture = Path.Combine(fx.NewFolder(), "huge.png");
+ File.WriteAllBytes(picture, claiming);
+ var pictured = new HDocument();
+ var pictureSection = new HSection();
+ pictureSection.Blocks.Add(Para(new HText("x", default), new HImage(picture), new HImage(Path.Combine(folder, "missing.png"))));
+ pictured.Sections.Add(pictureSection);
+ var (_, xml, _) = RoundTrip(pictured);
+ Assert.Empty(xml.Descendants("image"));
+ }
+
+ [Fact]
+ public void A_cancelled_write_leaves_no_file()
+ {
+ var folder = fx.NewFolder();
+ using var cancelled = new CancellationTokenSource();
+ cancelled.Cancel();
+ var document = new HDocument();
+ var section = new HSection();
+ section.Blocks.Add(Para("text"));
+ document.Sections.Add(section);
+
+ Assert.ThrowsAny(() => UyapWriter.Write(document, Path.Combine(folder, "out.udf"), cancelled.Token));
+
+ Assert.Empty(Directory.GetFiles(folder));
+ }
+}