protected string ExtractSourceCode(LeafBlock node) { var code = new StringBuilder(); var lines = node.Lines.Lines; int totalLines = lines.Length; for (int i = 0; i < totalLines; i++) { var line = lines[i]; var slice = line.Slice; if (slice.Text == null) { continue; } var lineText = slice.Text.Substring(slice.Start, slice.Length); if (i > 0) { code.AppendLine(); } code.Append(lineText); } return(code.ToString()); }
private string GetTextContent(LeafBlock leaf) { var inline = leaf?.Inline?.FirstChild; if (inline is null) { return(string.Empty); } var result = new StringBuilder(); do { switch (inline) { case LiteralInline li: var inlineContent = li.Content; result.Append(inlineContent.Text.Substring(inlineContent.Start, inlineContent.Length)); break; case LineBreakInline _: result.Append(Environment.NewLine); break; } inline = inline.NextSibling; } while (inline != null); return(result.ToString()); }
public void WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines) { StringBuilder sb = new StringBuilder(); if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (int i = 0; i < lines.Count; i++) { if (!writeEndOfLines && i > 0) { sb.AppendLine(); } var slice = slices[i].Slice; if (slice.Start <= slice.End) { sb.Append(slice.Text.Substring(slice.Start, slice.Length)); } if (writeEndOfLines) { sb.AppendLine(); } } } XmlWriter.WriteRaw(sb.ToString()); }
private static string GetCode(LeafBlock obj, out string firstLine) { var code = new StringBuilder(); firstLine = null; foreach (var line in obj.Lines.Lines) { var slice = line.Slice; if (slice.Text == null) { continue; } var lineText = slice.Text.Substring(slice.Start, slice.Length); if (firstLine == null) { firstLine = lineText; } else { code.AppendLine(); } code.Append(lineText); } return(code.ToString()); }
public static void WriteLeafBlock(LeafBlock paragraph) { foreach (Inline inline in paragraph.Inline) { WriteInline(inline); } }
/// <summary> /// Convert a Markdown element to a Forkdown one. /// </summary> /// <param name="mdo">Markdown object to convert.</param> public static Element ToForkdown(IMarkdownObject mdo) { Element result = mdo switch { MarkdownDocument _ => new Document(), HeadingBlock h => new Heading(h), ListBlock l => new Listing(l), ListItemBlock li => new ListItem(li), ParagraphBlock p => new Paragraph(p), CustomContainer c => new Section(c), CustomContainerInline c => new ExplicitInlineContainer(c), CodeInline c => new Code(c), FencedCodeBlock c => new CodeBlock(c), LinkInline l => new Link(l), LiteralInline t => new Text(t), LineBreakInline _ => new LineBreak(), ThematicBreakBlock _ => new Separator(), Tables.Table t => new Table(t), Tables.TableRow tr => new TableRow(tr), Tables.TableCell tc => new TableCell(tc), EmphasisInline e => new Emphasis(e), HtmlBlock b => new Html(b), _ => new Placeholder(mdo), }; var subs = mdo switch { LeafBlock b => b.Inline, IEnumerable <MarkdownObject> e => e, _ => null } ?? Nil.E <MarkdownObject>();
public void ExtractCiteUrl_ExtractsCiteUrlIfSuccessful() { // Arrange const string dummyUrl = "dummyLink"; var dummyAuthorLinkInline = new LinkInline(); var dummyCiteLinkInline = new LinkInline(dummyUrl, null); var dummyContainerInline = new ContainerInline(); dummyContainerInline. AppendChild(dummyAuthorLinkInline). AppendChild(dummyCiteLinkInline); LeafBlock dummyCitationBlock = _mockRepository.Create <LeafBlock>(null).Object; InlineProcessor dummyInlineProcessor = MarkdigTypesFactory.CreateInlineProcessor(); dummyInlineProcessor.ProcessInlineLeaf(dummyCitationBlock); // Sets InlineProcessor.Block to dummyCitationBlock dummyCitationBlock.Inline = dummyContainerInline; // Replace container created in ProcessInlineLeaf FlexiQuoteBlock dummyFlexiQuoteBlock = CreateFlexiQuoteBlock(); dummyFlexiQuoteBlock.Add(dummyCitationBlock); Mock <FlexiQuoteBlockFactory> mockTestSubject = CreateMockFlexiQuoteBlockFactory(); mockTestSubject.Setup(m => m.NormalizeCiteLinkIndex(2, dummyFlexiQuoteBlock)).Returns(1); // Act mockTestSubject.Object.ExtractCiteUrl(dummyInlineProcessor, null); // Assert _mockRepository.VerifyAll(); Assert.Equal(dummyUrl, dummyFlexiQuoteBlock.CiteUrl); }
/// <summary> /// Writes the lines of a <see cref="LeafBlock"/> /// </summary> /// <param name="leafBlock">The leaf block.</param> /// <param name="writeEndOfLines">if set to <c>true</c> write end of lines.</param> /// <param name="escape">if set to <c>true</c> escape the content for HTML</param> /// <param name="softEscape">Only escape < and &</param> /// <returns>This instance</returns> public HtmlRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool escape, bool softEscape = false) { if (leafBlock == null) { ThrowHelper.ArgumentNullException_leafBlock(); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (int i = 0; i < lines.Count; i++) { if (!writeEndOfLines && i > 0) { WriteLine(); } if (escape) { WriteEscape(ref slices[i].Slice, softEscape); } else { Write(ref slices[i].Slice); } if (writeEndOfLines) { WriteLine(); } } } return(this); }
/// <summary> /// Writes the lines of a <see cref="LeafBlock"/> /// </summary> /// <param name="leafBlock">The leaf block.</param> public void WriteLeafRawLines(LeafBlock leafBlock) { if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (var i = 0; i < lines.Count; i++) { if (i != 0) { //if (stack.Peek() is FlowLayoutWidget) //{ // this.Pop(); // this.Push(new ParagraphX()); //} WriteInline(new LineBreakX()); // new LineBreak()); } WriteText(ref slices[i].Slice); } } }
///// <summary> ///// Writes the attached <see cref="HtmlAttributes"/> on the specified <see cref="MarkdownObject"/>. ///// </summary> ///// <param name="obj">The object.</param> ///// <returns></returns> //public NormalizeRenderer WriteAttributes(MarkdownObject obj) //{ // if (obj == null) throw new ArgumentNullException(nameof(obj)); // return WriteAttributes(obj.TryGetAttributes()); //} ///// <summary> ///// Writes the specified <see cref="HtmlAttributes"/>. ///// </summary> ///// <param name="attributes">The attributes to render.</param> ///// <returns>This instance</returns> //public NormalizeRenderer WriteAttributes(HtmlAttributes attributes) //{ // if (attributes == null) // { // return this; // } // if (attributes.Id != null) // { // Write(" id=\"").WriteEscape(attributes.Id).Write("\""); // } // if (attributes.Classes != null && attributes.Classes.Count > 0) // { // Write(" class=\""); // for (int i = 0; i < attributes.Classes.Count; i++) // { // var cssClass = attributes.Classes[i]; // if (i > 0) // { // Write(" "); // } // WriteEscape(cssClass); // } // Write("\""); // } // if (attributes.Properties != null && attributes.Properties.Count > 0) // { // foreach (var property in attributes.Properties) // { // Write(" ").Write(property.Key); // if (property.Value != null) // { // Write("=").Write("\""); // WriteEscape(property.Value); // Write("\""); // } // } // } // return this; //} /// <summary> /// Writes the lines of a <see cref="LeafBlock"/> /// </summary> /// <param name="leafBlock">The leaf block.</param> /// <param name="writeEndOfLines">if set to <c>true</c> write end of lines.</param> /// <returns>This instance</returns> public NormalizeRenderer WriteLeafRawLines(LeafBlock leafBlock, bool writeEndOfLines, bool indent = false) { if (leafBlock == null) { ThrowHelper.ArgumentNullException_leafBlock(); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (int i = 0; i < lines.Count; i++) { if (!writeEndOfLines && i > 0) { WriteLine(); } if (indent) { Write(" "); } Write(ref slices[i].Slice); if (writeEndOfLines) { WriteLine(); } } } return(this); }
/// <inheritdoc /> public virtual T Create(T defaultBlockOptions, LeafBlock leafBlock) { if (defaultBlockOptions == null) { throw new ArgumentNullException(nameof(defaultBlockOptions)); } if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } T result = defaultBlockOptions.Clone(); try { using (var jsonTextReader = new JsonTextReader(new LeafBlockReader(leafBlock))) { _jsonSerializerService.Populate(jsonTextReader, result); } } catch (Exception exception) { throw new BlockException(leafBlock, string.Format(Strings.OptionsException_BlockOptionsFactory_InvalidJson, leafBlock.Lines.ToString()), exception); } return(result); }
public XamlRenderer WriteLeafRawLines([NotNull] LeafBlock leafBlock, bool writeEndOfLines, bool escape, bool softEscape = false) { if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (var i = 0; i < lines.Count; i++) { if (!writeEndOfLines && i > 0) { WriteLine(); } if (escape) { WriteEscape(ref slices[i].Slice, softEscape); } else { Write(ref slices[i].Slice); } if (writeEndOfLines) { WriteLine(); } } } return(this); }
public static string LeafToString(LeafBlock leaf) { if (leaf?.Lines.Lines == null) { return(""); } return(String.Join('\n', leaf.Lines.Lines.Select(a => a.ToString()))); }
public static string RenderLeafInlineRaw(LeafBlock leafBlock) { using (var stringWriter = new StringWriter()) { var renderer = new RawRenderer(stringWriter); renderer.WriteLeafInline(leafBlock); return(stringWriter.ToString()); } }
private static string GetBlockText(LeafBlock heading) { using (var stringWriter = new StringWriter()) { var renderer = new NormalizeRenderer(stringWriter); renderer.Write(heading.Inline); return(stringWriter.ToString()); } }
//////////////////////////////////////////////////////////////////////////////// /// <see cref="Markdig.Renderers.TextRendererBase.WriteLeafInline"/> internal void WriteLeafBlockInline(LeafBlock block) { var inline = block.Inline as Inline; while (inline != null) { Write(inline); inline = inline.NextSibling; } }
static string GetParagraphText(LeafBlock leaf) { var sb = new StringBuilder(); foreach (var item in leaf.Inline) { sb.Append(item); } return(sb.ToString()); }
public static string GetText(this LeafBlock block) { var sb = new StringBuilder(); foreach (var line in block.Lines.Lines) { sb.AppendLine(line.ToString()); } return(sb.ToString().Trim()); }
public void ExtractCiteUrl_DoesNothingIfThereAreNoLinkInlinesInCitationBlock() { // Arrange LeafBlock dummyCitationBlock = _mockRepository.Create <LeafBlock>(null).Object; InlineProcessor dummyInlineProcessor = MarkdigTypesFactory.CreateInlineProcessor(); dummyInlineProcessor.ProcessInlineLeaf(dummyCitationBlock); // Sets InlineProcessor.Block to dummyCitationBlock FlexiQuoteBlockFactory testSubject = CreateFlexiQuoteBlockFactory(); // Act and assert testSubject.ExtractCiteUrl(dummyInlineProcessor, null); // If this doesn't throw, we never attempted to extract cite URL }
/// <summary> /// Writes <see cref="LeafBlock.Inline"/> with the specified enable HTML for inlinesetting. /// </summary> public static HtmlRenderer WriteLeafInline(this HtmlRenderer htmlRenderer, LeafBlock leafBlock, bool enableHtmlForInline) { bool initialEnableHtmlForInline = htmlRenderer.EnableHtmlForInline; htmlRenderer.EnableHtmlForInline = enableHtmlForInline; htmlRenderer.WriteLeafInline(leafBlock); htmlRenderer.EnableHtmlForInline = initialEnableHtmlForInline; return(htmlRenderer); }
public void WriteLeafRawLines(RoundtripRenderer renderer, LeafBlock leafBlock) { if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (int i = 0; i < lines.Count; i++) { ref StringSlice slice = ref slices[i].Slice; renderer.Write(ref slice); renderer.WriteLine(slice.NewLine); } }
public void WriteLeafInline(LeafBlock leafBlock) { var inline = (Inline)leafBlock.Inline; if (inline != null) { while (inline != null) { Write(inline); inline = inline.NextSibling; } } }
public RenderFragmentMarkdownRenderer WriteLeafInline(LeafBlock leafBlock) { var inline = (Inline)leafBlock.Inline; if (inline != null) { while (inline != null) { Write(inline); inline = inline.NextSibling; } } return(this); }
public void WriteLeafInline(LeafBlock leafBlock) { if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } var inline = (Inline)leafBlock.Inline; while (inline != null) { Write(inline); inline = inline.NextSibling; } }
private static TerminalNode ParseBlock(LeafBlock block, CharacterPositionFinder finder, TextProvider textProvider) { var name = GetName(block, textProvider); var type = GetType(block); var locationSpan = GetLocationSpan(block, finder); var span = GetCharacterSpan(block); return(new TerminalNode { Type = type, Name = name, LocationSpan = locationSpan, Span = span, }); }
public void WriteLeafRawLines(LeafBlock leafBlock) { if (leafBlock.Lines.Lines != null) { for (int n = 0; n < leafBlock.Lines.Count; n++) { if (n > 0) { OpenElement("br"); CloseElement(); } this.Write(leafBlock.Lines.Lines[n].Slice); } } }
/// <summary> /// Output child nodes as raw text /// </summary> /// <see cref="Markdig.Renderers.HtmlRenderer.WriteLeafRawLines"/> internal void WriteLeafRawLines(LeafBlock block) { if (block.Lines.Lines == null) { return; } var lines = block.Lines; var slices = lines.Lines; for (int i = 0; i < lines.Count; i++) { Text(slices[i].ToString()); Layout.NewLine(); } }
/// <summary> /// Writes the lines of a <see cref="LeafBlock"/> /// </summary> /// <param name="leafBlock">The leaf block.</param> public void WriteLeafRawLines([NotNull] LeafBlock leafBlock) { if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } if (leafBlock.Lines.Lines != null) { var lines = leafBlock.Lines; var slices = lines.Lines; for (var i = 0; i < lines.Count; i++) { WriteText(ref slices[i].Slice); WriteInline(new LineBreak()); } } }
/// <summary> /// Writes the inlines of a leaf inline. /// </summary> /// <param name="leafBlock">The leaf block.</param> /// <returns>This instance</returns> public PPTXRenderer WriteLeafInline(LeafBlock leafBlock) { if (leafBlock == null) { throw new ArgumentNullException(nameof(leafBlock)); } var inline = (Inline)leafBlock.Inline; if (inline != null) { while (inline != null) { Write(inline); inline = inline.NextSibling; } } return(this); }
internal void ExtractCiteUrl(InlineProcessor inlineProcessor, Inline _) { LeafBlock citationBlock = inlineProcessor.Block; IEnumerable <LinkInline> linkInlines = citationBlock.Inline.FindDescendants <LinkInline>(); int numLinks = linkInlines.Count(); // No links, regardless of what sourceIndex is, return if (numLinks == 0) { return; } var flexiQuoteBlock = (FlexiQuoteBlock)citationBlock.Parent; int citeLinkIndex = NormalizeCiteLinkIndex(numLinks, flexiQuoteBlock); LinkInline linkInline = linkInlines.ElementAt(citeLinkIndex); flexiQuoteBlock.CiteUrl = linkInline.Url; }