yavsc/src/Yavsc/Helpers/AsciiDocHelpers.cs

212 lines
7.8 KiB
C#

9 months ago
3 years ago
using Microsoft.AspNetCore.Html;
using AsciiDocNet;
namespace Yavsc.Helpers
{
public static class AsciiDocHelpers
{
3 years ago
static void ToHtml(this IElement elt, IHtmlContentBuilder contentbuilder)
3 years ago
{
3 years ago
switch (elt.GetType().FullName)
{
case "AsciiDocNet.Paragraph":
Paragraph p = (Paragraph)elt;
3 years ago
contentbuilder.AppendHtmlLine("<p>");
foreach (var pitem in p)
{
pitem.ToHtml(contentbuilder);
}
contentbuilder.AppendHtmlLine("</p>");
break;
case "AsciiDocNet.SectionTitle":
3 years ago
SectionTitle stitle = (SectionTitle)elt;
3 years ago
contentbuilder.AppendHtmlLine($"<h{stitle.Level}>");
foreach (var titem in stitle)
{
titem.ToHtml(contentbuilder);
}
contentbuilder.AppendHtmlLine("</h>");
break;
case "AsciiDocNet.UnorderedList":
3 years ago
UnorderedList ul = (UnorderedList)elt;
3 years ago
contentbuilder.AppendHtmlLine("<ul>");
foreach (var li in ul.Items)
{
contentbuilder.AppendHtmlLine("<li>");
3 years ago
3 years ago
foreach (var lii in li)
{
3 years ago
lii.ToHtml(contentbuilder);
3 years ago
}
contentbuilder.AppendHtmlLine("</li>");
}
contentbuilder.AppendHtmlLine("</ul>");
break;
case "AsciiDocNet.Source":
3 years ago
Source source = (Source)elt;
3 years ago
// TODO syntact hilighting and fun js modules
contentbuilder.AppendHtmlLine("<pre><code>");
9 months ago
contentbuilder.AppendHtml(source.Text);
3 years ago
contentbuilder.AppendHtmlLine("</code></pre>");
break;
3 years ago
default:
string unsupportedType = elt.GetType().FullName;
throw new InvalidProgramException(unsupportedType);
}
3 years ago
}
2 years ago
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;
}
3 years ago
3 years ago
static void ToHtml(this IInlineElement elt, IHtmlContentBuilder sb)
3 years ago
{
3 years ago
switch (elt.GetType().FullName)
{
case "AsciiDocNet.Link":
Link link = (Link)elt;
2 years ago
Uri uri;
if (Uri.TryCreate(link.Href,
UriKind.RelativeOrAbsolute
, out uri))
{
if (string.IsNullOrEmpty(link.Text))
{
link.Text = $"{uri.Host}({uri.LocalPath})";
2 years ago
}
}
sb.AppendFormat("<a href=\"{0}\">{1}</a> ", link.GetValidHRef(), link.Text);
3 years ago
break;
3 years ago
3 years ago
case "AsciiDocNet.TextLiteral":
RenderLitteral(elt, sb);
3 years ago
break;
case "AsciiDocNet.Emphasis":
sb.AppendHtml("<i>");
AsciiDocNet.Emphasis em = (Emphasis)elt;
9 months ago
sb.AppendHtml(em.Text);
3 years ago
sb.AppendHtml("</i>");
break;
case "AsciiDocNet.Strong":
sb.AppendHtml("<b>");
AsciiDocNet.Strong str = (Strong)elt;
foreach (var stritem in str)
{
stritem.ToHtml(sb);
}
sb.AppendHtml("</b>");
break;
2 years ago
case "AsciiDocNet.InternalAnchor":
InternalAnchor a = (InternalAnchor)elt;
2 years ago
sb.AppendFormat("<a name=\"{0}\">{1}</a> ", a.Id, a.XRefLabel);
break;
1 year ago
case "AsciiDocNet.Subscript":
sb.AppendHtml("<sup>");
1 year ago
Subscript sub = (Subscript)elt;
RenderLitteral(sub, sb);
1 year ago
sb.AppendHtml("</sup>");
break;
case "AsciiDocNet.Superscript":
sb.AppendHtml("<sup>");
1 year ago
Superscript sup = (Superscript)elt;
RenderLitteral(sup, sb);
1 year ago
sb.AppendHtml("</sup>");
break;
case "AsciiDocNet.Mark":
sb.AppendHtml("<em>");
Mark mark = (Mark)elt;
if (mark.DoubleDelimited)
{
sb.AppendHtml("<b>");
RenderLitteral(mark, sb);
sb.AppendHtml("</b>");
}
else
RenderLitteral(mark, sb);
sb.AppendHtml("</em>");
break;
3 years ago
default:
string unsupportedType = elt.GetType().FullName;
throw new InvalidProgramException(unsupportedType);
}
3 years ago
}
private static void RenderLitteral(IInlineElement elt, IHtmlContentBuilder sb)
{
var tl = elt as TextLiteral;
if (tl?.Attributes.Anchor != null)
{
9 months ago
sb.AppendHtmlLine($"<a name=\"{tl.Attributes.Anchor.Id}\">{tl.Attributes.Anchor.XRefLabel}</a> ");
}
9 months ago
if (tl != null) sb.AppendHtml(tl.Text);
}
3 years ago
public static IHtmlContent ToHtml(this Document doc, int doclevel = 4)
3 years ago
{
3 years ago
var contentbuilder = new HtmlContentBuilder();
if (doc.Title != null)
3 years ago
{
3 years ago
if (!string.IsNullOrWhiteSpace(doc.Title.Title))
3 years ago
{
3 years ago
contentbuilder.AppendHtmlLine($"<h{doclevel}>{doc.Title.Title}</h{doclevel}>");
if (!string.IsNullOrWhiteSpace(doc.Title.Subtitle))
{
contentbuilder.AppendHtmlLine($"<i>{doc.Title.Title}</i><br/>");
}
3 years ago
}
}
3 years ago
foreach (var item in doc)
{
item.ToHtml(contentbuilder);
}
return contentbuilder;
3 years ago
}
9 months ago
/*
3 years ago
public static IHtmlContent AsciiDocFor<TModel>(this IHtmlHelper<TModel> html,
Expression<Func<TModel, string>> expression)
{
string ascii = html.ValueFor<string>(expression, "{0}");
if (string.IsNullOrWhiteSpace(ascii))
return new HtmlString(string.Empty);
Document document = Document.Parse(ascii);
var htmlDoc = document.ToHtml();
var span = new TagBuilder("p") { TagRenderMode = TagRenderMode.SelfClosing };
span.InnerHtml.AppendHtml(htmlDoc);
return span.RenderBody();
}
3 years ago
3 years ago
public static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text)
{
return AsciiDoc(htmlHelper, text, null);
}
3 years ago
3 years ago
private static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text, object htmlAttributes)
{
// Create tag builder
var builder = new TagBuilder("div");
var document = Document.Parse(text);
3 years ago
3 years ago
// builder.InnerHtml = .
3 years ago
3 years ago
// Add attributes
builder.MergeAttribute("class", "ascii");
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
3 years ago
3 years ago
// Render tag
return builder.ToString();
9 months ago
} */
3 years ago
}
}