SearXNG is a free internet metasearch engine which aggregates results from various search services and databases. Users are neither tracked nor profiled.
Python bingbravedegoogleduckduckgogooglemetasearchprivacypythonqwantsearchsearch-enginesearxsearxngstartpageyahoo
Info: code refactoring & bug fixes
Alexandre Flamentdbe3eaabc929
diff --git a/searx/infopage/__init__.py b/searx/infopage/__init__.pyindex 9b181ad60..44f3bdbd7 100644--- a/searx/infopage/__init__.py+++ b/searx/infopage/__init__.py@@ -116,20 +116,20 @@ class InfoPageSet: # pylint: disable=too-few-public-methodsself, page_class: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None):self.page_class = page_class or InfoPage- self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}-- # future: could be set from settings.xml-self.folder: str = info_folder or _INFO_FOLDER"""location of the Markdwon files"""+ self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}+self.locale_default: str = 'en'"""default language"""- self.locales: typing.List = [locale for locale in os.listdir(_INFO_FOLDER) if locale in LOCALE_NAMES]+ self.locales: typing.List[str] = [+ locale.replace('_', '-') for locale in os.listdir(_INFO_FOLDER) if locale.replace('_', '-') in LOCALE_NAMES+ ]"""list of supported languages (aka locales)"""- self.toc: typing.List = [+ self.toc: typing.List[str] = ['search-syntax','about',]@@ -161,7 +161,7 @@ class InfoPageSet: # pylint: disable=too-few-public-methods# not yet instantiated- fname = os.path.join(self.folder, locale, pagename) + '.md'+ fname = os.path.join(self.folder, locale.replace('-', '_'), pagename) + '.md'if not os.path.exists(fname):logger.info('file %s does not exists', fname)self.CACHE[cache_key] = None@@ -171,9 +171,13 @@ class InfoPageSet: # pylint: disable=too-few-public-methodsself.CACHE[cache_key] = pagereturn page- def all_pages(self, locale: typing.Optional[str] = None):+ def iter_pages(self, locale: typing.Optional[str] = None, fallback_to_default=False):"""Iterate over all pages of the TOC"""locale = locale or self.locale_default- for pagename in self.toc:- page = self.get_page(pagename, locale)- yield pagename, page+ for page_name in self.toc:+ page_locale = locale+ page = self.get_page(page_name, locale)+ if fallback_to_default and page is None:+ page_locale = self.locale_default+ page = self.get_page(page_name, self.locale_default)+ yield page_name, page_locale, pagediff --git a/searx/templates/oscar/info.html b/searx/templates/oscar/info.htmlindex 0c2044adb..c12446a08 100644--- a/searx/templates/oscar/info.html+++ b/searx/templates/oscar/info.html@@ -2,7 +2,7 @@{% block title %}{{ active_page.title }} - {% endblock %}{% block content %}<ul class="nav nav-tabs">- {% for pagename, page, locale in all_pages %}+ {% for pagename, locale, page in all_pages %}<li><a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a></li>diff --git a/searx/templates/simple/info.html b/searx/templates/simple/info.htmlindex 5030cd490..b94ad0037 100644--- a/searx/templates/simple/info.html+++ b/searx/templates/simple/info.html@@ -2,7 +2,7 @@{% block title %}{{ active_page.title }} - {% endblock %}{% block content %}<ul class="tabs">-{% for pagename, page, locale in all_pages %}+{% for pagename, locale, page in all_pages %}<li><a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a></li>diff --git a/searx/webapp.py b/searx/webapp.pyindex 5d3e7ebfd..8c3e78deb 100755--- a/searx/webapp.py+++ b/searx/webapp.py@@ -154,6 +154,7 @@ STATS_SORT_PARAMETERS = {'time': (False, 'total', 0),'reliability': (False, 'reliability', 100),}+_INFO_PAGES = infopage.InfoPageSet()# Flask appapp = Flask(__name__, static_folder=settings['ui']['static_path'], template_folder=templates_path)@@ -374,7 +375,7 @@ def get_result_template(theme_name: str, template_name: str):return 'result_templates/' + template_name-def url_for_theme(endpoint: str, override_theme: Optional[str] = None, **values):+def custom_url_for(endpoint: str, override_theme: Optional[str] = None, **values):suffix = ""if endpoint == 'static' and values.get('filename'):theme_name = get_current_theme_name(override=override_theme)@@ -506,7 +507,7 @@ def render(template_name: str, override_theme: str = None, **kwargs):kwargs['get_pretty_url'] = get_pretty_url# helpers to create links to other pages- kwargs['url_for'] = url_for_theme # override url_for function in templates+ kwargs['url_for'] = custom_url_for # override url_for function in templateskwargs['image_proxify'] = image_proxifykwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else Nonekwargs['proxify_results'] = settings.get('result_proxy', {}).get('proxify_results', True)@@ -906,34 +907,21 @@ def __get_translated_errors(unresponsive_engines: Iterable[UnresponsiveEngine]):@app.route('/about', methods=['GET'])def about():"""Redirect to about page"""- locale = request.preferences.get_value('locale')- return redirect(url_for('info', pagename='about', locale=locale))---_INFO_PAGES = infopage.InfoPageSet()+ # custom_url_for is going to add the locale+ return redirect(custom_url_for('info', pagename='about'))@app.route('/info/<locale>/<pagename>', methods=['GET'])def info(pagename, locale):"""Render page of online user documentation"""-page = _INFO_PAGES.get_page(pagename, locale)if page is None:flask.abort(404)- def all_pages():- user_locale = request.preferences.get_value('locale')- for for_pagename, for_page in _INFO_PAGES.all_pages(user_locale):- for_locale = locale- if for_page is None:- # we are sure that for_pagename != pagename- for_page = _INFO_PAGES.get_page(for_pagename, _INFO_PAGES.locale_default)- for_locale = _INFO_PAGES.locale_default- yield for_pagename, for_page, for_locale-+ user_locale = request.preferences.get_value('locale')return render('info.html',- all_pages=all_pages(),+ all_pages=_INFO_PAGES.iter_pages(user_locale, fallback_to_default=True),active_page=page,active_pagename=pagename,)@@ -1317,9 +1305,9 @@ def stats_checker():def robots():return Response("""User-agent: *-Allow: /-Allow: /about+Allow: /info/en/aboutDisallow: /stats+Disallow: /image_proxyDisallow: /preferencesDisallow: /*?*q=*""",diff --git a/searxng_extra/docs_prebuild b/searxng_extra/docs_prebuildindex 879007f64..738fd17bd 100755--- a/searxng_extra/docs_prebuild+++ b/searxng_extra/docs_prebuild@@ -24,7 +24,7 @@ def main():infopageset_ctx = _offline_infosetset_ctx()with infopageset_ctx as infopageset:- for _, page in infopageset.all_pages('en'):+ for _, _, page in infopageset.iter_pages('en'):fname = os.path.join(_doc_user, os.path.basename(page.fname))with open(fname, 'w') as f:f.write(page.content)