2022-03-12 10:18:08 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
|
|
|
# pyright: basic
|
|
|
|
"""Render SearXNG instance documentation.
|
|
|
|
|
|
|
|
Usage in a Flask app route:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from searx import infopage
|
|
|
|
|
|
|
|
_INFO_PAGES = infopage.InfoPageSet(infopage.MistletoePage)
|
|
|
|
|
|
|
|
@app.route('/info/<pagename>', methods=['GET'])
|
|
|
|
def info(pagename):
|
|
|
|
|
|
|
|
locale = request.preferences.get_value('locale')
|
|
|
|
page = _INFO_PAGES.get_page(pagename, locale)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
__all__ = ['InfoPage', 'InfoPageSet']
|
2022-03-12 10:18:08 +01:00
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
import os
|
2022-03-12 10:18:08 +01:00
|
|
|
import os.path
|
|
|
|
import logging
|
|
|
|
import typing
|
|
|
|
|
|
|
|
import urllib.parse
|
|
|
|
import jinja2
|
|
|
|
from flask.helpers import url_for
|
2022-04-06 15:49:50 +02:00
|
|
|
from markdown_it import MarkdownIt
|
2022-03-12 10:18:08 +01:00
|
|
|
|
|
|
|
from .. import get_setting
|
2022-03-12 15:37:45 +01:00
|
|
|
from ..compat import cached_property
|
2022-03-12 10:18:08 +01:00
|
|
|
from ..version import GIT_URL
|
2022-03-13 22:15:27 +01:00
|
|
|
from ..locales import LOCALE_NAMES
|
2022-03-12 10:18:08 +01:00
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger('searx.infopage')
|
|
|
|
_INFO_FOLDER = os.path.abspath(os.path.dirname(__file__))
|
2022-03-12 10:18:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
class InfoPage:
|
|
|
|
"""A page of the :py:obj:`online documentation <InfoPageSet>`."""
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
def __init__(self, fname):
|
2022-03-12 10:18:08 +01:00
|
|
|
self.fname = fname
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def raw_content(self):
|
|
|
|
"""Raw content of the page (without any jinja rendering)"""
|
|
|
|
with open(self.fname, 'r', encoding='utf-8') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def content(self):
|
|
|
|
"""Content of the page (rendered in a Jinja conntext)"""
|
|
|
|
ctx = self.get_ctx()
|
|
|
|
template = jinja2.Environment().from_string(self.raw_content)
|
|
|
|
return template.render(**ctx)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def title(self):
|
|
|
|
"""Title of the content (without any markup)"""
|
|
|
|
t = ""
|
|
|
|
for l in self.raw_content.split('\n'):
|
|
|
|
if l.startswith('# '):
|
|
|
|
t = l.strip('# ')
|
|
|
|
return t
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
@cached_property
|
|
|
|
def html(self):
|
2022-04-06 15:49:50 +02:00
|
|
|
"""Render Markdown (CommonMark_) to HTML by using markdown-it-py_.
|
2022-03-13 22:15:27 +01:00
|
|
|
|
|
|
|
.. _CommonMark: https://commonmark.org/
|
2022-04-06 15:49:50 +02:00
|
|
|
.. _markdown-it-py: https://github.com/executablebooks/markdown-it-py
|
2022-03-13 22:15:27 +01:00
|
|
|
|
|
|
|
"""
|
2022-05-07 18:23:10 +02:00
|
|
|
return (
|
|
|
|
MarkdownIt("commonmark", {"typographer": True}).enable(["replacements", "smartquotes"]).render(self.content)
|
|
|
|
)
|
2022-03-13 22:15:27 +01:00
|
|
|
|
2022-06-03 15:41:52 +02:00
|
|
|
def get_ctx(self):
|
2022-03-12 10:18:08 +01:00
|
|
|
"""Jinja context to render :py:obj:`InfoPage.content`"""
|
|
|
|
|
|
|
|
def _md_link(name, url):
|
2022-03-13 22:15:27 +01:00
|
|
|
url = url_for(url, _external=True)
|
2022-03-12 10:18:08 +01:00
|
|
|
return "[%s](%s)" % (name, url)
|
|
|
|
|
|
|
|
def _md_search(query):
|
2022-03-13 22:15:27 +01:00
|
|
|
url = '%s?q=%s' % (url_for('search', _external=True), urllib.parse.quote(query))
|
2022-03-12 10:18:08 +01:00
|
|
|
return '[%s](%s)' % (query, url)
|
|
|
|
|
|
|
|
ctx = {}
|
|
|
|
ctx['GIT_URL'] = GIT_URL
|
|
|
|
ctx['get_setting'] = get_setting
|
|
|
|
ctx['link'] = _md_link
|
|
|
|
ctx['search'] = _md_search
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return f'<{self.__class__.__name__} fname={self.fname!r}>'
|
2022-03-12 10:18:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
class InfoPageSet: # pylint: disable=too-few-public-methods
|
|
|
|
"""Cached rendering of the online documentation a SearXNG instance has.
|
|
|
|
|
|
|
|
:param page_class: render online documentation by :py:obj:`InfoPage` parser.
|
|
|
|
:type page_class: :py:obj:`InfoPage`
|
2022-03-13 22:15:27 +01:00
|
|
|
|
|
|
|
:param info_folder: information directory
|
|
|
|
:type info_folder: str
|
2022-03-12 10:18:08 +01:00
|
|
|
"""
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
def __init__(
|
|
|
|
self, page_class: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None
|
|
|
|
):
|
|
|
|
self.page_class = page_class or InfoPage
|
|
|
|
self.folder: str = info_folder or _INFO_FOLDER
|
2022-03-12 10:18:08 +01:00
|
|
|
"""location of the Markdwon files"""
|
|
|
|
|
2022-03-16 22:24:35 +01:00
|
|
|
self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
self.locale_default: str = 'en'
|
2022-03-12 10:18:08 +01:00
|
|
|
"""default language"""
|
|
|
|
|
2022-03-16 22:24:35 +01:00
|
|
|
self.locales: typing.List[str] = [
|
|
|
|
locale.replace('_', '-') for locale in os.listdir(_INFO_FOLDER) if locale.replace('_', '-') in LOCALE_NAMES
|
|
|
|
]
|
2022-03-12 10:18:08 +01:00
|
|
|
"""list of supported languages (aka locales)"""
|
|
|
|
|
2022-03-16 22:24:35 +01:00
|
|
|
self.toc: typing.List[str] = [
|
2022-03-12 10:18:08 +01:00
|
|
|
'search-syntax',
|
|
|
|
'about',
|
|
|
|
]
|
|
|
|
"""list of articles in the online documentation"""
|
|
|
|
|
|
|
|
def get_page(self, pagename: str, locale: typing.Optional[str] = None):
|
|
|
|
"""Return ``pagename`` instance of :py:obj:`InfoPage`
|
|
|
|
|
|
|
|
:param pagename: name of the page, a value from :py:obj:`InfoPageSet.toc`
|
|
|
|
:type pagename: str
|
|
|
|
|
|
|
|
:param locale: language of the page, e.g. ``en``, ``zh_Hans_CN``
|
|
|
|
(default: :py:obj:`InfoPageSet.i18n_origin`)
|
|
|
|
:type locale: str
|
|
|
|
|
|
|
|
"""
|
2022-03-13 22:15:27 +01:00
|
|
|
locale = locale or self.locale_default
|
|
|
|
|
2022-03-12 10:18:08 +01:00
|
|
|
if pagename not in self.toc:
|
|
|
|
return None
|
2022-03-13 22:15:27 +01:00
|
|
|
if locale not in self.locales:
|
2022-03-12 10:18:08 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
cache_key = (pagename, locale)
|
|
|
|
page = self.CACHE.get(cache_key)
|
|
|
|
|
|
|
|
if page is not None:
|
|
|
|
return page
|
|
|
|
|
|
|
|
# not yet instantiated
|
|
|
|
|
2022-03-16 22:24:35 +01:00
|
|
|
fname = os.path.join(self.folder, locale.replace('-', '_'), pagename) + '.md'
|
2022-03-12 10:18:08 +01:00
|
|
|
if not os.path.exists(fname):
|
2022-03-13 22:15:27 +01:00
|
|
|
logger.info('file %s does not exists', fname)
|
|
|
|
self.CACHE[cache_key] = None
|
2022-03-12 10:18:08 +01:00
|
|
|
return None
|
|
|
|
|
2022-03-13 22:15:27 +01:00
|
|
|
page = self.page_class(fname)
|
2022-03-12 10:18:08 +01:00
|
|
|
self.CACHE[cache_key] = page
|
|
|
|
return page
|
|
|
|
|
2022-03-16 22:24:35 +01:00
|
|
|
def iter_pages(self, locale: typing.Optional[str] = None, fallback_to_default=False):
|
2022-03-13 22:15:27 +01:00
|
|
|
"""Iterate over all pages of the TOC"""
|
|
|
|
locale = locale or self.locale_default
|
2022-03-16 22:24:35 +01:00
|
|
|
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
|