<commit_before>//
// File:            main.js
//
// Description:    This file defines the math mode for FoldingText. It
//                 processes the math lines using mathjs library. The
//                 library should be in the same directory as the extension.
//
// Author:        <NAME>
// Author Email:  <EMAIL>
//
define(function(require, exports, module) {
    'use strict';

    //
    // Load the libraries I need to use.
    //
    var Extensions = require('ft/core/extensions').Extensions;
    var math = require("./mathjs.js");

    //
    // Add the 'math' mode extension to the system.
    //
    Extensions.addMode({
        name: 'math'
    });

    //
    // Function:          inMathArea
    //
    // Description:       This function determines if the current node is in
    //                    a math area and is not empty.
    //
    function inMathArea(node) {
        if((node.modeContext() == "math")&&(node.text().trim()) != "") {
            return(true);
        } else {
            return(false);
        }
    };

    //
    // Function:          Calculate
    //
    // Description:       This function get the current node and the current node's
    //                    string contents. It returns the result of the calculation.
    //
    function Calculate(str) {
        var result = 0;
        try {
            //
            // Use the Mathjs library to evaluate the equation.
            //
           result = math.eval(str.substr(0,str.length-2));
        } catch(e) {
            //
            // Mathjs had a problem with the expressions. Return an ?
            //
            result = "?";
        }
        return(result);
    };

    //
    // Function:      ProcessNode
    //
    // Description:   This function is used to process a node in the math
    //                context. It expects the node to be passed to it.
    //
    function ProcessNode(node) {
        //
        // Calculate if needed. Get the text of the line.
        //
        var result = node.text();

        //
        // See if it ends in "=>". If so, process the line.
        //
        if(result.substr(-2) == "=>"){
            //
            // See if some of the previous lines had
            // variable declarations.
            //
            var loop = false;
            var pnode = node;
            do {
                //
                // Look for variable lines until reach the heading.
                //
                pnode = pnode.previousBranch();
                if(pnode.type() != "heading") {
                    //
                    // Not a heading, see if it has an evaluate command.
                    //
                    if(pnode.text().search("=>") < 0) {
                        //
                        // No evaluation, add it to the rest.
                        //
                        result = pnode.text() + "\n" + result;
                    }
                    loop = true;
                } else {
                    //
                    // Hit a header. Quit looping.
                    //
                    loop = false;
                }
            } while(loop);

            //
            // Calculate the result. If Calculate returns an array, there were
            // variables figured in as well. Just get the final result.
            //
            var cresult = Calculate(result);
            if(isArray(cresult)){
                //
                // We will get more than on answer back. Therefore,
                // just get the last result.
                //
                cresult = cresult[cresult.length-1];
            }

            //
            // Put the result together with the original line.
            //
            result = node.text() + " " + cresult;

            //
            // Update the line.
            //
            node.setText(result);
        }
    };

    //<commit_msg>Changed mode to imath and fixed the back search to not require a header.<commit_after>//
// File:            main.js
//
// Description:    This file defines the math mode for FoldingText. It
//                 processes the math lines using mathjs library. The
//                 library should be in the same directory as the extension.
//
// Author:        <NAME>
// Author Email:  <EMAIL>
//
define(function(require, exports, module) {
    'use strict';

    //
    // Load the libraries I need to use.
    //
    var Extensions = require('ft/core/extensions').Extensions;
    var math = require("./mathjs.js");

    //
    // Add the 'imath' mode extension to the system.
    //
    Extensions.addMode({
        name: 'imath'
    });

    //
    // Function:          inMathArea
    //
    // Description:       This function determines if the current node is in
    //                    a math area and is not empty.
    //
    function inMathArea(node) {
        if((node.modeContext() === 'imath')&&(node.text().trim() != "")) {
            return(true);
        } else {
            return(false);
        }
    };

    //
    // Function:          Calculate
    //
    // Description:       This function get the current node and the current node's
    //                    string contents. It returns the result of the calculation.
    //
    function Calculate(str) {
        var result = 0;
        try {
            //
            // Use the Mathjs library to evaluate the equation.
            //
           result = math.eval(str.substr(0,str.length-2));
        } catch(e) {
            //
            // Mathjs had a problem with the expressions. Return an ?
            //
            result = "?";
        }
        return(result);
    };

    //
    // Function:      ProcessNode
    //
    // Description:   This function is used to process a node in the math
    //                context. It expects the node to be passed to it.
    //
    function ProcessNode(node) {
        //
        // Calculate if needed. Get the text of the line.
        //
        var result = node.text();

        //
        // See if it ends in "=>". If so, process the line.
        //
        if(result.substr(-2) == "=>"){
            //
            // See if some of the previous lines had
            // variable declarations.
            //
            var pnode = node,
                text = "";
            while(pnode && (!pnode.mode()) && (pnode.modeContext() === 'imath')) {
                //
                // Not a heading, see if it has an evaluate command.
                //
                text = pnode.text();
                if(text.search("=>") < 0) {
                    //
                    // No evaluation, add it to the rest.
                    //
                    result = text + "\n" + result;
                }
                pnode = pnode.previousBranch();
            }

            //
            // Calculate the result. If Calculate returns an array, there were
            // variables figured in as well. Just get the final result.
            //
            var cresult = Calculate(result);
            if(isArray(cresult)){
                //
                // We will get more than on answer back. Therefore,
                // just get the last result.
                //
                cresult = cresult[cresult.length-1];
            }

            //
            // Put the result together with the original line.
            //
            result = node.text() + " " + cresult;

            //
            // Update the line.
            //
            node.setText(result);
        }
    };

    //
{'fraction_non_alphanumeric': 0.11241588600079166, 'fraction_numerical': 0.0015833223380393193, 'mean_word_length': 1.040926225094238, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '1381374', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1849, 'n_words': 611}