mirror of https://github.com/searxng/searxng.git
Info: code refactoring & bug fixes
This commit is contained in:
parent
bb19c7eccf
commit
dbe3eaabc9
|
@ -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: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None
|
||||||
):
|
):
|
||||||
self.page_class = page_class or InfoPage
|
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
|
self.folder: str = info_folder or _INFO_FOLDER
|
||||||
"""location of the Markdwon files"""
|
"""location of the Markdwon files"""
|
||||||
|
|
||||||
|
self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}
|
||||||
|
|
||||||
self.locale_default: str = 'en'
|
self.locale_default: str = 'en'
|
||||||
"""default language"""
|
"""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)"""
|
"""list of supported languages (aka locales)"""
|
||||||
|
|
||||||
self.toc: typing.List = [
|
self.toc: typing.List[str] = [
|
||||||
'search-syntax',
|
'search-syntax',
|
||||||
'about',
|
'about',
|
||||||
]
|
]
|
||||||
|
@ -161,7 +161,7 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
# not yet instantiated
|
# 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):
|
if not os.path.exists(fname):
|
||||||
logger.info('file %s does not exists', fname)
|
logger.info('file %s does not exists', fname)
|
||||||
self.CACHE[cache_key] = None
|
self.CACHE[cache_key] = None
|
||||||
|
@ -171,9 +171,13 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
self.CACHE[cache_key] = page
|
self.CACHE[cache_key] = page
|
||||||
return 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"""
|
"""Iterate over all pages of the TOC"""
|
||||||
locale = locale or self.locale_default
|
locale = locale or self.locale_default
|
||||||
for pagename in self.toc:
|
for page_name in self.toc:
|
||||||
page = self.get_page(pagename, locale)
|
page_locale = locale
|
||||||
yield pagename, page
|
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
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{% block title %}{{ active_page.title }} - {% endblock %}
|
{% block title %}{{ active_page.title }} - {% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<ul class="nav nav-tabs">
|
<ul class="nav nav-tabs">
|
||||||
{% for pagename, page, locale in all_pages %}
|
{% for pagename, locale, page in all_pages %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a>
|
<a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{% block title %}{{ active_page.title }} - {% endblock %}
|
{% block title %}{{ active_page.title }} - {% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<ul class="tabs">
|
<ul class="tabs">
|
||||||
{% for pagename, page, locale in all_pages %}
|
{% for pagename, locale, page in all_pages %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a>
|
<a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -154,6 +154,7 @@ STATS_SORT_PARAMETERS = {
|
||||||
'time': (False, 'total', 0),
|
'time': (False, 'total', 0),
|
||||||
'reliability': (False, 'reliability', 100),
|
'reliability': (False, 'reliability', 100),
|
||||||
}
|
}
|
||||||
|
_INFO_PAGES = infopage.InfoPageSet()
|
||||||
|
|
||||||
# Flask app
|
# Flask app
|
||||||
app = Flask(__name__, static_folder=settings['ui']['static_path'], template_folder=templates_path)
|
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
|
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 = ""
|
suffix = ""
|
||||||
if endpoint == 'static' and values.get('filename'):
|
if endpoint == 'static' and values.get('filename'):
|
||||||
theme_name = get_current_theme_name(override=override_theme)
|
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
|
kwargs['get_pretty_url'] = get_pretty_url
|
||||||
|
|
||||||
# helpers to create links to other pages
|
# 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['image_proxify'] = image_proxify
|
||||||
kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None
|
kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None
|
||||||
kwargs['proxify_results'] = settings.get('result_proxy', {}).get('proxify_results', True)
|
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'])
|
@app.route('/about', methods=['GET'])
|
||||||
def about():
|
def about():
|
||||||
"""Redirect to about page"""
|
"""Redirect to about page"""
|
||||||
locale = request.preferences.get_value('locale')
|
# custom_url_for is going to add the locale
|
||||||
return redirect(url_for('info', pagename='about', locale=locale))
|
return redirect(custom_url_for('info', pagename='about'))
|
||||||
|
|
||||||
|
|
||||||
_INFO_PAGES = infopage.InfoPageSet()
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/info/<locale>/<pagename>', methods=['GET'])
|
@app.route('/info/<locale>/<pagename>', methods=['GET'])
|
||||||
def info(pagename, locale):
|
def info(pagename, locale):
|
||||||
"""Render page of online user documentation"""
|
"""Render page of online user documentation"""
|
||||||
|
|
||||||
page = _INFO_PAGES.get_page(pagename, locale)
|
page = _INFO_PAGES.get_page(pagename, locale)
|
||||||
if page is None:
|
if page is None:
|
||||||
flask.abort(404)
|
flask.abort(404)
|
||||||
|
|
||||||
def all_pages():
|
user_locale = request.preferences.get_value('locale')
|
||||||
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(
|
return render(
|
||||||
'info.html',
|
'info.html',
|
||||||
all_pages=all_pages(),
|
all_pages=_INFO_PAGES.iter_pages(user_locale, fallback_to_default=True),
|
||||||
active_page=page,
|
active_page=page,
|
||||||
active_pagename=pagename,
|
active_pagename=pagename,
|
||||||
)
|
)
|
||||||
|
@ -1317,9 +1305,9 @@ def stats_checker():
|
||||||
def robots():
|
def robots():
|
||||||
return Response(
|
return Response(
|
||||||
"""User-agent: *
|
"""User-agent: *
|
||||||
Allow: /
|
Allow: /info/en/about
|
||||||
Allow: /about
|
|
||||||
Disallow: /stats
|
Disallow: /stats
|
||||||
|
Disallow: /image_proxy
|
||||||
Disallow: /preferences
|
Disallow: /preferences
|
||||||
Disallow: /*?*q=*
|
Disallow: /*?*q=*
|
||||||
""",
|
""",
|
||||||
|
|
|
@ -24,7 +24,7 @@ def main():
|
||||||
infopageset_ctx = _offline_infosetset_ctx()
|
infopageset_ctx = _offline_infosetset_ctx()
|
||||||
|
|
||||||
with infopageset_ctx as infopageset:
|
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))
|
fname = os.path.join(_doc_user, os.path.basename(page.fname))
|
||||||
with open(fname, 'w') as f:
|
with open(fname, 'w') as f:
|
||||||
f.write(page.content)
|
f.write(page.content)
|
||||||
|
|
Loading…
Reference in New Issue