__init__.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Render SearXNG instance documentation.
  3. Usage in a Flask app route:
  4. .. code:: python
  5. from searx import infopage
  6. from searx.extended_types import sxng_request
  7. _INFO_PAGES = infopage.InfoPageSet(infopage.MistletoePage)
  8. @app.route('/info/<pagename>', methods=['GET'])
  9. def info(pagename):
  10. locale = sxng_request.preferences.get_value('locale')
  11. page = _INFO_PAGES.get_page(pagename, locale)
  12. """
  13. from __future__ import annotations
  14. __all__ = ['InfoPage', 'InfoPageSet']
  15. import os
  16. import os.path
  17. import logging
  18. import typing
  19. import urllib.parse
  20. from functools import cached_property
  21. import jinja2
  22. from flask.helpers import url_for
  23. from markdown_it import MarkdownIt
  24. from .. import get_setting
  25. from ..version import GIT_URL
  26. from ..locales import LOCALE_NAMES
  27. logger = logging.getLogger('searx.infopage')
  28. _INFO_FOLDER = os.path.abspath(os.path.dirname(__file__))
  29. INFO_PAGES: 'InfoPageSet'
  30. def __getattr__(name):
  31. if name == 'INFO_PAGES':
  32. global INFO_PAGES # pylint: disable=global-statement
  33. INFO_PAGES = InfoPageSet()
  34. return INFO_PAGES
  35. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  36. class InfoPage:
  37. """A page of the :py:obj:`online documentation <InfoPageSet>`."""
  38. def __init__(self, fname):
  39. self.fname = fname
  40. @cached_property
  41. def raw_content(self):
  42. """Raw content of the page (without any jinja rendering)"""
  43. with open(self.fname, 'r', encoding='utf-8') as f:
  44. return f.read()
  45. @cached_property
  46. def content(self):
  47. """Content of the page (rendered in a Jinja context)"""
  48. ctx = self.get_ctx()
  49. template = jinja2.Environment().from_string(self.raw_content)
  50. return template.render(**ctx)
  51. @cached_property
  52. def title(self):
  53. """Title of the content (without any markup)"""
  54. t = ""
  55. for l in self.raw_content.split('\n'):
  56. if l.startswith('# '):
  57. t = l.strip('# ')
  58. return t
  59. @cached_property
  60. def html(self):
  61. """Render Markdown (CommonMark_) to HTML by using markdown-it-py_.
  62. .. _CommonMark: https://commonmark.org/
  63. .. _markdown-it-py: https://github.com/executablebooks/markdown-it-py
  64. """
  65. return (
  66. MarkdownIt("commonmark", {"typographer": True}).enable(["replacements", "smartquotes"]).render(self.content)
  67. )
  68. def get_ctx(self):
  69. """Jinja context to render :py:obj:`InfoPage.content`"""
  70. def _md_link(name, url):
  71. url = url_for(url, _external=True)
  72. return "[%s](%s)" % (name, url)
  73. def _md_search(query):
  74. url = '%s?q=%s' % (url_for('search', _external=True), urllib.parse.quote(query))
  75. return '[%s](%s)' % (query, url)
  76. ctx = {}
  77. ctx['GIT_URL'] = GIT_URL
  78. ctx['get_setting'] = get_setting
  79. ctx['link'] = _md_link
  80. ctx['search'] = _md_search
  81. return ctx
  82. def __repr__(self):
  83. return f'<{self.__class__.__name__} fname={self.fname!r}>'
  84. class InfoPageSet: # pylint: disable=too-few-public-methods
  85. """Cached rendering of the online documentation a SearXNG instance has.
  86. :param page_class: render online documentation by :py:obj:`InfoPage` parser.
  87. :type page_class: :py:obj:`InfoPage`
  88. :param info_folder: information directory
  89. :type info_folder: str
  90. """
  91. def __init__(
  92. self, page_class: typing.Optional[typing.Type[InfoPage]] = None, info_folder: typing.Optional[str] = None
  93. ):
  94. self.page_class = page_class or InfoPage
  95. self.folder: str = info_folder or _INFO_FOLDER
  96. """location of the Markdown files"""
  97. self.CACHE: typing.Dict[tuple, typing.Optional[InfoPage]] = {}
  98. self.locale_default: str = 'en'
  99. """default language"""
  100. self.locales: typing.List[str] = [
  101. locale.replace('_', '-') for locale in os.listdir(_INFO_FOLDER) if locale.replace('_', '-') in LOCALE_NAMES
  102. ]
  103. """list of supported languages (aka locales)"""
  104. self.toc: typing.List[str] = [
  105. 'search-syntax',
  106. 'about',
  107. 'donate',
  108. ]
  109. """list of articles in the online documentation"""
  110. def get_page(self, pagename: str, locale: typing.Optional[str] = None):
  111. """Return ``pagename`` instance of :py:obj:`InfoPage`
  112. :param pagename: name of the page, a value from :py:obj:`InfoPageSet.toc`
  113. :type pagename: str
  114. :param locale: language of the page, e.g. ``en``, ``zh_Hans_CN``
  115. (default: :py:obj:`InfoPageSet.i18n_origin`)
  116. :type locale: str
  117. """
  118. locale = locale or self.locale_default
  119. if pagename not in self.toc:
  120. return None
  121. if locale not in self.locales:
  122. return None
  123. cache_key = (pagename, locale)
  124. if cache_key in self.CACHE:
  125. return self.CACHE[cache_key]
  126. # not yet instantiated
  127. fname = os.path.join(self.folder, locale.replace('-', '_'), pagename) + '.md'
  128. if not os.path.exists(fname):
  129. logger.info('file %s does not exists', fname)
  130. self.CACHE[cache_key] = None
  131. return None
  132. page = self.page_class(fname)
  133. self.CACHE[cache_key] = page
  134. return page
  135. def iter_pages(self, locale: typing.Optional[str] = None, fallback_to_default=False):
  136. """Iterate over all pages of the TOC"""
  137. locale = locale or self.locale_default
  138. for page_name in self.toc:
  139. page_locale = locale
  140. page = self.get_page(page_name, locale)
  141. if fallback_to_default and page is None:
  142. page_locale = self.locale_default
  143. page = self.get_page(page_name, self.locale_default)
  144. if page is not None:
  145. # page is None if the page was deleted by the administrator
  146. yield page_name, page_locale, page