// 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 { public class HtmlBuilder { protected StringBuilder SB; protected bool CurrentlyBuildingTable = false; /// /// All of the possible ways to modify a stock table cell. /// public class TableCellInfo { public bool WithBorder = true; public bool CenterAlign = true; /// /// Number of columns the cell spans. /// public int CellColumnWidth = 1; public string BackgroundColor = "#ffffff"; public string BorderType = "border:1px solid black;"; public string TableContents; public void ResetToDefaults() { CellColumnWidth = 1; BackgroundColor = "#ffffff"; BorderType = "border:1px solid black;"; CenterAlign = true; WithBorder = true; } } /// /// All of the possible ways to modify a basic string via formatting. /// public class TextOptions { public bool Italics = false; public bool Bold = false; public string TextColorOverride = string.Empty; public string Hyperlink = string.Empty; public string ApplyToString(string TextToAlter) { string OutString = TextToAlter; if (Italics) { OutString = string.Format("{0}", OutString); } if (Bold) { OutString = string.Format("{0}", OutString); } if (!string.IsNullOrEmpty(TextColorOverride)) { OutString = string.Format("{1}", TextColorOverride, OutString); } if (!string.IsNullOrEmpty(Hyperlink)) { OutString = string.Format("{1}", Hyperlink, OutString); } return OutString; } } public HtmlBuilder() { SB = new StringBuilder(); } /// /// 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.ToString().EndsWith("
") ; } } /// /// Ensures any text after this starts on a new line /// /// public HtmlBuilder EnsureEndsWithNewLine() { if (SB.Length > 0 && !EndsWithNewLine) { NewLine(); } return this; } /// /// Returns true if the correct body ends with a new line /// public bool EndsWithRowClose { get { return SB.Length == 0 || SB.ToString().EndsWith(""); } } /// /// Ensures any text after this starts on a new line /// /// public HtmlBuilder EnsureEndsWithRowClose() { if (SB.Length > 0 && !EndsWithRowClose) { SB.Append(""); } return this; } /// /// Open up a new table, if one doesn't already exist, to start adding cells to. /// /// public HtmlBuilder StartBuildingTable() { if (!CurrentlyBuildingTable) { EnsureEndsWithNewLine(); SB.Append(""); CurrentlyBuildingTable = true; } return this; } /// /// Open a new row. Close any existing open rows if they already exist. /// /// public HtmlBuilder StartRow() { EnsureEndsWithRowClose(); SB.Append(""); return this; } /// /// Finalize a row with the proper tags. /// /// public HtmlBuilder EndRow() { SB.Append(""); return this; } protected string CreateCell(string TableContents, TableCellInfo CellInfo) { return string.Format("", CellInfo.BackgroundColor, CellInfo.CellColumnWidth, CellInfo.CenterAlign ? "align=center" : "", CellInfo.WithBorder ? string.Format(" style=\"{0}\"", CellInfo.BorderType) : "", TableContents); } /// /// Add a new cell with any formatting applied. /// /// /// /// public HtmlBuilder AddNewCell(string TableContents, TableCellInfo CellInfo = null) { SB.Append(CreateCell(TableContents, CellInfo != null ? CellInfo : new TableCellInfo())); return this; } /// /// Finish up an existing table. Close off any existing rows before closing the table and newlining. /// /// public HtmlBuilder FinalizeTable() { if (CurrentlyBuildingTable) { EnsureEndsWithRowClose(); SB.Append("
{4}

"); CurrentlyBuildingTable = false; } return this; } public HtmlBuilder NewLine() { SB.Append("
"); return this; } /// /// Insert a hyperlink /// /// /// /// public HtmlBuilder Hyperlink(string URL, string Text) { SB.AppendFormat("{1}", URL, Text); return this; } /// ///Insert an H1 Header /// /// /// public HtmlBuilder H1(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("

{0}

", Text); return this; } /// /// Insert an H2 header /// /// /// public HtmlBuilder H2(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("

{0}

", Text); return this; } /// /// Insert an H3 header /// /// /// public HtmlBuilder H3(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("

{0}

", Text); return this; } /// /// Insert an H4 Header /// /// /// public HtmlBuilder H4(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("

{0}

", Text); return this; } /// /// Insert an H5 header /// /// /// public HtmlBuilder H5(string Text) { EnsureEndsWithNewLine(); SB.AppendFormat("
{0}
", Text); return this; } /// /// Insert an ordered (numbered) list /// /// /// public HtmlBuilder OrderedList(IEnumerable Items) { EnsureEndsWithNewLine(); SB.Append("
    "); foreach (string Item in Items) { SB.AppendFormat("
  1. {0}
  2. ", Item); SB.AppendLine(); } SB.Append("
"); NewLine(); return this; } /// /// Insert an unordered (bulleted) list /// /// /// public HtmlBuilder UnorderedList(IEnumerable Items) { EnsureEndsWithNewLine(); SB.Append(""); NewLine(); return this; } /// /// Append the provided Markdown to our body /// /// /// public HtmlBuilder Append(HtmlBuilder RHS) { SB.Append(RHS.ToString()); return this; } /// /// Append the provided text to our body. No formatting will be applied /// /// /// public HtmlBuilder Append(string RHS) { SB.Append(RHS); return this; } /// /// Append the provided text to our body. No formatting will be applied /// /// /// /// public HtmlBuilder AppendFormatted(string RHS, TextOptions InOptions) { if (InOptions == null) { SB.Append(RHS); } else { SB.Append(InOptions.ApplyToString(RHS)); } return this; } } }