mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
can parse at least some markdown quiz questions
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace LocalModels;
|
||||
|
||||
public record LocalQuizQuestion
|
||||
@@ -21,7 +23,7 @@ public record LocalQuizQuestion
|
||||
|
||||
|
||||
var questionTypeIndicator = isMultipleChoice
|
||||
? $"{correctIndicator}{questionLetter}) "
|
||||
? $"{correctIndicator}{questionLetter}) "
|
||||
: $"[{correctIndicator}] ";
|
||||
|
||||
var textWithSpecificNewline = answer.Text.Replace(Environment.NewLine, Environment.NewLine + " ");
|
||||
@@ -35,6 +37,67 @@ public record LocalQuizQuestion
|
||||
---
|
||||
";
|
||||
}
|
||||
|
||||
private static readonly string[] validFirstAnswerDelimiters = new string[] { "*a)", "a)", "[ ]", "[*]" };
|
||||
public static LocalQuizQuestion ParseMarkdown(string input)
|
||||
{
|
||||
var lines = input.Split(Environment.NewLine);
|
||||
var firstLineIsPoints = lines.First().Contains("points: ", StringComparison.CurrentCultureIgnoreCase);
|
||||
int points = firstLineIsPoints ? int.Parse(lines.First().Split(": ")[1]) : 1;
|
||||
|
||||
var linesWithoutPoints = firstLineIsPoints ? lines[1..] : lines;
|
||||
|
||||
var linesWithoutAnswers = linesWithoutPoints
|
||||
.TakeWhile(line => !validFirstAnswerDelimiters.Any(prefix => line.TrimStart().StartsWith(prefix)))
|
||||
.ToArray();
|
||||
var description = string.Join(Environment.NewLine, linesWithoutAnswers);
|
||||
|
||||
|
||||
LocalQuizQuestionAnswer[] answers = getAnswers(linesWithoutPoints);
|
||||
|
||||
return new LocalQuizQuestion()
|
||||
{
|
||||
Text = description,
|
||||
Points = points,
|
||||
Answers = answers
|
||||
};
|
||||
}
|
||||
|
||||
private static LocalQuizQuestionAnswer[] getAnswers(string[] linesWithoutPoints)
|
||||
{
|
||||
var indexOfAnswerStart = linesWithoutPoints
|
||||
.ToList()
|
||||
.FindIndex(
|
||||
l => validFirstAnswerDelimiters.Any(prefix => l.TrimStart().StartsWith(prefix))
|
||||
);
|
||||
var answerLinesRaw = linesWithoutPoints[indexOfAnswerStart..];
|
||||
|
||||
var answerStartPattern = @"^(\*?[a-z]\))|\[\s*\]|\[\*\]";
|
||||
var answerLines = answerLinesRaw.Aggregate(new List<string>(), (acc, line) =>
|
||||
{
|
||||
if (!Regex.IsMatch(line, answerStartPattern))
|
||||
{
|
||||
if (acc.Count != 0) // Append to the previous line if there is one
|
||||
{
|
||||
int lastIndex = acc.Count - 1;
|
||||
acc[lastIndex] += Environment.NewLine + line;
|
||||
}
|
||||
else
|
||||
{
|
||||
acc.Add(line);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
acc.Add(line); // Add as a new line if it matches the pattern
|
||||
}
|
||||
|
||||
return acc;
|
||||
});
|
||||
|
||||
var answers = answerLines.Select(LocalQuizQuestionAnswer.ParseMarkdown).ToArray();
|
||||
return answers;
|
||||
}
|
||||
}
|
||||
|
||||
public static class QuestionType
|
||||
|
||||
Reference in New Issue
Block a user