public static Doc.FlowDocument CreateDocument(IRecipe recipe, ICommand linkToRecipeCommand) { Doc.FlowDocument doc = new Doc.FlowDocument (); doc.PagePadding = new System.Windows.Thickness (40); doc.ColumnWidth = 640; doc.Typography.Fraction = System.Windows.FontFraction.Slashed; doc.FontFamily = new System.Windows.Media.FontFamily ("Palatino Linotype"); var cookTimeConverter = new Converters.CookTimespanConverter (); { Doc.Section recipeHeader = new Doc.Section (); doc.Blocks.Add (recipeHeader); Doc.Paragraph p; // Recipe Name p = recipeHeader.AddParagraph (); p.Inlines.Add (new Doc.Underline (new Doc.Run(recipe.Name))); if (recipe.Ratings != null && recipe.Ratings.Count > 0) { int rating = (recipe.Ratings == null || recipe.Ratings.Count == 0) ? 0 : (int)(recipe.Ratings.Aggregate (0.0, (sum, r) => sum + r.Value) / recipe.Ratings.Count); string ratedString = new string('«', rating); string notRatedString = new string('«', m_ratingDescriptions.Length - rating); p = recipeHeader.AddParagraph (); if (!string.IsNullOrEmpty (ratedString)) p.Inlines.Add (new Doc.Run(ratedString){FontFamily = new System.Windows.Media.FontFamily ("Wingdings"), Foreground=System.Windows.Media.Brushes.Red}); if (!string.IsNullOrEmpty (notRatedString)) p.Inlines.Add (new Doc.Run(notRatedString){FontFamily = new System.Windows.Media.FontFamily ("Wingdings"), Foreground=System.Windows.Media.Brushes.Silver}); } if (recipe.Source != null) { p = recipeHeader.AddParagraph (); p.Inlines.Add (new Doc.Italic (new Doc.Run("From "))); p.Inlines.Add (new Doc.Run(recipe.Source.Name)); } if (recipe.Servings.ToDouble () > 0 || recipe.Yield.ToDouble () > 0) { string yieldAndOrServings = null; if (recipe.Servings.ToDouble () > 0) { yieldAndOrServings = "Serves " + recipe.Servings; } if (recipe.Yield.ToDouble () > 0) { string yieldUnit = recipe.YieldUnit == null ? null : recipe.YieldUnit.Name; string makes = "Makes " + recipe.Yield + " " + yieldUnit; if (yieldAndOrServings != null) yieldAndOrServings = yieldAndOrServings + " \u2022 " + makes; else yieldAndOrServings = makes; } recipeHeader.AddParagraph ().Inlines.Add (new Doc.Run(yieldAndOrServings)); } //string tags = // recipe.Tags // .Aggregate( // new StringBuilder ("Tags: "), // (sb, t) => // { // sb.Append (t.Name); // sb.Append (", "); // return sb; // }, // sb => sb[sb.Length - 2] == ',' ? sb.ToString (0, sb.Length - 2) : sb.ToString ()); //recipeHeader.AddParagraph ().Inlines.Add (new Doc.Run(tags))); } foreach (IRecipePart part in recipe.Parts) { Doc.Section partHeader = new Doc.Section (); doc.Blocks.Add (partHeader); // // Part Name // if (!string.IsNullOrWhiteSpace (part.Name) && part.Name != recipe.Name) { var pName = partHeader.AddParagraph (); pName.Margin=new Thickness (0); pName.Inlines.Add (new Doc.Underline (new Doc.Run(part.Name))); } // // Times/prep method // string prepMethod = part.PreparationMethod != null ? part.PreparationMethod.Name : null; SmartListFormatter times = new SmartListFormatter (" \u2022 ", ": "); if (part.PreparationTime != TimeSpan.Zero) times.Append ("Prep Time", cookTimeConverter.Convert (part.PreparationTime, typeof (string), null, null) as string); if (prepMethod != null) times.Append (prepMethod); if (part.CookTime != TimeSpan.Zero) times.Append ("Cook", cookTimeConverter.Convert (part.CookTime, typeof (string), null, null) as string); if (part.Temperature != 0) times.Append (part.Temperature + " \u00BA F"); if (part.ChillTime != TimeSpan.Zero) times.Append ("Chill", cookTimeConverter.Convert (part.ChillTime, typeof (string), null, null) as string); if (times.GetStringBuilder ().Length > 0) { Doc.Paragraph stats = partHeader.AddParagraph (); stats.Margin=new Thickness (0); stats.Inlines.Add (new Doc.Run(times.ToString ())); } // // Ingredients // if (part.Ingredients != null) { Doc.Paragraph partIngredients = partHeader.AddParagraph (); partIngredients.TextAlignment = TextAlignment.Left; partIngredients.Margin = new Thickness (0); partIngredients.Inlines.Add (new Doc.LineBreak ()); partIngredients.Inlines.Add (new Doc.Run ("Ingredients:")); foreach (var ingredient in part.Ingredients) { partIngredients.Inlines.Add (new Doc.LineBreak ()); Sellars.Data.Model.ModelId<IRecipe> recipeId; if (ingredient.Ingredient == null) recipeId = null; else recipeId = ingredient.Ingredient.Id; string text = IngredientToString (ingredient); string ingredientName = ingredient.Ingredient.Name; if (recipeId != null && recipeId.Id != Guid.Empty && linkToRecipeCommand != null && !string.IsNullOrEmpty (ingredientName)) { int iName = text.IndexOf (ingredientName); if (iName > 0) partIngredients.Inlines.Add (new Doc.Run (text.Substring (0, iName))); partIngredients.Inlines.Add (new Doc.Hyperlink (new Doc.Run (ingredientName)){Command=linkToRecipeCommand, CommandParameter=recipeId}); if (text.Length - iName - ingredientName.Length > 0) partIngredients.Inlines.Add (new Doc.Run (text.Substring (iName + ingredientName.Length))); } else { partIngredients.Inlines.Add (new Doc.Run (text)); } }; } if (!string.IsNullOrWhiteSpace (part.Instructions)) { var p = partHeader.AddParagraph (); p.Inlines.Add (new Doc.LineBreak ()); p.Inlines.Add (new Doc.Run("Instructions:")); foreach (string paragraph in part.Instructions.Split (new [] {Environment.NewLine}, StringSplitOptions.None)) { p.Inlines.Add (new Doc.LineBreak ()); p.Inlines.Add (new Doc.Run(paragraph)); } } string comments = (part.Comments == null || part.Comments.Count == 0) ? "" : part.Comments .Aggregate( new StringBuilder (), (sb, t) => { sb.Append ("On " + t.CreatedOn + ", " + t.UserName + " commented: " + t.Text + Environment.NewLine); return sb; }, sb => sb.ToString (0, sb.Length - Environment.NewLine.Length)); if (!string.IsNullOrWhiteSpace (comments)) partHeader.AddParagraph ().Inlines.Add (new Doc.Run(comments)); } return doc; }
CreateDocument(IRecipe recipe, ICommand linkToRecipeCommand) { Doc.FlowDocument doc = new Doc.FlowDocument(); doc.PagePadding = new System.Windows.Thickness(40); doc.ColumnWidth = 640; doc.Typography.Fraction = System.Windows.FontFraction.Slashed; doc.FontFamily = new System.Windows.Media.FontFamily("Palatino Linotype"); var cookTimeConverter = new Converters.CookTimespanConverter(); { Doc.Section recipeHeader = new Doc.Section(); doc.Blocks.Add(recipeHeader); Doc.Paragraph p; // Recipe Name p = recipeHeader.AddParagraph(); p.Inlines.Add(new Doc.Underline(new Doc.Run(recipe.Name))); if (recipe.Ratings != null && recipe.Ratings.Count > 0) { int rating = (recipe.Ratings == null || recipe.Ratings.Count == 0) ? 0 : (int)(recipe.Ratings.Aggregate(0.0, (sum, r) => sum + r.Value) / recipe.Ratings.Count); string ratedString = new string('«', rating); string notRatedString = new string('«', m_ratingDescriptions.Length - rating); p = recipeHeader.AddParagraph(); if (!string.IsNullOrEmpty(ratedString)) { p.Inlines.Add(new Doc.Run(ratedString) { FontFamily = new System.Windows.Media.FontFamily("Wingdings"), Foreground = System.Windows.Media.Brushes.Red }); } if (!string.IsNullOrEmpty(notRatedString)) { p.Inlines.Add(new Doc.Run(notRatedString) { FontFamily = new System.Windows.Media.FontFamily("Wingdings"), Foreground = System.Windows.Media.Brushes.Silver }); } } if (recipe.Source != null) { p = recipeHeader.AddParagraph(); p.Inlines.Add(new Doc.Italic(new Doc.Run("From "))); p.Inlines.Add(new Doc.Run(recipe.Source.Name)); } if (recipe.Servings.ToDouble() > 0 || recipe.Yield.ToDouble() > 0) { string yieldAndOrServings = null; if (recipe.Servings.ToDouble() > 0) { yieldAndOrServings = "Serves " + recipe.Servings; } if (recipe.Yield.ToDouble() > 0) { string yieldUnit = recipe.YieldUnit == null ? null : recipe.YieldUnit.Name; string makes = "Makes " + recipe.Yield + " " + yieldUnit; if (yieldAndOrServings != null) { yieldAndOrServings = yieldAndOrServings + " \u2022 " + makes; } else { yieldAndOrServings = makes; } } recipeHeader.AddParagraph().Inlines.Add(new Doc.Run(yieldAndOrServings)); } //string tags = // recipe.Tags // .Aggregate( // new StringBuilder ("Tags: "), // (sb, t) => // { // sb.Append (t.Name); // sb.Append (", "); // return sb; // }, // sb => sb[sb.Length - 2] == ',' ? sb.ToString (0, sb.Length - 2) : sb.ToString ()); //recipeHeader.AddParagraph ().Inlines.Add (new Doc.Run(tags))); } foreach (IRecipePart part in recipe.Parts) { Doc.Section partHeader = new Doc.Section(); doc.Blocks.Add(partHeader); // // Part Name // if (!string.IsNullOrWhiteSpace(part.Name) && part.Name != recipe.Name) { var pName = partHeader.AddParagraph(); pName.Margin = new Thickness(0); pName.Inlines.Add(new Doc.Underline(new Doc.Run(part.Name))); } // // Times/prep method // string prepMethod = part.PreparationMethod != null ? part.PreparationMethod.Name : null; SmartListFormatter times = new SmartListFormatter(" \u2022 ", ": "); if (part.PreparationTime != TimeSpan.Zero) { times.Append("Prep Time", cookTimeConverter.Convert(part.PreparationTime, typeof(string), null, null) as string); } if (prepMethod != null) { times.Append(prepMethod); } if (part.CookTime != TimeSpan.Zero) { times.Append("Cook", cookTimeConverter.Convert(part.CookTime, typeof(string), null, null) as string); } if (part.Temperature != 0) { times.Append(part.Temperature + " \u00BA F"); } if (part.ChillTime != TimeSpan.Zero) { times.Append("Chill", cookTimeConverter.Convert(part.ChillTime, typeof(string), null, null) as string); } if (times.GetStringBuilder().Length > 0) { Doc.Paragraph stats = partHeader.AddParagraph(); stats.Margin = new Thickness(0); stats.Inlines.Add(new Doc.Run(times.ToString())); } // // Ingredients // if (part.Ingredients != null) { Doc.Paragraph partIngredients = partHeader.AddParagraph(); partIngredients.TextAlignment = TextAlignment.Left; partIngredients.Margin = new Thickness(0); partIngredients.Inlines.Add(new Doc.LineBreak()); partIngredients.Inlines.Add(new Doc.Run("Ingredients:")); foreach (var ingredient in part.Ingredients) { partIngredients.Inlines.Add(new Doc.LineBreak()); Sellars.Data.Model.ModelId <IRecipe> recipeId; if (ingredient.Ingredient == null) { recipeId = null; } else { recipeId = ingredient.Ingredient.Id; } string text = IngredientToString(ingredient); string ingredientName = ingredient.Ingredient.Name; if (recipeId != null && recipeId.Id != Guid.Empty && linkToRecipeCommand != null && !string.IsNullOrEmpty(ingredientName)) { int iName = text.IndexOf(ingredientName); if (iName > 0) { partIngredients.Inlines.Add(new Doc.Run(text.Substring(0, iName))); } partIngredients.Inlines.Add(new Doc.Hyperlink(new Doc.Run(ingredientName)) { Command = linkToRecipeCommand, CommandParameter = recipeId }); if (text.Length - iName - ingredientName.Length > 0) { partIngredients.Inlines.Add(new Doc.Run(text.Substring(iName + ingredientName.Length))); } } else { partIngredients.Inlines.Add(new Doc.Run(text)); } } ; } if (!string.IsNullOrWhiteSpace(part.Instructions)) { var p = partHeader.AddParagraph(); p.Inlines.Add(new Doc.LineBreak()); p.Inlines.Add(new Doc.Run("Instructions:")); foreach (string paragraph in part.Instructions.Split(new [] { Environment.NewLine }, StringSplitOptions.None)) { p.Inlines.Add(new Doc.LineBreak()); p.Inlines.Add(new Doc.Run(paragraph)); } } string comments = (part.Comments == null || part.Comments.Count == 0) ? "" : part.Comments .Aggregate( new StringBuilder(), (sb, t) => { sb.Append("On " + t.CreatedOn + ", " + t.UserName + " commented: " + t.Text + Environment.NewLine); return(sb); }, sb => sb.ToString(0, sb.Length - Environment.NewLine.Length)); if (!string.IsNullOrWhiteSpace(comments)) { partHeader.AddParagraph().Inlines.Add(new Doc.Run(comments)); } } return(doc); }