From fb9eedbf40410a5558bfc03b4c50f7ff41b023b4 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 31 Jan 2022 11:24:45 +0100 Subject: [PATCH] [enh] introduce /help route Translation will be implemented in the future. For now the "en" in /help/en/ is hardcoded. --- searx/templates/oscar/about.html | 6 ------ searx/templates/oscar/help.html | 5 +++++ searx/templates/simple/about.html | 5 ----- searx/templates/simple/help.html | 5 +++++ searx/user_help.py | 27 +++++++++++++++++++++------ searx/webapp.py | 15 +++++++++++++-- tests/unit/test_webapp.py | 2 +- 7 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 searx/templates/oscar/about.html create mode 100644 searx/templates/oscar/help.html delete mode 100644 searx/templates/simple/about.html create mode 100644 searx/templates/simple/help.html diff --git a/searx/templates/oscar/about.html b/searx/templates/oscar/about.html deleted file mode 100644 index a644761b6..000000000 --- a/searx/templates/oscar/about.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "oscar/base.html" %} -{% block title %}{{ _('about') }} - {% endblock %} -{% block content %} -{{ help.about | safe }} -{% include "__common__/aboutextend.html" ignore missing %} -{% endblock %} diff --git a/searx/templates/oscar/help.html b/searx/templates/oscar/help.html new file mode 100644 index 000000000..7865f6649 --- /dev/null +++ b/searx/templates/oscar/help.html @@ -0,0 +1,5 @@ +{% extends "oscar/base.html" %} +{% block title %}{{ page.title }} - {% endblock %} +{% block content %} +{{ page.content | safe }} +{% endblock %} diff --git a/searx/templates/simple/about.html b/searx/templates/simple/about.html deleted file mode 100644 index f6a0a96a9..000000000 --- a/searx/templates/simple/about.html +++ /dev/null @@ -1,5 +0,0 @@ -{% extends 'simple/page_with_header.html' %} -{% block content %} -{{ help.about | safe }} -{% include "__common__/aboutextend.html" ignore missing %} -{% endblock %} diff --git a/searx/templates/simple/help.html b/searx/templates/simple/help.html new file mode 100644 index 000000000..0be59f495 --- /dev/null +++ b/searx/templates/simple/help.html @@ -0,0 +1,5 @@ +{% extends 'simple/page_with_header.html' %} +{% block title %}{{ page.title }} - {% endblock %} +{% block content %} +{{ page.content | safe }} +{% endblock %} diff --git a/searx/user_help.py b/searx/user_help.py index bb2c1b1ca..62628ab40 100644 --- a/searx/user_help.py +++ b/searx/user_help.py @@ -1,5 +1,5 @@ # pyright: basic -from typing import Dict +from typing import Dict, NamedTuple import os.path import pkg_resources @@ -10,8 +10,14 @@ import mistletoe from . import get_setting from .version import GIT_URL -HELP: Dict[str, str] = {} -""" Maps a filename under help/ without the file extension to the rendered HTML. """ + +class HelpPage(NamedTuple): + title: str + content: str + + +PAGES: Dict[str, HelpPage] = {} +""" Maps a filename under help/ without the file extension to the rendered page. """ def render(app: flask.Flask): @@ -44,6 +50,15 @@ def render(app: flask.Flask): if ext != '.md': continue - markdown = pkg_resources.resource_string(__name__, 'help/' + filename).decode() - markdown = define_link_targets + markdown - HELP[rootname] = mistletoe.markdown(markdown) + file_content = pkg_resources.resource_string(__name__, 'help/' + filename).decode() + markdown = define_link_targets + file_content + assert file_content.startswith('# ') + title = file_content.split('\n', maxsplit=1)[0].strip('# ') + content: str = mistletoe.markdown(markdown) + + if filename == 'about.md': + try: + content += pkg_resources.resource_string(__name__, 'templates/__common__/aboutextend.html').decode() + except FileNotFoundError: + pass + PAGES[rootname] = HelpPage(title=title, content=content) diff --git a/searx/webapp.py b/searx/webapp.py index 2e6e388e5..bd296cddc 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -877,8 +877,19 @@ def __get_translated_errors(unresponsive_engines: Iterable[UnresponsiveEngine]): @app.route('/about', methods=['GET']) def about(): - """Render about page""" - return render('about.html', help=user_help.HELP) + """Redirect to about page""" + return redirect(url_for('help_page', pagename='about')) + + +@app.route('/help/en/', methods=['GET']) +def help_page(pagename): + """Render help page""" + page = user_help.PAGES.get(pagename) + + if page is None: + flask.abort(404) + + return render('help.html', page=user_help.PAGES[pagename]) @app.route('/autocompleter', methods=['GET', 'POST']) diff --git a/tests/unit/test_webapp.py b/tests/unit/test_webapp.py index 60d59ccf8..cd9472135 100644 --- a/tests/unit/test_webapp.py +++ b/tests/unit/test_webapp.py @@ -174,7 +174,7 @@ class ViewsTestCase(SearxTestCase): self.assertIn(b'first test content', result.data) def test_about(self): - result = self.app.get('/about') + result = self.app.get('/help/en/about') self.assertEqual(result.status_code, 200) self.assertIn(b'

About SearXNG

', result.data)