public PreParsedRecipe Preparse(string text) { PreParsedRecipe preParsed = new PreParsedRecipe(); text.Split(new string[] { '\n'.ToString() }, StringSplitOptions.None) .ToList() .ForEach((string s) => { preParsed.AddLine(new RecipeLine { Data = s, DataType = RecipeDataType.Unknown }); }); RecipeStateMachine stateMachine = new RecipeStateMachine(); preParsed.Lines.ForEach((RecipeLine line) => { if (line.DataType != RecipeDataType.Unknown) return; if (line.IsEmpty && (line.Previous == null || !line.Previous.IsEmpty)) stateMachine.AdvanceState(); CategorizeLine(line, stateMachine); if (line.DataType != RecipeDataType.Ingredients) return; if (line.Data.Trim().StartsWith("-") && line.Previous != null) { line.DataType = RecipeDataType.NotNeeded; var previousIngredient = FindPreviousIngredient(line); var additionalData = line.Data.Replace("- ", ""); previousIngredient.Data = previousIngredient.Data + additionalData; } }); preParsed.Condense(); return preParsed; }
private void CategorizeLine(RecipeLine line, RecipeStateMachine stateMachine) { if (line.IsEmpty) line.DataType = RecipeDataType.NotNeeded; else { switch (stateMachine.CurrentState) { case MMFRecipeStates.StartingSection: line.DataType = RecipeDataType.Unknown; break; case MMFRecipeStates.TitleSection: line.DataType = RecipeDataType.Title; break; case MMFRecipeStates.IngredientsSection: line.DataType = RecipeDataType.Ingredients; break; case MMFRecipeStates.DescriptionSection: line.DataType = RecipeDataType.Instructions; break; } } }