From a9cf38ecf227ee4161152b0b9a24dd56dda6a161 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 29 Mar 2024 14:34:38 -0600 Subject: [PATCH] fixed multiline regex --- .../Markdown/Quiz/MultipleAnswersTests.cs | 16 +++++++++++++++- Management/Models/Local/Quiz/LocalQuiz.cs | 3 ++- .../Models/Local/Quiz/LocalQuizQuestion.cs | 6 +++--- .../Models/Local/Quiz/LocalQuizQuestionAnswer.cs | 7 ++++++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Management.Test/Markdown/Quiz/MultipleAnswersTests.cs b/Management.Test/Markdown/Quiz/MultipleAnswersTests.cs index 41e6ebe..3adc7fc 100644 --- a/Management.Test/Markdown/Quiz/MultipleAnswersTests.cs +++ b/Management.Test/Markdown/Quiz/MultipleAnswersTests.cs @@ -54,7 +54,7 @@ DueAt: 2023-08-21T23:59:00 LockAt: 2023-08-21T23:59:00 AssignmentGroup: Assignments AllowedAttempts: -1 -Description: this is the +Description: this is the multi line description --- @@ -80,4 +80,18 @@ Which events are triggered when the user clicks on an input field? firstQuestion.Answers.ElementAt(3).Text.Should().Be("submit"); } + + [Test] + public void CanUseBracesInAnswerFormultipleAnswer() + { + var rawMarkdownQuestion = @" +Which events are triggered when the user clicks on an input field? +[*] `int[] theThing()` +[] keydown +"; + + var question = LocalQuizQuestion.ParseMarkdown(rawMarkdownQuestion, 0); + question.Answers.First().Text.Should().Be("`int[] theThing()`"); + question.Answers.Count().Should().Be(2); + } } diff --git a/Management/Models/Local/Quiz/LocalQuiz.cs b/Management/Models/Local/Quiz/LocalQuiz.cs index 8d13375..bde6d4b 100644 --- a/Management/Models/Local/Quiz/LocalQuiz.cs +++ b/Management/Models/Local/Quiz/LocalQuiz.cs @@ -67,7 +67,8 @@ Description: {Description} var settings = splitInput[0]; var quizWithoutQuestions = getQuizWithOnlySettings(settings); - var questions = splitInput[1..] + var rawQuestions = splitInput[1..]; + var questions = rawQuestions .Where(str => !string.IsNullOrWhiteSpace(str)) .Select((q, i) => LocalQuizQuestion.ParseMarkdown(q, i)) .ToArray(); diff --git a/Management/Models/Local/Quiz/LocalQuizQuestion.cs b/Management/Models/Local/Quiz/LocalQuizQuestion.cs index 1efe8f4..9ef4b88 100644 --- a/Management/Models/Local/Quiz/LocalQuizQuestion.cs +++ b/Management/Models/Local/Quiz/LocalQuizQuestion.cs @@ -114,7 +114,7 @@ public record LocalQuizQuestion if (linesWithoutPoints[^1].Equals("short_answer", StringComparison.CurrentCultureIgnoreCase)) return "short_answer"; - var answerLines = getAnswersGroupedByLines(linesWithoutPoints, questionIndex); + var answerLines = getAnswerStringsWithMultilineSupport(linesWithoutPoints, questionIndex); var firstAnswerLine = answerLines.First(); var isMultipleChoice = firstAnswerLine.StartsWith("a)") @@ -138,7 +138,7 @@ public record LocalQuizQuestion return ""; } - private static List getAnswersGroupedByLines(string[] linesWithoutPoints, int questionIndex) + private static List getAnswerStringsWithMultilineSupport(string[] linesWithoutPoints, int questionIndex) { var indexOfAnswerStart = linesWithoutPoints .ToList() @@ -175,7 +175,7 @@ public record LocalQuizQuestion private static LocalQuizQuestionAnswer[] getAnswers(string[] linesWithoutPoints, int questionIndex, string questionType) { - var answerLines = getAnswersGroupedByLines(linesWithoutPoints, questionIndex); + var answerLines = getAnswerStringsWithMultilineSupport(linesWithoutPoints, questionIndex); var answers = answerLines .Select((a, i) => LocalQuizQuestionAnswer.ParseMarkdown(a, questionType)) diff --git a/Management/Models/Local/Quiz/LocalQuizQuestionAnswer.cs b/Management/Models/Local/Quiz/LocalQuizQuestionAnswer.cs index 0d44199..2bf2dbb 100644 --- a/Management/Models/Local/Quiz/LocalQuizQuestionAnswer.cs +++ b/Management/Models/Local/Quiz/LocalQuizQuestionAnswer.cs @@ -30,7 +30,12 @@ public record LocalQuizQuestionAnswer } string startingQuestionPattern = @"^(\*?[a-z]?\))|\[\s*\]|\[\*\]|\^ "; - var text = Regex.Replace(input, startingQuestionPattern, string.Empty).Trim(); + + int replaceCount = 0; + var text = Regex.Replace(input, startingQuestionPattern, (m) => + { + return replaceCount++ == 0 ? "" : m.Value; + }).Trim(); return new LocalQuizQuestionAnswer() { Correct = isCorrect,