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 static void DecorateSpansWithSkippedText(LessonDocument document) { // Decorate the LessonSpans with skipped parts var spans = document.FlattenSpans(); // Find gaps between the spans LessonSpan lastSpan = null; foreach (var span in spans) { var nextExpectedIndex = lastSpan != null?lastSpan.Content.GetIndexAfter() : 0; if (span.Content.Index > nextExpectedIndex) { // Get skipped content var skipLength = span.Content.Index - nextExpectedIndex; var skipContent = new StringWithIndex(document.Content.Source, nextExpectedIndex, skipLength); span.SkippedPreText = skipContent; } else if (span.Content.Index == nextExpectedIndex) { // Blank Skipped span.SkippedPreText = new StringWithIndex(document.Content.Source, nextExpectedIndex, 0); } // TODO: move to testing else { var lastSpanContent = lastSpan.Content; var skipped = span.SkippedPreText; var spanContent = span.Content; throw new ArgumentException("The document is malformed and has overlapping spans"); } lastSpan = span; } }
public async Task <string> DocumentUpload(List <IFormFile> files, int lessonId, string path) { foreach (var formFile in files) { if (formFile.Length > 0) { var filename = formFile.FileName; var guidKey = Guid.NewGuid(); if (!Directory.Exists(path + "\\Upload\\Documents\\")) { Directory.CreateDirectory(path + "\\Upload\\Documents\\"); } var extension = formFile.FileName.Split(".")[1]; var fullPath = path + "\\Upload\\Documents\\" + guidKey + "." + extension; using (var stream = System.IO.File.Create(fullPath)) { await formFile.CopyToAsync(stream); } var lessonDocument = new LessonDocument() { LessonId = lessonId, FileKey = guidKey, FileName = filename, FilePath = fullPath }; ctx.LessonDocuments.Add(lessonDocument); } } await ctx.SaveChangesAsync(); return("Upload Successyfully"); }