working on parsing markdown as a quiz

This commit is contained in:
alex
2023-10-03 15:52:05 -06:00
parent c690f074f3
commit 9ca475dc44
6 changed files with 95 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using YamlDotNet.Serialization;
namespace LocalModels;
@@ -56,4 +57,61 @@ Description: {Description}
{questionMarkdown}
";
}
public static LocalQuiz ParseMarkdown(string input)
{
var splitInput = input.Split(Environment.NewLine + Environment.NewLine);
var settings = splitInput[0];
var name = extractLabelValue(settings, "Name");
var lockAtDueDate = bool.Parse(extractLabelValue(settings, "LockAtDueDate"));
var shuffleAnswers = bool.Parse(extractLabelValue(settings, "ShuffleAnswers"));
var oneQuestionAtATime = bool.Parse(extractLabelValue(settings, "OneQuestionAtATime"));
var allowedAttempts = int.Parse(extractLabelValue(settings, "AllowedAttempts"));
var dueAt = DateTime.Parse(extractLabelValue(settings, "DueAt"));
var lockAt = DateTime.Parse(extractLabelValue(settings, "LockAt"));
var description = extractDescription(settings);
// var assignmentGroup = ExtractLabelValue(settings, "AssignmentGroup");
return new LocalQuiz()
{
Id = "id-" + name,
Name = name,
Description = description,
LockAtDueDate = lockAtDueDate,
LockAt = lockAt,
DueAt = dueAt,
ShuffleAnswers = shuffleAnswers,
OneQuestionAtATime = oneQuestionAtATime,
// LocalAssignmentGroupId = "someId",
AllowedAttempts = allowedAttempts,
Questions = new LocalQuizQuestion[] { }
};
}
static string extractLabelValue(string input, string label)
{
string pattern = $@"{label}: (.*?)\n";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
return match.Groups[1].Value;
}
return string.Empty;
}
static string extractDescription(string input)
{
string pattern = "Description: (.*?)$";
Match match = Regex.Match(input, pattern, RegexOptions.Singleline);
if (match.Success)
{
return match.Groups[1].Value;
}
return string.Empty;
}
}