Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e2fff0b6 authored by Nivesh Krishna's avatar Nivesh Krishna
Browse files

remove calculator plugin and add math.js

parent 8d9df357
Loading
Loading
Loading
Loading
+2076 −309

File changed.

Preview size limit exceeded, changes collapsed.

package.json

0 → 100644
+8 −0
Original line number Diff line number Diff line
{
  "dependencies": {
    "grunt-cli": "^1.4.3",
    "less": "2.7",
    "less-plugin-clean-css": "^1.5.1",
    "mathjs": "^10.5.3"
  }
}
+1 −1
Original line number Diff line number Diff line
@@ -269,7 +269,7 @@ def response(resp):
    # results --> number_of_results
        if not use_mobile_ui:
            try:
                _txt = eval_xpath_getindex(dom, '//div[@id="result-stats"]//text()', 0)
                _txt = eval_xpath_getindex(dom, '//div[@id="result-stats"]//text()', 0, 0)
                _digit = ''.join([n for n in _txt if n.isdigit()])
                number_of_results = int(_digit)
                results.append({'number_of_results': number_of_results})
+0 −2
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ logger = logger.getChild('plugins')
from searx.plugins import (oa_doi_rewrite,
                           ahmia_filter,
                           hash_plugin,
                           calculator,
                           https_rewrite,
                           infinite_scroll,
                           self_info,
@@ -176,7 +175,6 @@ plugins.register(search_on_category_select)
plugins.register(tracker_url_remover)
plugins.register(vim_hotkeys)
plugins.register(rest_api)
plugins.register(calculator)
# load external plugins
if 'plugins' in settings:
    plugins.register(*settings['plugins'], external=True)

searx/plugins/calculator.py

deleted100644 → 0
+0 −67
Original line number Diff line number Diff line
from searx import logger
from numexpr import evaluate
from flask_babel import gettext
from wrapt_timeout_decorator import timeout

name = gettext('Calculator')
description = gettext('This plugin extends results when the query is a mathematical expression')
default_on = True
logger = logger.getChild("calculator")


def check_if_loaded():
    logger.debug("initializing calculator plugin")


# Set timeout so that the plugin doesn't hang for long computations
@timeout(5)
def calculate(query):
    return evaluate(query).item()


def post_search(request, search):
    if search.search_query.pageno > 1:
        return
    try:
        query = search.search_query.query.lower()
        unmodified_query = query

        # Replace all frequently used substitutes
        query = query.replace("x", "*")
        query = query.replace("^", "**")
        query = query.replace("%", "*(0.01)*")

        # Not going to compute if only one number is present
        try:
            x = int(query) or float(query)
            return
        except ValueError:
            pass

        # Not going to compute if no numbers are present
        if not any(i.isdigit() for i in query):
            return

        # Not going to compute the result if the query is too big
        if len(query) > 30:
            return

        # Multiply by float to upcast all numbers to floats
        # https://numexpr.readthedocs.io/projects/NumExpr3/en/latest/user_guide.html#casting-rules
        query += "*1.0"

        value = calculate(query)
        if type(value) in (int, float):
            search.result_container.answers.clear()
            answer = "{} = {}".format(unmodified_query, value)
            search.result_container.answers['calculator'] = {'answer': answer, 'calculator': True}
    except (ZeroDivisionError, ValueError, FloatingPointError, MemoryError, OverflowError, TimeoutError) as e:
        answer = gettext('Error')
        search.result_container.answers['calculator'] = {'answer': answer, 'calculator': True}
    except Exception as e:
        logger.debug(e)

    return


check_if_loaded()
Loading