mirror of https://github.com/searxng/searxng.git
Various change on PR 930
This commit is contained in:
parent
59100e8525
commit
1157462ff9
|
@ -19,8 +19,9 @@ Usage in a Flask app route:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ['InfoPage', 'MistletoePage', 'InfoPageSet']
|
__all__ = ['InfoPage', 'InfoPageSet']
|
||||||
|
|
||||||
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import logging
|
import logging
|
||||||
import typing
|
import typing
|
||||||
|
@ -33,16 +34,18 @@ import mistletoe
|
||||||
from .. import get_setting
|
from .. import get_setting
|
||||||
from ..compat import cached_property
|
from ..compat import cached_property
|
||||||
from ..version import GIT_URL
|
from ..version import GIT_URL
|
||||||
|
from ..locales import LOCALE_NAMES
|
||||||
|
|
||||||
logger = logging.getLogger('doc')
|
|
||||||
|
logger = logging.getLogger('searx.infopage')
|
||||||
|
_INFO_FOLDER = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
class InfoPage:
|
class InfoPage:
|
||||||
"""A page of the :py:obj:`online documentation <InfoPageSet>`."""
|
"""A page of the :py:obj:`online documentation <InfoPageSet>`."""
|
||||||
|
|
||||||
def __init__(self, fname, base_url=None):
|
def __init__(self, fname):
|
||||||
self.fname = fname
|
self.fname = fname
|
||||||
self.base_url = base_url
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def raw_content(self):
|
def raw_content(self):
|
||||||
|
@ -66,19 +69,25 @@ class InfoPage:
|
||||||
t = l.strip('# ')
|
t = l.strip('# ')
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def html(self):
|
||||||
|
"""Render Markdown (CommonMark_) to HTML by using mistletoe_.
|
||||||
|
|
||||||
|
.. _CommonMark: https://commonmark.org/
|
||||||
|
.. _mistletoe: https://github.com/miyuchina/mistletoe
|
||||||
|
|
||||||
|
"""
|
||||||
|
return mistletoe.markdown(self.content)
|
||||||
|
|
||||||
def get_ctx(self): # pylint: disable=no-self-use
|
def get_ctx(self): # pylint: disable=no-self-use
|
||||||
"""Jinja context to render :py:obj:`InfoPage.content`"""
|
"""Jinja context to render :py:obj:`InfoPage.content`"""
|
||||||
|
|
||||||
def _md_link(name, url):
|
def _md_link(name, url):
|
||||||
url = url_for(url)
|
url = url_for(url, _external=True)
|
||||||
if self.base_url:
|
|
||||||
url = self.base_url + url
|
|
||||||
return "[%s](%s)" % (name, url)
|
return "[%s](%s)" % (name, url)
|
||||||
|
|
||||||
def _md_search(query):
|
def _md_search(query):
|
||||||
url = '%s?q=%s' % (url_for('search'), urllib.parse.quote(query))
|
url = '%s?q=%s' % (url_for('search', _external=True), urllib.parse.quote(query))
|
||||||
if self.base_url:
|
|
||||||
url = self.base_url + url
|
|
||||||
return '[%s](%s)' % (query, url)
|
return '[%s](%s)' % (query, url)
|
||||||
|
|
||||||
ctx = {}
|
ctx = {}
|
||||||
|
@ -89,33 +98,8 @@ class InfoPage:
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
def render(self):
|
def __repr__(self):
|
||||||
"""Render / return content"""
|
return f'<{self.__class__.__name__} fname={self.fname!r}>'
|
||||||
return self.content
|
|
||||||
|
|
||||||
|
|
||||||
class MistletoePage(InfoPage):
|
|
||||||
"""A HTML page of the :py:obj:`online documentation <InfoPageSet>`."""
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def html(self):
|
|
||||||
"""HTML representation of this page"""
|
|
||||||
return self.render()
|
|
||||||
|
|
||||||
def render(self):
|
|
||||||
"""Render Markdown (CommonMark_) to HTML by using mistletoe_.
|
|
||||||
|
|
||||||
.. _CommonMark: https://commonmark.org/
|
|
||||||
.. _mistletoe: https://github.com/miyuchina/mistletoe
|
|
||||||
|
|
||||||
"""
|
|
||||||
return mistletoe.markdown(self.content)
|
|
||||||
|
|
||||||
|
|
||||||
_INFO_FOLDER = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'info'))
|
|
||||||
|
|
||||||
|
|
||||||
class InfoPageSet: # pylint: disable=too-few-public-methods
|
class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
|
@ -123,24 +107,26 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
:param page_class: render online documentation by :py:obj:`InfoPage` parser.
|
:param page_class: render online documentation by :py:obj:`InfoPage` parser.
|
||||||
:type page_class: :py:obj:`InfoPage`
|
:type page_class: :py:obj:`InfoPage`
|
||||||
|
|
||||||
|
:param info_folder: information directory
|
||||||
|
:type info_folder: str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, page_class: typing.Type[InfoPage], base_url=None):
|
def __init__(
|
||||||
self.page_class = page_class
|
self, page_class: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None
|
||||||
self.base_url = base_url
|
):
|
||||||
self.CACHE: typing.Dict[tuple, InfoPage] = {}
|
self.page_class = page_class or InfoPage
|
||||||
|
self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}
|
||||||
|
|
||||||
# future: could be set from settings.xml
|
# future: could be set from settings.xml
|
||||||
|
|
||||||
self.folder: str = _INFO_FOLDER
|
self.folder: str = info_folder or _INFO_FOLDER
|
||||||
"""location of the Markdwon files"""
|
"""location of the Markdwon files"""
|
||||||
|
|
||||||
self.i18n_origin: str = 'en'
|
self.locale_default: str = 'en'
|
||||||
"""default language"""
|
"""default language"""
|
||||||
|
|
||||||
self.l10n: typing.List = [
|
self.locales: typing.List = [locale for locale in os.listdir(_INFO_FOLDER) if locale in LOCALE_NAMES]
|
||||||
'en',
|
|
||||||
]
|
|
||||||
"""list of supported languages (aka locales)"""
|
"""list of supported languages (aka locales)"""
|
||||||
|
|
||||||
self.toc: typing.List = [
|
self.toc: typing.List = [
|
||||||
|
@ -160,12 +146,13 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
:type locale: str
|
:type locale: str
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
locale = locale or self.locale_default
|
||||||
|
|
||||||
if pagename not in self.toc:
|
if pagename not in self.toc:
|
||||||
return None
|
return None
|
||||||
if locale is not None and locale not in self.l10n:
|
if locale not in self.locales:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
locale = locale or self.i18n_origin
|
|
||||||
cache_key = (pagename, locale)
|
cache_key = (pagename, locale)
|
||||||
page = self.CACHE.get(cache_key)
|
page = self.CACHE.get(cache_key)
|
||||||
|
|
||||||
|
@ -176,16 +163,17 @@ class InfoPageSet: # pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
fname = os.path.join(self.folder, locale, pagename) + '.md'
|
fname = os.path.join(self.folder, locale, pagename) + '.md'
|
||||||
if not os.path.exists(fname):
|
if not os.path.exists(fname):
|
||||||
logger.error('file %s does not exists', fname)
|
logger.info('file %s does not exists', fname)
|
||||||
|
self.CACHE[cache_key] = None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
page = self.page_class(fname, self.base_url)
|
page = self.page_class(fname)
|
||||||
self.CACHE[cache_key] = page
|
self.CACHE[cache_key] = page
|
||||||
return page
|
return page
|
||||||
|
|
||||||
def all_pages(self, locale: typing.Optional[str] = None):
|
def all_pages(self, locale: typing.Optional[str] = None):
|
||||||
"""Iterate over all pages"""
|
"""Iterate over all pages of the TOC"""
|
||||||
locale = locale or self.i18n_origin
|
locale = locale or self.locale_default
|
||||||
for pagename in self.toc:
|
for pagename in self.toc:
|
||||||
page = self.get_page(pagename, locale)
|
page = self.get_page(pagename, locale)
|
||||||
yield pagename, page
|
yield pagename, page
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
{% 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 in all_pages('en') %}
|
{% for pagename, page, locale in all_pages %}
|
||||||
<li {% if pagename == active_pagename %}class="active"{% endif %}>
|
<li>
|
||||||
<a href="{{pagename}}">{{page.title}}</a>
|
<a href="{{ url_for('info', pagename=pagename, locale=locale) }}" {% if pagename == active_pagename %}class="active"{% endif %}>{{page.title}}</a>
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<a href="{{ url_for('index') }}">{{ instance_name }}</a>{{- "" -}}
|
<a href="{{ url_for('index') }}">{{ instance_name }}</a>{{- "" -}}
|
||||||
</span>{{- "" -}}
|
</span>{{- "" -}}
|
||||||
<span class="{% if rtl %}pull-left{% else %}pull-right{% endif %}">{{- "" -}}
|
<span class="{% if rtl %}pull-left{% else %}pull-right{% endif %}">{{- "" -}}
|
||||||
<a href="{{ url_for('info', pagename='about', locale=current_locale) }}">{{ _('about') }}</a>{{- "" -}}
|
<a href="{{ url_for('info', pagename='about') }}">{{ _('about') }}</a>{{- "" -}}
|
||||||
<a href="{{ url_for('preferences') }}">{{ _('preferences') }}</a>{{- "" -}}
|
<a href="{{ url_for('preferences') }}">{{ _('preferences') }}</a>{{- "" -}}
|
||||||
</span>{{- "" -}}
|
</span>{{- "" -}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -58,7 +58,7 @@
|
||||||
</main>
|
</main>
|
||||||
<footer>
|
<footer>
|
||||||
<p>
|
<p>
|
||||||
{{ _('Powered by') }} <a href="{{ url_for('info', pagename='about', locale=current_locale) }}">searxng</a> - {{ searx_version }} — {{ _('a privacy-respecting, hackable metasearch engine') }}<br/>
|
{{ _('Powered by') }} <a href="{{ url_for('info', pagename='about') }}">searxng</a> - {{ searx_version }} — {{ _('a privacy-respecting, hackable metasearch engine') }}<br/>
|
||||||
<a href="{{ searx_git_url }}">{{ _('Source code') }}</a> |
|
<a href="{{ searx_git_url }}">{{ _('Source code') }}</a> |
|
||||||
<a href="{{ get_setting('brand.issue_url') }}">{{ _('Issue tracker') }}</a> |
|
<a href="{{ get_setting('brand.issue_url') }}">{{ _('Issue tracker') }}</a> |
|
||||||
<a href="{{ url_for('stats') }}">{{ _('Engine stats') }}</a> |
|
<a href="{{ url_for('stats') }}">{{ _('Engine stats') }}</a> |
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
{% 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 in all_pages('en') %}
|
{% for pagename, page, locale in all_pages %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{pagename}}" {% 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>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -383,6 +383,11 @@ def url_for_theme(endpoint: str, override_theme: Optional[str] = None, **values)
|
||||||
if file_hash:
|
if file_hash:
|
||||||
values['filename'] = filename_with_theme
|
values['filename'] = filename_with_theme
|
||||||
suffix = "?" + file_hash
|
suffix = "?" + file_hash
|
||||||
|
if endpoint == 'info' and 'locale' not in values:
|
||||||
|
locale = request.preferences.get_value('locale')
|
||||||
|
if _INFO_PAGES.get_page(values['pagename'], locale) is None:
|
||||||
|
locale = _INFO_PAGES.locale_default
|
||||||
|
values['locale'] = locale
|
||||||
return url_for(endpoint, **values) + suffix
|
return url_for(endpoint, **values) + suffix
|
||||||
|
|
||||||
|
|
||||||
|
@ -905,23 +910,30 @@ def about():
|
||||||
return redirect(url_for('info', pagename='about', locale=locale))
|
return redirect(url_for('info', pagename='about', locale=locale))
|
||||||
|
|
||||||
|
|
||||||
_INFO_PAGES = infopage.InfoPageSet(infopage.MistletoePage)
|
_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"""
|
||||||
|
|
||||||
locale = locale or request.preferences.get_value('locale')
|
|
||||||
page = _INFO_PAGES.get_page(pagename, locale)
|
page = _INFO_PAGES.get_page(pagename, locale)
|
||||||
if page is None:
|
|
||||||
page = _INFO_PAGES.get_page(pagename)
|
|
||||||
if page is None:
|
if page is None:
|
||||||
flask.abort(404)
|
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(
|
return render(
|
||||||
'info.html',
|
'info.html',
|
||||||
all_pages=_INFO_PAGES.all_pages,
|
all_pages=all_pages(),
|
||||||
active_page=page,
|
active_page=page,
|
||||||
active_pagename=pagename,
|
active_pagename=pagename,
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,24 +8,26 @@
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
import time
|
import time
|
||||||
|
from contextlib import contextmanager
|
||||||
from searx import settings, get_setting
|
from searx import settings, get_setting
|
||||||
from searx.infopage import InfoPageSet, InfoPage
|
from searx.infopage import InfoPageSet, InfoPage
|
||||||
|
|
||||||
|
|
||||||
_doc_user = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'user'))
|
_doc_user = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'docs', 'user'))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
DOC = None
|
|
||||||
base_url = get_setting('server.base_url', None)
|
base_url = get_setting('server.base_url', None)
|
||||||
|
|
||||||
if base_url:
|
if base_url:
|
||||||
DOC = _render_all_with_flask_ctx(base_url)
|
infopageset_ctx = _instance_infosetset_ctx(base_url)
|
||||||
else:
|
else:
|
||||||
DOC = _render_all()
|
infopageset_ctx = _offline_infosetset_ctx()
|
||||||
for pagename, page in DOC.all_pages('en'):
|
|
||||||
fname = os.path.join(_doc_user, os.path.basename(page.fname))
|
with infopageset_ctx as infopageset:
|
||||||
with open(fname, 'w') as f:
|
for _, page in infopageset.all_pages('en'):
|
||||||
f.write(page.content)
|
fname = os.path.join(_doc_user, os.path.basename(page.fname))
|
||||||
|
with open(fname, 'w') as f:
|
||||||
|
f.write(page.content)
|
||||||
|
|
||||||
|
|
||||||
class OfflinePage(InfoPage):
|
class OfflinePage(InfoPage):
|
||||||
|
@ -41,21 +43,17 @@ class OfflinePage(InfoPage):
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
def _render_all():
|
@contextmanager
|
||||||
DOC = InfoPageSet(OfflinePage)
|
def _offline_infosetset_ctx():
|
||||||
for pagename, page in DOC.all_pages('en'):
|
yield InfoPageSet(OfflinePage)
|
||||||
page.render()
|
|
||||||
return DOC
|
|
||||||
|
|
||||||
|
|
||||||
def _render_all_with_flask_ctx(base_url):
|
@contextmanager
|
||||||
|
def _instance_infosetset_ctx(base_url):
|
||||||
DOC = InfoPageSet(InfoPage, base_url)
|
|
||||||
|
|
||||||
# The url_for functions in the jinja templates need all routes to be
|
# The url_for functions in the jinja templates need all routes to be
|
||||||
# registered in the Flask app.
|
# registered in the Flask app.
|
||||||
|
|
||||||
settings['server']['secret_key'] = "x"
|
settings['server']['secret_key'] = ''
|
||||||
from searx.webapp import app
|
from searx.webapp import app
|
||||||
|
|
||||||
# Specify base_url so that url_for() works for base_urls. If base_url is
|
# Specify base_url so that url_for() works for base_urls. If base_url is
|
||||||
|
@ -63,8 +61,7 @@ def _render_all_with_flask_ctx(base_url):
|
||||||
# generics (see flaskfix.py).
|
# generics (see flaskfix.py).
|
||||||
|
|
||||||
with app.test_request_context(base_url=base_url):
|
with app.test_request_context(base_url=base_url):
|
||||||
for pagename, page in DOC.all_pages('en'):
|
yield InfoPageSet()
|
||||||
page.render()
|
|
||||||
|
|
||||||
# The searx.webapp import from above fires some HTTP requests, thats
|
# The searx.webapp import from above fires some HTTP requests, thats
|
||||||
# why we get a RuntimeError::
|
# why we get a RuntimeError::
|
||||||
|
|
Loading…
Reference in New Issue