From b1912607ae9783d6ccf648bd7706a64eca5bedb9 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sat, 12 Mar 2022 10:18:08 +0100 Subject: [PATCH] [mod] replace /help by /info pages and include pages in project docs This patch implements a bolierplate to share content from info-pages of the SearXNG instance (URL /info) with the project documentation (path /docs/user). The info pages are using Markdown (CommonMark), to include them in the project documentation (reST) the myst-parser [1] is used in the Sphinx-doc build chain. If base_url is known (defined in settings.yml) links to the instance are also inserted into the project documentation:: searxng_extra/docs_prebuild [1] https://www.sphinx-doc.org/en/master/usage/markdown.html Signed-off-by: Markus Heiser --- docs/conf.py | 6 +- docs/src/searx.infopage.rst | 8 ++ docs/user/.gitignore | 1 + docs/user/index.rst | 14 ++- docs/user/search_syntax.rst | 39 ------ manage | 1 + requirements-dev.txt | 1 + searx/{help => info}/en/about.md | 52 ++++---- searx/info/en/search-syntax.md | 35 ++++++ searx/infopage/__init__.py | 191 ++++++++++++++++++++++++++++++ searx/templates/oscar/help.html | 12 -- searx/templates/oscar/info.html | 12 ++ searx/templates/oscar/navbar.html | 2 +- searx/templates/simple/base.html | 2 +- searx/templates/simple/help.html | 12 -- searx/templates/simple/info.html | 14 +++ searx/user_help.py | 61 ---------- searx/webapp.py | 28 +++-- searxng_extra/docs_prebuild | 87 ++++++++++++++ setup.py | 3 +- tests/unit/test_webapp.py | 10 +- 21 files changed, 423 insertions(+), 168 deletions(-) create mode 100644 docs/src/searx.infopage.rst create mode 100644 docs/user/.gitignore delete mode 100644 docs/user/search_syntax.rst rename searx/{help => info}/en/about.md (57%) create mode 100644 searx/info/en/search-syntax.md create mode 100644 searx/infopage/__init__.py delete mode 100644 searx/templates/oscar/help.html create mode 100644 searx/templates/oscar/info.html delete mode 100644 searx/templates/simple/help.html create mode 100644 searx/templates/simple/info.html delete mode 100644 searx/user_help.py create mode 100755 searxng_extra/docs_prebuild diff --git a/docs/conf.py b/docs/conf.py index 2f58bdb12..89d6bb8f6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -35,7 +35,7 @@ master_doc = "index" source_suffix = '.rst' numfig = True -exclude_patterns = ['build-templates/*.rst'] +exclude_patterns = ['build-templates/*.rst', 'user/*.md'] import searx.engines import searx.plugins @@ -94,7 +94,6 @@ extlinks['pull-searx'] = ('https://github.com/searx/searx/pull/%s', 'PR ') # links to custom brand extlinks['origin'] = (GIT_URL + '/blob/' + GIT_BRANCH + '/%s', 'git://') extlinks['patch'] = (GIT_URL + '/commit/%s', '#') -extlinks['search'] = (SEARXNG_URL + '/%s', '#') extlinks['docs'] = (DOCS_URL + '/%s', 'docs: ') extlinks['pypi'] = ('https://pypi.org/project/%s', 'PyPi: ') extlinks['man'] = ('https://manpages.debian.org/jump?q=%s', '') @@ -123,8 +122,11 @@ extensions = [ 'linuxdoc.rstFlatTable', # Implementation of the 'flat-table' reST-directive. 'linuxdoc.kfigure', # Sphinx extension which implements scalable image handling. "sphinx_tabs.tabs", # https://github.com/djungelorm/sphinx-tabs + 'myst_parser', # https://www.sphinx-doc.org/en/master/usage/markdown.html ] +suppress_warnings = ['myst.domains'] + intersphinx_mapping = { "python": ("https://docs.python.org/3/", None), "flask": ("https://flask.palletsprojects.com/", None), diff --git a/docs/src/searx.infopage.rst b/docs/src/searx.infopage.rst new file mode 100644 index 000000000..243e5b0bd --- /dev/null +++ b/docs/src/searx.infopage.rst @@ -0,0 +1,8 @@ +.. _searx.infopage: + +================ +Online ``/info`` +================ + +.. automodule:: searx.infopage + :members: diff --git a/docs/user/.gitignore b/docs/user/.gitignore new file mode 100644 index 000000000..2e1fa2d52 --- /dev/null +++ b/docs/user/.gitignore @@ -0,0 +1 @@ +*.md \ No newline at end of file diff --git a/docs/user/index.rst b/docs/user/index.rst index 0cdb7310c..dc9fb0da2 100644 --- a/docs/user/index.rst +++ b/docs/user/index.rst @@ -2,8 +2,14 @@ User documentation ================== -.. toctree:: - :maxdepth: 2 - :caption: Contents +.. contents:: Contents + :depth: 3 + :local: + :backlinks: entry + + +.. _search-syntax: + +.. include:: search-syntax.md + :parser: myst_parser.sphinx_ - search_syntax diff --git a/docs/user/search_syntax.rst b/docs/user/search_syntax.rst deleted file mode 100644 index 346b3162d..000000000 --- a/docs/user/search_syntax.rst +++ /dev/null @@ -1,39 +0,0 @@ - -.. _search-syntax: - -============= -Search syntax -============= - -SearXNG allows you to modify the default categories, engines and search language -via the search query. - -Prefix ``!`` - to set Category/engine - -Prefix: ``:`` - to set language - -Abbrevations of the engines and languages are also accepted. Engine/category -modifiers are chainable and inclusive (e.g. with :search:`!it !ddg !wp qwer -` search in IT category **and** duckduckgo -**and** wikipedia for ``qwer``). - -See the :search:`/preferences page ` for the list of engines, -categories and languages. - -Examples -======== - -Search in wikipedia for ``qwer``: - -- :search:`!wp qwer ` or -- :search:`!wikipedia qwer :search:` - -Image search: - -- :search:`!images Cthulhu ` - -Custom language in wikipedia: - -- :search:`:hu !wp hackerspace ` diff --git a/manage b/manage index 678f64017..e70b7f4dd 100755 --- a/manage +++ b/manage @@ -419,6 +419,7 @@ docs.prebuild() { ./utils/searx.sh doc | cat > "${DOCS_BUILD}/includes/searx.rst" ./utils/filtron.sh doc | cat > "${DOCS_BUILD}/includes/filtron.rst" ./utils/morty.sh doc | cat > "${DOCS_BUILD}/includes/morty.rst" + pyenv.cmd searxng_extra/docs_prebuild ) dump_return $? } diff --git a/requirements-dev.txt b/requirements-dev.txt index 7460caf78..6b4efa2f8 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -14,6 +14,7 @@ sphinx-jinja==1.4.0 sphinx-tabs==3.2.0 sphinxcontrib-programoutput==0.17 sphinx-autobuild==2021.3.14 +myst-parser==0.17.0 linuxdoc==20211220 aiounittest==1.4.1 yamllint==1.26.3 diff --git a/searx/help/en/about.md b/searx/info/en/about.md similarity index 57% rename from searx/help/en/about.md rename to searx/info/en/about.md index dd7cd4515..9f323870e 100644 --- a/searx/help/en/about.md +++ b/searx/info/en/about.md @@ -1,30 +1,29 @@ # About SearXNG SearXNG is a fork from the well-known [searx] [metasearch engine], aggregating -the results of other [search engines][url_for:preferences] while not storing -information about its users. +the results of other {{link('search engines', 'preferences')}} while not +storing information about its users. More about SearXNG ... -* [SearXNG sources][brand.git_url] +* [SearXNG sources]({{GIT_URL}}) * [weblate] ---- ## Why use it? -* SearXNG may not offer you as personalised results as Google, - but it doesn't generate a profile about you. +* SearXNG may not offer you as personalised results as Google, but it doesn't + generate a profile about you. -* SearXNG doesn't care about what you search for, never shares anything - with a third party, and it can't be used to compromise you. +* SearXNG doesn't care about what you search for, never shares anything with a + third party, and it can't be used to compromise you. -* SearXNG is free software, the code is 100% open and you can help - to make it better. See more on [SearXNG sources][brand.git_url]. +* SearXNG is free software, the code is 100% open and you can help to make it + better. See more on [SearXNG sources]({{GIT_URL}}). -If you do care about privacy, want to be a conscious user, or otherwise -believe in digital freedom, make SearXNG your default search engine or run -it on your own server +If you do care about privacy, want to be a conscious user, or otherwise believe +in digital freedom, make SearXNG your default search engine or run it on your +own server ## Technical details - How does it work? @@ -37,35 +36,40 @@ exception: searx uses the search bar to perform GET requests. SearXNG can be added to your browser's search bar; moreover, it can be set as the default search engine. - ## How to set as the default search engine? SearXNG supports [OpenSearch]. For more information on changing your default search engine, see your browser's documentation: -* [Firefox](https://support.mozilla.org/en-US/kb/add-or-remove-search-engine-firefox) -* [Microsoft Edge](https://support.microsoft.com/en-us/help/4028574/microsoft-edge-change-the-default-search-engine) -* Chromium-based browsers [only add websites that the user navigates to without a path.](https://www.chromium.org/tab-to-search) +* [Firefox] +* [Microsoft Edge] +* Chromium-based browsers [only add websites that the user navigates to without + a path.](https://www.chromium.org/tab-to-search) ## Where to find anonymous usage statistics of this instance ? -[Stats page][url_for:stats] contains some useful data about the engines used. +{{link('Stats page', 'stats')}} contains some useful data about the engines +used. ## How can I make it my own? -SearXNG appreciates your concern regarding logs, so take the code from -the [SearXNG project][brand.git_url] and run it yourself! +SearXNG appreciates your concern regarding logs, so take the code from the +[SearXNG project]({{GIT_URL}}) and run it yourself! -Add your instance to this [list of public instances][brand.public_instances] to -help other people reclaim their privacy and make the Internet freer! The more -decentralized the Internet is, the more freedom we have! +Add your instance to this [list of public +instances]({{get_setting('brand.public_instances')}}) to help other people +reclaim their privacy and make the Internet freer! The more decentralized the +Internet is, the more freedom we have! ## Where are the docs & code of this instance? -See the [SearXNG docs][brand.docs_url] and [SearXNG sources][brand.git_url] +See the [SearXNG docs]({{get_setting('brand.docs_url')}}) and [SearXNG +sources]({{GIT_URL}}) [searx]: https://github.com/searx/searx [metasearch engine]: https://en.wikipedia.org/wiki/Metasearch_engine [weblate]: https://weblate.bubu1.eu/projects/searxng/ [seeks project]: https://beniz.github.io/seeks/ [OpenSearch]: https://github.com/dewitt/opensearch/blob/master/opensearch-1-1-draft-6.md +[Firefox]: https://support.mozilla.org/en-US/kb/add-or-remove-search-engine-firefox +[Microsoft Edge]: https://support.microsoft.com/en-us/help/4028574/microsoft-edge-change-the-default-search-engine diff --git a/searx/info/en/search-syntax.md b/searx/info/en/search-syntax.md new file mode 100644 index 000000000..ae820dfbc --- /dev/null +++ b/searx/info/en/search-syntax.md @@ -0,0 +1,35 @@ +# Search syntax + +SearXNG allows you to modify the default categories, engines and search language +via the search query. + +Prefix `!` to set category and engine names. + +Prefix: `:` to set the language. + +Abbrevations of the engines and languages are also accepted. Engine/category +modifiers are chainable and inclusive. E.g. with {{search('!map !ddg !wp paris')}} +search in map category **and** duckduckgo **and** wikipedia for +`paris`. + +See the {{link('preferences', 'preferences')}} for the list of engines, +categories and languages. + +## Examples + +Search in wikipedia for `paris`: + +* {{search('!wp paris')}} +* {{search('!wikipedia paris')}} + +Search in category `map` for `paris`: + +* {{search('!map paris')}} + +Image search: + +* {{search('!images Wau Holland')}} + +Custom language in wikipedia: + +* {{search(':fr !wp Wau Holland')}} diff --git a/searx/infopage/__init__.py b/searx/infopage/__init__.py new file mode 100644 index 000000000..d7736a934 --- /dev/null +++ b/searx/infopage/__init__.py @@ -0,0 +1,191 @@ +# 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/', methods=['GET']) + def info(pagename): + + locale = request.preferences.get_value('locale') + page = _INFO_PAGES.get_page(pagename, locale) + +""" + +__all__ = ['InfoPage', 'MistletoePage', 'InfoPageSet'] + +import os.path +import logging +from functools import cached_property +import typing + +import urllib.parse +import jinja2 +from flask.helpers import url_for +import mistletoe + +from .. import get_setting +from ..version import GIT_URL + +logger = logging.getLogger('doc') + + +class InfoPage: + """A page of the :py:obj:`online documentation `.""" + + def __init__(self, fname, base_url=None): + self.fname = fname + self.base_url = base_url + + @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 + + def get_ctx(self): # pylint: disable=no-self-use + """Jinja context to render :py:obj:`InfoPage.content`""" + + def _md_link(name, url): + url = url_for(url) + if self.base_url: + url = self.base_url + url + return "[%s](%s)" % (name, url) + + def _md_search(query): + url = '%s?q=%s' % (url_for('search'), urllib.parse.quote(query)) + if self.base_url: + url = self.base_url + url + 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 + + def render(self): + """Render / return content""" + return self.content + + +class MistletoePage(InfoPage): + """A HTML page of the :py:obj:`online documentation `.""" + + 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 + """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` + """ + + def __init__(self, page_class: typing.Type[InfoPage], base_url=None): + self.page_class = page_class + self.base_url = base_url + self.CACHE: typing.Dict[tuple, InfoPage] = {} + + # future: could be set from settings.xml + + self.folder: str = _INFO_FOLDER + """location of the Markdwon files""" + + self.i18n_origin: str = 'en' + """default language""" + + self.l10n: typing.List = [ + 'en', + ] + """list of supported languages (aka locales)""" + + self.toc: typing.List = [ + '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 + + """ + if pagename not in self.toc: + return None + if locale is not None and locale not in self.l10n: + return None + + locale = locale or self.i18n_origin + cache_key = (pagename, locale) + page = self.CACHE.get(cache_key) + + if page is not None: + return page + + # not yet instantiated + + fname = os.path.join(self.folder, locale, pagename) + '.md' + if not os.path.exists(fname): + logger.error('file %s does not exists', fname) + return None + + page = self.page_class(fname, self.base_url) + self.CACHE[cache_key] = page + return page + + def all_pages(self, locale: typing.Optional[str] = None): + """Iterate over all pages""" + locale = locale or self.i18n_origin + for pagename in self.toc: + page = self.get_page(pagename, locale) + yield pagename, page diff --git a/searx/templates/oscar/help.html b/searx/templates/oscar/help.html deleted file mode 100644 index bf311fb4a..000000000 --- a/searx/templates/oscar/help.html +++ /dev/null @@ -1,12 +0,0 @@ -{% extends "oscar/base.html" %} -{% block title %}{{ page.title }} - {% endblock %} -{% block content %} - -{{ page.content | safe }} -{% endblock %} diff --git a/searx/templates/oscar/info.html b/searx/templates/oscar/info.html new file mode 100644 index 000000000..3a696da50 --- /dev/null +++ b/searx/templates/oscar/info.html @@ -0,0 +1,12 @@ +{% extends "oscar/base.html" %} +{% block title %}{{ active_page.title }} - {% endblock %} +{% block content %} + +{{ active_page.html | safe }} +{% endblock %} diff --git a/searx/templates/oscar/navbar.html b/searx/templates/oscar/navbar.html index b1d344c11..4c0ec0068 100644 --- a/searx/templates/oscar/navbar.html +++ b/searx/templates/oscar/navbar.html @@ -3,7 +3,7 @@ {{ instance_name }}{{- "" -}} {{- "" -}} {{- "" -}} - {{ _('about') }}{{- "" -}} + {{ _('about') }}{{- "" -}} {{ _('preferences') }}{{- "" -}} {{- "" -}} diff --git a/searx/templates/simple/base.html b/searx/templates/simple/base.html index 644c6df9a..135a95669 100644 --- a/searx/templates/simple/base.html +++ b/searx/templates/simple/base.html @@ -58,7 +58,7 @@