summaryrefslogtreecommitdiff
path: root/searx/infopage
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarIT.de>2022-03-13 23:12:46 +0100
committerGitHub <noreply@github.com>2022-03-13 23:12:46 +0100
commitcd92a7eacdd6b4c333e57111e1503b8455628067 (patch)
treea2f72a42d2024c1f612bee5f9058920c643e6878 /searx/infopage
parente847313007aa3a61c26acda122bb183df1df76a7 (diff)
parent1157462ff9fc08504297e259356ccbefb339965f (diff)
Merge pull request #930 from return42/merge-user-doc2
Integrate the user documentation into the application
Diffstat (limited to 'searx/infopage')
-rw-r--r--searx/infopage/__init__.py179
-rw-r--r--searx/infopage/en/about.md75
-rw-r--r--searx/infopage/en/search-syntax.md35
3 files changed, 289 insertions, 0 deletions
diff --git a/searx/infopage/__init__.py b/searx/infopage/__init__.py
new file mode 100644
index 000000000..9b181ad60
--- /dev/null
+++ b/searx/infopage/__init__.py
@@ -0,0 +1,179 @@
+# 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)
+
+"""
+
+__all__ = ['InfoPage', 'InfoPageSet']
+
+import os
+import os.path
+import logging
+import typing
+
+import urllib.parse
+import jinja2
+from flask.helpers import url_for
+import mistletoe
+
+from .. import get_setting
+from ..compat import cached_property
+from ..version import GIT_URL
+from ..locales import LOCALE_NAMES
+
+
+logger = logging.getLogger('searx.infopage')
+_INFO_FOLDER = os.path.abspath(os.path.dirname(__file__))
+
+
+class InfoPage:
+ """A page of the :py:obj:`online documentation <InfoPageSet>`."""
+
+ def __init__(self, fname):
+ 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
+
+ @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
+ """Jinja context to render :py:obj:`InfoPage.content`"""
+
+ def _md_link(name, url):
+ url = url_for(url, _external=True)
+ return "[%s](%s)" % (name, url)
+
+ def _md_search(query):
+ url = '%s?q=%s' % (url_for('search', _external=True), urllib.parse.quote(query))
+ 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 __repr__(self):
+ return f'<{self.__class__.__name__} fname={self.fname!r}>'
+
+
+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`
+
+ :param info_folder: information directory
+ :type info_folder: str
+ """
+
+ 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.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.locale_default: str = 'en'
+ """default language"""
+
+ self.locales: typing.List = [locale for locale in os.listdir(_INFO_FOLDER) if locale in LOCALE_NAMES]
+ """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
+
+ """
+ locale = locale or self.locale_default
+
+ if pagename not in self.toc:
+ return None
+ if locale not in self.locales:
+ return None
+
+ 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.info('file %s does not exists', fname)
+ self.CACHE[cache_key] = None
+ return None
+
+ page = self.page_class(fname)
+ self.CACHE[cache_key] = page
+ return page
+
+ def all_pages(self, locale: typing.Optional[str] = None):
+ """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
diff --git a/searx/infopage/en/about.md b/searx/infopage/en/about.md
new file mode 100644
index 000000000..9f323870e
--- /dev/null
+++ b/searx/infopage/en/about.md
@@ -0,0 +1,75 @@
+# About SearXNG
+
+SearXNG is a fork from the well-known [searx] [metasearch engine], aggregating
+the results of other {{link('search engines', 'preferences')}} while not
+storing information about its users.
+
+More about SearXNG ...
+
+* [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 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]({{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
+
+## Technical details - How does it work?
+
+SearXNG is a [metasearch engine], inspired by the [seeks project]. It provides
+basic privacy by mixing your queries with searches on other platforms without
+storing search data. Queries are made using a POST request on every browser
+(except Chromium-based browsers*). Therefore they show up in neither our logs,
+nor your url history. In the case of Chromium-based browser users there is an
+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]
+* [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 ?
+
+{{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]({{GIT_URL}}) and run it yourself!
+
+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]({{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/infopage/en/search-syntax.md b/searx/infopage/en/search-syntax.md
new file mode 100644
index 000000000..ae820dfbc
--- /dev/null
+++ b/searx/infopage/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')}}