Skip to content

Commit

Permalink
Markdown Core
Browse files Browse the repository at this point in the history
Core classes to support Markdown generation
  • Loading branch information
modery committed Nov 8, 2022
1 parent 262d0c7 commit cf2a66e
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
132 changes: 132 additions & 0 deletions PowerDocu.Common/MarkdownBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PowerDocu.Common
{
public abstract class MarkdownBuilder
{
protected readonly Random random = new Random();
protected Dictionary<string, string> SVGImages;

protected string AddExpressionDetails(List<Expression> inputs)
{
StringBuilder tableSB = new StringBuilder("<table>");
foreach (Expression input in inputs)
{
StringBuilder operandsCellSB = new StringBuilder("<td>");

if (input.expressionOperands.Count > 1)
{
StringBuilder operandsTableSB = new StringBuilder("<table>");
foreach (object actionInputOperand in input.expressionOperands)
{
if (actionInputOperand.GetType() == typeof(Expression))
{
operandsTableSB.Append(AddExpressionTable((Expression)actionInputOperand, false));
}
else
{
operandsTableSB.Append("<tr><td>").Append(actionInputOperand.ToString()).Append("</td></tr>");
}
}
operandsTableSB.Append("</table>");
operandsCellSB.Append(operandsTableSB).Append("</td>");
}
else
{
if (input.expressionOperands.Count > 0)
{
if (input.expressionOperands[0]?.GetType() == typeof(Expression))
{
operandsCellSB.Append(AddExpressionTable((Expression)input.expressionOperands[0]).Append("</table>"));
}
else
{
operandsCellSB.Append(input.expressionOperands[0]?.ToString());
}
}
else
{
operandsCellSB.Append("");
}
operandsCellSB.Append("</td>");
}
tableSB.Append("<tr><td>").Append(input.expressionOperator).Append("</td>").Append(operandsCellSB).Append("</tr>");
}
tableSB.Append("</table>");
return tableSB.ToString();
}

protected StringBuilder AddExpressionTable(Expression expression, bool createNewTable = true, bool firstColumnBold = false)
{
StringBuilder table = createNewTable ? new StringBuilder("<table>") : new StringBuilder();

if (expression?.expressionOperator != null)
{
StringBuilder tr = new StringBuilder("<tr>");
StringBuilder tc = new StringBuilder("<td>");

if (firstColumnBold)
{
tc.Append("<b>").Append(expression.expressionOperator).Append("</b>");
}
else
{
tc.Append(expression.expressionOperator);
}
tr.Append(tc.Append("</td>"));
tc = new StringBuilder("<td>");
if (expression.expressionOperands.Count > 1)
{
StringBuilder operandsTable = new StringBuilder("<table>");
foreach (var expressionOperand in expression.expressionOperands.OrderBy(o => o.ToString()).ToList())
{
if (expressionOperand.GetType().Equals(typeof(string)))
{
operandsTable.Append("<tr><td>").Append((string)expressionOperand).Append("</td></tr>");
}
else if (expressionOperand.GetType().Equals(typeof(Expression)))
{
operandsTable.Append(AddExpressionTable((Expression)expressionOperand, false));
}
else
{
operandsTable.Append("<tr><td></td></tr>");
}
}
tc.Append(operandsTable).Append("</table>");
}
else if (expression.expressionOperands.Count == 0)
{
//nothing to do here
}
else
{
object expo = expression.expressionOperands[0];
if (expo.GetType().Equals(typeof(string)))
{
tc.Append((expression.expressionOperands.Count == 0) ? "" : expression.expressionOperands[0]?.ToString());
}
else
{
tc.Append(AddExpressionTable((Expression)expo, true));
}
}
tr.Append(tc).Append("</td>");
table.Append(tr.Append("</tr>"));
}
if (createNewTable)
{
table.Append("</table>");
}
return table;
}

protected string getLinkFromText(string text)
{
return "#" + text.ToLower();
}
}
}
13 changes: 13 additions & 0 deletions PowerDocu.Common/OutputFormatHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace PowerDocu.Common
{
public static class OutputFormatHelper
{
public static readonly string Word = "Word";
public static readonly string Markdown = "Markdown";
public static readonly string All = "All";
}
}
4 changes: 3 additions & 1 deletion PowerDocu.Common/PowerDocu.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<DebugType>embedded</DebugType>
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<!-- To generate Word document -->
<PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
<!-- To generate Markdown files -->
<PackageReference Include="Grynwald.MarkdownGenerator" Version="2.5.34" />
<!-- For some of the JSON parsing -->
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<!-- to convert the SVG into a PNG -->
Expand Down

0 comments on commit cf2a66e

Please sign in to comment.