Esempio n. 1
0
        private static IEnumerable <T> BlockDescendantsInternal <T>(ContainerBlock block) where T : MarkdownObject
        {
            Debug.Assert(typeof(T).IsSubclassOf(typeof(Block)));

            Stack <Block> stack = new Stack <Block>();

            int childrenCount = block.Count;

            while (childrenCount-- > 0)
            {
                stack.Push(block[childrenCount]);
            }

            while (stack.Count > 0)
            {
                var subBlock = stack.Pop();
                if (subBlock is T subBlockT)
                {
                    yield return(subBlockT);
                }

                if (subBlock is ContainerBlock subBlockContainer)
                {
                    childrenCount = subBlockContainer.Count;
                    while (childrenCount-- > 0)
                    {
                        stack.Push(subBlockContainer[childrenCount]);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}.
        /// </summary>
        /// <typeparam name="T">Type to use for filtering the descendants</typeparam>
        /// <param name="block">The markdown object.</param>
        /// <returns>
        /// An iteration over the descendant elements
        /// </returns>
        public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block
        {
            // Fast-path an empty container to avoid allocating a Stack
            if (block.Count == 0)
            {
                yield break;
            }

            Stack <Block> stack = new Stack <Block>();

            int childrenCount = block.Count;

            while (childrenCount-- > 0)
            {
                stack.Push(block[childrenCount]);
            }

            while (stack.Count > 0)
            {
                var subBlock = stack.Pop();
                if (subBlock is T subBlockT)
                {
                    yield return(subBlockT);
                }

                if (subBlock is ContainerBlock subBlockContainer)
                {
                    childrenCount = subBlockContainer.Count;
                    while (childrenCount-- > 0)
                    {
                        stack.Push(subBlockContainer[childrenCount]);
                    }
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">Type to use for filtering the descendants</typeparam>
 /// <param name="block">The markdown object.</param>
 /// <returns>
 /// An iteration over the descendant elements
 /// </returns>
 public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block
 {
     if (block != null && block.Count > 0)
     {
         return(BlockDescendantsInternal <T>(block));
     }
     else
     {
         return(ArrayHelper <T> .Empty);
     }
 }
Esempio n. 4
0
        private static IEnumerable <T> InlineDescendantsInternal <T>(ContainerBlock block) where T : MarkdownObject
        {
            Debug.Assert(typeof(T).IsSubclassOf(typeof(Inline)));

            foreach (MarkdownObject descendant in block.Descendants())
            {
                if (descendant is T descendantT)
                {
                    yield return(descendantT);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type {T}.
        /// </summary>
        /// <typeparam name="T">Type to use for filtering the descendants</typeparam>
        /// <param name="block">The markdown object.</param>
        /// <returns>
        /// An iteration over the descendant elements
        /// </returns>
        public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block
        {
            foreach (var subBlock in block)
            {
                var subBlockT = subBlock as T;
                if (subBlockT != null)
                {
                    yield return(subBlockT);
                }

                var subBlockContainer = subBlock as ContainerBlock;
                if (subBlockContainer != null)
                {
                    foreach (var sub in subBlockContainer.Descendants <T>())
                    {
                        yield return(sub);
                    }
                }
            }
        }
Esempio n. 6
0
 internal Enumerator(ContainerBlock block)
 {
     this.block = block;
     index      = 0;
     current    = null;
 }
Esempio n. 7
0
 /// <summary>
 /// Iterates over the descendant elements for the specified markdown <see cref="Block" /> element and filters by the type <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">Type to use for filtering the descendants</typeparam>
 /// <param name="block">The markdown object.</param>
 /// <returns>
 /// An iteration over the descendant elements
 /// </returns>
 public static IEnumerable <T> Descendants <T>(this ContainerBlock block) where T : Block
 {
     if (block is { Count : > 0 })