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);
}
}
public static string GetValidHRef(this Link link)
{
if (link.Href.StartsWith("link:\\"))
return link.Href.Substring(7);
if (link.Href.StartsWith("link:"))
return link.Href.Substring(5);
return link.Href;
}
static void ToHtml(this IInlineElement elt, IHtmlContentBuilder sb)
{
switch (elt.GetType().FullName)
{
case "AsciiDocNet.Link":
Link link = (Link)elt;
Uri uri;
if (Uri.TryCreate(link.Href,
UriKind.RelativeOrAbsolute
, out uri))
{
if (string.IsNullOrEmpty(link.Text))
{
link.Text = $"{uri.Host}({uri.LocalPath})";
}
}
sb.AppendFormat("{1} ", link.GetValidHRef(), link.Text);
break;
case "AsciiDocNet.TextLiteral":
var tl = elt as TextLiteral;
if (tl?.Attributes.Anchor!=null)
{
sb.AppendFormat("{1} ", tl.Attributes.Anchor.Id, tl.Attributes.Anchor.XRefLabel);
}
if (tl!=null) sb.Append(tl.Text);
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;
case "AsciiDocNet.InternalAnchor":
InternalAnchor a = (InternalAnchor) elt;
sb.AppendFormat("{1} ", a.Id, a.XRefLabel);
break;
case "AsciiDocNet.Subscript":
sb.AppendHtml("");
Subscript sub = (Subscript)elt;
sub.ToHtml(sb);
sb.AppendHtml("");
break;
case "AsciiDocNet.Superscript":
sb.AppendHtml("");
Superscript sup = (Superscript)elt;
sup.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($"