private LessonDocument ParseDocument(StringWithIndex text) { var sections = text.SplitWithoutModification(new string[] { "\n# STEP = " }); var header = sections[0]; var steps = sections.Skip(1).ToList(); var document = new LessonDocument(text); // Get the header metadata var headerLines = header.GetLines(); var title = headerLines.GetFirstLineValue("% TITLE = ").Trim(); document.Children.Add(new LessonDocumentTitle(title)); // Process the steps foreach (var s in steps) { document.Children.Add(ParseStep(s)); } // Add an end of file span (to catch pretext that was not processed) document.Children.Add(new LessonEnd(new StringWithIndex(text.Text, text.Length, 0))); DecorateNodesWithParent(document); DecorateSpansWithSkippedText(document); return(document); }
private LessonNode ParseStep(StringWithIndex text) { text = text.Trim(); var step = new LessonStep(text); // Divide the parts var parts = text.SplitWithoutModification("\n#"); // The header includes the title and the instructions var header = parts[0]; var headerParts = header.SplitAfterFirstLine(); // Parse the step title var titlePart = headerParts[0]; step.Children.Add(new LessonStepTitle(titlePart.TrimStart("# STEP = "))); // Add instructions - the first section (no header) var instructionsPart = headerParts[1]; step.Children.Add(ParseInstructions(instructionsPart)); // Add Goal var goalPart = parts.First(p => p.Text.StartsWith("\n## GOAL")); step.Children.Add(ParseGoal(goalPart)); // Add Summary var summaryPart = parts.First(p => p.Text.StartsWith("\n## SUMMARY")); step.Children.Add(ParseSummary(summaryPart)); // Add Test var testPart = parts.FirstOrDefault(p => p.Text.StartsWith("\n## TEST")); if (testPart != null) { step.Children.Add(ParseTest(testPart)); } // Add Explanation var explanationPart = parts.FirstOrDefault(p => p.Text.StartsWith("\n## EXPLANATION")); if (explanationPart != null) { step.Children.Add(ParseExplanation(explanationPart)); } // Add File var filePart = parts.FirstOrDefault(p => p.Text.StartsWith("\n## FILE = ")); if (filePart != null) { step.Children.Add(ParseFile(filePart)); } // TODO: Add other parts // Order children var ordered = step.Children.OrderBy(c => c.Content.Index).ToList(); step.Children.Clear(); step.AddChildren(ordered); return(step); }