Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
    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());
    }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 5
0
 public static void WriteLeafBlock(LeafBlock paragraph)
 {
     foreach (Inline inline in paragraph.Inline)
     {
         WriteInline(inline);
     }
 }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 8
0
 /// <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 &lt; and &amp;</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);
 }
Ejemplo n.º 9
0
 /// <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);
         }
     }
 }
Ejemplo n.º 10
0
        ///// <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);
        }
Ejemplo n.º 11
0
        /// <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);
        }
Ejemplo n.º 12
0
 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);
 }
Ejemplo n.º 13
0
 public static string LeafToString(LeafBlock leaf)
 {
     if (leaf?.Lines.Lines == null)
     {
         return("");
     }
     return(String.Join('\n', leaf.Lines.Lines.Select(a => a.ToString())));
 }
Ejemplo n.º 14
0
 public static string RenderLeafInlineRaw(LeafBlock leafBlock)
 {
     using (var stringWriter = new StringWriter())
     {
         var renderer = new RawRenderer(stringWriter);
         renderer.WriteLeafInline(leafBlock);
         return(stringWriter.ToString());
     }
 }
Ejemplo n.º 15
0
 private static string GetBlockText(LeafBlock heading)
 {
     using (var stringWriter = new StringWriter())
     {
         var renderer = new NormalizeRenderer(stringWriter);
         renderer.Write(heading.Inline);
         return(stringWriter.ToString());
     }
 }
Ejemplo n.º 16
0
        ////////////////////////////////////////////////////////////////////////////////

        /// <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;
            }
        }
Ejemplo n.º 17
0
            static string GetParagraphText(LeafBlock leaf)
            {
                var sb = new StringBuilder();

                foreach (var item in leaf.Inline)
                {
                    sb.Append(item);
                }
                return(sb.ToString());
            }
Ejemplo n.º 18
0
        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
        }
Ejemplo n.º 20
0
        /// <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);
        }
Ejemplo n.º 21
0
 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);
         }
     }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 24
0
        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;
            }
        }
Ejemplo n.º 25
0
        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,
            });
        }
Ejemplo n.º 26
0
        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);
                }
            }
        }
Ejemplo n.º 27
0
        /// <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();
            }
        }
Ejemplo n.º 28
0
 /// <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());
         }
     }
 }
Ejemplo n.º 29
0
        /// <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);
        }
Ejemplo n.º 30
0
        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;
        }