[help] introduce API to parse and render Markdown

This commit is contained in:
Martin Fischer 2022-01-31 16:16:06 +01:00
parent 532680be67
commit 33ad043218
4 changed files with 23 additions and 15 deletions

View file

@ -8,5 +8,6 @@
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<h1>{{page.title}}</h1>
{{ page.content | safe }} {{ page.content | safe }}
{% endblock %} {% endblock %}

View file

@ -8,5 +8,6 @@
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<h1>{{page.title}}</h1>
{{ page.content | safe }} {{ page.content | safe }}
{% endblock %} {% endblock %}

View file

@ -31,7 +31,24 @@ class Template(string.Template):
idpattern = '(:?[a-z._:]+)' idpattern = '(:?[a-z._:]+)'
def render(app: flask.Flask): def render(markdown: str, variables=Dict[str, str], filename='') -> HelpPage:
first_line, markdown = markdown.split('\n', maxsplit=1)
assert first_line.startswith('# ')
try:
markdown = Template(markdown).substitute(variables)
except KeyError as e:
print('[FATAL ERROR] undefined variable ${} in {}'.format(e.args[0], filename))
print('available variables are:')
for key in variables:
print('\t' + key)
sys.exit(1)
content: str = mistletoe.markdown(markdown)
return HelpPage(title=first_line.strip('# '), content=content)
def initialize(app: flask.Flask):
""" """
Renders the user documentation. Must be called after all Flask routes have been Renders the user documentation. Must be called after all Flask routes have been
registered, because the documentation might try to link to them with Flask's `url_for`. registered, because the documentation might try to link to them with Flask's `url_for`.
@ -57,21 +74,10 @@ def render(app: flask.Flask):
for pagename in _TOC: for pagename in _TOC:
filename = 'help/en/' + pagename + '.md' filename = 'help/en/' + pagename + '.md'
file_content = pkg_resources.resource_string(__name__, filename).decode() file_content = pkg_resources.resource_string(__name__, filename).decode()
try:
markdown = Template(file_content).substitute(variables)
except KeyError as e:
print('[FATAL ERROR] undefined variable ${} in {}'.format(e.args[0], filename))
print('available variables are:')
for key in variables:
print('\t' + key)
sys.exit(1)
assert file_content.startswith('# ')
title = file_content.split('\n', maxsplit=1)[0].strip('# ')
content: str = mistletoe.markdown(markdown)
if pagename == 'about': if pagename == 'about':
try: try:
content += pkg_resources.resource_string(__name__, 'templates/__common__/aboutextend.html').decode() file_content += pkg_resources.resource_string(__name__, 'templates/__common__/aboutextend.html').decode()
except FileNotFoundError: except FileNotFoundError:
pass pass
PAGES[pagename] = HelpPage(title=title, content=content) PAGES[pagename] = render(file_content, variables, filename=filename)

View file

@ -1391,7 +1391,7 @@ werkzeug_reloader = flask_run_development or (searx_debug and __name__ == "__mai
if not werkzeug_reloader or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"): if not werkzeug_reloader or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
plugin_initialize(app) plugin_initialize(app)
search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics']) search_initialize(enable_checker=True, check_network=True, enable_metrics=settings['general']['enable_metrics'])
user_help.render(app) user_help.initialize(app)
def run(): def run():