Carry sources into exported code blocks

This commit is contained in:
Thorsten Sommer committed 2026-09-23 13:41:29 +02:00
1 parent 2a6ae556eb
commit 465e46b8d4
4 files changed
+222 -17

No files matched your search

+83 -3
View File
@@ -162,16 +162,85 @@ public sealed class IContentExtensionsTests
"|---|---|",
"| Q1 | 100 |"), TOOL_SOURCE);
content.TryGetMarkdownText(out var markdown);
var tables = PlainFileExport.ExtractFiles(markdown, ',');
var tables = FilesOf(content);
Assert.Multiple(() =>
{
Assert.That(tables, Has.Count.EqualTo(1), "One table in the message, one table offered for it.");
Assert.That(tables[0].Content, Does.Not.Contain("example.org"), "A data table has no column a link list would fit into.");
Assert.That(content.ToExportContent(tables[0]), Is.EqualTo(tables[0].Content), "A data table has no column a link list would fit into.");
});
}
[Test]
public void AMarkdownBlockCarriesTheSourcesVisibly()
{
var content = TextWith(Lines(
"Here are your notes:",
string.Empty,
"```markdown",
"# Notes",
string.Empty,
"The notes.",
"```"), TOOL_SOURCE);
var exported = content.ToExportContent(FilesOf(content).Single());
Assert.That(TopLevelBlocksOf(exported), Is.EqualTo(new[] { "h1", "ParagraphBlock", "h1", "h2", "ListBlock" }), "The notes without the text around them, followed by the source list just as the entire answer carries it.");
}
[Test]
public void AWebPageCarriesTheSourcesInAComment()
{
var content = TextWith(Lines(
"```html",
"<!DOCTYPE html>",
"<html><body><p>Hello</p></body></html>",
"```"), TOOL_SOURCE);
var file = FilesOf(content).Single();
var exported = content.ToExportContent(file);
var appended = exported[file.Content.Length..].Trim();
Assert.Multiple(() =>
{
Assert.That(exported, Does.StartWith(file.Content), "The page stays as the model wrote it.");
Assert.That(appended, Does.StartWith("<!--").And.EndWith("-->"), "Below the page stands one comment and nothing a browser would show.");
Assert.That(appended, Does.Contain(TOOL_SOURCE.URL));
});
}
[Test]
public void ALatexBlockCarriesTheSourcesInComments()
{
var content = TextWith(Lines(
"```latex",
@"\section{Results}",
"```"), TOOL_SOURCE);
var file = FilesOf(content).Single();
var exported = content.ToExportContent(file);
var appended = exported[file.Content.Length..].Trim();
Assert.Multiple(() =>
{
Assert.That(exported, Does.StartWith(file.Content), "The document stays as the model wrote it.");
Assert.That(appended.Split(Environment.NewLine), Has.All.StartWith("%"), "A line LaTeX would read could stop the whole run.");
Assert.That(appended, Does.Contain(TOOL_SOURCE.URL));
});
}
[TestCase("markdown")]
[TestCase("html")]
[TestCase("latex")]
public void WithoutSourcesACodeBlockStaysAsTheModelWroteIt(string language)
{
var content = TextWith(Lines($"```{language}", "The content.", "```"));
var file = FilesOf(content).Single();
Assert.That(content.ToExportContent(file), Is.EqualTo(file.Content), "No comment, no heading, no empty line.");
}
/// <summary>
/// A text message with the given sources hanging on it.
/// </summary>
@@ -184,6 +253,17 @@ public sealed class IContentExtensionsTests
Sources = [..sources],
};
/// <summary>
/// Reads the files of a message the way the export menu does.
/// </summary>
/// <param name="content">The content to read.</param>
/// <returns>The files the export menu offers for it.</returns>
private static IReadOnlyList<MessageFile> FilesOf(IContent content)
{
content.TryGetMarkdownText(out var markdown);
return PlainFileExport.ExtractFiles(markdown, ',');
}
/// <summary>
/// Names the blocks a Markdown text is made of, headings by their level.
/// </summary>
+50
View File
@@ -69,4 +69,54 @@ public sealed class FileExportFormatTests
Assert.That(format, Is.EqualTo(FileExportFormat.NONE));
});
}
[TestCase(FileExportFormat.HTML)]
[TestCase(FileExportFormat.MARKDOWN)]
public void AnHtmlCommentEndsWhereItShouldAndNowhereElse(FileExportFormat format)
{
var found = format.TryToComment("A page titled --> Start, and one titled --!> Next", out var comment);
Assert.Multiple(() =>
{
Assert.That(found, Is.True);
Assert.That(comment, Does.StartWith("<!--"));
Assert.That(comment.IndexOf("-->", StringComparison.Ordinal), Is.EqualTo(comment.Length - 3), "Only the end of the comment may end it; the title would spill onto the page otherwise.");
Assert.That(comment, Does.Not.Contain("--!>"), "A browser ends a comment there as well.");
Assert.That(comment, Does.Contain("Start").And.Contain("Next"), "The title stays readable.");
});
}
[Test]
public void AnHtmlCommentKeepsTheDashesOfAnAddress()
{
FileExportFormat.HTML.TryToComment("https://xn--mnchen-3ya.de/", out var comment);
Assert.That(comment, Does.Contain("https://xn--mnchen-3ya.de/"), "A domain with an umlaut is written with two dashes, and the link has to keep working.");
}
[Test]
public void EveryLineOfALatexCommentIsOne()
{
var found = FileExportFormat.LATEX.TryToComment(Lines("# Sources", string.Empty, "- [1] A title with 100 % and a_b"), out var comment);
Assert.Multiple(() =>
{
Assert.That(found, Is.True);
Assert.That(comment.Split(Environment.NewLine), Is.EqualTo(new[] { "% # Sources", "%", "% - [1] A title with 100 % and a_b" }), "LaTeX has no end of a comment, only the end of a line.");
});
}
[TestCase(FileExportFormat.CSV)]
[TestCase(FileExportFormat.TSV)]
[TestCase(FileExportFormat.MICROSOFT_WORD)]
public void AFormatWithoutCommentsSaysSo(FileExportFormat format)
{
Assert.Multiple(() =>
{
Assert.That(format.TryToComment("A text.", out var comment), Is.False);
Assert.That(comment, Is.Empty);
});
}
private static string Lines(params string[] lines) => string.Join(Environment.NewLine, lines);
}