AnkiIO 1.0.2
Build, validate, import, and export Anki-compatible decks from .NET
Loading...
Searching...
No Matches
AnkiCloze.cs
1using System.Globalization;
2
3namespace AnkiIO;
4
26public static class AnkiCloze
27{
55 public static string Wrap(string text, int index = 1, string? hint = null)
56 {
57 ArgumentException.ThrowIfNullOrEmpty(text);
58 ArgumentOutOfRangeException.ThrowIfLessThan(index, 1);
59 RejectDelimiter(text, nameof(text));
60 if (hint is not null)
61 {
62 RejectDelimiter(hint, nameof(hint));
63 }
64
65 var indexText = index.ToString(CultureInfo.InvariantCulture);
66 return hint is null
67 ? $"{{{{c{indexText}::{text}}}}}"
68 : $"{{{{c{indexText}::{text}::{hint}}}}}";
69 }
70
71 private static void RejectDelimiter(string value, string parameterName)
72 {
73 if (value.Contains("{{", StringComparison.Ordinal)
74 || value.Contains("::", StringComparison.Ordinal)
75 || value.Contains("}}", StringComparison.Ordinal))
76 {
77 throw new ArgumentException("Cloze content cannot contain the structural delimiters '{{', '::', or '}}'.", parameterName);
78 }
79 }
80}
Builds conservative Anki cloze-deletion markup for a Cloze note's Text field.
Definition AnkiCloze.cs:27
static string Wrap(string text, int index=1, string? hint=null)
Wraps answer text in Anki cloze-deletion markup.
Definition AnkiCloze.cs:55