using System.Text; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using AsciiDocNet; using Yavsc.Models.Blog; using System.Linq.Expressions; namespace Yavsc.Helpers { public static class AsciiDocHelpers { static void ToHtml(this IElement elt, IHtmlContentBuilder contentbuilder) { switch (elt.GetType().FullName) { case "AsciiDocNet.Paragraph": Paragraph p = (Paragraph) elt; contentbuilder.AppendHtmlLine("
"); foreach (var pitem in p) { pitem.ToHtml(contentbuilder); } contentbuilder.AppendHtmlLine("
"); break; case "AsciiDocNet.SectionTitle": SectionTitle stitle = (SectionTitle) elt; contentbuilder.AppendHtmlLine($"");
contentbuilder.Append(source.Text);
contentbuilder.AppendHtmlLine("");
break;
default:
string unsupportedType = elt.GetType().FullName;
throw new InvalidProgramException(unsupportedType);
}
}
static void ToHtml(this IInlineElement elt, IHtmlContentBuilder sb)
{
switch (elt.GetType().FullName)
{
case "AsciiDocNet.Link":
Link link = (Link) elt;
sb.AppendFormat("{1} ",link.Href, link.Text);
break;
case "AsciiDocNet.TextLiteral":
sb.Append(elt.ToString());
break;
case "AsciiDocNet.Emphasis":
sb.AppendHtml("");
AsciiDocNet.Emphasis em = (Emphasis) elt;
sb.Append(em.Text);
sb.AppendHtml("");
break;
case "AsciiDocNet.Strong":
sb.AppendHtml("");
AsciiDocNet.Strong str = (Strong) elt;
foreach (var stritem in str)
{
stritem.ToHtml(sb);
}
sb.AppendHtml("");
break;
default:
string unsupportedType = elt.GetType().FullName;
throw new InvalidProgramException(unsupportedType);
}
}
public static IHtmlContent ToHtml(this Document doc, int doclevel = 4)
{
var contentbuilder = new HtmlContentBuilder();
if (doc.Title != null)
{
if (!string.IsNullOrWhiteSpace(doc.Title.Title))
{
contentbuilder.AppendHtmlLine($"