[enh] introduce /help route

Translation will be implemented in the future.
For now the "en" in /help/en/<pagename> is hardcoded.
This commit is contained in:
Martin Fischer 2022-01-31 11:24:45 +01:00
parent b93711b45d
commit fb9eedbf40
7 changed files with 45 additions and 20 deletions

View file

@ -1,6 +0,0 @@
{% extends "oscar/base.html" %}
{% block title %}{{ _('about') }} - {% endblock %}
{% block content %}
{{ help.about | safe }}
{% include "__common__/aboutextend.html" ignore missing %}
{% endblock %}

View file

@ -0,0 +1,5 @@
{% extends "oscar/base.html" %}
{% block title %}{{ page.title }} - {% endblock %}
{% block content %}
{{ page.content | safe }}
{% endblock %}

View file

@ -1,5 +0,0 @@
{% extends 'simple/page_with_header.html' %}
{% block content %}
{{ help.about | safe }}
{% include "__common__/aboutextend.html" ignore missing %}
{% endblock %}

View file

@ -0,0 +1,5 @@
{% extends 'simple/page_with_header.html' %}
{% block title %}{{ page.title }} - {% endblock %}
{% block content %}
{{ page.content | safe }}
{% endblock %}

View file

@ -1,5 +1,5 @@
# pyright: basic # pyright: basic
from typing import Dict from typing import Dict, NamedTuple
import os.path import os.path
import pkg_resources import pkg_resources
@ -10,8 +10,14 @@ import mistletoe
from . import get_setting from . import get_setting
from .version import GIT_URL 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): def render(app: flask.Flask):
@ -44,6 +50,15 @@ def render(app: flask.Flask):
if ext != '.md': if ext != '.md':
continue continue
markdown = pkg_resources.resource_string(__name__, 'help/' + filename).decode() file_content = pkg_resources.resource_string(__name__, 'help/' + filename).decode()
markdown = define_link_targets + markdown markdown = define_link_targets + file_content
HELP[rootname] = mistletoe.markdown(markdown) 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)

View file

@ -877,8 +877,19 @@ def __get_translated_errors(unresponsive_engines: Iterable[UnresponsiveEngine]):
@app.route('/about', methods=['GET']) @app.route('/about', methods=['GET'])
def about(): def about():
"""Render about page""" """Redirect to about page"""
return render('about.html', help=user_help.HELP) return redirect(url_for('help_page', pagename='about'))
@app.route('/help/en/<pagename>', 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']) @app.route('/autocompleter', methods=['GET', 'POST'])

View file

@ -174,7 +174,7 @@ class ViewsTestCase(SearxTestCase):
self.assertIn(b'<description>first test content</description>', result.data) self.assertIn(b'<description>first test content</description>', result.data)
def test_about(self): def test_about(self):
result = self.app.get('/about') result = self.app.get('/help/en/about')
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertIn(b'<h1>About SearXNG</h1>', result.data) self.assertIn(b'<h1>About SearXNG</h1>', result.data)