<commit_before>﻿// Copyright (c) <NAME>. All rights reserved.
// This file is licensed under the BSD-Clause 2 license. 
// See the license.txt file in the project root for more information.
using Textamina.Markdig.Parsers;
using Textamina.Markdig.Syntax;

namespace Textamina.Markdig.Extensions.Figures
{
    /// <summary>
    /// The block parser for a <see cref="Figure"/> block.
    /// </summary>
    /// <seealso cref="Textamina.Markdig.Parsers.BlockParser" />
    public class FigureBlockParser : BlockParser
    { 
        /// <summary>
        /// Initializes a new instance of the <see cref="FencedBlockParserBase"/> class.
        /// </summary>
        public FigureBlockParser()
        {
            OpeningCharacters = new [] {'^'};
        }

        public override BlockState TryOpen(BlockProcessor processor)
        {
            // We expect no indentation for a figure block.
            if (processor.IsCodeIndent)
            {
                return BlockState.None;
            }

            // Match fenced char
            int count = 0;
            var line = processor.Line;
            char c = line.CurrentChar;
            var matchChar = c;
            while (c != '\0')
            {
                if (c != matchChar)
                {
                    break;
                }
                count++;
                c = line.NextChar();
            }

            // Requires at least 3 opening chars
            if (count < 3)
            {
                return BlockState.None;
            }

            var figure = new Figure(this)
            {
                Column = processor.Column,
                OpeningCharacter = matchChar,
                OpeningCharacterCount = count
            };

            line.TrimStart();
            if (!line.IsEmpty)
            {
                var caption = new FigureCaption(this) {IsOpen = false};
                caption.AppendLine(ref line, line.Start, processor.LineIndex);
                figure.Add(caption);
            }
            processor.NewBlocks.Push(figure);

            // Discard the current line as it is already parsed
            return BlockState.ContinueDiscard;
        }

        public override BlockState TryContinue(BlockProcessor processor, Block block)
        {
            var figure = (Figure)block;
            var count = figure.OpeningCharacterCount;
            var matchChar = figure.OpeningCharacter;
            var c = processor.CurrentChar;

            // Match if we have a closing fence
            var line = processor.Line;
            while (c == matchChar)
            {
                c = line.NextChar();
                count--;
            }

            // If we have a closing fence, close it and discard the current line
            // The line must contain only fence opening character followed only by whitespaces.
            if (count <= 0 && !processor.IsCodeIndent)
            {
                line.TrimStart();
                if (!line.IsEmpty)
                {
                    var caption = new FigureCaption(this) {IsOpen = false};
                    caption.AppendLine(ref line, line.Start, processor.LineIndex);
                    figure.Add(caption);
                }

                // Don't keep the last line
                return BlockState.BreakDiscard;
            }

            // Reset the indentation to the column before the indent
            processor.GoToColumn(processor.ColumnBeforeIndent);

            return BlockState.Continue;
        }
    }
}<commit_msg>Make sure that OnBlockCreated is called for FigureCaption<commit_after>﻿// Copyright (c) <NAME>. All rights reserved.
// This file is licensed under the BSD-Clause 2 license. 
// See the license.txt file in the project root for more information.
using Textamina.Markdig.Parsers;
using Textamina.Markdig.Syntax;

namespace Textamina.Markdig.Extensions.Figures
{
    /// <summary>
    /// The block parser for a <see cref="Figure"/> block.
    /// </summary>
    /// <seealso cref="Textamina.Markdig.Parsers.BlockParser" />
    public class FigureBlockParser : BlockParser
    { 
        /// <summary>
        /// Initializes a new instance of the <see cref="FencedBlockParserBase"/> class.
        /// </summary>
        public FigureBlockParser()
        {
            OpeningCharacters = new [] {'^'};
        }

        public override BlockState TryOpen(BlockProcessor processor)
        {
            // We expect no indentation for a figure block.
            if (processor.IsCodeIndent)
            {
                return BlockState.None;
            }

            // Match fenced char
            int count = 0;
            var line = processor.Line;
            char c = line.CurrentChar;
            var matchChar = c;
            while (c != '\0')
            {
                if (c != matchChar)
                {
                    break;
                }
                count++;
                c = line.NextChar();
            }

            // Requires at least 3 opening chars
            if (count < 3)
            {
                return BlockState.None;
            }

            var figure = new Figure(this)
            {
                Column = processor.Column,
                OpeningCharacter = matchChar,
                OpeningCharacterCount = count
            };

            line.TrimStart();
            if (!line.IsEmpty)
            {
                var caption = new FigureCaption(this) {IsOpen = false};
                caption.AppendLine(ref line, line.Start, processor.LineIndex);
                figure.Add(caption);
                processor.OnBlockCreated(caption);
            }
            processor.NewBlocks.Push(figure);

            // Discard the current line as it is already parsed
            return BlockState.ContinueDiscard;
        }

        public override BlockState TryContinue(BlockProcessor processor, Block block)
        {
            var figure = (Figure)block;
            var count = figure.OpeningCharacterCount;
            var matchChar = figure.OpeningCharacter;
            var c = processor.CurrentChar;

            // Match if we have a closing fence
            var line = processor.Line;
            while (c == matchChar)
            {
                c = line.NextChar();
                count--;
            }

            // If we have a closing fence, close it and discard the current line
            // The line must contain only fence opening character followed only by whitespaces.
            if (count <= 0 && !processor.IsCodeIndent)
            {
                line.TrimStart();
                if (!line.IsEmpty)
                {
                    var caption = new FigureCaption(this) {IsOpen = false};
                    caption.AppendLine(ref line, line.Start, processor.LineIndex);
                    figure.Add(caption);
                    processor.OnBlockCreated(caption);
                }

                // Don't keep the last line
                return BlockState.BreakDiscard;
            }

            // Reset the indentation to the column before the indent
            processor.GoToColumn(processor.ColumnBeforeIndent);

            return BlockState.Continue;
        }
    }
}
{'fraction_non_alphanumeric': 0.08539944903581267, 'fraction_numerical': 0.001652892561983471, 'mean_word_length': 1.3620689655172413, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6438608', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1688, 'n_words': 509}