using System.Text; using System.Text.RegularExpressions; namespace AIStudio.Tools.DocumentSegments; /// /// Makes new words fit into a Markdown file, so that they stay words. /// /// /// /// The words of a Markdown file reach the model as they read, i.e., with their escapes and entities decoded, see /// MarkdownSegmentFormat; the model never sees markup. So the new words need escapes where Markdown would read them /// as markup: /// /// /// Characters which start or end inline markup, e.g., * of emphasis, [ of a link, | of a table cell, or $ of a /// formula, get a backslash. Markdown shows an escaped character as the character itself, wherever it stands. /// Characters which start or end inline markup only when doubled, e.g., ++ of inserted words, == of marked /// words, or "" of a citation, get a backslash when doubled. /// Words which would start a block at the start of a line, e.g., "1. January" as a numbered list, "- " as a /// list, "#" as a heading, or ">" as a quote, get a backslash before that mark. The extensions also take a letter /// or a Roman numeral for a numbered list, e.g., "A. Smith" or "IV. Results". /// An ampersand which would start an entity, e.g., &copy;, becomes &amp;. /// Line breaks become spaces: a blank line would end the paragraph or the table row. /// /// internal static partial class MarkdownWords { /// /// The characters which start or end inline markup of Markdig with the extensions the format reads, e.g., /// emphasis, links, code, tables, formulas, strikethrough, and superscript. /// private const string INLINE_MARKUP = "\\`*_[]<|$~^"; /// /// The characters which start or end inline markup of the extensions only when doubled, e.g., ++ of inserted words. /// private const string DOUBLED_INLINE_MARKUP = "+=\""; [GeneratedRegex(@"\A(?[ \t]*)(?[-+>]|#{1,6}|(?:\d{1,9}|[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)(?=[.)]))(?[.)]?)(?=[ \t]|\z|(?<=>))")] private static partial Regex BlockStartRegex(); [GeneratedRegex(@"&(?=#?[A-Za-z0-9]+;)")] private static partial Regex EntityStartRegex(); /// /// Makes new words fit into a Markdown file. /// /// The new words, as the model wrote them. public static string Encoded(string words) { var line = TextSegmentWriter.InOneLine(words); var result = new StringBuilder(line.Length + 8); for (var index = 0; index < line.Length; index++) { var character = line[index]; if (INLINE_MARKUP.Contains(character) || (DOUBLED_INLINE_MARKUP.Contains(character) && IsDoubled(line, index))) result.Append('\\'); result.Append(character); } // The mark of a numbered list keeps its number or letter and gets the backslash before its period or parenthesis: var encoded = EntityStartRegex().Replace(result.ToString(), "&"); return BlockStartRegex().Replace(encoded, match => match.Groups["mark"].Value.All(char.IsAsciiLetterOrDigit) ? $"{match.Groups["indent"].Value}{match.Groups["mark"].Value}\\{match.Groups["rest"].Value}" : $"{match.Groups["indent"].Value}\\{match.Groups["mark"].Value}{match.Groups["rest"].Value}"); } private static bool IsDoubled(string line, int index) => (index > 0 && line[index - 1] == line[index]) || (index + 1 < line.Length && line[index + 1] == line[index]); }