// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Gauntlet { /// /// Helper class to generate text formatted as markdown /// public class MarkdownBuilder { protected StringBuilder SB; public MarkdownBuilder() { SB = new StringBuilder(); } public MarkdownBuilder(string Contents) { SB = new StringBuilder(Contents); } /// /// Returns our formatted text /// /// public override string ToString() { return SB.ToString(); } /// /// Returns true if the correct body ends with a new line /// public bool EndsWithNewLine { get { return SB.Length == 0 || SB[SB.Length-1] == '\n'; } } /// /// Ensures any text after this starts on a new line /// /// public MarkdownBuilder EnsureEndsWithNewLine() { if (SB.Length > 0 && !EndsWithNewLine) { NewLine(); } return this; } /// ///Insert an H1 Header /// /// /// public MarkdownBuilder H1(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("# {0}", Text); return this; } /// /// Insert an H2 header /// /// /// public MarkdownBuilder H2(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("## {0}", Text); return this; } /// /// Insert an H3 header /// /// /// public MarkdownBuilder H3(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("### {0}", Text); return this; } /// /// Insert an H4 Header /// /// /// public MarkdownBuilder H4(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("##### {0}", Text); return this; } /// /// Insert an H5 header /// /// /// public MarkdownBuilder H5(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("###### {0}\n", Text); return this; } /// /// Insert a newline /// /// public MarkdownBuilder NewLine() { SB.AppendLine(); return this; } /// /// Insert an H5 header /// /// /// public MarkdownBuilder HorizontalLine(int Count=80) { EnsureEndsWithNewLine(); for (int i = 0; i < Count; i++) { SB.Append("-"); } SB.AppendLine(); return this; } /// /// Insert a paragraph block /// /// /// public MarkdownBuilder Paragraph(string Text) { EnsureEndsWithNewLine(); SB.Append(Text); NewLine(); NewLine(); return this; } /// /// Insert an ordered (numbered) list /// /// /// public MarkdownBuilder OrderedList(IEnumerable Items) { EnsureEndsWithNewLine(); int Index = 1; foreach (string Item in Items) { SB.AppendFormat("{0}. {1}", Index++, Item); SB.AppendLine(); } NewLine(); return this; } /// /// Insert an unordered (bulleted) list /// /// /// public MarkdownBuilder UnorderedList(IEnumerable Items) { EnsureEndsWithNewLine(); foreach (string Item in Items) { if (Item != null) { SB.AppendFormat("* {0}", Item); SB.AppendLine(); } } NewLine(); return this; } /// /// Append the provided Markdown to our body /// /// /// public MarkdownBuilder Append(MarkdownBuilder RHS) { EnsureEndsWithNewLine(); SB.Append(RHS.ToString()); return this; } /// /// Append the provided text to our body. No formatting will be applied /// /// /// public MarkdownBuilder Append(string RHS) { EnsureEndsWithNewLine(); SB.Append(RHS); return this; } } }