Exemplo n.º 1
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]);
                    }
                }
            }
        }
Exemplo n.º 2
0
 internal Enumerator(ContainerBlock block)
 {
     this.block = block;
     index      = 0;
     current    = null;
 }