Merge pull request #969 from dalf/fix_info

Fix info
This commit is contained in:
Markus Heiser 2022-03-17 01:37:10 +01:00 committed by GitHub
commit 8c78c895a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 38 deletions

View File

@ -1,6 +1,6 @@
==================
User documentation
==================
================
User information
================
.. contents:: Contents
:depth: 3

View File

@ -116,20 +116,20 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
self, 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-methods
self.CACHE[cache_key] = page
return 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, page

View File

@ -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>

View File

@ -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>

View File

@ -154,6 +154,7 @@ STATS_SORT_PARAMETERS = {
'time': (False, 'total', 0),
'reliability': (False, 'reliability', 100),
}
_INFO_PAGES = infopage.InfoPageSet()
# Flask app
app = 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 templates
kwargs['image_proxify'] = image_proxify
kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None
kwargs['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
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/about
Disallow: /stats
Disallow: /image_proxy
Disallow: /preferences
Disallow: /*?*q=*
""",

View File

@ -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)