render pages
This commit is contained in:
parent
ec6424803b
commit
ca92867243
14 changed files with 126 additions and 124 deletions
|
|
@ -8,13 +8,13 @@ using System.Linq.Expressions;
|
|||
namespace Yavsc.Helpers
|
||||
{
|
||||
public static class AsciiDocHelpers
|
||||
{
|
||||
static void ToHtml(this IElement elt, IHtmlContentBuilder contentbuilder)
|
||||
{
|
||||
switch (elt.GetType().FullName)
|
||||
static void ToHtml(this IElement elt, IHtmlContentBuilder contentbuilder)
|
||||
{
|
||||
case "AsciiDocNet.Paragraph":
|
||||
Paragraph p = (Paragraph) elt;
|
||||
switch (elt.GetType().FullName)
|
||||
{
|
||||
case "AsciiDocNet.Paragraph":
|
||||
Paragraph p = (Paragraph)elt;
|
||||
contentbuilder.AppendHtmlLine("<p>");
|
||||
foreach (var pitem in p)
|
||||
{
|
||||
|
|
@ -23,8 +23,8 @@ namespace Yavsc.Helpers
|
|||
contentbuilder.AppendHtmlLine("</p>");
|
||||
break;
|
||||
case "AsciiDocNet.SectionTitle":
|
||||
SectionTitle stitle = (SectionTitle) elt;
|
||||
|
||||
SectionTitle stitle = (SectionTitle)elt;
|
||||
|
||||
contentbuilder.AppendHtmlLine($"<h{stitle.Level}>");
|
||||
foreach (var titem in stitle)
|
||||
{
|
||||
|
|
@ -33,123 +33,124 @@ namespace Yavsc.Helpers
|
|||
contentbuilder.AppendHtmlLine("</h>");
|
||||
break;
|
||||
case "AsciiDocNet.UnorderedList":
|
||||
UnorderedList ul = (UnorderedList) elt;
|
||||
UnorderedList ul = (UnorderedList)elt;
|
||||
contentbuilder.AppendHtmlLine("<ul>");
|
||||
foreach (var li in ul.Items)
|
||||
{
|
||||
contentbuilder.AppendHtmlLine("<li>");
|
||||
|
||||
|
||||
foreach (var lii in li)
|
||||
{
|
||||
lii.ToHtml(contentbuilder);
|
||||
lii.ToHtml(contentbuilder);
|
||||
}
|
||||
contentbuilder.AppendHtmlLine("</li>");
|
||||
}
|
||||
contentbuilder.AppendHtmlLine("</ul>");
|
||||
break;
|
||||
case "AsciiDocNet.Source":
|
||||
Source source = (Source) elt ;
|
||||
Source source = (Source)elt;
|
||||
// TODO syntact hilighting and fun js modules
|
||||
contentbuilder.AppendHtmlLine("<pre><code>");
|
||||
contentbuilder.Append(source.Text);
|
||||
contentbuilder.AppendHtmlLine("</code></pre>");
|
||||
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("<a href=\"{0}\">{1}</a> ",link.Href, link.Text);
|
||||
break;
|
||||
|
||||
case "AsciiDocNet.TextLiteral":
|
||||
sb.Append(elt.ToString());
|
||||
break;
|
||||
|
||||
case "AsciiDocNet.Emphasis":
|
||||
sb.AppendHtml("<i>");
|
||||
AsciiDocNet.Emphasis em = (Emphasis) elt;
|
||||
sb.Append(em.Text);
|
||||
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;
|
||||
|
||||
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($"<h{doclevel}>{doc.Title.Title}</h{doclevel}>");
|
||||
if (!string.IsNullOrWhiteSpace(doc.Title.Subtitle))
|
||||
{
|
||||
contentbuilder.AppendHtmlLine($"<i>{doc.Title.Title}</i><br/>");
|
||||
}
|
||||
default:
|
||||
string unsupportedType = elt.GetType().FullName;
|
||||
throw new InvalidProgramException(unsupportedType);
|
||||
}
|
||||
}
|
||||
foreach (var item in doc)
|
||||
|
||||
|
||||
static void ToHtml(this IInlineElement elt, IHtmlContentBuilder sb)
|
||||
{
|
||||
item.ToHtml(contentbuilder);
|
||||
switch (elt.GetType().FullName)
|
||||
{
|
||||
case "AsciiDocNet.Link":
|
||||
Link link = (Link)elt;
|
||||
sb.AppendFormat("<a href=\"{0}\">{1}</a> ", link.Href, link.Text);
|
||||
break;
|
||||
|
||||
case "AsciiDocNet.TextLiteral":
|
||||
sb.Append(elt.ToString());
|
||||
break;
|
||||
|
||||
case "AsciiDocNet.Emphasis":
|
||||
sb.AppendHtml("<i>");
|
||||
AsciiDocNet.Emphasis em = (Emphasis)elt;
|
||||
sb.Append(em.Text);
|
||||
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;
|
||||
|
||||
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($"<h{doclevel}>{doc.Title.Title}</h{doclevel}>");
|
||||
if (!string.IsNullOrWhiteSpace(doc.Title.Subtitle))
|
||||
{
|
||||
contentbuilder.AppendHtmlLine($"<i>{doc.Title.Title}</i><br/>");
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var item in doc)
|
||||
{
|
||||
item.ToHtml(contentbuilder);
|
||||
}
|
||||
return contentbuilder;
|
||||
}
|
||||
return contentbuilder;
|
||||
}
|
||||
|
||||
|
||||
public static IHtmlContent AsciiDocFor<TModel>(this IHtmlHelper<TModel> html,
|
||||
Expression<Func<TModel, string>> expression)
|
||||
{
|
||||
string ascii = html.ValueFor<string>(expression, "{0}");
|
||||
Document document = Document.Parse(ascii);
|
||||
var htmlDoc = document.ToHtml();
|
||||
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();
|
||||
}
|
||||
|
||||
var span = new TagBuilder("p") { TagRenderMode = TagRenderMode.SelfClosing };
|
||||
span.InnerHtml.AppendHtml(htmlDoc);
|
||||
return span.RenderBody();
|
||||
}
|
||||
public static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text)
|
||||
{
|
||||
return AsciiDoc(htmlHelper, text, null);
|
||||
}
|
||||
|
||||
public static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text)
|
||||
{
|
||||
return AsciiDoc(htmlHelper, text, null);
|
||||
}
|
||||
private static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text, object htmlAttributes)
|
||||
{
|
||||
// Create tag builder
|
||||
var builder = new TagBuilder("div");
|
||||
var document = Document.Parse(text);
|
||||
|
||||
private static string AsciiDoc(IHtmlHelper<BlogPost> htmlHelper, string text, object htmlAttributes)
|
||||
{
|
||||
// Create tag builder
|
||||
var builder = new TagBuilder("div");
|
||||
var document = Document.Parse(text);
|
||||
// builder.InnerHtml = .
|
||||
|
||||
// builder.InnerHtml = .
|
||||
// Add attributes
|
||||
builder.MergeAttribute("class", "ascii");
|
||||
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
|
||||
|
||||
// Add attributes
|
||||
builder.MergeAttribute("class", "ascii");
|
||||
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
|
||||
|
||||
// Render tag
|
||||
return builder.ToString();
|
||||
// Render tag
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@ namespace Yavsc.Helpers
|
|||
public override async Task ProcessAsync (TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var content = await output.GetChildContentAsync();
|
||||
|
||||
Document document = Document.Parse(content.GetContent());
|
||||
string text = content.GetContent();
|
||||
if (string.IsNullOrWhiteSpace(text)) return;
|
||||
Document document = Document.Parse(text);
|
||||
var html = document.ToHtml(4);
|
||||
using var stringWriter = new StringWriter();
|
||||
html.WriteTo(stringWriter, HtmlEncoder.Default);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue