<commit_before>"use strict";

const defaultOptions = {
    enableHeadingId: true,
    autoNumberingRegex: "^\\@numbering\\s*?\\{([\\s\\S]*?)\\}",
    autoNumbering: {
        "enable": false,
        "pattern": [],
        "defaultSuffix": ". ",
        "defaultPrefix": "",
        "defaultStart": 1,
        "defaultSeparator": "."
    },
    tocIncludeLevels: [1, 2, 3, 4, 5, 6],
    tocContainerClass: "toc",
    tocRegex: "^\\@toc$",
    excludeFromTocRegex: "\\{notoc\\}",
    tocItemIndentInEm: 2,
    headingIdPrefix: "heading."
}; //defaultOptions

module.exports = (md, options) => {

    const util = require("util");
    const utility = require("./utility");
    const autoNumberingParser = require("./autoNumbering.optionParser");
    const autoNumbering = require("./autoNumbering");

    let renderedHtml, usedIds, headingSet, tocLocations;
    const cleanUp = () => {
        renderedHtml = null;
        usedIds = { headings: {}, toc: {}, excludeFromToc: {} };
        headingSet = {};
        tocLocations = [];
    };
    
    if (options) {
        const isObject = obj => { return obj && obj.constructor == Object; };
        function clone(source) {
            let target = {};
            for (let index in source)
                if (source.hasOwnProperty(index))
                    if (isObject(source[index]))
                        target[index] = clone(source[index]);
                    else
                        target[index] = source[index];
            return target;
        } //clone
        options = clone(options);    
    } else
        options = {};

    utility.populateWithDefault(options, defaultOptions);

    const tocIncludeLevelSet = new Set(options.tocIncludeLevels);
    const tocRegex = new RegExp(options.tocRegex);
    const excludeFromTocRegex = new RegExp(options.excludeFromTocRegex);
    const enumerationRuleSetRegexp = new RegExp(options.autoNumberingRegex);

    md.core.ruler.before("block", "detectAutoNumbering", state => {
        const match = enumerationRuleSetRegexp.exec(state.src);
        if (!match) return;
        try {
            const privilegedOptions = autoNumberingParser(match[1]);
            if (privilegedOptions) {
                utility.populateWithDefault(privilegedOptions, options.autoNumbering);
                options.autoNumbering = privilegedOptions;
            } // if
        } finally {
            if (match)
                state.src = state.src.slice(match[0].length, state.src.length);
        } //exception
    }); //md.core.ruler.before

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    const autoNumberGenerator = {
        init: function() {
            this.broken = false;
            this.stack = [];
            this.current = { level: undefined, indexIterator: undefined, parentPrefix: "", prefix: undefined };
            this.levelOptionDictionary = {};<commit_msg>TOC, auto-numbering: fixed bug: defined options would accumulate from previous results<commit_after>"use strict";

const defaultOptions = {
    enableHeadingId: true,
    autoNumberingRegex: "^\\@numbering\\s*?\\{([\\s\\S]*?)\\}",
    autoNumbering: {
        "enable": false,
        "pattern": [],
        "defaultSuffix": ". ",
        "defaultPrefix": "",
        "defaultStart": 1,
        "defaultSeparator": "."
    },
    tocIncludeLevels: [1, 2, 3, 4, 5, 6],
    tocContainerClass: "toc",
    tocRegex: "^\\@toc$",
    excludeFromTocRegex: "\\{notoc\\}",
    tocItemIndentInEm: 2,
    headingIdPrefix: "heading."
}; //defaultOptions

module.exports = (md, options) => {

    const util = require("util");
    const utility = require("./utility");
    const autoNumberingParser = require("./autoNumbering.optionParser");
    const autoNumbering = require("./autoNumbering");

    let renderedHtml, usedIds, headingSet, tocLocations;
    const cleanUp = () => {
        renderedHtml = null;
        usedIds = { headings: {}, toc: {}, excludeFromToc: {} };
        headingSet = {};
        tocLocations = [];
    };
    
    if (options) {
        const isObject = obj => { return obj && obj.constructor == Object; };
        function clone(source) {
            let target = {};
            for (let index in source)
                if (source.hasOwnProperty(index))
                    if (isObject(source[index]))
                        target[index] = clone(source[index]);
                    else
                        target[index] = source[index];
            return target;
        } //clone
        options = clone(options);    
    } else
        options = {};

    utility.populateWithDefault(options, defaultOptions);

    const tocIncludeLevelSet = new Set(options.tocIncludeLevels);
    const tocRegex = new RegExp(options.tocRegex);
    const excludeFromTocRegex = new RegExp(options.excludeFromTocRegex);
    const enumerationRuleSetRegexp = new RegExp(options.autoNumberingRegex);

    md.core.ruler.before("block", "detectAutoNumbering", state => {
        const match = enumerationRuleSetRegexp.exec(state.src);
        if (!match) return;
        try {
            const privilegedOptions = autoNumberingParser(match[1]);
            if (privilegedOptions) {
                utility.populateWithDefault(privilegedOptions, defaultOptions.autoNumbering);
                options.autoNumbering = privilegedOptions;
            } // if
        } finally {
            if (match)
                state.src = state.src.slice(match[0].length, state.src.length);
        } //exception
    }); //md.core.ruler.before

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    const autoNumberGenerator = {
        init: function() {
            this.broken = false;
            this.stack = [];
            this.current = { level: undefined, indexIterator: undefined, parentPrefix: "", prefix: undefined };
            this.levelOptionDictionary = {};
{'fraction_non_alphanumeric': 0.17048853439680958, 'fraction_numerical': 0.003323363243602526, 'mean_word_length': 1.9476003917727718, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 11, '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': '709732', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1568, 'n_words': 332}