/// <summary> /// Writes the inline list to the given writer as HTML code. /// </summary> private static void InlinesToHtml(HtmlTextWriter writer, Inline inline, CommonMarkSettings settings, Stack <InlineStackEntry> stack) { var uriResolver = settings.UriResolver; bool withinLink = false; bool stackWithinLink = false; bool visitChildren; bool trackPositions = settings.TrackSourcePosition; string stackLiteral = null; while (inline != null) { visitChildren = false; switch (inline.Tag) { case InlineTag.String: if (trackPositions) { writer.WriteConstant("<span"); PrintPosition(writer, inline); writer.Write('>'); EscapeHtml(inline.LiteralContentValue, writer); writer.WriteConstant("</span>"); } else { EscapeHtml(inline.LiteralContentValue, writer); } break; case InlineTag.LineBreak: writer.WriteLineConstant("<br />"); break; case InlineTag.SoftBreak: if (settings.RenderSoftLineBreaksAsLineBreaks) { writer.WriteLineConstant("<br />"); } else { writer.WriteLine(); } break; case InlineTag.Code: writer.WriteConstant("<code"); if (trackPositions) { PrintPosition(writer, inline); } writer.Write('>'); EscapeHtml(inline.LiteralContentValue, writer); writer.WriteConstant("</code>"); break; case InlineTag.RawHtml: // cannot output source position for HTML blocks writer.Write(inline.LiteralContentValue); break; case InlineTag.Link: if (withinLink) { writer.Write('['); stackLiteral = "]"; stackWithinLink = withinLink; visitChildren = true; } else { writer.WriteConstant("<a href=\""); if (uriResolver != null) { EscapeUrl(uriResolver(inline.TargetUrl), writer); } else { EscapeUrl(inline.TargetUrl, writer); } writer.Write('\"'); if (inline.LiteralContentValue.Length > 0) { writer.WriteConstant(" title=\""); EscapeHtml(inline.LiteralContentValue, writer); writer.Write('\"'); } if (trackPositions) { PrintPosition(writer, inline); } writer.Write('>'); visitChildren = true; stackWithinLink = true; stackLiteral = "</a>"; } break; case InlineTag.Image: writer.WriteConstant("<img src=\""); if (uriResolver != null) { EscapeUrl(uriResolver(inline.TargetUrl), writer); } else { EscapeUrl(inline.TargetUrl, writer); } writer.WriteConstant("\" alt=\""); InlinesToPlainText(writer, inline.FirstChild, stack); writer.Write('\"'); if (inline.LiteralContentValue.Length > 0) { writer.WriteConstant(" title=\""); EscapeHtml(inline.LiteralContentValue, writer); writer.Write('\"'); } if (trackPositions) { PrintPosition(writer, inline); } writer.WriteConstant(" />"); break; case InlineTag.Strong: writer.WriteConstant("<strong"); if (trackPositions) { PrintPosition(writer, inline); } writer.Write('>'); stackLiteral = "</strong>"; stackWithinLink = withinLink; visitChildren = true; break; case InlineTag.Emphasis: writer.WriteConstant("<em"); if (trackPositions) { PrintPosition(writer, inline); } writer.Write('>'); stackLiteral = "</em>"; visitChildren = true; stackWithinLink = withinLink; break; case InlineTag.Strikethrough: writer.WriteConstant("<del"); if (trackPositions) { PrintPosition(writer, inline); } writer.Write('>'); stackLiteral = "</del>"; visitChildren = true; stackWithinLink = withinLink; break; default: throw new CommonMarkException("Inline type " + inline.Tag + " is not supported.", inline); } if (visitChildren) { stack.Push(new InlineStackEntry(stackLiteral, inline.NextSibling, withinLink)); withinLink = stackWithinLink; inline = inline.FirstChild; } else if (inline.NextSibling != null) { inline = inline.NextSibling; } else { inline = null; } while (inline == null && stack.Count > 0) { var entry = stack.Pop(); writer.WriteConstant(entry.Literal); inline = entry.Target; withinLink = entry.IsWithinLink; } } }
private static void BlocksToHtmlInner(HtmlTextWriter writer, Block block, CommonMarkSettings settings) { var stack = new Stack <BlockStackEntry>(); var inlineStack = new Stack <InlineStackEntry>(); bool visitChildren; string stackLiteral = null; bool stackTight = false; bool tight = false; bool trackPositions = settings.TrackSourcePosition; int x; while (block != null) { visitChildren = false; switch (block.Tag) { case BlockTag.Document: stackLiteral = null; stackTight = false; visitChildren = true; break; case BlockTag.Paragraph: if (tight) { InlinesToHtml(writer, block.InlineContent, settings, inlineStack); } else { writer.EnsureLine(); writer.WriteConstant("<p"); if (trackPositions) { PrintPosition(writer, block); } writer.Write('>'); InlinesToHtml(writer, block.InlineContent, settings, inlineStack); writer.WriteLineConstant("</p>"); } break; case BlockTag.BlockQuote: writer.EnsureLine(); writer.WriteConstant("<blockquote"); if (trackPositions) { PrintPosition(writer, block); } writer.WriteLine('>'); stackLiteral = "</blockquote>"; stackTight = false; visitChildren = true; break; case BlockTag.ListItem: writer.EnsureLine(); writer.WriteConstant("<li"); if (trackPositions) { PrintPosition(writer, block); } writer.Write('>'); stackLiteral = "</li>"; stackTight = tight; visitChildren = true; break; case BlockTag.List: // make sure a list starts at the beginning of the line: writer.EnsureLine(); var data = block.ListData; writer.WriteConstant(data.ListType == ListType.Bullet ? "<ul" : "<ol"); if (data.Start != 1) { writer.WriteConstant(" start=\""); writer.WriteConstant(data.Start.ToString(System.Globalization.CultureInfo.InvariantCulture)); writer.Write('\"'); } if (trackPositions) { PrintPosition(writer, block); } writer.WriteLine('>'); stackLiteral = data.ListType == ListType.Bullet ? "</ul>" : "</ol>"; stackTight = data.IsTight; visitChildren = true; break; case BlockTag.AtxHeader: case BlockTag.SETextHeader: writer.EnsureLine(); x = block.HeaderLevel; if (trackPositions) { writer.WriteConstant("<h" + x.ToString(CultureInfo.InvariantCulture)); PrintPosition(writer, block); InlinesToHtml(writer, block.InlineContent, settings, inlineStack); writer.WriteLineConstant(x > 0 && x < 7 ? HeaderCloserTags[x - 1] : "</h" + x.ToString(CultureInfo.InvariantCulture) + ">"); } else { writer.WriteConstant(x > 0 && x < 7 ? HeaderOpenerTags[x - 1] : "<h" + x.ToString(CultureInfo.InvariantCulture) + ">"); InlinesToHtml(writer, block.InlineContent, settings, inlineStack); writer.WriteLineConstant(x > 0 && x < 7 ? HeaderCloserTags[x - 1] : "</h" + x.ToString(CultureInfo.InvariantCulture) + ">"); } break; case BlockTag.IndentedCode: case BlockTag.FencedCode: writer.EnsureLine(); writer.WriteConstant("<pre><code"); if (trackPositions) { PrintPosition(writer, block); } var info = block.FencedCodeData == null ? null : block.FencedCodeData.Info; if (info != null && info.Length > 0) { x = info.IndexOf(' '); if (x == -1) { x = info.Length; } writer.WriteConstant(" class=\"language-"); EscapeHtml(new StringPart(info, 0, x), writer); writer.Write('\"'); } writer.Write('>'); EscapeHtml(block.StringContent, writer); writer.WriteLineConstant("</code></pre>"); break; case BlockTag.HtmlBlock: // cannot output source position for HTML blocks block.StringContent.WriteTo(writer); break; case BlockTag.HorizontalRuler: if (trackPositions) { writer.WriteConstant("<hr"); PrintPosition(writer, block); writer.WriteLine(); } else { writer.WriteLineConstant("<hr />"); } break; case BlockTag.ReferenceDefinition: break; default: throw new CommonMarkException("Block type " + block.Tag + " is not supported.", block); } if (visitChildren) { stack.Push(new BlockStackEntry(stackLiteral, block.NextSibling, tight)); tight = stackTight; block = block.FirstChild; } else if (block.NextSibling != null) { block = block.NextSibling; } else { block = null; } while (block == null && stack.Count > 0) { var entry = stack.Pop(); writer.WriteLineConstant(entry.Literal); tight = entry.IsTight; block = entry.Target; } } }